Search in sources :

Example 1 with TypeConversionException

use of org.alfresco.service.cmr.repository.datatype.TypeConversionException in project alfresco-remote-api by Alfresco.

the class AuditQueryGet method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    final Map<String, Object> model = new HashMap<String, Object>(7);
    String appName = getParamAppName(req);
    String path = getParamPath(req);
    Serializable value = getParamValue(req);
    String valueType = getParamValueType(req);
    Long fromTime = getParamFromTime(req);
    Long toTime = getParamToTime(req);
    Long fromId = getParamFromId(req);
    Long toId = getParamToId(req);
    String user = getParamUser(req);
    boolean forward = getParamForward(req);
    int limit = getParamLimit(req);
    final boolean verbose = getParamVerbose(req);
    if (appName == null) {
        Map<String, AuditApplication> appsByName = auditService.getAuditApplications();
        AuditApplication app = appsByName.get(appName);
        if (app == null) {
            throw new WebScriptException(Status.STATUS_NOT_FOUND, "audit.err.app.notFound", appName);
        }
    }
    // Transform the value to the correct type
    if (value != null && valueType != null) {
        try {
            @SuppressWarnings("unchecked") Class<? extends Serializable> clazz = (Class<? extends Serializable>) Class.forName(valueType);
            value = DefaultTypeConverter.INSTANCE.convert(clazz, value);
        } catch (ClassNotFoundException e) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.value.classNotFound", valueType);
        } catch (Throwable e) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "audit.err.value.convertFailed", value, valueType);
        }
    }
    // Execute the query
    AuditQueryParameters params = new AuditQueryParameters();
    params.setApplicationName(appName);
    params.setFromTime(fromTime);
    params.setToTime(toTime);
    params.setFromId(fromId);
    params.setToId(toId);
    params.setUser(user);
    params.setForward(forward);
    if (path != null || value != null) {
        params.addSearchKey(path, value);
    }
    final List<Map<String, Object>> entries = new ArrayList<Map<String, Object>>(limit);
    AuditQueryCallback callback = new AuditQueryCallback() {

        @Override
        public boolean valuesRequired() {
            return verbose;
        }

        @Override
        public boolean handleAuditEntryError(Long entryId, String errorMsg, Throwable error) {
            return true;
        }

        @Override
        public boolean handleAuditEntry(Long entryId, String applicationName, String user, long time, Map<String, Serializable> values) {
            Map<String, Object> entry = new HashMap<String, Object>(11);
            entry.put(JSON_KEY_ENTRY_ID, entryId);
            entry.put(JSON_KEY_ENTRY_APPLICATION, applicationName);
            if (user != null) {
                entry.put(JSON_KEY_ENTRY_USER, user);
            }
            entry.put(JSON_KEY_ENTRY_TIME, new Date(time));
            if (values != null) {
                // Convert values to Strings
                Map<String, String> valueStrings = new HashMap<String, String>(values.size() * 2);
                for (Map.Entry<String, Serializable> mapEntry : values.entrySet()) {
                    String key = mapEntry.getKey();
                    Serializable value = mapEntry.getValue();
                    try {
                        String valueString = DefaultTypeConverter.INSTANCE.convert(String.class, value);
                        valueStrings.put(key, valueString);
                    } catch (ClassCastException e) {
                        // a ClassCastException.
                        if (!(value instanceof MLText)) {
                            // Rethrow if the exception was not caused by the expected MLText conversion.
                            throw e;
                        }
                        valueStrings.put(key, value.toString());
                    } catch (TypeConversionException e) {
                        // Use the toString()
                        valueStrings.put(key, value.toString());
                    }
                }
                entry.put(JSON_KEY_ENTRY_VALUES, valueStrings);
            }
            entries.add(entry);
            return true;
        }
    };
    auditService.auditQuery(callback, params, limit);
    model.put(JSON_KEY_ENTRY_COUNT, entries.size());
    model.put(JSON_KEY_ENTRIES, entries);
    // Done
    if (logger.isDebugEnabled()) {
        logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
    }
    return model;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) TypeConversionException(org.alfresco.service.cmr.repository.datatype.TypeConversionException) AuditQueryParameters(org.alfresco.service.cmr.audit.AuditQueryParameters) ArrayList(java.util.ArrayList) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) MLText(org.alfresco.service.cmr.repository.MLText) AuditQueryCallback(org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback) AuditApplication(org.alfresco.service.cmr.audit.AuditService.AuditApplication) Date(java.util.Date) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with TypeConversionException

use of org.alfresco.service.cmr.repository.datatype.TypeConversionException in project alfresco-remote-api by Alfresco.

the class TaskInstancePut method parseTaskProperties.

@SuppressWarnings("unchecked")
private Map<QName, Serializable> parseTaskProperties(JSONObject json, WorkflowTask workflowTask) throws JSONException {
    Map<QName, Serializable> props = new HashMap<QName, Serializable>();
    // gets the array of properties names
    String[] names = JSONObject.getNames(json);
    if (names != null) {
        // array is not empty
        for (String name : names) {
            // build the qname of property
            QName key = QName.createQName(name.replaceFirst("_", ":"), namespaceService);
            Object jsonValue = json.get(name);
            Serializable value = null;
            // process null values
            if (jsonValue.equals(JSONObject.NULL)) {
                props.put(key, null);
            } else {
                // gets the property definition from dictionary
                PropertyDefinition prop = dictionaryService.getProperty(key);
                if (prop != null) {
                    if (prop.isMultiValued() && jsonValue instanceof JSONArray) {
                        value = new ArrayList<Serializable>();
                        for (int i = 0; i < ((JSONArray) jsonValue).length(); i++) {
                            ((List<Serializable>) value).add((Serializable) DefaultTypeConverter.INSTANCE.convert(prop.getDataType(), ((JSONArray) jsonValue).get(i)));
                        }
                    } else {
                        // convert property using its data type specified in model
                        value = (Serializable) DefaultTypeConverter.INSTANCE.convert(prop.getDataType(), jsonValue);
                    }
                } else {
                    // property definition was not found in dictionary
                    if (jsonValue instanceof JSONArray) {
                        value = new ArrayList<String>();
                        for (int i = 0; i < ((JSONArray) jsonValue).length(); i++) {
                            ((List<String>) value).add(((JSONArray) jsonValue).getString(i));
                        }
                    } else {
                        // Otherwise, we try to convert the string
                        if (jsonValue instanceof String) {
                            // Check if the task already has the property, use that type.
                            Serializable existingValue = workflowTask.getProperties().get(key);
                            if (existingValue != null) {
                                try {
                                    value = DefaultTypeConverter.INSTANCE.convert(existingValue.getClass(), jsonValue);
                                } catch (TypeConversionException tce) {
                                // TODO: is this the right approach, ignoring exception?
                                // Ignore the exception, revert to using String-value
                                }
                            } else {
                                // Revert to using string-value
                                value = (String) jsonValue;
                            }
                        } else {
                            // Use the value provided by JSON
                            value = (Serializable) jsonValue;
                        }
                    }
                }
            }
            props.put(key, value);
        }
    }
    return props;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) TypeConversionException(org.alfresco.service.cmr.repository.datatype.TypeConversionException) QName(org.alfresco.service.namespace.QName) JSONArray(org.json.JSONArray) PropertyDefinition(org.alfresco.service.cmr.dictionary.PropertyDefinition) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TypeConversionException (org.alfresco.service.cmr.repository.datatype.TypeConversionException)2 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 AuditQueryParameters (org.alfresco.service.cmr.audit.AuditQueryParameters)1 AuditApplication (org.alfresco.service.cmr.audit.AuditService.AuditApplication)1 AuditQueryCallback (org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback)1 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)1 MLText (org.alfresco.service.cmr.repository.MLText)1 QName (org.alfresco.service.namespace.QName)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1