use of org.qi4j.api.constraint.Name in project qi4j-sdk by Qi4j.
the class ContextResource method formForMethod.
private Form formForMethod(Method interactionMethod) {
Form form = new Form();
Form queryAsForm = Request.getCurrent().getResourceRef().getQueryAsForm();
Form entityAsForm;
Representation representation = Request.getCurrent().getEntity();
if (representation != null && !EmptyRepresentation.class.isInstance(representation)) {
entityAsForm = new Form(representation);
} else {
entityAsForm = new Form();
}
Class<?> valueType = interactionMethod.getParameterTypes()[0];
if (ValueComposite.class.isAssignableFrom(valueType)) {
ValueDescriptor valueDescriptor = module.valueDescriptor(valueType.getName());
for (PropertyDescriptor propertyDescriptor : valueDescriptor.state().properties()) {
String value = getValue(propertyDescriptor.qualifiedName().name(), queryAsForm, entityAsForm);
if (value == null) {
Object initialValue = propertyDescriptor.initialValue(module);
if (initialValue != null) {
value = initialValue.toString();
}
}
form.add(propertyDescriptor.qualifiedName().name(), value);
}
} else if (valueType.isInterface() && interactionMethod.getParameterTypes().length == 1) {
// Single entity as input
form.add("entity", getValue("entity", queryAsForm, entityAsForm));
} else {
// Construct form out of individual parameters instead
for (Annotation[] annotations : interactionMethod.getParameterAnnotations()) {
Name name = (Name) first(filter(isType(Name.class), iterable(annotations)));
form.add(name.value(), getValue(name.value(), queryAsForm, entityAsForm));
}
}
return form;
}
use of org.qi4j.api.constraint.Name in project qi4j-sdk by Qi4j.
the class CompositeAssemblyImpl method constraintsFor.
// Model
private ConstraintsModel constraintsFor(Method method, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
List<ValueConstraintsModel> parameterConstraintModels = Collections.emptyList();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
Type[] parameterTypes = method.getGenericParameterTypes();
boolean constrained = false;
for (int i = 0; i < parameterAnnotations.length; i++) {
Annotation[] parameterAnnotation = parameterAnnotations[i];
Name nameAnnotation = (Name) first(filter(isType(Name.class), iterable(parameterAnnotation)));
String name = nameAnnotation == null ? "param" + (i + 1) : nameAnnotation.value();
boolean optional = first(filter(isType(Optional.class), iterable(parameterAnnotation))) != null;
ValueConstraintsModel parameterConstraintsModel = constraintsFor(asList(parameterAnnotation), parameterTypes[i], name, optional, constraintClasses, method);
if (parameterConstraintsModel.isConstrained()) {
constrained = true;
}
if (parameterConstraintModels.isEmpty()) {
parameterConstraintModels = new ArrayList<>();
}
parameterConstraintModels.add(parameterConstraintsModel);
}
if (!constrained) {
return new ConstraintsModel(Collections.<ValueConstraintsModel>emptyList());
} else {
return new ConstraintsModel(parameterConstraintModels);
}
}
use of org.qi4j.api.constraint.Name in project qi4j-sdk by Qi4j.
the class DefaultRequestReader method parseMethodArguments.
@SuppressWarnings("unchecked")
private void parseMethodArguments(Method method, Object[] args, Form queryAsForm, Form entityAsForm) {
// Parse each argument separately using the @Name annotation as help
int idx = 0;
for (Annotation[] annotations : method.getParameterAnnotations()) {
Name name = (Name) first(filter(isType(Name.class), iterable(annotations)));
if (name == null) {
throw new IllegalStateException("No @Name annotation found on parameter of method:" + method);
}
String argString = getValue(name.value(), queryAsForm, entityAsForm);
// Parameter conversion
Class<?> parameterType = method.getParameterTypes()[idx];
Object arg = null;
if (parameterType.equals(String.class)) {
arg = argString;
} else if (parameterType.equals(EntityReference.class)) {
arg = EntityReference.parseEntityReference(argString);
} else if (parameterType.isEnum()) {
arg = Enum.valueOf((Class<Enum>) parameterType, argString);
} else if (Integer.TYPE.isAssignableFrom(parameterType)) {
arg = Integer.valueOf(argString);
} else if (Integer.class.isAssignableFrom(parameterType)) {
if (argString != null) {
arg = Integer.valueOf(argString);
}
} else if (Long.TYPE.isAssignableFrom(parameterType)) {
arg = Long.valueOf(argString);
} else if (Long.class.isAssignableFrom(parameterType)) {
if (argString != null) {
arg = Long.valueOf(argString);
}
} else if (Short.TYPE.isAssignableFrom(parameterType)) {
arg = Short.valueOf(argString);
} else if (Short.class.isAssignableFrom(parameterType)) {
if (argString != null) {
arg = Short.valueOf(argString);
}
} else if (Double.TYPE.isAssignableFrom(parameterType)) {
arg = Double.valueOf(argString);
} else if (Double.class.isAssignableFrom(parameterType)) {
if (argString != null) {
arg = Double.valueOf(argString);
}
} else if (Float.TYPE.isAssignableFrom(parameterType)) {
arg = Float.valueOf(argString);
} else if (Float.class.isAssignableFrom(parameterType)) {
if (argString != null) {
arg = Float.valueOf(argString);
}
} else if (Character.TYPE.isAssignableFrom(parameterType)) {
arg = argString.charAt(0);
} else if (Character.class.isAssignableFrom(parameterType)) {
if (argString != null) {
arg = argString.charAt(0);
}
} else if (Boolean.TYPE.isAssignableFrom(parameterType)) {
arg = Boolean.valueOf(argString);
} else if (Boolean.class.isAssignableFrom(parameterType)) {
if (argString != null) {
arg = Boolean.valueOf(argString);
}
} else if (Date.class.isAssignableFrom(parameterType)) {
arg = Dates.fromString(argString);
} else if (parameterType.isInterface()) {
arg = module.currentUnitOfWork().get(parameterType, argString);
} else {
throw new IllegalArgumentException("Don't know how to parse parameter " + name.value() + " of type " + parameterType.getName());
}
if (arg == null && !matchesAny(isType(Optional.class), iterable(annotations))) {
throw new IllegalArgumentException("Parameter " + name.value() + " was not set");
}
args[idx++] = arg;
}
}
Aggregations