CollectionDescriptor collectionDescriptor,
ManageableObjects objects, String jcrName)
throws PathNotFoundException, RepositoryException,
VersionException, LockException, ConstraintViolationException,
ItemExistsException {
ClassDescriptor elementClassDescriptor = mapper.getClassDescriptorByClass( ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
Node collectionNode = parentNode.getNode(jcrName);
// If the collection elements have not an id, it is not possible to find the matching JCR nodes
// => delete the complete collection
if (!elementClassDescriptor.hasIdField() && !elementClassDescriptor.hasUUIdField()) {
String primaryNodeTypeName = collectionNode.getPrimaryNodeType().getName();
collectionNode.remove();
collectionNode = parentNode.addNode(jcrName, primaryNodeTypeName);
}
Iterator collectionIterator = objects.getIterator();
Map updatedItems = new HashMap();
List<String> validUuidsForTheNode = new ArrayList<String>();
while (collectionIterator.hasNext()) {
Object item = collectionIterator.next();
String elementJcrName = null;
if (elementClassDescriptor.hasUUIdField()){
elementJcrName = collectionDescriptor.getJcrElementName();
elementJcrName = (elementJcrName == null)? COLLECTION_ELEMENT_NAME : elementJcrName;
String uuidFieldName = elementClassDescriptor.getUuidFieldDescriptor().getFieldName();
Object objUuid = ReflectionUtils.getNestedProperty(item, uuidFieldName);
String currentItemUuid = (objUuid == null) ? null : objUuid.toString();
if (currentItemUuid != null){
//The Node already exists so we need to update the existing node
//rather than to replace it.
Node nodeToUpdate = collectionNode.getSession().getNodeByIdentifier(currentItemUuid);
objectConverter.update(session, currentItemUuid, item);
validUuidsForTheNode.add(currentItemUuid);
}
else{
objectConverter.insert(session, collectionNode, elementJcrName, item);
validUuidsForTheNode.add(ReflectionUtils.getNestedProperty(item, uuidFieldName).toString());
}
}
else if (elementClassDescriptor.hasIdField()) {
String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
elementJcrName = ReflectionUtils.getNestedProperty(item, idFieldName).toString();
// Update existing JCR Nodes
if (collectionNode.hasNode(elementJcrName)) {
objectConverter.update(session, collectionNode, elementJcrName, item);
}
else {
// Add new collection elements
objectConverter.insert(session, collectionNode, elementJcrName, item);
}
updatedItems.put(elementJcrName, item);
}
else {
elementJcrName = collectionDescriptor.getJcrElementName();
if (elementJcrName == null) { // use PathFormat.checkFormat() here?
elementJcrName = COLLECTION_ELEMENT_NAME;
}
objectConverter.insert(session, collectionNode, elementJcrName, item);
}
}
// Delete JCR nodes that are not present in the collection
if (elementClassDescriptor.hasUUIdField()) {
NodeIterator nodeIterator = collectionNode.getNodes();
List<Node> removeNodes = new ArrayList<Node>();
while (nodeIterator.hasNext()) {
Node currentNode = nodeIterator.nextNode();
if (!validUuidsForTheNode.contains(currentNode.getIdentifier())) {
removeNodes.add(currentNode);
}
}
for (Node aNode : removeNodes){
aNode.remove();
}
return;
}
// Delete JCR nodes that are not present in the collection
if (elementClassDescriptor.hasIdField()) {
NodeIterator nodeIterator = collectionNode.getNodes();
List removeNodes = new ArrayList();
while (nodeIterator.hasNext()) {
Node child = nodeIterator.nextNode();
if (!updatedItems.containsKey(child.getName())) {