Adding generic params to FilterFormProcessor and associated classes. Now the type of object being processed by the form processor is statically types at compile time.

The Filter interface uses the same generic params, ensuring that a filter can only be assigned to a FormProcessor if it handles the correct item type.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@16016 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
N Smith
2009-09-01 14:42:05 +00:00
parent 1793a46d7c
commit 9aef9620ad
8 changed files with 668 additions and 519 deletions

View File

@@ -32,7 +32,7 @@ import org.apache.commons.logging.LogFactory;
*
* @author Gavin Cornwell
*/
public abstract class AbstractFilter implements Filter
public abstract class AbstractFilter<ItemType, PersistType> implements Filter<ItemType, PersistType>
{
private static final Log logger = LogFactory.getLog(AbstractFilter.class);

View File

@@ -36,7 +36,7 @@ import org.alfresco.repo.forms.FormData;
*
* @author Gavin Cornwell
*/
public interface Filter
public interface Filter<ItemType, PersistType>
{
/**
* Determines whether the filter is active
@@ -61,7 +61,7 @@ public interface Filter
* @param @param context Map representing optional context that
* can be used during retrieval of the form
*/
public void beforeGenerate(Object item, List<String> fields, List<String> forcedFields,
public void beforeGenerate(ItemType item, List<String> fields, List<String> forcedFields,
Form form, Map<String, Object> context);
/**
@@ -80,7 +80,7 @@ public interface Filter
* @param context Map representing optional context that
* can be used during retrieval of the form
*/
public void afterGenerate(Object item, List<String> fields, List<String> forcedFields,
public void afterGenerate(ItemType item, List<String> fields, List<String> forcedFields,
Form form, Map<String, Object> context);
/**
@@ -95,7 +95,7 @@ public interface Filter
* @param item The item to persist the form data for
* @param data The form data
*/
public void beforePersist(Object item, FormData data);
public void beforePersist(ItemType item, FormData data);
/**
* Callback used to indicate that the given form data was just persisted
@@ -112,5 +112,5 @@ public interface Filter
* @param persistedObject The object created or modified as a result of
* the form persistence
*/
public void afterPersist(Object item, FormData data, Object persistedObject);
public void afterPersist(ItemType item, FormData data, PersistType persistedObject);
}

View File

@@ -22,6 +22,7 @@
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.forms.processor;
import java.util.ArrayList;
@@ -33,74 +34,71 @@ import org.apache.commons.logging.LogFactory;
/**
* Holds a list of filters for a type of form processor.
* <p>
* Each filter is called before and after the processor generates and
* persists the form, thus allowing the form and the effected objects
* to be manipulated prior to generation or persistence or after the
* fact.
* Each filter is called before and after the processor generates and persists
* the form, thus allowing the form and the effected objects to be manipulated
* prior to generation or persistence or after the fact.
* </p>
* <p>
* Each filter is responsible for determing whether it applies to the item
* being processed.
* Each filter is responsible for determing whether it applies to the item being
* processed.
* </p>
*
* @see org.alfresco.repo.forms.processor.Filter
* @author Gavin Cornwell
*/
public class FilterRegistry
public class FilterRegistry<ItemType, PersistType>
{
private static final Log logger = LogFactory.getLog(FilterRegistry.class);
protected List<Filter> filters;
protected List<Filter<ItemType, PersistType>> filters;
/**
* Constructs the registry
*/
public FilterRegistry()
{
this.filters = new ArrayList<Filter>(4);
this.filters = new ArrayList<Filter<ItemType, PersistType>>(4);
}
/**
* Registers a filter
*
* @param filter The Filter to regsiter
*/
public void addFilter(Filter filter)
public void addFilter(Filter<ItemType, PersistType> filter)
{
if (filter.isActive())
{
this.filters.add(filter);
if (logger.isDebugEnabled())
logger.debug("Registered filter: " + filter + " in registry: " + this);
if (logger.isDebugEnabled()) logger.debug("Registered filter: " + filter + " in registry: " + this);
}
else if (logger.isWarnEnabled())
{
logger.warn("Ignored registration of filter " + filter + " as it was marked as inactive");
}
}
/**
* Returns a list of active filters
*
* @return List of active Filter objects
*/
public List<Filter> getFilters()
public List<Filter<ItemType, PersistType>> getFilters()
{
List<Filter> activeFilters = new ArrayList<Filter>(4);
List<Filter<ItemType, PersistType>> activeFilters = new ArrayList<Filter<ItemType, PersistType>>(4);
// iterate round the filters and add each active filter to the list
for (Filter filter: this.filters)
for (Filter<ItemType, PersistType> filter : this.filters)
{
if (filter.isActive())
{
activeFilters.add(filter);
}
}
if (logger.isDebugEnabled())
logger.debug("Returning active filters: " + activeFilters);
if (logger.isDebugEnabled()) logger.debug("Returning active filters: " + activeFilters);
return activeFilters;
}
}

View File

@@ -22,6 +22,7 @@
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.forms.processor;
import java.util.List;
@@ -34,71 +35,73 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Abstract base class for all FormProcessor implementations that wish to use the
* filter mechanism.
*
* Abstract base class for all FormProcessor implementations that wish to use
* the filter mechanism.
*
* @author Gavin Cornwell
*/
public abstract class FilteredFormProcessor extends AbstractFormProcessor
public abstract class FilteredFormProcessor<ItemType, PersistType> extends AbstractFormProcessor
{
private static final Log logger = LogFactory.getLog(FilteredFormProcessor.class);
protected FilterRegistry filterRegistry;
protected FilterRegistry<ItemType, PersistType> filterRegistry;
/**
* Sets the filter registry
* Sets the filter registry
*
* @param filterRegistry The FilterRegistry instance
*/
public void setFilterRegistry(FilterRegistry filterRegistry)
public void setFilterRegistry(FilterRegistry<ItemType, PersistType> filterRegistry)
{
this.filterRegistry = filterRegistry;
if (logger.isDebugEnabled())
logger.debug("Set filter registry: " + this.filterRegistry + " for processor: " + this);
}
/*
* @see org.alfresco.repo.forms.processor.FormProcessor#generate(org.alfresco.repo.forms.Item, java.util.List, java.util.List, java.util.Map)
* @see
* org.alfresco.repo.forms.processor.FormProcessor#generate(org.alfresco
* .repo.forms.Item, java.util.List, java.util.List, java.util.Map)
*/
public Form generate(Item item, List<String> fields, List<String> forcedFields,
Map<String, Object> context)
public Form generate(Item item, List<String> fields, List<String> forcedFields, Map<String, Object> context)
{
// get the typed object representing the item
Object typedItem = getTypedItem(item);
ItemType typedItem = getTypedItem(item);
// create an empty Form
Form form = new Form(item);
// inform all regsitered filters the form is about to be generated
if (this.filterRegistry != null)
{
for (Filter filter: this.filterRegistry.getFilters())
for (Filter filter : this.filterRegistry.getFilters())
{
filter.beforeGenerate(typedItem, fields, forcedFields, form, context);
}
}
// perform the actual generation of the form
internalGenerate(typedItem, fields, forcedFields, form, context);
// inform all regsitered filters the form has been generated
if (this.filterRegistry != null)
{
for (Filter filter: this.filterRegistry.getFilters())
for (Filter filter : this.filterRegistry.getFilters())
{
filter.afterGenerate(typedItem, fields, forcedFields, form, context);
}
}
return form;
}
/**
* Persists the given form data for the given item, completed by calling
* Persists the given form data for the given item, completed by calling
* each applicable registered handler
*
* @see org.alfresco.repo.forms.processor.FormProcessor#persist(org.alfresco.repo.forms.Item, org.alfresco.repo.forms.FormData)
* @see org.alfresco.repo.forms.processor.FormProcessor#persist(org.alfresco.repo.forms.Item,
* org.alfresco.repo.forms.FormData)
* @param item The item to save the form for
* @param data The object representing the form data
* @return The object persisted
@@ -106,44 +109,54 @@ public abstract class FilteredFormProcessor extends AbstractFormProcessor
public Object persist(Item item, FormData data)
{
// get the typed object representing the item
Object typedItem = getTypedItem(item);
ItemType typedItem = getTypedItem(item);
// inform all regsitered filters the form is about to be persisted
if (this.filterRegistry != null)
{
for (Filter filter: this.filterRegistry.getFilters())
for (Filter filter : this.filterRegistry.getFilters())
{
filter.beforePersist(typedItem, data);
}
}
// perform the actual persistence of the form
Object persistedObject = internalPersist(typedItem, data);
// inform all regsitered filters the form has been persisted
if (this.filterRegistry != null)
{
for (Filter filter: this.filterRegistry.getFilters())
for (Filter filter : this.filterRegistry.getFilters())
{
filter.afterPersist(typedItem, data, persistedObject);
}
}
return persistedObject;
}
protected void setItemType(Form form, String type)
{
form.getItem().setType(type);
}
protected void setItemUrl(Form form, String url)
{
form.getItem().setUrl(url);
}
/**
* Returns a typed Object representing the given item.
* Returns a typed Object representing the given item.
* <p>
* Subclasses that represent a form type will return a typed object
* that is then passed to each of it's handlers, the handlers can
* therefore safely cast the Object to the type they expect.
* Subclasses that represent a form type will return a typed object that is
* then passed to each of it's handlers, the handlers can therefore safely
* cast the Object to the type they expect.
*
* @param item The item to get a typed object for
* @return The typed object
*/
protected abstract Object getTypedItem(Item item);
protected abstract ItemType getTypedItem(Item item);
/**
* Generates the form.
*
@@ -151,12 +164,12 @@ public abstract class FilteredFormProcessor extends AbstractFormProcessor
* @param fields Restricted list of fields to include
* @param forcedFields List of fields to forcibly include
* @param form The form object being generated
* @param context Map representing optional context that
* can be used during retrieval of the form
* @param context Map representing optional context that can be used during
* retrieval of the form
*/
protected abstract void internalGenerate(Object item, List<String> fields, List<String> forcedFields,
Form form, Map<String, Object> context);
protected abstract void internalGenerate(ItemType item, List<String> fields, List<String> forcedFields, Form form,
Map<String, Object> context);
/**
* Persists the form data.
*
@@ -164,5 +177,6 @@ public abstract class FilteredFormProcessor extends AbstractFormProcessor
* @param data The data to persist
* @return The object that got created or modified
*/
protected abstract Object internalPersist(Object item, FormData data);
protected abstract PersistType internalPersist(ItemType item, FormData data);
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.forms.processor.node;
/**
* @author Nick Smith
*/
public interface FormFieldConstants
{
/** Public constants */
public static final String ON = "on";
public static final String PROP = "prop";
public static final String ASSOC = "assoc";
public static final String DATA_KEY_SEPARATOR = "_";
public static final String PROP_DATA_PREFIX = PROP + DATA_KEY_SEPARATOR;
public static final String ASSOC_DATA_PREFIX = ASSOC + DATA_KEY_SEPARATOR;
public static final String ASSOC_DATA_ADDED_SUFFIX = DATA_KEY_SEPARATOR + "added";
public static final String ASSOC_DATA_REMOVED_SUFFIX = DATA_KEY_SEPARATOR + "removed";
public static final String TRANSIENT_MIMETYPE = "mimetype";
public static final String TRANSIENT_SIZE = "size";
public static final String TRANSIENT_ENCODING = "encoding";
}

View File

@@ -22,6 +22,7 @@
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.forms.processor.node;
import java.io.Serializable;
@@ -44,18 +45,20 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* FormProcessor implementation that can generate and persist Form objects
* for repository nodes.
*
* FormProcessor implementation that can generate and persist Form objects for
* repository nodes.
*
* @author Gavin Cornwell
*/
public class NodeFormProcessor extends ContentModelFormProcessor
public class NodeFormProcessor extends ContentModelFormProcessor<NodeRef, NodeRef>
{
/** Logger */
private static Log logger = LogFactory.getLog(NodeFormProcessor.class);
/*
* @see org.alfresco.repo.forms.processor.node.ContentModelFormProcessor#getLogger()
* @see
* org.alfresco.repo.forms.processor.node.ContentModelFormProcessor#getLogger
* ()
*/
@Override
protected Log getLogger()
@@ -64,10 +67,12 @@ public class NodeFormProcessor extends ContentModelFormProcessor
}
/*
* @see org.alfresco.repo.forms.processor.FilteredFormProcessor#getTypedItem(org.alfresco.repo.forms.Item)
* @see
* org.alfresco.repo.forms.processor.FilteredFormProcessor#getTypedItem(
* org.alfresco.repo.forms.Item)
*/
@Override
protected Object getTypedItem(Item item)
protected NodeRef getTypedItem(Item item)
{
// create NodeRef representation, the id could already be in a valid
// NodeRef format or it may be in a URL friendly format
@@ -89,25 +94,22 @@ public class NodeFormProcessor extends ContentModelFormProcessor
catch (IllegalArgumentException iae)
{
// ignored for now, dealt with below
if (logger.isDebugEnabled())
logger.debug("NodeRef creation failed for: " + item.getId(), iae);
}
}
}
// check we have a valid node ref
if (nodeRef == null)
{
throw new FormNotFoundException(item,
new IllegalArgumentException(item.getId()));
}
// check we have a valid node ref
if (nodeRef == null) { throw new FormNotFoundException(item, new IllegalArgumentException(
item.getId())); }
// check the node itself exists
if (this.nodeService.exists(nodeRef) == false)
{
throw new FormNotFoundException(item,
new InvalidNodeRefException("Node does not exist: " + nodeRef, nodeRef));
throw new FormNotFoundException(item, new InvalidNodeRefException(
"Node does not exist: " + nodeRef, nodeRef));
}
else
{
@@ -115,27 +117,25 @@ public class NodeFormProcessor extends ContentModelFormProcessor
return nodeRef;
}
}
/*
* @see org.alfresco.repo.forms.processor.FilteredFormProcessor#internalGenerate(java.lang.Object, java.util.List, java.util.List, org.alfresco.repo.forms.Form, java.util.Map)
* @see
* org.alfresco.repo.forms.processor.FilteredFormProcessor#internalGenerate
* (java.lang.Object, java.util.List, java.util.List,
* org.alfresco.repo.forms.Form, java.util.Map)
*/
@Override
protected void internalGenerate(Object item, List<String> fields, List<String> forcedFields,
protected void internalGenerate(NodeRef item, List<String> fields, List<String> forcedFields,
Form form, Map<String, Object> context)
{
if (logger.isDebugEnabled())
logger.debug("Generating form for: " + item);
// cast to the expected NodeRef representation
NodeRef nodeRef = (NodeRef)item;
if (logger.isDebugEnabled()) logger.debug("Generating form for: " + item);
// generate the form for the node
generateNode(nodeRef, fields, forcedFields, form);
if (logger.isDebugEnabled())
logger.debug("Generated form: " + form);
generateNode(item, fields, forcedFields, form);
if (logger.isDebugEnabled()) logger.debug("Generated form: " + form);
}
/**
* Sets up the Form object for the given NodeRef
*
@@ -144,7 +144,8 @@ public class NodeFormProcessor extends ContentModelFormProcessor
* @param forcedFields List of fields to forcibly include
* @param form The Form instance to populate
*/
protected void generateNode(NodeRef nodeRef, List<String> fields, List<String> forcedFields, Form form)
protected void generateNode(NodeRef nodeRef, List<String> fields, List<String> forcedFields,
Form form)
{
// set the type and URL of the item
QName type = this.nodeService.getType(nodeRef);
@@ -154,7 +155,7 @@ public class NodeFormProcessor extends ContentModelFormProcessor
builder.append(nodeRef.getStoreRef().getIdentifier()).append("/");
builder.append(nodeRef.getId());
form.getItem().setUrl(builder.toString());
if (fields != null && fields.size() > 0)
{
generateSelectedFields(nodeRef, null, fields, forcedFields, form);
@@ -167,7 +168,7 @@ public class NodeFormProcessor extends ContentModelFormProcessor
generateTransientFields(nodeRef, form);
}
}
/**
* Sets up the field definitions for all the node's properties.
*
@@ -178,20 +179,20 @@ public class NodeFormProcessor extends ContentModelFormProcessor
{
// get data dictionary definition for node
QName type = this.nodeService.getType(nodeRef);
TypeDefinition typeDef = this.dictionaryService.getAnonymousType(type,
this.nodeService.getAspects(nodeRef));
// iterate round the property definitions for the node and create
TypeDefinition typeDef = this.dictionaryService.getAnonymousType(type, this.nodeService
.getAspects(nodeRef));
// iterate round the property definitions for the node and create
// the equivalent field definition and setup the data for the property
Map<QName, PropertyDefinition> propDefs = typeDef.getProperties();
Map<QName, Serializable> propValues = this.nodeService.getProperties(nodeRef);
for (PropertyDefinition propDef : propDefs.values())
{
generatePropertyField(propDef, form, propValues.get(propDef.getName()),
generatePropertyField(propDef, form, propValues.get(propDef.getName()),
this.namespaceService);
}
}
/**
* Sets up the field definitions for all the node's associations.
*
@@ -202,19 +203,18 @@ public class NodeFormProcessor extends ContentModelFormProcessor
{
// get data dictionary definition for the node
QName type = this.nodeService.getType(nodeRef);
TypeDefinition typeDef = this.dictionaryService.getAnonymousType(type,
this.nodeService.getAspects(nodeRef));
TypeDefinition typeDef = this.dictionaryService.getAnonymousType(type, this.nodeService
.getAspects(nodeRef));
// iterate round the association defintions and setup field definition
Map<QName, AssociationDefinition> assocDefs = typeDef.getAssociations();
for (AssociationDefinition assocDef : assocDefs.values())
{
generateAssociationField(assocDef, form,
retrieveAssociationValues(nodeRef, assocDef),
generateAssociationField(assocDef, form, retrieveAssociationValues(nodeRef, assocDef),
this.namespaceService);
}
}
/**
* Sets up the field definitions for any transient fields that may be
* useful, for example, 'mimetype', 'size' and 'encoding'.
@@ -224,41 +224,40 @@ public class NodeFormProcessor extends ContentModelFormProcessor
*/
protected void generateTransientFields(NodeRef nodeRef, Form form)
{
// if the node is content add the 'mimetype', 'size' and 'encoding' fields.
// if the node is content add the 'mimetype', 'size' and 'encoding'
// fields.
QName type = this.nodeService.getType(nodeRef);
if (this.dictionaryService.isSubClass(type, ContentModel.TYPE_CONTENT))
{
ContentData content = (ContentData)this.nodeService.getProperty(nodeRef, ContentModel.PROP_CONTENT);
ContentData content = (ContentData) this.nodeService.getProperty(nodeRef,
ContentModel.PROP_CONTENT);
if (content != null)
{
// setup mimetype field
generateMimetypePropertyField(content, form);
// setup encoding field
generateEncodingPropertyField(content, form);
// setup size field
generateSizePropertyField(content, form);
}
}
}
/*
* @see org.alfresco.repo.forms.processor.FilteredFormProcessor#internalPersist(java.lang.Object, org.alfresco.repo.forms.FormData)
* @see
* org.alfresco.repo.forms.processor.FilteredFormProcessor#internalPersist
* (java.lang.Object, org.alfresco.repo.forms.FormData)
*/
@Override
protected Object internalPersist(Object item, FormData data)
protected NodeRef internalPersist(NodeRef item, FormData data)
{
if (logger.isDebugEnabled())
logger.debug("Persisting form for: " + item);
// cast to the expected NodeRef representation
NodeRef nodeRef = (NodeRef)item;
if (logger.isDebugEnabled()) logger.debug("Persisting form for: " + item);
// persist the node
persistNode(nodeRef, data);
persistNode(item, data);
return item;
}
}

View File

@@ -22,6 +22,7 @@
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.forms.processor.node;
import java.io.Serializable;
@@ -49,37 +50,42 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* FormProcessor implementation that can generate and persist Form objects
* for types in the Alfresco content model.
*
* FormProcessor implementation that can generate and persist Form objects for
* types in the Alfresco content model.
*
* @author Gavin Cornwell
*/
public class TypeFormProcessor extends ContentModelFormProcessor
public class TypeFormProcessor extends ContentModelFormProcessor<TypeDefinition, NodeRef>
{
/** Logger */
private static Log logger = LogFactory.getLog(TypeFormProcessor.class);
protected static final String NAME_PROP_DATA = PROP + DATA_KEY_SEPARATOR + "cm" + DATA_KEY_SEPARATOR + "name";
protected static final String NAME_PROP_DATA = PROP + DATA_KEY_SEPARATOR + "cm"
+ DATA_KEY_SEPARATOR + "name";
public static final String DESTINATION = "alf_destination";
/*
* @see org.alfresco.repo.forms.processor.node.ContentModelFormProcessor#getLogger()
* @see
* org.alfresco.repo.forms.processor.node.ContentModelFormProcessor#getLogger
* ()
*/
@Override
protected Log getLogger()
{
return logger;
}
/*
* @see org.alfresco.repo.forms.processor.node.NodeFormProcessor#getTypedItem(org.alfresco.repo.forms.Item)
* @see
* org.alfresco.repo.forms.processor.node.NodeFormProcessor#getTypedItem
* (org.alfresco.repo.forms.Item)
*/
@Override
protected Object getTypedItem(Item item)
protected TypeDefinition getTypedItem(Item item)
{
TypeDefinition typeDef = null;
try
{
// convert the prefix type into full QName representation
@@ -98,7 +104,7 @@ public class TypeFormProcessor extends ContentModelFormProcessor
// if item id contains _ change the first occurrence to :
// as it's more than likely been converted for URL use
int idx = itemId.indexOf("_");
String parsedItemId = itemId.substring(0, idx) + ":" + itemId.substring(idx+1);
String parsedItemId = itemId.substring(0, idx) + ":" + itemId.substring(idx + 1);
type = QName.createQName(parsedItemId, this.namespaceService);
}
else
@@ -106,45 +112,39 @@ public class TypeFormProcessor extends ContentModelFormProcessor
// try and create the QName using the item id as is
type = QName.createQName(itemId, this.namespaceService);
}
// retrieve the type from the dictionary
typeDef = this.dictionaryService.getType(type);
if (typeDef == null)
{
throw new FormNotFoundException(item,
new IllegalArgumentException("Type does not exist: " + item.getId()));
}
if (typeDef == null) { throw new FormNotFoundException(item,
new IllegalArgumentException("Type does not exist: " + item.getId())); }
}
catch (InvalidQNameException iqne)
{
throw new FormNotFoundException(item, iqne);
}
// return the type definition object for the requested type
return typeDef;
}
/*
* @see org.alfresco.repo.forms.processor.FilteredFormProcessor#internalGenerate(java.lang.Object, java.util.List, java.util.List, org.alfresco.repo.forms.Form, java.util.Map)
* @see
* org.alfresco.repo.forms.processor.FilteredFormProcessor#internalGenerate
* (java.lang.Object, java.util.List, java.util.List,
* org.alfresco.repo.forms.Form, java.util.Map)
*/
@Override
protected void internalGenerate(Object item, List<String> fields, List<String> forcedFields,
Form form, Map<String, Object> context)
protected void internalGenerate(TypeDefinition item, List<String> fields,
List<String> forcedFields, Form form, Map<String, Object> context)
{
if (logger.isDebugEnabled())
logger.debug("Generating form for item: " + item);
// cast to the expected NodeRef representation
TypeDefinition typeDef = (TypeDefinition)item;
if (logger.isDebugEnabled()) logger.debug("Generating form for item: " + item);
// generate the form for the node
generateType(typeDef, fields, forcedFields, form);
if (logger.isDebugEnabled())
logger.debug("Generating form: " + form);
generateType(item, fields, forcedFields, form);
if (logger.isDebugEnabled()) logger.debug("Generating form: " + form);
}
/**
* Sets up the Form object for the given NodeRef
*
@@ -153,12 +153,16 @@ public class TypeFormProcessor extends ContentModelFormProcessor
* @param forcedFields List of fields to forcibly include
* @param form The Form instance to populate
*/
protected void generateType(TypeDefinition typeDef, List<String> fields, List<String> forcedFields, Form form)
protected void generateType(TypeDefinition typeDef, List<String> fields,
List<String> forcedFields, Form form)
{
// set the type and URL of the item
form.getItem().setType(typeDef.getName().toPrefixString(this.namespaceService));
form.getItem().setUrl("/api/classes/" + typeDef.getName().toPrefixString(this.namespaceService).replace(":", "_"));
form.getItem().setUrl(
"/api/classes/"
+ typeDef.getName().toPrefixString(this.namespaceService).replace(
":", "_"));
if (fields != null && fields.size() > 0)
{
generateSelectedFields(null, typeDef, fields, forcedFields, form);
@@ -168,11 +172,11 @@ public class TypeFormProcessor extends ContentModelFormProcessor
// setup field definitions and data
generateAllPropertyFields(typeDef, form);
generateAllAssociationFields(typeDef, form);
// TODO: generate transient properties for content types?
}
}
/**
* Sets up the field definitions for all the type's properties.
*
@@ -187,8 +191,8 @@ public class TypeFormProcessor extends ContentModelFormProcessor
{
generatePropertyField(propDef, form, this.namespaceService);
}
// get all default aspects for the type and iterate round their
// get all default aspects for the type and iterate round their
// property definitions too
List<AspectDefinition> aspects = typeDef.getDefaultAspects(true);
for (AspectDefinition aspect : aspects)
@@ -200,7 +204,7 @@ public class TypeFormProcessor extends ContentModelFormProcessor
}
}
}
/**
* Sets up the field definitions for all the type's associations.
*
@@ -215,8 +219,8 @@ public class TypeFormProcessor extends ContentModelFormProcessor
{
generateAssociationField(assocDef, form, this.namespaceService);
}
// get all default aspects for the type and iterate round their
// get all default aspects for the type and iterate round their
// association definitions too
List<AspectDefinition> aspects = typeDef.getDefaultAspects(true);
for (AspectDefinition aspect : aspects)
@@ -230,23 +234,21 @@ public class TypeFormProcessor extends ContentModelFormProcessor
}
/*
* @see org.alfresco.repo.forms.processor.node.NodeFormProcessor#internalPersist(java.lang.Object, org.alfresco.repo.forms.FormData)
* @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)
protected NodeRef internalPersist(TypeDefinition item, FormData data)
{
if (logger.isDebugEnabled())
logger.debug("Persisting form for: " + item);
// cast to the expected NodeRef representation
TypeDefinition typeDef = (TypeDefinition)item;
if (logger.isDebugEnabled()) logger.debug("Persisting form for: " + item);
// create a new instance of the type
NodeRef nodeRef = createNode(typeDef, data);
NodeRef nodeRef = createNode(item, data);
// persist the form data
persistNode(nodeRef, data);
// return the newly created node
return nodeRef;
}
@@ -254,13 +256,13 @@ public class TypeFormProcessor extends ContentModelFormProcessor
/**
* Creates a new instance of the given type.
* <p>
* If the form data has the name property present it is used as
* the name of the node.
* </p><p>
* 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.
* If the form data has the name property present it is used as the name of
* the node.
* </p>
* <p>
* 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.
* </p>
*
* @param typeDef The type defintion of the type to create
@@ -270,53 +272,57 @@ public class TypeFormProcessor extends ContentModelFormProcessor
protected NodeRef createNode(TypeDefinition typeDef, FormData data)
{
NodeRef nodeRef = null;
if (data != null)
{
// firstly, ensure we have a destination to create the node in
NodeRef parentRef = null;
FieldData destination = data.getFieldData(DESTINATION);
if (destination == null)
{
throw new FormException("Failed to persist form for '" +
typeDef.getName().toPrefixString(this.namespaceService) +
"' as '" + DESTINATION + "' data was not provided.");
}
if (destination == null) { throw new FormException("Failed to persist form for '"
+ typeDef.getName().toPrefixString(this.namespaceService) + "' as '"
+ DESTINATION + "' data was not provided."); }
// create the parent NodeRef
parentRef = new NodeRef((String)destination.getValue());
// remove the destination data to avoid warning during persistence, this can
parentRef = new NodeRef((String) destination.getValue());
// remove the destination data to avoid warning during persistence,
// this can
// always be retrieved by looking up the created node's parent
data.removeFieldData(DESTINATION);
// TODO: determine what association to use when creating the node in the destination,
// 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,
// 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 = data.getFieldData(NAME_PROP_DATA);
if (nameData != null)
{
nodeName = (String)nameData.getValue();
// remove the name data otherwise 'rename' gets called in persistNode
nodeName = (String) nameData.getValue();
// remove the name data otherwise 'rename' gets called in
// persistNode
data.removeFieldData(NAME_PROP_DATA);
}
if (nodeName == null || nodeName.length() == 0)
{
nodeName = GUID.generate();
}
// create the node
Map<QName, Serializable> nodeProps = new HashMap<QName, Serializable>(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();
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;
}
}