Cannot initialise subclass of command object using properties? - grails

I am using Grails 2.4.4. In a controller I have used a command object for handling input from a view. That works fine. But now I want to use subclasses for command objects. Depending on some id in the params I want create a specific subclass for the command object. Next I want to fill this command object using this syntax:
def finish() {
final commandObject = createSubclassInstance(params.task.id)
commandObject.properties = params
...
}
The latter assignment fails with the error message "Cannot set readonly property: properties for class: ".
I read in the documentation that the properties field works for domain classes. Does it not work for subclasses of command objects?

If you want to bind properties from the request to a command or any other object, you can use:
org.codehaus.groovy.grails.web.binding.DataBindingUtils.bindObjectToInstance(command, params)
This and the other static methods of the DataBindingUtils class are the real underlying methods that Grails uses to bind commands/domains with all the Grails binding features (listeners, binding annotations like #BindUsing, ...) and validate it, returning a BindingResult object. If you handle the properties property, you are loosing a lot of great binding features from Grails.
PS: Pay attention with these methods you can bind from params, a Map, the request (if the request hava a POST with a JSON).

Related

Instantiating a class by String name in Dart

I am trying to call a method of a class that I only know by name as a String. Now therefore I would need a ClassMirror of that class that allowes me to instantiate an instance. However, creating ClassMirrors seems to be only possible by entering a type using reflectClass(Type) or by passing an already existing instance of that class into reflect(dynamic). So these aren`t helping if I only have a String.
In Java you can do this pretty easily, by calling Class.forName(String). Then you would get a Constructor instance, make it accessibly and call it.
Does anyone know if this is even possible in dart? What seems weird is that once you have a ClassMirror you can access fields and methods by passing symbols, which can be created by Strings.
You can put a specific list of strings to map to a specific list of closures to create a new object with specific parameters.
But you can't get a reflection without using dart:mirrors, which is being deprecated, and also had a negative impact on tree shaking to get the payload size down.
In general, you're invited to look at the package:reflectable to achieve most of what you'd want out of dart:mirrors, using source-to-source builders.

Which Groovy (or Java) type to pass which supports "inputStream"?

I have a method like this.
def process(file){
if(file != null && !file.empty){
anotherMethod(file.inputStream);
}
}
If I call this method from Java, what type of object do I need to pass into this method? I tried File,ZipFile but they don't have empty method.
It's impossible to say for definite without seeing more context (where is this method usually called from the Grails application, for example), but a possible candidate would be MultipartFile, or rather one of its concrete implementations such as MockMultipartFile. The empty and inputStream properties that the groovy code is accessing correspond to the isEmpty() and getInputStream() bean-style accessor methods of this interface.
The normal purpose of this type is to handle multipart/form-data file uploads - it's what you get when you call request.getFile('fieldName') in a controller.

Check for annotation present on superclass field

I have problems to check if a persistent property in grails has an specific annotation for fields that belong to superclass ... ane then get it's name and value.
I am getting the persistence properties as:
GrailsDomainClassProperty[] persistentProperties = new DefaultGrailsDomainClass(entityClass).getPersistentProperties();
That works great ... but later i found that getDeclaredFields only retrieves the actual class fields (not superclass) and things starts to look not very Groovy.
Is there a prefered Groovy way to do this?
No, you should use this code for all super classes. The same will be for children classes.

Serializing Interface objects to JSON

I have a number of interfaces I use to pass business objects around in my application. Using Ninject, I can do whatever I want with them without needing to know the type of the actual object; in this case, most of them are actually various Linq to Sql objects that implement the interface.
For example, I have a Linq to Sql object called Listing that implements IListing.
Where I've run into a problem is with serialization. I've tried using the built in JsonResult and JsonNet. Both of them attempt to serialize the actual Linq to Sql object, not just the properties defined in the interface type. This leads to circular reference issues, but the larger problem is that I only want the properties defined in the interface being serialized and passed around.
So, is there an elegant way to serialize just the properties defined in an instance of an interface that I pass to a serializer?
You could define a new transport type containing only the properties you need and then use AutoMapper to convert between your domain object to this type which will be used for serialization.

Distinguishing between Grails domain-class fields and getBlah() methods via GrailsDomainClassProperty

I'm writing a Groovy script (as part of a Grails plugin) and I want to get a list of properties for a GrailsDomainClass that a user of my plugin might define. I can do this using domainClass.properties (where domainClass is a GrailsDomainClass).
However, suppose a user has the grails domain class:
class Example {
String name
static constraints = {
}
def getSomeNonExistingProperty(){
return "Not-a-real-property"
}
}
In this case, domainClass.properties returns a list with both name and someNoneExistingProperty
I understand that this is because of Grails is generating a read-only property on-the-fly for use where someone has a getBlah() method. That's great, but in my script I want to perform some actions with the "real" properties only (or at least non read-only properties).
That is, I would like some way of distinguishing or identifying someNonExistingProperty as a read-only property, or, alternatively, as a property generated by Grails and not entered explicitly as a field in the domainClass by the user of my plugin.
I've looked at the GrailsDomainClassProperty Class and it has a range of methods providing information about the property. However, none of them appear to tell me whether a property is read-only or not, or to allow me to distinguish between a field defined in the domainClass and a field created on-the-fly by Grails as a result of a "getSomeNonExistingProperty()" method.
Am I missing something obvious here? Is there a way of getting a list of just the explicitly user-defined fields (eg name, in the above example)?
I believe transient properties are what you are trying to exclude
I've run into this problem a few times, and instead of trying to work around it I typically just end up renaming my getX() method. It's probably the easiest option.
Edit:
Alternatively, I wonder if you could use reflection to see which methods are defined on the class, and while iterating over your properties see if the property has an explicit getter defined, and omit it. I'm not very familiar with reflection when it comes to Groovy and Grails, especially with the dynamic methods, but it's a possible route of investigation.

Resources