forcedFields, Form form)
{
@@ -178,6 +195,9 @@ public class TypeFormProcessor extends NodeFormProcessor
}
}
+ /*
+ * @see org.alfresco.repo.forms.processor.node.NodeFormProcessor#internalPersist(java.lang.Object, org.alfresco.repo.forms.FormData)
+ */
@Override
protected Object internalPersist(Object item, FormData data)
{
@@ -187,10 +207,82 @@ public class TypeFormProcessor extends NodeFormProcessor
// cast to the expected NodeRef representation
TypeDefinition typeDef = (TypeDefinition)item;
- if (logger.isWarnEnabled())
- logger.warn("Persisting of 'type' form items has not been implemented yet!");
+ // create a new instance of the type
+ NodeRef nodeRef = createNode(typeDef, data);
- return item;
+ // persist the form data
+ persistNode(nodeRef, data);
+
+ // return the newly created node
+ return nodeRef;
}
+ /**
+ * Creates a new instance of the given type.
+ *
+ * If the form data has the name property present it is used as
+ * the name of the node.
+ *
+ * The new node is placed in the location defined by the "destination"
+ * data item in the form data (this will usually be a hidden field),
+ * this will also be the NodeRef representation of the parent for the
+ * new node.
+ *
+ *
+ * @param typeDef The type defintion of the type to create
+ * @param data The form data
+ * @return NodeRef representing the newly created node
+ */
+ protected NodeRef createNode(TypeDefinition typeDef, FormData data)
+ {
+ NodeRef nodeRef = null;
+
+ if (data != null)
+ {
+ Map fieldData = data.getData();
+ if (fieldData != null)
+ {
+ // firstly, ensure we have a destination to create the node in
+ NodeRef parentRef = null;
+ FieldData destination = fieldData.get(DESTINATION);
+ if (destination == null)
+ {
+ throw new FormException("Failed to persist form for '" +
+ typeDef.getName().toPrefixString(this.namespaceService) +
+ "' as destination data was not present.");
+ }
+
+ // create the parent NodeRef
+ parentRef = new NodeRef((String)destination.getValue());
+
+ // TODO: determine what association to use when creating the node in the destination,
+ // defaults to ContentModel.ASSOC_CONTAINS
+
+ // if a name property is present in the form data use it as the node name,
+ // otherwise generate a guid
+ String nodeName = null;
+ FieldData nameData = fieldData.get(NAME_PROP_DATA);
+ if (nameData != null)
+ {
+ nodeName = (String)nameData.getValue();
+
+ // remove the name data otherwise 'rename' gets called in persistNode
+ fieldData.remove(NAME_PROP_DATA);
+ }
+ if (nodeName == null || nodeName.length() == 0)
+ {
+ nodeName = GUID.generate();
+ }
+
+ // create the node
+ Map nodeProps = new HashMap(1);
+ nodeProps.put(ContentModel.PROP_NAME, nodeName);
+ nodeRef = this.nodeService.createNode(parentRef, ContentModel.ASSOC_CONTAINS,
+ QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, QName.createValidLocalName(nodeName)),
+ typeDef.getName(), nodeProps).getChildRef();
+ }
+ }
+
+ return nodeRef;
+ }
}