Search in sources :

Example 1 with JPAPlainSchema

use of org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema in project syncope by apache.

the class AbstractAnySearchDAO method check.

protected Triple<PlainSchema, PlainAttrValue, AnyCond> check(final AnyCond cond, final AnyTypeKind kind) {
    AnyCond condClone = SerializationUtils.clone(cond);
    AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
    // Keeps track of difference between entity's getKey() and JPA @Id fields
    if ("key".equals(condClone.getSchema())) {
        condClone.setSchema("id");
    }
    Field anyField = ReflectionUtils.findField(attrUtils.anyClass(), condClone.getSchema());
    if (anyField == null) {
        LOG.warn("Ignoring invalid schema '{}'", condClone.getSchema());
        throw new IllegalArgumentException();
    }
    PlainSchema schema = new JPAPlainSchema();
    schema.setKey(anyField.getName());
    for (AttrSchemaType attrSchemaType : AttrSchemaType.values()) {
        if (anyField.getType().isAssignableFrom(attrSchemaType.getType())) {
            schema.setType(attrSchemaType);
        }
    }
    // Deal with any Integer fields logically mapping to boolean values
    boolean foundBooleanMin = false;
    boolean foundBooleanMax = false;
    if (Integer.class.equals(anyField.getType())) {
        for (Annotation annotation : anyField.getAnnotations()) {
            if (Min.class.equals(annotation.annotationType())) {
                foundBooleanMin = ((Min) annotation).value() == 0;
            } else if (Max.class.equals(annotation.annotationType())) {
                foundBooleanMax = ((Max) annotation).value() == 1;
            }
        }
    }
    if (foundBooleanMin && foundBooleanMax) {
        schema.setType(AttrSchemaType.Boolean);
    }
    // Deal with any fields representing relationships to other entities
    if (anyField.getType().getAnnotation(Entity.class) != null) {
        Method relMethod = null;
        try {
            relMethod = ClassUtils.getPublicMethod(anyField.getType(), "getKey", new Class<?>[0]);
        } catch (Exception e) {
            LOG.error("Could not find {}#getKey", anyField.getType(), e);
        }
        if (relMethod != null && String.class.isAssignableFrom(relMethod.getReturnType())) {
            condClone.setSchema(condClone.getSchema() + "_id");
            schema.setType(AttrSchemaType.String);
        }
    }
    PlainAttrValue attrValue = attrUtils.newPlainAttrValue();
    if (condClone.getType() != AttributeCond.Type.LIKE && condClone.getType() != AttributeCond.Type.ILIKE && condClone.getType() != AttributeCond.Type.ISNULL && condClone.getType() != AttributeCond.Type.ISNOTNULL) {
        try {
            ((JPAPlainSchema) schema).validator().validate(condClone.getExpression(), attrValue);
        } catch (ValidationException e) {
            LOG.error("Could not validate expression '" + condClone.getExpression() + "'", e);
            throw new IllegalArgumentException();
        }
    }
    return Triple.of(schema, attrValue, condClone);
}
Also used : Entity(javax.persistence.Entity) ValidationException(javax.validation.ValidationException) Max(javax.validation.constraints.Max) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) ValidationException(javax.validation.ValidationException) Field(java.lang.reflect.Field) Min(javax.validation.constraints.Min) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 2 with JPAPlainSchema

use of org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema in project syncope by apache.

the class AbstractAnySearchDAO method check.

protected Pair<PlainSchema, PlainAttrValue> check(final AttributeCond cond, final AnyTypeKind kind) {
    AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
    PlainSchema schema = schemaDAO.find(cond.getSchema());
    if (schema == null) {
        LOG.warn("Ignoring invalid schema '{}'", cond.getSchema());
        throw new IllegalArgumentException();
    }
    PlainAttrValue attrValue = attrUtils.newPlainAttrValue();
    try {
        if (cond.getType() != AttributeCond.Type.LIKE && cond.getType() != AttributeCond.Type.ILIKE && cond.getType() != AttributeCond.Type.ISNULL && cond.getType() != AttributeCond.Type.ISNOTNULL) {
            ((JPAPlainSchema) schema).validator().validate(cond.getExpression(), attrValue);
        }
    } catch (ValidationException e) {
        LOG.error("Could not validate expression '" + cond.getExpression() + "'", e);
        throw new IllegalArgumentException();
    }
    return Pair.of(schema, attrValue);
}
Also used : ValidationException(javax.validation.ValidationException) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Aggregations

ValidationException (javax.validation.ValidationException)2 AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)2 PlainAttrValue (org.apache.syncope.core.persistence.api.entity.PlainAttrValue)2 PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)2 JPAPlainSchema (org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Entity (javax.persistence.Entity)1 Max (javax.validation.constraints.Max)1 Min (javax.validation.constraints.Min)1 AttrSchemaType (org.apache.syncope.common.lib.types.AttrSchemaType)1 AnyCond (org.apache.syncope.core.persistence.api.dao.search.AnyCond)1