Grails XML converters - change root node name - grails

I have following code in my groovy class and I want to change the name for root node. My domain object name is EMP and I want to change it to when I convert my domain in XML.
def init = {
XML.registerObjectMarshaller Emp
{ emp, xml ->
xml.build {
emp_name(emp.name)
emp_dept(emp.dept)
}
}
}

In order to change the root name you will need to use the NameAwareMarshaller which is slightly more complicated than the implementation you are currently using.
While slightly dated, this blog entry explains all the steps in detail. In particular you want to pay attention to the startNode property of the converter.
public void marshalObject(Object object, XML converter) {
Foo foo = object as Foo
converter.startNode 'name'
converter.chars foo.name
converter.end()
Map map = [firstKey: 'firstValue', secondKey: 'secondValue']
converter.startNode 'locallyDefinedMap'
converter.convertAnother map
converter.end()
}

Related

Grails named query - find by association

I want to create a named query that will find all objects of one class, based on a property (or properties) of child class properties.
I want to be able to find all Bars where Foo.me == "some string"
So, I have this, and it doesn't work:
class Foo {
String me
}
class Bar {
Foo foo
static namedQueries = {
findByFooMe { meStr ->
eq(foo.me, meStr)
}
}
}
What does the correct syntax look like please? Also, how does it change if Bar hasMany Foos, and I want to find all Bars where one of its Foo,me properties is "search string"?
i.e.
class Bar {
static hasMany [foos: Foo]
}
While I wouldn't recommend using findBy as the prefix to a named query, you are close to having the right implementation. Here is an updated version with a new name for the query too.
static namedQueries = {
locateByFooMe { meStr ->
foo {
eq('me', meStr)
}
}
}
If you change your relationship to a collection (One to Many) just make sure the property name foo (in this case) changes to whatever you change it to (foos in your question) and the above query will still continue to work.

Grails data binding: creating instances of an abstract class with a Map hasMany

Similar to my last question (Grails databinding: creating instances of an abstract class), I want to use data binding with a class that contains a collection of abstract classes with a hasMany relationship, but in this case, instead of using a List, I'm using a Map.
I created a smalll project with a failing integration test to show the problem that can be found in Github, run it with:
grails test-app -integration -echoOut DataBinding
Anyway, I'll explain the problem by describing the classes and the test here:
class LocalizableContent {
Map contentByLocale = [:].withDefault { locale -> new Text() }
static hasMany = [ contentByLocale : Content ]
}
abstract class Content {
static belongsTo = [ localizableContent : LocalizableContent ]
static constraints = {
localizableContent nullable:true
}
}
class Text extends Content {
String text
}
As you can see, I'm already using the withDefault trick, but apparently it's not being called by Grails / Spring (I even tried to throw an exception in the default closure to verify that the code is not executed).
For the sake of the test, I also created a LocalizableContentController which is empty. With all that, the following integration test then fails:
void testMapDatabinding() {
def rawParams = [ 'contentByLocale[en].text': 'Content' ]
def controller = new LocalizableContentController()
controller.request.addParameters(rawParams)
controller.request.setAttribute(GrailsApplicationAttributes.CONTROLLER, controller)
def localizableContent = new LocalizableContent(controller.params)
assert localizableContent?.contentByLocale['en']?.text == 'Content'
}
It says that localizableContent.contentByLocale is a map which looks like ['en': null], so apparently the data binding is understanding the map syntax and trying to create an entry for the 'en' key. But is not trying first to get the entry for that key, since the withDefault is not being called.
The following one tests that the withDefault works fine, and it passes:
void testMapByDefaultWithNoDatabinding() {
assert new LocalizableContent().contentByLocale['en']?.getClass() == Text
}
What am I missing here?
withDefault is nothing but a pattern to provide a valid value if you face an unknown key. For example, consider the below use case:
def map = [:].withDefault{k->
println k //Should print 'a'
10
}
map.test = 32
assert map.test == 32
assert map.a == 10
It takes the unknown key as the parameter, you cannot pass in any value to it, which is kind of logical, because it provides a default value instead of a value being provided.
In your case, the data binding would work if set the value to Text like:
Map contentByLocale = [:].withDefault { locale ->
//locale is the key. 'en' in this case
new Text(locale: locale, text: 'Content')
}
provided you have your Text class defined as
class Text extends Content{
String locale
String text
}

grails unknown number of model objects in the view

Working in Grails 2.2
I have a situation where I need to be able to handle an unknown number of CommitteeMembers in the view. These need to be both created and displayed.
Each one has the usual attributes - name, address, contact information, userid.
I understand that if I name form fields the same name, Grails will return a collection for me to iterate over. In this case, however, I am faced with this situation:
cm_firstname
cm_lastname
cm_address
cm_email
cm_userid
So does this mean I will be given collections of each of these fields? That is not as useful as there is no way to corelate the various firstnames with the correct lastnames, etc.
I am enjoying Grails and am looking forward to your feedback.
You can use Grails Command objects to do this work for you. Here's an example in a SO question. Basically you will have a single collection of CommitteeMembers that will be populated in your controller thorugh data binding.
As #Gregg says, in the view you need the fields to have an index.
class MyDomain {
String name
}
class MyDomainCommand {
List<MyDomain> instances = ListUtils.lazyList([], FactoryUtils.instantiateFactory(MyDomain))
}
class MyController {
def save() {
MyDomainCommand command = new MyDomainCommand()
bindData(command, params, [include: 'instances'])
}
}
I'll tell you what I do, which may or may not be the best option. I do this mainly because I don't like data binding.
For your case as an example, I would name my fields: "cm.firstName, cm.lastName, cm.address, cm.email, cm.userId".
If you are in a service:
GrailsWebRequest webUtils = WebUtils.retrieveGrailsWebRequest()
List committeeMembers = [].withDefault {new GrailsParameterMap([:], webUtils.getCurrentRequest())}
In a controller:
List committeeMembers = [].withDefault {new GrailsParameterMap([:], request)}
Then
params.cm.each { k, v ->
if (v instanceof String[]) {
v.eachWithIndex { val, idx ->
committeeMembers[idx]."$k" = val
}
}
else {
committeeMembers[0]."$k" = v
}
}
Then you can do:
committeeMembers.each {
<Create from it.firstName, it.lastName, etc>
}

Finding The Super Class of a class just before Top Class with Jena

I am using jena framework to process my owl ontology.
I want to write a method which can find the super class it belongs which is just under the Thing class.
Four example, if there are 5 level hierarchy, lets say first level is Thing, second level is secondAncestor, third level is ThirdAncestor and so on. If I pass a class FifthAncestor, I want to return SecondAncestor because Thing does not make any sense. If I pass ThirdAncestor, I want to return SecondAncestor. In other words, most general class it belongs to but not the top one (Thing).
Method one
This will depend on your model having a reasoner, because owl:Thing isn't normally asserted into a model, and so won't be present in a model with no reasoner. Given that, then:
OntModel m = ... your OntModel ...;
OntClass thing = m.getOntClass( OWL.Thing.getURI() );
for (Iterator<OntClass> i = thing.listSubClasses(true); i.hasNext(); ) {
OntClass hierarchyRoot = i.next();
....
}
Note the use of the flag direct = true in the listSubClasses() call.
Method two
Does not require a reasoner.
for (Iterator<OntClass> i = m.listHierarchyRootClasses(); i.hasNext(); ) {
OntClass hierarchyRoot = i.next();
....
}
Note that this method will return the root classes, even if they are anonymous resources representing a class expression. For UI purposes, this often isn't what you want (it's hard to display a bNode in a meaningful way to a user). In this case, use OntTools.namedHierarchyRoots instead.
Update
I now understand that Alan wants the root classes that are parents of a particular class, whereas namedHierarchyRoots will list all of the root classes of the class hierarchy. Note that, in general, a class may have zero, one or many named-superclasses between it and Thing.
Anyway, here's how I would solve this. Again, this solution assumes the model is not using a reasoner. With a reasoner, it would be much easier:
private boolean hasSubClassTransitive( OntClass parent, OntClass child ) {
return OntTools.findShortestPath( child.getOntModel(), child, parent,
new OntTools.PredicateFilter( RDFS.subClassOf ) ) != null;
}
public List<OntClass> namedRootsOf( OntClass c ) {
List<OntClass> cRoots = new ArrayList<OntClass>();
for (OntClass root: OntTools.namedHierarchyRoots( c.getOntModel() )) {
if (hasSubClassTransitive( root, c )) {
cRoots.add( root );
}
}
return cRoots;
}
I find solution in following way without using reasoner. It is not perfect solution but it works. This solution also solves problem, if you get unnamed (anonymous) class as super class.
First I created an array which stores top level class names.
A simple method which searches in my created array, if the passed parameter is a top class.
public Boolean IsTopClass(String ontologyClass)
{
//NS is URI of ontology
String onClass=ontologyClass.replace(NS, "");
for(String oClass: topLevelClassList)
{
if(oClass.equalsIgnoreCase(onClass))
return true;
}
return false;
}
Then the main method which finds most general class under thing:
public String FindSuperClassUnderThing(OntClass subClass)
{
OntClass prevSubClass=subClass;
OntClass prevprevSubClass=null;
String topClass="";
String supClass=subClass.toString();
ExtendedIterator<OntClass> superClassList=null;
while(!this.IsTopClass(topClass))
{
prevprevSubClass=prevSubClass;
prevSubClass=prevSubClass.getSuperClass();
//if returned class is a anonymous class (not a named one)
//get list of superclasses and check if there is a topclass
//inside the super class list
if(!prevSubClass.toString().startsWith(NS))
{
prevSubClass=prevprevSubClass;
superClassList= prevSubClass.listSuperClasses();
while(superClassList.hasNext())
{
OntClass OntClassFromList= superClassList.next();
if(this.IsTopClass(OntClassFromList.toString()))
{
topClass= OntClassFromList.toString();
}
}
}
else
{
if (this.IsTopClass(prevSubClass.toString()))
{
topClass= prevSubClass.toString();
}
}
}
return topClass;
}

Best way to model map values in Grails?

I have to implement map values in my Grails app.
I have a class that can contain 0..N OsmTags, and the key is unique.
In Java I would model this with a Map in each object, but I don't know how to map classes in Grails.
So I defined this class:
class OsmTag {
/** OSM tag name, e.g. natural */
String key
/** OSM tag value, e.g. park */
String value
static constraints = {
key blank:false, size:2..80,matches:/[\S]+/, unique:false
value blank:false, size:1..250,matches:/[\S]+/, unique:false
}
}
That works ok, but it's actually quite ugly because the tag key is not unique.
Is there a better way to model this issue?
Cheers
If your tags are simple strings, then you can use a map directly.
class Taggable {
Map tags // key : String, value : String
}
If I understand your question correctly, then you want to ensure that each tag is unique within a particular instance of the tagged entity?
Assume that the entity to which the tags are attached is named Taggable, then
you can enforce this requirement using a custom constraint:
class Taggable {
static hasMany = [ tags: OsmTag ]
}
class OsmTag {
static belongsTo = [ taggable: Taggable ]
/** OSM tag name, e.g. natural */
String key
/** OSM tag value, e.g. park */
String value
static constraints = {
key(blank:false, size:2..80,matches:/[\S]+/, unique:false,
validator: { val, obj ->
if (obj.taggable.tags.key.count(val > 1)) {
return false
}
}
)
value(blank:false, size:1..250,matches:/[\S]+/, unique:false)
}
}
If you're looking for a NoSQL solution, you could try using MongoDB with Grails. The most recent version (1.4) supports Geospatial indexing and querying.

Resources