Search in sources :

Example 1 with Name

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;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Form(org.restlet.data.Form) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) Name(org.qi4j.api.constraint.Name)

Example 2 with Name

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);
    }
}
Also used : Optional(org.qi4j.api.common.Optional) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) ConstraintsModel(org.qi4j.runtime.composite.ConstraintsModel) Constraint(org.qi4j.api.constraint.Constraint) Annotations.hasAnnotation(org.qi4j.api.util.Annotations.hasAnnotation) Annotation(java.lang.annotation.Annotation) QualifiedName(org.qi4j.api.common.QualifiedName) Name(org.qi4j.api.constraint.Name) Annotations.isType(org.qi4j.api.util.Annotations.isType) Type(java.lang.reflect.Type) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel)

Example 3 with Name

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;
    }
}
Also used : Optional(org.qi4j.api.common.Optional) Annotation(java.lang.annotation.Annotation) Date(java.util.Date) Name(org.qi4j.api.constraint.Name) EntityReference(org.qi4j.api.entity.EntityReference)

Aggregations

Name (org.qi4j.api.constraint.Name)3 Annotation (java.lang.annotation.Annotation)2 Optional (org.qi4j.api.common.Optional)2 Type (java.lang.reflect.Type)1 Date (java.util.Date)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 Constraint (org.qi4j.api.constraint.Constraint)1 EntityReference (org.qi4j.api.entity.EntityReference)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 Annotations.hasAnnotation (org.qi4j.api.util.Annotations.hasAnnotation)1 Annotations.isType (org.qi4j.api.util.Annotations.isType)1 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)1 ConstraintsModel (org.qi4j.runtime.composite.ConstraintsModel)1 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)1 Form (org.restlet.data.Form)1 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)1 Representation (org.restlet.representation.Representation)1 StringRepresentation (org.restlet.representation.StringRepresentation)1