use of org.alfresco.service.cmr.dictionary.DataTypeDefinition in project alfresco-remote-api by Alfresco.
the class AbstractPropertiesGet method executeImpl.
/**
* Override method from DeclarativeWebScript
*/
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
QName classQName = getClassQName(req);
String[] names = req.getParameterValues(PARAM_NAME);
String namespacePrefix = req.getParameter(REQ_URL_TEMPL_VAR_NAMESPACE_PREFIX);
String namespaceURI = null;
if (namespacePrefix != null) {
namespaceURI = this.namespaceService.getNamespaceURI(namespacePrefix);
}
Map<QName, PropertyDefinition> propMap = null;
if (classQName == null) {
if (names != null) {
propMap = new HashMap<QName, PropertyDefinition>(names.length);
for (String name : names) {
QName propQName = QName.createQName(name, namespaceService);
PropertyDefinition propDef = dictionaryservice.getProperty(propQName);
if (propDef != null) {
propMap.put(propQName, propDef);
}
}
} else {
Collection<QName> propQNames = dictionaryservice.getAllProperties(null);
propMap = new HashMap<QName, PropertyDefinition>(propQNames.size());
for (QName propQName : propQNames) {
propMap.put(propQName, dictionaryservice.getProperty(propQName));
}
}
} else {
// Get all the property definitions for the class
propMap = dictionaryservice.getClass(classQName).getProperties();
}
// Filter the properties by URI
List<PropertyDefinition> props = new ArrayList<PropertyDefinition>(propMap.size());
for (Map.Entry<QName, PropertyDefinition> entry : propMap.entrySet()) {
if ((namespaceURI != null && namespaceURI.equals(entry.getKey().getNamespaceURI()) == true) || namespaceURI == null) {
props.add(entry.getValue());
}
}
// Filter the properties by the allowed types...
String[] filterTypes = req.getParameterValues(REQ_PARM_ALLOWED_TYPE);
if (filterTypes != null && filterTypes.length > 0) {
List<PropertyDefinition> typeFilteredProps = new ArrayList<PropertyDefinition>(props.size());
for (PropertyDefinition prop : props) {
for (String type : filterTypes) {
DataTypeDefinition dtd = prop.getDataType();
if (dtd.getName().getPrefixString().equals(type)) {
typeFilteredProps.add(prop);
break;
}
}
}
// Important to change the props variable to reference the type filtered properties...
props = typeFilteredProps;
}
// Order property definitions by title
Collections.sort(props, new DictionaryComparators.PropertyDefinitionComparator(dictionaryservice));
// Pass list of property definitions to template
Map<String, Object> model = new HashMap<String, Object>();
model.put(MODEL_PROP_KEY_PROPERTY_DETAILS, props);
model.put(MODEL_PROP_KEY_MESSAGE_LOOKUP, dictionaryservice);
return model;
}
use of org.alfresco.service.cmr.dictionary.DataTypeDefinition in project alfresco-remote-api by Alfresco.
the class ProcessesImpl method create.
@Override
public ProcessInfo create(ProcessInfo process) {
if (process == null) {
throw new InvalidArgumentException("post body expected when starting a new process instance");
}
boolean definitionExistingChecked = false;
RuntimeService runtimeService = activitiProcessEngine.getRuntimeService();
String processDefinitionId = null;
if (process.getProcessDefinitionId() != null) {
processDefinitionId = process.getProcessDefinitionId();
} else if (process.getProcessDefinitionKey() != null) {
ProcessDefinition definition = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionKey(getProcessDefinitionKey(process.getProcessDefinitionKey())).latestVersion().singleResult();
if (definition == null) {
throw new InvalidArgumentException("No workflow definition could be found with key '" + process.getProcessDefinitionKey() + "'.");
}
processDefinitionId = definition.getId();
definitionExistingChecked = true;
} else {
throw new InvalidArgumentException("Either processDefinitionId or processDefinitionKey is required");
}
if (definitionExistingChecked == false) {
// Check if the required definition actually exists
ProcessDefinitionQuery query = activitiProcessEngine.getRepositoryService().createProcessDefinitionQuery().processDefinitionId(processDefinitionId);
if (tenantService.isEnabled() && deployWorkflowsInTenant) {
query.processDefinitionKeyLike("@" + TenantUtil.getCurrentDomain() + "@%");
}
if (query.count() == 0) {
throw new InvalidArgumentException("No workflow definition could be found with id '" + processDefinitionId + "'.");
}
}
Map<QName, Serializable> startParams = new HashMap<QName, Serializable>();
StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(processDefinitionId);
if (startFormData != null) {
if (CollectionUtils.isEmpty(process.getVariables()) == false) {
TypeDefinition startTaskType = getWorkflowFactory().getTaskFullTypeDefinition(startFormData.getFormKey(), true);
// Lookup type definition for the startTask
Map<QName, PropertyDefinition> taskProperties = startTaskType.getProperties();
Map<String, QName> propNameMap = new HashMap<String, QName>();
for (QName key : taskProperties.keySet()) {
propNameMap.put(key.getPrefixString().replace(':', '_'), key);
}
Map<QName, AssociationDefinition> taskAssociations = startTaskType.getAssociations();
for (QName key : taskAssociations.keySet()) {
propNameMap.put(key.getPrefixString().replace(':', '_'), key);
}
for (String variableName : process.getVariables().keySet()) {
if (propNameMap.containsKey(variableName)) {
Object variableValue = process.getVariables().get(variableName);
if (taskAssociations.containsKey(propNameMap.get(variableName))) {
AssociationDefinition associationDef = taskAssociations.get(propNameMap.get(variableName));
variableValue = convertAssociationDefinitionValue(associationDef, variableName, variableValue);
} else if (taskProperties.containsKey(propNameMap.get(variableName))) {
PropertyDefinition propDef = taskProperties.get(propNameMap.get(variableName));
DataTypeDefinition propDataType = propDef.getDataType();
if ("java.util.Date".equalsIgnoreCase(propDataType.getJavaClassName())) {
// fix for different ISO 8601 Date format classes in Alfresco (org.alfresco.util and Spring Surf)
variableValue = ISO8601DateFormat.parse((String) variableValue);
}
}
if (variableValue instanceof Serializable) {
startParams.put(propNameMap.get(variableName), (Serializable) variableValue);
}
}
}
}
}
String currentUserName = AuthenticationUtil.getFullyAuthenticatedUser();
Authentication.setAuthenticatedUserId(currentUserName);
NodeRef workflowPackageNodeRef = null;
try {
workflowPackageNodeRef = workflowPackageComponent.createPackage(null);
startParams.put(WorkflowModel.ASSOC_PACKAGE, workflowPackageNodeRef);
} catch (Exception e) {
throw new ApiException("couldn't create workflow package: " + e.getMessage(), e);
}
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(process.getItems())) {
try {
for (String item : process.getItems()) {
NodeRef itemNodeRef = getNodeRef(item);
QName workflowPackageItemId = QName.createQName("wpi", itemNodeRef.toString());
nodeService.addChild(workflowPackageNodeRef, itemNodeRef, WorkflowModel.ASSOC_PACKAGE_CONTAINS, workflowPackageItemId);
}
} catch (Exception e) {
throw new ApiException("Error while adding items to package: " + e.getMessage(), e);
}
}
// Set start task properties. This should be done before instance is started, since it's id will be used
Map<String, Object> variables = getPropertyConverter().getStartVariables(processDefinitionId, startParams);
variables.put(WorkflowConstants.PROP_CANCELLED, Boolean.FALSE);
// Add company home
Object companyHome = getNodeConverter().convertNode(repositoryHelper.getCompanyHome());
variables.put(WorkflowConstants.PROP_COMPANY_HOME, companyHome);
// Add the initiator
NodeRef initiator = getPersonNodeRef(currentUserName);
if (initiator != null) {
variables.put(WorkflowConstants.PROP_INITIATOR, nodeConverter.convertNode(initiator));
// Also add the initiator home reference, if one exists
NodeRef initiatorHome = (NodeRef) nodeService.getProperty(initiator, ContentModel.PROP_HOMEFOLDER);
if (initiatorHome != null) {
variables.put(WorkflowConstants.PROP_INITIATOR_HOME, nodeConverter.convertNode(initiatorHome));
}
}
if (tenantService.isEnabled()) {
// Specify which tenant domain the workflow was started in.
variables.put(ActivitiConstants.VAR_TENANT_DOMAIN, TenantUtil.getCurrentDomain());
}
// Start the process-instance
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinitionId, process.getBusinessKey(), variables);
if (processInstance.isEnded() == false) {
runtimeService.setVariable(processInstance.getProcessInstanceId(), ActivitiConstants.PROP_START_TASK_END_DATE, new Date());
}
HistoricProcessInstance historicProcessInstance = activitiProcessEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
return createProcessInfo(historicProcessInstance);
}
use of org.alfresco.service.cmr.dictionary.DataTypeDefinition in project alfresco-remote-api by Alfresco.
the class ProcessesImpl method updateVariableInProcess.
protected Variable updateVariableInProcess(String processId, String processDefinitionId, Variable variable) {
if (variable.getName() == null) {
throw new InvalidArgumentException("Variable name is required.");
}
// Get start-task definition for explicit typing of variables submitted at the start
String formKey = null;
StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(processDefinitionId);
if (startFormData != null) {
formKey = startFormData.getFormKey();
}
DataTypeDefinition dataTypeDefinition = null;
TypeDefinition startTaskTypeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(formKey, true);
TypeDefinitionContext context = new TypeDefinitionContext(startTaskTypeDefinition, getQNameConverter());
if (context.getPropertyDefinition(variable.getName()) != null) {
dataTypeDefinition = context.getPropertyDefinition(variable.getName()).getDataType();
if (variable.getType() != null && dataTypeDefinition.getName().toPrefixString(namespaceService).equals(variable.getType()) == false) {
throw new InvalidArgumentException("type of variable " + variable.getName() + " should be " + dataTypeDefinition.getName().toPrefixString(namespaceService));
}
} else if (context.getAssociationDefinition(variable.getName()) != null) {
dataTypeDefinition = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
}
if (dataTypeDefinition == null && variable.getType() != null) {
try {
QName dataType = QName.createQName(variable.getType(), namespaceService);
dataTypeDefinition = dictionaryService.getDataType(dataType);
} catch (InvalidQNameException iqne) {
throw new InvalidArgumentException("Unsupported type of variable: '" + variable.getType() + "'.");
}
} else if (dataTypeDefinition == null) {
// Fallback to raw value when no type has been passed and not present in model
dataTypeDefinition = dictionaryService.getDataType(restVariableHelper.extractTypeFromValue(variable.getValue()));
}
if (dataTypeDefinition == null) {
throw new InvalidArgumentException("Unsupported type of variable: '" + variable.getType() + "'.");
}
Object actualValue = null;
if ("java.util.Date".equalsIgnoreCase(dataTypeDefinition.getJavaClassName())) {
// fix for different ISO 8601 Date format classes in Alfresco (org.alfresco.util and Spring Surf)
actualValue = ISO8601DateFormat.parse((String) variable.getValue());
} else if (variable.getName().equals(WorkflowConstants.PROP_INITIATOR)) {
// update the initiator if exists
NodeRef initiator = getNodeRef((String) variable.getValue());
if (nodeService.exists(initiator)) {
actualValue = getNodeConverter().convertNode(initiator);
// Also update the initiator home reference, if one exists
NodeRef initiatorHome = (NodeRef) nodeService.getProperty(initiator, ContentModel.PROP_HOMEFOLDER);
if (initiatorHome != null) {
Variable initiatorHomeVar = new Variable();
initiatorHomeVar.setName(WorkflowConstants.PROP_INITIATOR_HOME);
initiatorHomeVar.setValue(initiatorHome);
updateVariableInProcess(processId, processDefinitionId, initiatorHomeVar);
}
} else {
throw new InvalidArgumentException("Variable value should be a valid person NodeRef.");
}
} else {
if (context.getAssociationDefinition(variable.getName()) != null) {
actualValue = convertAssociationDefinitionValue(context.getAssociationDefinition(variable.getName()), variable.getName(), variable.getValue());
} else {
actualValue = DefaultTypeConverter.INSTANCE.convert(dataTypeDefinition, variable.getValue());
}
}
activitiProcessEngine.getRuntimeService().setVariable(processId, variable.getName(), actualValue);
// Variable value needs to be of type NodeRef
if (actualValue instanceof ActivitiScriptNode) {
variable.setValue(((ActivitiScriptNode) actualValue).getNodeRef());
} else {
variable.setValue(actualValue);
}
// Set actual used type before returning
variable.setType(dataTypeDefinition.getName().toPrefixString(namespaceService));
return variable;
}
use of org.alfresco.service.cmr.dictionary.DataTypeDefinition in project alfresco-remote-api by Alfresco.
the class TasksImpl method convertToTypedVariable.
protected TaskVariable convertToTypedVariable(TaskVariable taskVariable, org.activiti.engine.task.Task taskInstance) {
if (taskVariable.getName() == null) {
throw new InvalidArgumentException("Variable name is required.");
}
if (taskVariable.getVariableScope() == null || (taskVariable.getVariableScope() != VariableScope.GLOBAL && taskVariable.getVariableScope() != VariableScope.LOCAL)) {
throw new InvalidArgumentException("Variable scope is required and can only be 'local' or 'global'.");
}
DataTypeDefinition dataTypeDefinition = null;
TypeDefinitionContext context = null;
if (taskVariable.getVariableScope() == VariableScope.GLOBAL) {
// Get start-task definition for explicit typing of variables submitted at the start
String formKey = null;
StartFormData startFormData = activitiProcessEngine.getFormService().getStartFormData(taskInstance.getProcessDefinitionId());
if (startFormData != null) {
formKey = startFormData.getFormKey();
}
TypeDefinition startTaskTypeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(formKey, true);
context = new TypeDefinitionContext(startTaskTypeDefinition, getQNameConverter());
if (context.getPropertyDefinition(taskVariable.getName()) != null) {
dataTypeDefinition = context.getPropertyDefinition(taskVariable.getName()).getDataType();
if (taskVariable.getType() != null && dataTypeDefinition.getName().toPrefixString(namespaceService).equals(taskVariable.getType()) == false) {
throw new InvalidArgumentException("type of variable " + taskVariable.getName() + " should be " + dataTypeDefinition.getName().toPrefixString(namespaceService));
}
} else if (context.getAssociationDefinition(taskVariable.getName()) != null) {
dataTypeDefinition = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
}
} else {
// Revert to either the content-model type or the raw type provided by the request
try {
String formKey = activitiProcessEngine.getFormService().getTaskFormKey(taskInstance.getProcessDefinitionId(), taskInstance.getTaskDefinitionKey());
TypeDefinition typeDefinition = getWorkflowFactory().getTaskFullTypeDefinition(formKey, false);
context = new TypeDefinitionContext(typeDefinition, getQNameConverter());
if (context.getPropertyDefinition(taskVariable.getName()) != null) {
dataTypeDefinition = context.getPropertyDefinition(taskVariable.getName()).getDataType();
if (taskVariable.getType() != null && dataTypeDefinition.getName().toPrefixString(namespaceService).equals(taskVariable.getType()) == false) {
throw new InvalidArgumentException("type of variable " + taskVariable.getName() + " should be " + dataTypeDefinition.getName().toPrefixString(namespaceService));
}
} else if (context.getAssociationDefinition(taskVariable.getName()) != null) {
dataTypeDefinition = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
}
} catch (InvalidQNameException ignore) {
// In case the property is not part of the model, it's possible that the property-name is not a valid.
// This can be ignored safeley as it falls back to the raw type
}
}
if (dataTypeDefinition == null && taskVariable.getType() != null) {
try {
QName dataType = QName.createQName(taskVariable.getType(), namespaceService);
dataTypeDefinition = dictionaryService.getDataType(dataType);
} catch (InvalidQNameException iqne) {
throw new InvalidArgumentException("Unsupported type of variable: '" + taskVariable.getType() + "'.");
}
} else if (dataTypeDefinition == null) {
// Final fallback to raw value when no type has been passed and not present in model
dataTypeDefinition = dictionaryService.getDataType(restVariableHelper.extractTypeFromValue(taskVariable.getValue()));
}
if (dataTypeDefinition == null) {
throw new InvalidArgumentException("Unsupported type of variable: '" + taskVariable.getType() + "'.");
}
Object actualValue = null;
if ("java.util.Date".equalsIgnoreCase(dataTypeDefinition.getJavaClassName())) {
// fix for different ISO 8601 Date format classes in Alfresco (org.alfresco.util and Spring Surf)
actualValue = ISO8601DateFormat.parse((String) taskVariable.getValue());
} else {
if (context != null && context.getAssociationDefinition(taskVariable.getName()) != null) {
actualValue = convertAssociationDefinitionValue(context.getAssociationDefinition(taskVariable.getName()), taskVariable.getName(), taskVariable.getValue());
} else {
actualValue = DefaultTypeConverter.INSTANCE.convert(dataTypeDefinition, taskVariable.getValue());
}
}
taskVariable.setValue(actualValue);
// Set type so it's returned in case it was left empty
taskVariable.setType(dataTypeDefinition.getName().toPrefixString(namespaceService));
return taskVariable;
}
Aggregations