use of org.alfresco.repo.forms.FormData.FieldData in project alfresco-repository by Alfresco.
the class TypeFormProcessor method createNode.
/**
* 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.
* </p>
*
* @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) {
// 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.");
}
// create the parent NodeRef
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,
// 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 = data.getFieldData(NAME_PROP_DATA);
if (nameData != null) {
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();
}
return nodeRef;
}
use of org.alfresco.repo.forms.FormData.FieldData in project alfresco-repository by Alfresco.
the class ActionFormProcessor method isAsynchronousActionRequest.
/**
* This method works out whether the submitted action should be executed asynchronously.
*/
private boolean isAsynchronousActionRequest(ActionDefinition item, FormData data) {
FieldData executeAsynchronously = data.getFieldData(EXECUTE_ASYNCHRONOUSLY);
boolean result = false;
if (executeAsynchronously != null) {
result = Boolean.valueOf((String) executeAsynchronously.getValue());
}
return result;
}
use of org.alfresco.repo.forms.FormData.FieldData in project alfresco-repository by Alfresco.
the class ActionFormProcessor method createActionAndParams.
/**
* This method creates the {@link Action} based on the submitted form data.
* It then sets the action parameters based on the form data also.
*/
private Action createActionAndParams(ActionDefinition item, FormData data) {
// First create the action.
Action action = null;
try {
action = actionService.createAction(item.getName());
} catch (NoSuchBeanDefinitionException nsbdx) {
throw new FormException("Unrecognised action name " + item.getName(), nsbdx);
}
Map<String, Serializable> actionParameters = new HashMap<String, Serializable>();
List<ParameterDefinition> actionParamDefs = item.getParameterDefinitions();
for (ParameterDefinition actionParamDef : actionParamDefs) {
String paramDefinitionName = actionParamDef.getName();
// We need to find the form field data for this action parameter definition name.
// However the form service may have added "prop_", "assoc_", "_added" and "_removed"
// prefixes/suffixes, which we are not interested in.
FieldData fieldData = data.getFieldData(FormFieldConstants.PROP_DATA_PREFIX + paramDefinitionName);
if (fieldData == null) {
fieldData = data.getFieldData(FormFieldConstants.ASSOC_DATA_PREFIX + paramDefinitionName + FormFieldConstants.ASSOC_DATA_ADDED_SUFFIX);
}
if (fieldData != null) {
Object fieldValueObj = fieldData.getValue();
QName expectedParamType = actionParamDef.getType();
DataTypeDefinition typeDef = dictionaryService.getDataType(expectedParamType);
Object convertedObj = null;
if (actionParamDef.isMultiValued()) {
if (fieldValueObj instanceof String) {
if (((String) fieldValueObj).length() == 0) {
// empty string for multi-valued parameters
// should be treated as null
fieldValueObj = null;
} else if (DataTypeDefinition.QNAME.equals(expectedParamType)) {
// if value is a String convert to List of QName
// In order to allow short-form QNames, which are not handled by the default type converter,
// we'll do QName conversion ourselves.
StringTokenizer tokenizer = new StringTokenizer((String) fieldValueObj, ",");
List<QName> list = new ArrayList<QName>(8);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
QName qname = null;
if (token.charAt(0) == QName.NAMESPACE_BEGIN) {
qname = QName.createQName(token);
} else {
qname = QName.createQName(token, namespaceService);
}
list.add(qname);
}
// process the list the List
fieldValueObj = list;
} else {
// if value is a String convert to List of String
StringTokenizer tokenizer = new StringTokenizer((String) fieldValueObj, ",");
List<String> list = new ArrayList<String>(8);
while (tokenizer.hasMoreTokens()) {
list.add(tokenizer.nextToken());
}
// process the list the List
fieldValueObj = list;
}
} else if (fieldValueObj instanceof JSONArray) {
// if value is a JSONArray convert to List of Object
JSONArray jsonArr = (JSONArray) fieldValueObj;
int arrLength = jsonArr.length();
List<Object> list = new ArrayList<Object>(arrLength);
try {
for (int x = 0; x < arrLength; x++) {
list.add(jsonArr.get(x));
}
} catch (JSONException je) {
throw new FormException("Failed to convert JSONArray to List", je);
}
// persist the list
fieldValueObj = list;
}
if (fieldValueObj instanceof Collection<?>) {
convertedObj = DefaultTypeConverter.INSTANCE.convert(typeDef, (Collection<?>) fieldValueObj);
} else if (fieldValueObj != null) {
throw new FormException("Not a recognized multi-value parameter value instance: " + convertedObj);
}
} else {
if (fieldValueObj instanceof String) {
if (((String) fieldValueObj).length() == 0) {
if (!DataTypeDefinition.TEXT.equals(expectedParamType)) {
// empty string for multi-valued parameters
// should be treated as null
fieldValueObj = null;
}
} else if (DataTypeDefinition.BOOLEAN.equals(expectedParamType)) {
// check for browser representation of true, that being "on"
if (ON.equals(fieldValueObj)) {
fieldValueObj = Boolean.TRUE;
}
} else if (DataTypeDefinition.QNAME.equals(expectedParamType)) {
// we'll do QName conversion ourselves.
if (((String) fieldValueObj).charAt(0) == QName.NAMESPACE_BEGIN) {
fieldValueObj = QName.createQName((String) fieldValueObj);
} else {
fieldValueObj = QName.createQName((String) fieldValueObj, namespaceService);
}
}
}
convertedObj = DefaultTypeConverter.INSTANCE.convert(typeDef, fieldValueObj);
}
actionParameters.put(paramDefinitionName, (Serializable) convertedObj);
}
}
// Now set the action parameter values on the action.
action.setParameterValues(actionParameters);
return action;
}
use of org.alfresco.repo.forms.FormData.FieldData in project alfresco-repository by Alfresco.
the class FormProcessorTest method checkSingleField.
protected void checkSingleField(Form form, String fieldName, Serializable fieldData, String expDataKey) {
List<FieldDefinition> fieldDefs = form.getFieldDefinitions();
assertEquals(1, fieldDefs.size());
FieldDefinition fieldDef = fieldDefs.get(0);
assertEquals(fieldName, fieldDef.getName());
String dataKey = fieldDef.getDataKeyName();
assertEquals(expDataKey, dataKey);
FieldData data = form.getFormData().getFieldData(dataKey);
if (fieldData != null && data != null) {
assertEquals(fieldData, data.getValue());
} else {
assertNull(data);
}
}
use of org.alfresco.repo.forms.FormData.FieldData in project alfresco-repository by Alfresco.
the class FormServiceImplTest method disabledTestFDKModel.
@SuppressWarnings({ "deprecation" })
public void disabledTestFDKModel() throws Exception {
// NOTE: The FDK is not loaded by default, for this test to work you must
// import and make the "Forms Development Kit" project a dependency
// of the "Repository" project.
DictionaryService dictionary = (DictionaryService) this.applicationContext.getBean("DictionaryService");
try {
dictionary.getType(QName.createQName("fdk", "everything", this.namespaceService));
} catch (NamespaceException ne) {
fail("FDK namespace is missing, ensure you've made the 'Forms Development Kit' project a dependency of the 'Repository' project when enabling this test!");
}
// from the check above we know the 'fdk' namespace is present so we can safely
// use the FDK model, firstly create an instance of an everything node
String fdkUri = "http://www.alfresco.org/model/fdk/1.0";
QName everythingType = QName.createQName(fdkUri, "everything");
QName textProperty = QName.createQName(fdkUri, "text");
QName underscoreProperty = QName.createQName(fdkUri, "with_underscore");
QName dashProperty = QName.createQName(fdkUri, "with-dash");
QName duplicateProperty = QName.createQName(fdkUri, "duplicate");
QName periodProperty = QName.createQName(fdkUri, "period");
String guid = GUID.generate();
String name = "everything" + guid + ".txt";
String textValue = "This is some text.";
String underscoreValue = "Property with an underscore in the name.";
String dashValue = "Property with a dash in the name.";
String duplicateValue = "Property with the same name as an association.";
String periodValue = "day|1";
Map<QName, Serializable> docProps = new HashMap<QName, Serializable>(4);
docProps.put(ContentModel.PROP_NAME, name);
docProps.put(textProperty, textValue);
docProps.put(underscoreProperty, underscoreValue);
docProps.put(dashProperty, dashValue);
docProps.put(duplicateProperty, duplicateValue);
docProps.put(periodProperty, periodValue);
NodeRef everythingNode = this.nodeService.createNode(this.folder, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name), everythingType, docProps).getChildRef();
// define a list of fields to retrieve from the node
List<String> fields = new ArrayList<String>(4);
fields.add("cm:name");
fields.add("fdk:text");
fields.add("fdk:with_underscore");
fields.add("fdk:with-dash");
fields.add("prop:fdk:duplicate");
fields.add("assoc:fdk:duplicate");
fields.add("fdk:period");
Form form = this.formService.getForm(new Item(NODE_FORM_ITEM_KIND, everythingNode.toString()), fields);
// check a form got returned
assertNotNull("Expecting form to be present", form);
// check the type is correct
assertEquals("fdk:everything", form.getItem().getType());
// check the field definitions
Collection<FieldDefinition> fieldDefs = form.getFieldDefinitions();
assertNotNull("Expecting to find fields", fieldDefs);
assertEquals("Expecting to find " + fields.size() + " fields", fields.size(), fieldDefs.size());
// find the fields, as we have a duplicate we can't use a Map
PropertyFieldDefinition nameField = null;
PropertyFieldDefinition textField = null;
PropertyFieldDefinition underscoreField = null;
PropertyFieldDefinition dashField = null;
PropertyFieldDefinition periodField = null;
PropertyFieldDefinition duplicatePropField = null;
AssociationFieldDefinition duplicateAssocField = null;
for (FieldDefinition field : fieldDefs) {
if (field.getName().equals("cm:name")) {
nameField = (PropertyFieldDefinition) field;
} else if (field.getName().equals("fdk:text")) {
textField = (PropertyFieldDefinition) field;
} else if (field.getName().equals("fdk:with_underscore")) {
underscoreField = (PropertyFieldDefinition) field;
} else if (field.getName().equals("fdk:with-dash")) {
dashField = (PropertyFieldDefinition) field;
} else if (field.getName().equals("fdk:duplicate")) {
if (field instanceof PropertyFieldDefinition) {
duplicatePropField = (PropertyFieldDefinition) field;
} else if (field instanceof AssociationFieldDefinition) {
duplicateAssocField = (AssociationFieldDefinition) field;
}
} else if (field.getName().equals("fdk:period")) {
periodField = (PropertyFieldDefinition) field;
}
}
assertNotNull("Expected to find nameField", nameField);
assertNotNull("Expected to find textField", textField);
assertNotNull("Expected to find underscoreField", underscoreField);
assertNotNull("Expected to find dashField", dashField);
assertNotNull("Expected to find periodField", periodField);
assertNotNull("Expected to find duplicatePropField", duplicatePropField);
assertNotNull("Expected to find duplicateAssocField", duplicateAssocField);
// check the field values
FormData values = form.getFormData();
assertEquals(name, values.getFieldData(nameField.getDataKeyName()).getValue());
assertEquals(textValue, values.getFieldData(textField.getDataKeyName()).getValue());
assertEquals(underscoreValue, values.getFieldData(underscoreField.getDataKeyName()).getValue());
assertEquals(dashValue, values.getFieldData(dashField.getDataKeyName()).getValue());
assertEquals(periodValue, values.getFieldData(periodField.getDataKeyName()).getValue().toString());
assertEquals(duplicateValue, values.getFieldData(duplicatePropField.getDataKeyName()).getValue());
FieldData fieldData = values.getFieldData(duplicateAssocField.getDataKeyName());
assertNotNull(fieldData);
List<?> assocs = (List<?>) fieldData.getValue();
assertNotNull(assocs);
assertEquals(0, assocs.size());
// check the period property data type parameters were returned
DataTypeParameters dtp = periodField.getDataTypeParameters();
assertNotNull("Expected to find data type parameters for the fdk:period field", dtp);
// update the properties via FormService
FormData data = new FormData();
// setup the new property values
String newText = "This is the new text property";
data.addFieldData(textField.getDataKeyName(), newText);
String newUnderscore = "This is the new value for the underscore property.";
data.addFieldData(underscoreField.getDataKeyName(), newUnderscore);
String newDash = "This is the new value for the dash property.";
data.addFieldData(dashField.getDataKeyName(), newDash);
String newDuplicateProp = "This is the new value for the duplicate property.";
data.addFieldData(duplicatePropField.getDataKeyName(), newDuplicateProp);
// add new association value
data.addFieldData(duplicateAssocField.getDataKeyName() + "_added", this.document.toString());
// persist the data
this.formService.saveForm(new Item(NODE_FORM_ITEM_KIND, everythingNode.toString()), data);
// retrieve the data directly from the node service to ensure its been changed
Map<QName, Serializable> updatedProps = this.nodeService.getProperties(everythingNode);
String updatedText = (String) updatedProps.get(textProperty);
String updatedUnderscore = (String) updatedProps.get(underscoreProperty);
String updatedDash = (String) updatedProps.get(dashProperty);
String updatedDuplicate = (String) updatedProps.get(duplicateProperty);
// check values were updated
assertEquals(newText, updatedText);
assertEquals(newUnderscore, updatedUnderscore);
assertEquals(newDash, updatedDash);
assertEquals(newDuplicateProp, updatedDuplicate);
// retrieve the association that should now be present
assocs = this.nodeService.getTargetAssocs(everythingNode, duplicateProperty);
assertEquals(1, assocs.size());
// request a form for a type with an underscore in it's name
fields = new ArrayList<String>(1);
fields.add("cm:name");
form = this.formService.getForm(new Item(TYPE_FORM_ITEM_KIND, "fdk:with_underscore"), fields);
assertNotNull(form);
// make sure there is 1 fields
fieldDefs = form.getFieldDefinitions();
assertNotNull(fieldDefs);
assertEquals(1, fieldDefs.size());
// save the form to ensure persistence works too
String nodeName = GUID.generate() + ".txt";
data = new FormData();
data.addFieldData("prop_cm_name", nodeName);
data.addFieldData(AbstractFormProcessor.DESTINATION, this.folder.toString());
NodeRef newNode = (NodeRef) this.formService.saveForm(new Item(TYPE_FORM_ITEM_KIND, "fdk:with_underscore"), data);
assertNotNull(newNode);
// get and save a form for a type and property that has a dash in the namespace prefix
fields = new ArrayList<String>(2);
fields.add("cm:name");
fields.add("my-fdk:more_text");
form = this.formService.getForm(new Item(TYPE_FORM_ITEM_KIND, "my-fdk:namespace-with-dash"), fields);
assertNotNull(form);
// make sure there are 2 fields
fieldDefs = form.getFieldDefinitions();
assertNotNull(fieldDefs);
assertEquals(2, fieldDefs.size());
// save the form to ensure persistence works too
nodeName = GUID.generate() + ".txt";
data = new FormData();
data.addFieldData("prop_cm_name", nodeName);
data.addFieldData("prop_my-fdk_more_text", "This is some text");
data.addFieldData(TypeFormProcessor.DESTINATION, this.folder.toString());
newNode = (NodeRef) this.formService.saveForm(new Item(TYPE_FORM_ITEM_KIND, "my-fdk_namespace-with-dash"), data);
assertNotNull(newNode);
// retrieve the properties and check the values
Map<QName, Serializable> props = nodeService.getProperties(newNode);
assertEquals(nodeName, (String) props.get(ContentModel.PROP_NAME));
assertEquals("This is some text", (String) props.get(QName.createQName("http://www.alfresco.org/model/my-fdk/1.0", "more_text")));
}
Aggregations