|
|
|
@ -83,7 +83,7 @@ public class Node2ItemConverter {
|
|
|
|
|
setGenericFields(node.getFrozenNode(), Content.class, null, content);
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Message getMessageItem(Node node) throws RepositoryException{
|
|
|
|
|
if (!(node.getPrimaryNodeType().getName().equals("nthl:itemSentRequest")
|
|
|
|
|
|| node.getPrimaryNodeType().getName().equals("nthl:itemSentRequestSH")))
|
|
|
|
@ -95,7 +95,7 @@ public class Node2ItemConverter {
|
|
|
|
|
}catch (Throwable e) {
|
|
|
|
|
msg.setWithAttachments(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setRootItemCommonFields(node, Collections.emptyList(), Message.class, msg);
|
|
|
|
|
return msg;
|
|
|
|
|
}
|
|
|
|
@ -129,7 +129,7 @@ public class Node2ItemConverter {
|
|
|
|
|
item.setExternalManaged(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
item.setLocked(node.isLocked());
|
|
|
|
|
|
|
|
|
|
setRootItemCommonFields(node, excludes, classToHandle, item);
|
|
|
|
@ -137,7 +137,7 @@ public class Node2ItemConverter {
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T extends Item> boolean hasTypedParent(Node node, Class<T> parentType) throws BackendGenericError, RepositoryException{
|
|
|
|
|
if(node==null) return false;
|
|
|
|
@ -146,16 +146,16 @@ public class Node2ItemConverter {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T extends RootItem> void setRootItemCommonFields(Node node, List<String> excludes, Class<T> classToHandle, T instance) throws RepositoryException{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
instance.setParentId(node.getParent().getIdentifier());
|
|
|
|
|
instance.setParentPath(node.getParent().getPath());
|
|
|
|
|
}catch (Throwable e) {
|
|
|
|
|
logger.trace("Root node doesn't have a parent");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
instance.setRelatedNode(node);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
instance.setId(node.getIdentifier());
|
|
|
|
|
instance.setName(Text.unescapeIllegalJcrChars(node.getName()));
|
|
|
|
|
|
|
|
|
@ -166,7 +166,7 @@ public class Node2ItemConverter {
|
|
|
|
|
setGenericFields(node, classToHandle, excludes, instance);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T> void setGenericFields(Node node, Class<T> classToHandle,List<String> excludes, T instance){
|
|
|
|
|
for (Field field : retrieveAllFields(classToHandle)){
|
|
|
|
|
if (field.isAnnotationPresent(Attribute.class)){
|
|
|
|
@ -192,7 +192,7 @@ public class Node2ItemConverter {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T> T iterateNodeAttributeFields(Class<T> clazz, Node node) throws Exception{
|
|
|
|
|
T obj = clazz.newInstance();
|
|
|
|
|
for (Field field : retrieveAllFields(clazz)){
|
|
|
|
@ -200,84 +200,113 @@ public class Node2ItemConverter {
|
|
|
|
|
setAttributeFieldCheckingDefault(field, obj, node);
|
|
|
|
|
} else if (field.isAnnotationPresent(MapAttribute.class)){
|
|
|
|
|
logger.trace("found field {} of type annotated as MapAttribute in class {} and node name {}", field.getName(), clazz.getName(), node.getName());
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
String exclude = field.getAnnotation(MapAttribute.class).excludeStartWith();
|
|
|
|
|
Map<String, Object> mapToset = new HashMap<String, Object>();
|
|
|
|
|
PropertyIterator iterator = node.getProperties();
|
|
|
|
|
if (iterator!=null) {
|
|
|
|
|
while (iterator.hasNext()){
|
|
|
|
|
Property prop = iterator.nextProperty();
|
|
|
|
|
if (!exclude.isEmpty() && prop.getName().startsWith(exclude)) continue;
|
|
|
|
|
try{
|
|
|
|
|
logger.trace("adding {} in the map",prop.getName());
|
|
|
|
|
|
|
|
|
|
mapToset.put(prop.getName(), getPropertyValue(prop));
|
|
|
|
|
}catch(PathNotFoundException e){
|
|
|
|
|
logger.warn("the property {} is not mapped",prop.getName());
|
|
|
|
|
} catch (Exception e ) {
|
|
|
|
|
logger.debug("error setting value {}",e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
field.set(obj, mapToset);
|
|
|
|
|
setMapAttribute(field, obj, node);
|
|
|
|
|
} else if (field.isAnnotationPresent(ListNodes.class)){
|
|
|
|
|
logger.trace("found field {} of type annotated as ListNodes in class {} on node {}", field.getName(), clazz.getName(), node.getName());
|
|
|
|
|
setListNode(field, obj, node);
|
|
|
|
|
} else if (field.isAnnotationPresent(NodeAttribute.class)){
|
|
|
|
|
logger.debug("found field {} of type annotated as NodeAttribute in class {} on node {}", field.getName(), clazz.getName(), node.getName());
|
|
|
|
|
String fieldNodeName = field.getAnnotation(NodeAttribute.class).value();
|
|
|
|
|
//for now it excludes only first level node
|
|
|
|
|
//if (excludes!=null && excludes.contains(fieldNodeName)) continue;
|
|
|
|
|
//for now it excludes only first level node
|
|
|
|
|
logger.info("retrieving field node {} on field {}", fieldNodeName, field.getName());
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
String exclude = field.getAnnotation(ListNodes.class).excludeTypeStartWith();
|
|
|
|
|
String include = field.getAnnotation(ListNodes.class).includeTypeStartWith();
|
|
|
|
|
try{
|
|
|
|
|
Node fieldNode = node.getNode(fieldNodeName);
|
|
|
|
|
logger.info("looking in node {} searched with {}",fieldNode.getName(),fieldNodeName);
|
|
|
|
|
field.set(obj, iterateNodeAttributeFields(field.getType(), fieldNode));
|
|
|
|
|
}catch(PathNotFoundException e){
|
|
|
|
|
logger.warn("the current node dosn't contain {} node",fieldNodeName);
|
|
|
|
|
} catch (Exception e ) {
|
|
|
|
|
logger.warn("error setting value",e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Class listType = field.getAnnotation(ListNodes.class).listClass();
|
|
|
|
|
|
|
|
|
|
Map<String, Class> subTypesMap = Collections.emptyMap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!typeToSubtypeMap.containsKey(listType)) {
|
|
|
|
|
private <T> void setMapAttribute(Field field, T instance, Node node) throws Exception{
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
String exclude = field.getAnnotation(MapAttribute.class).excludeStartWith();
|
|
|
|
|
Map<String, Object> mapToset = new HashMap<String, Object>();
|
|
|
|
|
PropertyIterator iterator = node.getProperties();
|
|
|
|
|
if (iterator!=null) {
|
|
|
|
|
while (iterator.hasNext()){
|
|
|
|
|
Property prop = iterator.nextProperty();
|
|
|
|
|
if (!exclude.isEmpty() && prop.getName().startsWith(exclude)) continue;
|
|
|
|
|
try{
|
|
|
|
|
logger.trace("adding {} in the map",prop.getName());
|
|
|
|
|
|
|
|
|
|
Configuration config = new ConfigurationBuilder().forPackages(listType.getPackage().getName());
|
|
|
|
|
Reflections reflections = new Reflections(config);
|
|
|
|
|
Set<Class> subTypes = reflections.getSubTypesOf(listType);
|
|
|
|
|
mapToset.put(prop.getName(), getPropertyValue(prop));
|
|
|
|
|
}catch(PathNotFoundException e){
|
|
|
|
|
logger.warn("the property {} is not mapped",prop.getName());
|
|
|
|
|
} catch (Exception e ) {
|
|
|
|
|
logger.debug("error setting value {}",e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
field.set(instance, mapToset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (subTypes.size()>0) {
|
|
|
|
|
subTypesMap = new HashMap<>();
|
|
|
|
|
for (Class subtype: subTypes)
|
|
|
|
|
if (subtype.isAnnotationPresent(AttributeRootNode.class)) {
|
|
|
|
|
AttributeRootNode attributeRootNode = (AttributeRootNode)subtype.getAnnotation(AttributeRootNode.class);
|
|
|
|
|
subTypesMap.put(attributeRootNode.value(), subtype);
|
|
|
|
|
}
|
|
|
|
|
} else logger.trace("no subtypes found for {}",listType.getName());
|
|
|
|
|
private <T> void setListNode(Field field, T instance, Node node) throws Exception{
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
|
String exclude = field.getAnnotation(ListNodes.class).excludeTypeStartWith();
|
|
|
|
|
String include = field.getAnnotation(ListNodes.class).includeTypeStartWith();
|
|
|
|
|
|
|
|
|
|
typeToSubtypeMap.put(listType, subTypesMap);
|
|
|
|
|
Class listType = field.getAnnotation(ListNodes.class).listClass();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
logger.info("subtypes already found in cache");
|
|
|
|
|
subTypesMap = typeToSubtypeMap.get(listType);
|
|
|
|
|
}
|
|
|
|
|
Map<String, Class> subTypesMap = Collections.emptyMap();
|
|
|
|
|
|
|
|
|
|
List<Object> toSetList = new ArrayList<>();
|
|
|
|
|
if (!typeToSubtypeMap.containsKey(listType)) {
|
|
|
|
|
|
|
|
|
|
NodeIterator iterator = node.getNodes();
|
|
|
|
|
Configuration config = new ConfigurationBuilder().forPackages(listType.getPackage().getName());
|
|
|
|
|
Reflections reflections = new Reflections(config);
|
|
|
|
|
Set<Class> subTypes = reflections.getSubTypesOf(listType);
|
|
|
|
|
|
|
|
|
|
while (iterator.hasNext()){
|
|
|
|
|
Node currentNode = iterator.nextNode();
|
|
|
|
|
|
|
|
|
|
String primaryType = currentNode.getPrimaryNodeType().getName();
|
|
|
|
|
if (subTypes.size()>0) {
|
|
|
|
|
subTypesMap = new HashMap<>();
|
|
|
|
|
for (Class subtype: subTypes)
|
|
|
|
|
if (subtype.isAnnotationPresent(AttributeRootNode.class)) {
|
|
|
|
|
AttributeRootNode attributeRootNode = (AttributeRootNode)subtype.getAnnotation(AttributeRootNode.class);
|
|
|
|
|
subTypesMap.put(attributeRootNode.value(), subtype);
|
|
|
|
|
}
|
|
|
|
|
} else logger.trace("no subtypes found for {}",listType.getName());
|
|
|
|
|
|
|
|
|
|
logger.trace("the current node {} has a list",currentNode.getName());
|
|
|
|
|
typeToSubtypeMap.put(listType, subTypesMap);
|
|
|
|
|
|
|
|
|
|
if (!include.isEmpty() && !primaryType.startsWith(include))
|
|
|
|
|
continue;
|
|
|
|
|
if (!exclude.isEmpty() && primaryType.startsWith(exclude))
|
|
|
|
|
continue;
|
|
|
|
|
if (subTypesMap.containsKey(primaryType))
|
|
|
|
|
toSetList.add(iterateNodeAttributeFields(subTypesMap.get(primaryType), currentNode));
|
|
|
|
|
else toSetList.add(iterateNodeAttributeFields(listType, currentNode));
|
|
|
|
|
}
|
|
|
|
|
if (toSetList.size()!=0) field.set(obj, toSetList);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
logger.info("subtypes already found in cache");
|
|
|
|
|
subTypesMap = typeToSubtypeMap.get(listType);
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
|
|
List<Object> toSetList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
NodeIterator iterator = node.getNodes();
|
|
|
|
|
|
|
|
|
|
while (iterator.hasNext()){
|
|
|
|
|
Node currentNode = iterator.nextNode();
|
|
|
|
|
|
|
|
|
|
String primaryType = currentNode.getPrimaryNodeType().getName();
|
|
|
|
|
|
|
|
|
|
logger.trace("the current node {} has a list",currentNode.getName());
|
|
|
|
|
|
|
|
|
|
if (!include.isEmpty() && !primaryType.startsWith(include))
|
|
|
|
|
continue;
|
|
|
|
|
if (!exclude.isEmpty() && primaryType.startsWith(exclude))
|
|
|
|
|
continue;
|
|
|
|
|
if (subTypesMap.containsKey(primaryType))
|
|
|
|
|
toSetList.add(iterateNodeAttributeFields(subTypesMap.get(primaryType), currentNode));
|
|
|
|
|
else toSetList.add(iterateNodeAttributeFields(listType, currentNode));
|
|
|
|
|
}
|
|
|
|
|
if (toSetList.size()!=0) field.set(instance, toSetList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T> void setAttributeFieldCheckingDefault(Field field, T instance, Node node) {
|
|
|
|
|
Attribute attribute = field.getAnnotation(Attribute.class);
|
|
|
|
|
field.setAccessible(true);
|
|
|
|
@ -301,8 +330,8 @@ public class Node2ItemConverter {
|
|
|
|
|
logger.debug("error setting value for property {} ",attribute.value());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings({ "unchecked" })
|
|
|
|
|
private Object getPropertyValue(Class returnType, Property prop) throws Exception{
|
|
|
|
|
if (returnType.equals(String.class)) return prop.getString();
|
|
|
|
|