As of ADT 14, resource fields cannot be used as switch cases.

Standard

I just moved one of our Android projects from svn to Github. I did an import to a new Android project in Eclipse and the project wouldn’t build. I got an error on everyone of the switch() statements in the app:

As of ADT 14, resource fields cannot be used as switch cases.

See this article for more on the error. But it I got the error because I had accidentally set my new project as a library.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Using GORM To Move Data Between MySQL And Couchdb – Part 1

Standard

Couchdb can run on Android and easily sync with a server in the cloud. It’s a great solution for providing apps that work “offline”. But I’m fairly new to Couchdb and the whole nosql thing. And actually, I when I first started coding we used flat files. Relational databases and SQL made life much better. So I’m a still a bit skeptical about nosql. But the offline/online sync is something I really need.

Couchdb will give me the sync that I need. But so far reporting off nosql is at least behind from what I can tell. So I’d like to use Couchdb (probably with a couchapp) on the mobile side and have the data normalized and put into MySQL where I can work in familar surroundings.

And since my preferred development environment is Grails with its all-powerful GORM, I have an idea. There’s a Couchdb plugin for Grails that will use GORM to create databases and documents from domain classes. And of course, it creates MySQL tables and records. So it seams feasible that I can create a Grails plugin that will take Couchdb documents, normalize them and put their data into MySQL.

I’d certainly appreciate any feedback on this idea.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

How To Do A Global Grep

Standard

Here’s an example of how to do a recursive grep through your file system. In this case I’m looking for the text ‘otalo.AO.models’ in all Python scripts (*.py).

It turns out that the find command is way to do this. It lets you execute another command on its results. So here we pass the results to grep. The -H grep param is needed to get the file names to print in front the actual content.

So the general form is:

sudo find / -name [search string] -exec [any command] {} \;

And my example search is:

sudo find / -name *.py -exec grep -H 'otalo.AO.models' {} \;

which happily gives this result:


/home/otalo/otalo/AO/fixtures/seed_data.py:from otalo.AO.models import User, Forum, Tag, Admin, Line
/opt/freeswitch/scripts/AO/experiment-digest.py:from otalo.AO.models import Line, Message_forum
. . .

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Create A Unique File Name Using A MAC Address

Standard

I needed a scheme to generate file names coming from different locations to a central server. The file names are generated in a script and I didn’t want to have to change the script for different locations. So I decided to include the MAC Address of the machine in the file name.

This little script grabs the address and converts the : characters to a – and stores there result in a var called MAC.


set - `ifconfig | grep -B 1 inet | head -1 | sed -e "s/\:/-/g"`
MAC=$5

So if you run that command then do

echo $MAC

you’ll get something like this:

00-16-3E-11-09-20

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Creating One-To-Many Records In A Grails Service

Standard

A parent can have many children. This article explains how to write a service such that, if after adding a parent there is an error when adding a child, the entire transaction is rolled back. For example, add parent p1, successfully add child c1, then when adding child c2 an error occurs, both p1 and c1 should be rolled back.

Note that this requires Grails 1.2-M4.

Here are the domain objects:

1
2
3
4
5
6
7
8
9
10
class Parent {
 
static hasMany = [ children : Child ]
 
String  name
 
static constraints = {
name(blank:false,unique:true)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Child {
 
static belongsTo = Parent
 
String name
 
String pet
 
Parent parent
 
static constraints = {
name(blank:false,unique:true)
pet(blank:false)
}
}

Now if you do:

1
grails generate-all *

You’ll get separate forms that do work. But if we were talking about an invoice application, for example, you’d really want to have 1 data entry form. That’s what we do here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
<%@ page contentType="text/html;charset=UTF-8" %>
 
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="layout" content="main" />
    <title>Sample title</title>
  </head>
  <body>
    <h1>Add A Record</h1>
 
  <g:if test="${flash.message}">
    <div class="message">${flash.message}</div>
  </g:if>
  <g:if test="${flash.errors}">
    <div class="errors">
      <ul>
      <g:render template="renderflasherrors" collection="${flash.errors}"  as="list"/>
      </ul>
    </div>
  </g:if>
 
 
  <g:form action="add" name="doAdd">
    <table>
      <tr>
        <td>
          Parent Name
        </td>
        <td>
          Child Name
        </td>
        <td>
          Pet Name
        </td>
      </tr>
      <tr>
        <td>
      //<g:textField name="parentName" value="${flash.params?.parentName}" />
       <g:textField name="parentName" value="${params?.parentName}" />
      </td>
      <td>
      //<g:textField name="childName" value="${flash.params?.childName}" />
       <g:textField name="childName" value="${params?.childName}" />
      </td>
      <td>
      //<g:textField name="petName" value="${flash.params?.petName}" />
      <g:textField name="petName" value="${params?.petName}" />
      </td>
      </tr>
      <tr><td><g:submitButton name="update" value="Update" /></td></tr>
    </table>
  </g:form>
</body>
</html>

The tricky part in this was figuring out how to create both records in the controller and return any errors and the entered data back to client. More on that later, but for now just notice lines 13-16 where we render the errors. This is a little different than in the generated forms. There you can check if the object has errors and display them.

But in this example, all of the objects are created in a service, so the .gsp (and the controller) don’t know anything about the objects. So we end up passing the errors back in a collection flash.errors.

Now look at lines 37, 40 and 43 and compare those to similar lines in the generated forms. There you use a bean to get the value from. But here, we just redisplay the params which contains everything the user originally entered. Actually, the params are being stored in flash. For some reason, the form fields end up getting dropped from the params by the time we get back to action:show.

Here’s the controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class AddrecordController {
 
    def addRecordsService
 
    def index = {
        redirect action:"show", params:params
    }
 
    def add = {
        println "do add"
 
        try {
            addRecordsService.addAll(params)
        } catch (java.lang.RuntimeException re){
 
          def errorMessages = re.errors.allErrors.collect { g.message(error:it) }
            println  errorMessages
            flash.message = g.message(code:"addRecord.save.failed")
            flash.errors = errorMessages
           //flash.params = params
            //render action:"show",  params:params
            render view: "show", params:params
            return
        }
        redirect action:"show"
 
    }
 
    def show = {}
 
}

Make sure you render the view not the action and follow it with a return.

If there’s an error in the service a RunTimeError get’s thrown. As of Grails 1.2-M4, the exception thrown is a grails.validation.ValidationException, which has the list of errors, so at line 18 we pass each error to a message tag to get the readable messages [1].

Finally, here’s the service:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class AddRecordsService {
 
    static transactional = true
 
    def addAll(params) {
        println "add all"
        println params
        def Parent theParent =  addParent(params.parentName)
        def Child theChild   = addChild(params.childName,params.petName,theParent)
 
        println theParent
        println theChild
 
    }
 
    def addParent(pName) {
        println "add parent: ${pName}"
        def theParent = new Parent(name:pName)
        theParent.save(failOnError:true)
 
        return theParent
    }
 
    def addChild(cName,pName,Parent theParent) {
        println "add child: ${cName}"
        def theChild = new Child(name:cName,pet:pName,parent:theParent)
 
       theChild.save(failOnError:true)
 
        return theChild
    }
 
}

Grails 1.2-M4 added failOnError:true as an option to save(). So now the cause of an error is returned.

Thanks to everyone who answered my questions on this ([1],[2],[3],[4]).

[1]Re: How To Parse Runttime Exception Error (GRAILS-5146)

[2]How To Make Transactions Work In Grails

[3]How To Know The Cause Of A Validation Error

[4]Render Errors From A Service

Download transtest

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Using Google Docs For Software Localization

Standard

Google provides some truly amazing automatic translation tools. And now translation is included in Google Docs. This turns out to be a powerful tool for software localization.

My current and probably long-term favorite development environment is Grails. Grails has great localization support build into it. An important piece of that is the messages.properties file, where you store all of your values that you want to be localized.

Actually, there can be lots of properties files. Grails ships with:

  • messages_de.properties
  • messages_es.properties
  • messages_fr.properties
  • messages_it.properties
  • messages_ja.properties
  • messages_nl.properties
  • messages_pt_BR.properties
  • messages_ru.properties
  • messages_th.properties
  • messages_zh_CN.properties

For example, messages_fr.properties is for French.

All the base files have translations of commonly used words. As you add more functionality to your application, you of course need more labels and buttons, etc. So you add them to your messages.properties file. Then all you have to do is translate your file into all these and other languages. The good news is that you can use Google Docs to get the job done. Or at least it can give you a head start on the translation process.

Just load your properties file into Google Docs and select Tools|Translate.

translate

translate

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Calling A Service From The Grails Console

Standard

I’m doing my first project in Grails and intend to blog some of thing things I’m learning.

During development, the grails console is very useful for testing specific pieces of code without having to go through your whole system to get to that particular bit of code.

For the most part, you can just enter grails commands and hit execute. But apparently dependency inject doesn’t happen in the console. So there’s a bit of a trick to execute a service from the console.

If you had a service named MySecretService with a function named secretSquirell, you could access it via dependency injection in a domain class, a controller or another service by first adding

def mySecretService

then later you can say
def result = mySecretService.secretSquirell()

But to test in the console you need to do this:

def service = ctx.getBean("mySecretService")
def result = service.secretSquirell()

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]