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;
}
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;
}
Aggregations