Pages

Monday, March 30, 2015

java transliterate cyrillic to latin

Hi! Fast solution without any 3rd party API's:

public static String transliterate(String message){
char[] abcCyr =   {' ','а','б','в','г','д','е', 'ё', 'ж','з','и','й','к','л','м','н','о','п','р','с','т','у','ф','х', 'ц', 'ч', 'ш''щ','ъ','ы','ь','э', 'ю', 'я'
  ,'А','Б','В','Г','Д','Е', 'Ё', 'Ж','З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х', 'Ц', 'Ч', 'Ш''Щ','Ъ','Ы','Б','Э', 'Ю', 'Я'
  ,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
  ,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
String[] abcLat = {" ","a","b","v","g","d","e","e","zh","z","i","y","k","l","m","n","o","p","r","s","t","u","f","h","ts","ch","sh","sch", "","i", "","e","ju","ja"
  ,"A","B","V","G","D","E","E","Zh","Z","I","Y","K","L","M","N","O","P","R","S","T","U","F","H","Ts","Ch","Sh","Sch", "","I", "","E","Ju","Ja"
  ,"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"
  ,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
StringBuilder builder = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
for(int x = 0; x < abcCyr.length; x++ )
if (message.charAt(i) == abcCyr[x]) {
builder.append(abcLat[x]);
}
}
return builder.toString();

}

Wednesday, March 18, 2015

Grails flash message not working in production mode

In dev mode it works, but on production flash messages do not appear. If i put message in session scope then it appears, but not in flash.message. The first idea is that there are some additional redirects happening in production mode, those clear the flash message. Also if i run my app with command grails prod run-app, then flash messages also do not appear. There must be some configuration that disables flash messages for production mode!

The only difference that i found in config.groovy between prod and dev modes was this line:

 grails.logging.jul.usebridge = false

But altering the value didn't affect the production environment flash messages in any way.

Also when i created a test project and ran it in prod mode, the flash message appeared correctly. So there must be something wrong with my project. 

SOLVED:
With a bit of help from ZYRO <3 from grails at freenode.

I had a heroku plugin installed, that was sucking in a database session plugin that had some deprecated class and turns out that was causing the problem.
OMFG. How easy was that:

  1. Check logs.
  2. See what causes the problem.
  3. Disable that thing.

Two days of desperate researching were wasted because of incorrect approach. 

Thursday, March 5, 2015

Groovy:unexpected char: 0xFFFF

Just had this strange error. There was some strange symbol at the end of the class. Turned out that i had an extra double quote in one of my strings, removing which resulted in the error disappearing.

Grails mail plugin: email validation (part 1)

Long story short - i couldn't find a easy and understandable tutorial about this stuff, so i will just save the knowledge here.

1) Add to build config:  compile ":mail:1.0.7"

2) Add email account configuration settings in Config.groovy:

grails.mail.default.from="YOURNAME@gmail.com"

grails {
mail {
  host = "smtp.gmail.com"
  port = 465
  username = "your username"  //ex "bob@gmail.com"
  password = "aplication_specific_password_from_gmail"
  props = ["mail.smtp.auth":"true",
  "mail.smtp.socketFactory.port":"465",
  "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
  "mail.smtp.socketFactory.fallback":"false"]
}

 }

These settings are for gmail. There are also setting for hotmai/live and yahoo. Actually this can be configured for any email service. Just like you would configure outlook for one. 

Now your email account is set up.

3) Now in your controllers you can use such a closure:

sendMail {
to "email adress"
subject "subject"
body "This is the email body"
}

Or you can use the next approach, like i did:

def sendMail(String email, String confirmationCode){
sendMail {
to email
subject "Queit - confirm your email"
Map model = [confirmationCode:confirmationCode]
html g.render(template:"emailConfirmationTemplate", model:model)
}
}

For additional info check the official documentation: http://grails.org/plugin/mail

Wednesday, March 4, 2015

Grails scheduled execution of some method aka job

In my project i needed a method to execute once per day, that did something to my database, or schedule a job. There is a plugin for this - quartz. Setting up took 5 minutes.

1) In build config add next line, then refresh dependencies:

compile ":quartz:1.0.2"

2) run command: grails create-job YourJobName

3) In your project grails-app/jobs folder the file will be created. In the execute() method put what needs to be executed, or a call to a service method, as i did.

4) Inside the static triggers add some trigger. There are several types of triggers, easy one, cron trigger and some other one. Easy trigger just triggers the execute method, for example, each 5 minutes. The cron trigger allows you to enter a cron expression to schedule the execute method.

static triggers = {
  cron  cronExpression: "00 00 00 * * ?"
  /**
  * cronExpression: "s m h D M W Y"
       *           | | | | | | `- Year [optional]
       *           | | | | | `- Day of Week, 1-7 or SUN-SAT, ?
       *               | | | | `- Month, 1-12 or JAN-DEC
       *               | | | `- Day of Month, 1-31, ?
       *               | | `- Hour, 0-23
       *               | `- Minute, 0-59
       *          `- Second, 0-59
  */

    }

The cron trigger above triggers the execute method every day at 00 hours, 00 minutes and 00 seconds. Not that if the expression would be * * 00 * * ?, then the job would be executed at 00 hours all the time, and will stop cycling the execute method when hours become 01.

Well, now when you do grails run-app the scheduling is turned on and you have a working scheduled job waiting for the specified time to be executed.

Oh, and in case something is unclear, check the documentation -> http://grails-plugins.github.io/grails-quartz/guide/introduction.html