use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class ServicesControllerManager method _invokeServiceMethod.
@Nullable
protected ServiceCallResult _invokeServiceMethod(String serviceName, String methodName, List<String> paramNames, List<String> paramValuesStr, String modelVersion) {
Object service = AppBeans.get(serviceName);
RestServicesConfiguration.RestMethodInfo restMethodInfo = restServicesConfiguration.getRestMethodInfo(serviceName, methodName, paramNames);
if (restMethodInfo == null) {
throw new RestAPIException("Service method not found", serviceName + "." + methodName + "(" + paramNames.stream().collect(Collectors.joining(",")) + ")", HttpStatus.NOT_FOUND);
}
Method serviceMethod = restMethodInfo.getMethod();
List<Object> paramValues = new ArrayList<>();
Type[] types = restMethodInfo.getMethod().getGenericParameterTypes();
for (int i = 0; i < types.length; i++) {
int idx = i;
try {
idx = paramNames.indexOf(restMethodInfo.getParams().get(i).getName());
paramValues.add(restParseUtils.toObject(types[i], paramValuesStr.get(idx), modelVersion));
} catch (Exception e) {
log.error("Error on parsing service param value", e);
throw new RestAPIException("Invalid parameter value", "Invalid parameter value for " + paramNames.get(idx), HttpStatus.BAD_REQUEST);
}
}
Object methodResult;
try {
methodResult = serviceMethod.invoke(service, paramValues.toArray());
} catch (InvocationTargetException | IllegalAccessException ex) {
if (ex.getCause() instanceof ValidationException) {
throw (ValidationException) ex.getCause();
} else {
log.error("Error on service method invoke", ex.getCause());
throw new RestAPIException("Error on service method invoke", "", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
if (methodResult == null) {
return null;
}
Class<?> methodReturnType = serviceMethod.getReturnType();
if (Entity.class.isAssignableFrom(methodReturnType)) {
Entity entity = (Entity) methodResult;
restControllerUtils.applyAttributesSecurity(entity);
String entityJson = entitySerializationAPI.toJson(entity, null, EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
entityJson = restControllerUtils.transformJsonIfRequired(entity.getMetaClass().getName(), modelVersion, JsonTransformationDirection.TO_VERSION, entityJson);
return new ServiceCallResult(entityJson, true);
} else if (Collection.class.isAssignableFrom(methodReturnType)) {
Class returnTypeArgument = getMethodReturnTypeArgument(serviceMethod);
if ((returnTypeArgument != null && Entity.class.isAssignableFrom(returnTypeArgument)) || isEntitiesCollection((Collection) methodResult)) {
Collection<? extends Entity> entities = (Collection<? extends Entity>) methodResult;
entities.forEach(entity -> restControllerUtils.applyAttributesSecurity(entity));
String entitiesJson = entitySerializationAPI.toJson(entities, null, EntitySerializationOption.SERIALIZE_INSTANCE_NAME);
if (returnTypeArgument != null) {
MetaClass metaClass = metadata.getClass(returnTypeArgument);
if (metaClass != null) {
entitiesJson = restControllerUtils.transformJsonIfRequired(metaClass.getName(), modelVersion, JsonTransformationDirection.TO_VERSION, entitiesJson);
} else {
log.error("MetaClass for service collection parameter type {} not found", returnTypeArgument);
}
}
return new ServiceCallResult(entitiesJson, true);
} else {
return new ServiceCallResult(restParseUtils.serialize(methodResult), true);
}
} else {
Datatype<?> datatype = Datatypes.get(methodReturnType);
if (datatype != null) {
return new ServiceCallResult(datatype.format(methodResult), false);
} else {
return new ServiceCallResult(restParseUtils.serialize(methodResult), true);
}
}
}
use of com.haulmont.chile.core.datatypes.Datatype in project cuba by cuba-platform.
the class WebAbstractTextField method getValue.
@Override
public <V> V getValue() {
String value = super.getValue();
if (isTrimming()) {
value = StringUtils.trim(value);
}
value = Strings.emptyToNull(value);
Datatype datatype = getActualDatatype();
if (value != null && datatype != null) {
try {
return (V) datatype.parse(value, locale);
} catch (ParseException e) {
Logger log = LoggerFactory.getLogger(WebAbstractTextField.class);
log.debug("Unable to parse value of component {}\n{}", getId(), e.getMessage());
return null;
}
} else {
return (V) value;
}
}
Aggregations