Your Rating: |
![]() ![]() ![]() ![]() ![]() |
Results: |
![]() ![]() ![]() ![]() ![]() |
108 | rates |
Node2Bean
Basically Node2Bean works in same way as Content2Bean with some little changes.
Packages:
info.magnolia.jcr.node2bean
info.magnolia.jcr.node2bean.impl
Configurations were moved from system components container to platform components.
API
Node2BeanProcessor#toBean(Node)
- transforms node recursively to bean.
Node2BeanProcessor#toBean(Node, Class)
- transforms node recursively to bean, if type of bean can't be resolved, it will use type passed as Class parameter. Interface can be passed also - node2bean will try to obtain implementation from component provider.
Node2BeanProcessor#toBean(Node, Boolean, Node2BeanTransformer, ComponentProvider)
Populating bean
Bean does not require to have any add methods to populate collections and maps. What we need is to write setter methods with parameters with generics.
class MyBean {
private List<String> strings;
private String text;
public setStrings(List<String> strings) { .. }
public setText(String text) { ..
}
Node2Bean will get type to build from parameter with generics and it will populate bean with values.
Text property will be set in same way as it was in Content2Bean.
Setting collections and maps
When trying to set a collection or a map, node2bean will first check if setter with generics exists. If so then this setter will be used to set property. Note that field in class doesn't need to be initialized when using setter because node2bean will create collection/map itself.
When a concrete implementation is specified in setter (e.g. ArrayList<String>
) then node2bean will create instance of ArrayList
and will populate it with values.
If an interface of collection/map is passed in setter as parameter, Node2Bean will create by default:
HashSet
ifSet
interface is passedLinkedList
ifList
orQueue
interface is passedLinkedHashMap
ifMap
interface is passed
Also we can set specific collection/map implementation as class property for node. Then all children of this node will be put into collection/map specified in class property.
Beans with enabled property set to false will not be present in collection/map passed to bean, there is no need to check passed collections/maps for beans which are set to false as it was needed with add methods.
Add methods
Add methods can be still used but are deprecated. Note that when using add methods, you have to initialize class field used by add method.
Custom transformers
All custom transformers must be registered in module descriptor as components and must follow naming convention, e.g.: we want transformer for MyBean class, then transformer name must be set to MyBeanTransformer and must be in same package as bean class.
Here is an example of definition of custom transformer in module descriptor.
<component>
<type>some.package.MyBeanTransformer</type>
<implementation>some.package.MyBeanTransformer</implementation>
</component>
Define transformers via annotation
Possibility to set transformers via annotation:
@N2B(transformer=CommandTransformer.class)
void setCommand(Command command) { .. }
Accessing Node2Bean
If we want to use N2B, we have to create a new Node2BeanProcessorImpl object or we can access it via Components#getComponent(Node2BeanProcessor.class) or inject it to the class.
Possible improvements
Use generics to avoid casting object returned by Node2BeanProcessor#toBean and Node2BeanProcessor#setProperties methods from Object type to Bean object.
Node2Bean suggestions