Search in sources :

Example 1 with InvalidQNameException

use of org.alfresco.service.namespace.InvalidQNameException in project records-management by Alfresco.

the class BaseAuditRetrievalWebScript method parseQueryParameters.

/**
 * Parses the given request and builds an instance of
 * RecordsManagementAuditQueryParameters to retrieve the relevant audit entries
 *
 * @param req The request
 * @return RecordsManagementAuditQueryParameters instance
 */
protected RecordsManagementAuditQueryParameters parseQueryParameters(WebScriptRequest req) {
    // create parameters for audit trail retrieval
    RecordsManagementAuditQueryParameters params = new RecordsManagementAuditQueryParameters();
    // the webscripts can have a couple of different forms of url, work out
    // whether a nodeRef has been supplied or whether the whole audit
    // log should be displayed
    NodeRef nodeRef = null;
    Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
    String storeType = templateVars.get("store_type");
    if (storeType != null && storeType.length() > 0) {
        // there is a store_type so all other params are likely to be present
        String storeId = templateVars.get("store_id");
        String nodeId = templateVars.get("id");
        // create the nodeRef
        nodeRef = new NodeRef(new StoreRef(storeType, storeId), nodeId);
    }
    // gather all the common filtering parameters, these could be on the
    // query string, in a multipart/form-data request or in a JSON body
    String size = null;
    String user = null;
    String event = null;
    String from = null;
    String to = null;
    String property = null;
    if (MimetypeMap.MIMETYPE_JSON.equals(req.getContentType())) {
        try {
            JSONObject json = new JSONObject(new JSONTokener(req.getContent().getContent()));
            if (json.has(PARAM_SIZE)) {
                size = json.getString(PARAM_SIZE);
            }
            if (json.has(PARAM_USER)) {
                user = json.getString(PARAM_USER);
            }
            if (json.has(PARAM_EVENT)) {
                event = json.getString(PARAM_EVENT);
            }
            if (json.has(PARAM_FROM)) {
                from = json.getString(PARAM_FROM);
            }
            if (json.has(PARAM_TO)) {
                to = json.getString(PARAM_TO);
            }
            if (json.has(PARAM_PROPERTY)) {
                property = json.getString(PARAM_PROPERTY);
            }
        } catch (IOException ioe) {
            // log a warning
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to parse JSON parameters for audit query: " + ioe.getMessage());
            }
        } catch (JSONException je) {
            // log a warning
            if (logger.isWarnEnabled()) {
                logger.warn("Failed to parse JSON parameters for audit query: " + je.getMessage());
            }
        }
    } else {
        size = req.getParameter(PARAM_SIZE);
        user = req.getParameter(PARAM_USER);
        event = req.getParameter(PARAM_EVENT);
        from = req.getParameter(PARAM_FROM);
        to = req.getParameter(PARAM_TO);
        property = req.getParameter(PARAM_PROPERTY);
    }
    // setup the audit query parameters object
    params.setNodeRef(nodeRef);
    params.setUser(user);
    params.setEvent(event);
    if (size != null && size.length() > 0) {
        try {
            params.setMaxEntries(Integer.parseInt(size));
        } catch (NumberFormatException nfe) {
            if (logger.isWarnEnabled()) {
                logger.warn("Ignoring size parameter as '" + size + "' is not a number!");
            }
        }
    }
    if (from != null && from.length() > 0) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
            params.setDateFrom(dateFormat.parse(from));
        } catch (ParseException pe) {
            if (logger.isWarnEnabled()) {
                logger.warn("Ignoring from parameter as '" + from + "' does not conform to the date pattern: " + DATE_PATTERN);
            }
        }
    }
    if (to != null && to.length() > 0) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_PATTERN);
            params.setDateTo(dateFormat.parse(to));
        } catch (ParseException pe) {
            if (logger.isWarnEnabled()) {
                logger.warn("Ignoring to parameter as '" + to + "' does not conform to the date pattern: " + DATE_PATTERN);
            }
        }
    }
    if (property != null && property.length() > 0) {
        try {
            params.setProperty(QName.createQName(property, namespaceService));
        } catch (InvalidQNameException iqe) {
            if (logger.isWarnEnabled()) {
                logger.warn("Ignoring property parameter as '" + property + "' is an invalid QName");
            }
        }
    }
    return params;
}
Also used : StoreRef(org.alfresco.service.cmr.repository.StoreRef) InvalidQNameException(org.alfresco.service.namespace.InvalidQNameException) RecordsManagementAuditQueryParameters(org.alfresco.module.org_alfresco_module_rm.audit.RecordsManagementAuditQueryParameters) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONTokener(org.json.JSONTokener) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.JSONObject) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with InvalidQNameException

use of org.alfresco.service.namespace.InvalidQNameException 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;
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) InvalidQNameException(org.alfresco.service.namespace.InvalidQNameException) QName(org.alfresco.service.namespace.QName) StartFormData(org.activiti.engine.form.StartFormData) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition)

Example 3 with InvalidQNameException

use of org.alfresco.service.namespace.InvalidQNameException 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;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Variable(org.alfresco.rest.workflow.api.model.Variable) ActivitiScriptNode(org.alfresco.repo.workflow.activiti.ActivitiScriptNode) InvalidQNameException(org.alfresco.service.namespace.InvalidQNameException) QName(org.alfresco.service.namespace.QName) StartFormData(org.activiti.engine.form.StartFormData) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) DataTypeDefinition(org.alfresco.service.cmr.dictionary.DataTypeDefinition)

Aggregations

InvalidQNameException (org.alfresco.service.namespace.InvalidQNameException)3 StartFormData (org.activiti.engine.form.StartFormData)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 DataTypeDefinition (org.alfresco.service.cmr.dictionary.DataTypeDefinition)2 TypeDefinition (org.alfresco.service.cmr.dictionary.TypeDefinition)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 QName (org.alfresco.service.namespace.QName)2 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 RecordsManagementAuditQueryParameters (org.alfresco.module.org_alfresco_module_rm.audit.RecordsManagementAuditQueryParameters)1 ActivitiScriptNode (org.alfresco.repo.workflow.activiti.ActivitiScriptNode)1 Variable (org.alfresco.rest.workflow.api.model.Variable)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1