Search in sources :

Example 16 with DerSchema

use of org.apache.syncope.core.persistence.api.entity.DerSchema in project syncope by apache.

the class SchemaDataBinderImpl method fill.

// --------------- DERIVED -----------------
private DerSchema fill(final DerSchema schema, final DerSchemaTO schemaTO) {
    SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
    if (StringUtils.isBlank(schemaTO.getExpression())) {
        SyncopeClientException requiredValuesMissing = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
        requiredValuesMissing.getElements().add("expression");
        scce.addException(requiredValuesMissing);
    } else if (!JexlUtils.isExpressionValid(schemaTO.getExpression())) {
        SyncopeClientException e = SyncopeClientException.build(ClientExceptionType.InvalidValues);
        e.getElements().add(schemaTO.getExpression());
        scce.addException(e);
    }
    if (scce.hasExceptions()) {
        throw scce;
    }
    BeanUtils.copyProperties(schemaTO, schema, IGNORE_PROPERTIES);
    DerSchema merged = derSchemaDAO.save(schema);
    if (schemaTO.getAnyTypeClass() != null && (merged.getAnyTypeClass() == null || !schemaTO.getAnyTypeClass().equals(merged.getAnyTypeClass().getKey()))) {
        AnyTypeClass anyTypeClass = anyTypeClassDAO.find(schemaTO.getAnyTypeClass());
        if (anyTypeClass == null) {
            LOG.debug("Invalid " + AnyTypeClass.class.getSimpleName() + "{}, ignoring...", schemaTO.getAnyTypeClass());
        } else {
            anyTypeClass.add(merged);
            merged.setAnyTypeClass(anyTypeClass);
        }
    } else if (schemaTO.getAnyTypeClass() == null && merged.getAnyTypeClass() != null) {
        merged.getAnyTypeClass().getDerSchemas().remove(merged);
        merged.setAnyTypeClass(null);
    }
    return merged;
}
Also used : SyncopeClientCompositeException(org.apache.syncope.common.lib.SyncopeClientCompositeException) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass)

Example 17 with DerSchema

use of org.apache.syncope.core.persistence.api.entity.DerSchema in project syncope by apache.

the class SchemaLogic method create.

@PreAuthorize("hasRole('" + StandardEntitlement.SCHEMA_CREATE + "')")
@SuppressWarnings("unchecked")
public <T extends SchemaTO> T create(final SchemaType schemaType, final T schemaTO) {
    if (StringUtils.isBlank(schemaTO.getKey())) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
        sce.getElements().add("Schema key");
        throw sce;
    }
    if (doesSchemaExist(schemaType, schemaTO.getKey())) {
        throw new DuplicateException(schemaType + "/" + schemaTO.getKey());
    }
    T created;
    switch(schemaType) {
        case VIRTUAL:
            VirSchema virSchema = virSchemaDAO.save(binder.create((VirSchemaTO) schemaTO));
            created = (T) binder.getVirSchemaTO(virSchema);
            break;
        case DERIVED:
            DerSchema derSchema = derSchemaDAO.save(binder.create((DerSchemaTO) schemaTO));
            created = (T) binder.getDerSchemaTO(derSchema);
            break;
        case PLAIN:
        default:
            PlainSchema plainSchema = plainSchemaDAO.save(binder.create((PlainSchemaTO) schemaTO));
            created = (T) binder.getPlainSchemaTO(plainSchema);
    }
    return created;
}
Also used : PlainSchemaTO(org.apache.syncope.common.lib.to.PlainSchemaTO) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) DuplicateException(org.apache.syncope.core.persistence.api.dao.DuplicateException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) VirSchemaTO(org.apache.syncope.common.lib.to.VirSchemaTO) DerSchemaTO(org.apache.syncope.common.lib.to.DerSchemaTO) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 18 with DerSchema

use of org.apache.syncope.core.persistence.api.entity.DerSchema in project syncope by apache.

the class SchemaLogic method search.

@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public <T extends SchemaTO> List<T> search(final SchemaType schemaType, final List<String> anyTypeClasses, final String keyword) {
    List<AnyTypeClass> classes = new ArrayList<>(anyTypeClasses == null ? 0 : anyTypeClasses.size());
    if (anyTypeClasses != null) {
        anyTypeClasses.remove(AnyTypeKind.USER.name());
        anyTypeClasses.remove(AnyTypeKind.GROUP.name());
        anyTypeClasses.forEach(anyTypeClass -> {
            AnyTypeClass clazz = anyTypeClassDAO.find(anyTypeClass);
            if (clazz == null) {
                LOG.warn("Ignoring invalid {}: {}", AnyTypeClass.class.getSimpleName(), anyTypeClass);
            } else {
                classes.add(clazz);
            }
        });
    }
    List<T> result;
    switch(schemaType) {
        case VIRTUAL:
            result = (classes.isEmpty() ? keyword == null ? virSchemaDAO.findAll() : virSchemaDAO.findByKeyword(keyword) : virSchemaDAO.findByAnyTypeClasses(classes)).stream().map(new Function<VirSchema, T>() {

                @Override
                public T apply(final VirSchema schema) {
                    return (T) binder.getVirSchemaTO(schema);
                }
            }).collect(Collectors.toList());
            break;
        case DERIVED:
            result = (classes.isEmpty() ? keyword == null ? derSchemaDAO.findAll() : derSchemaDAO.findByKeyword(keyword) : derSchemaDAO.findByAnyTypeClasses(classes)).stream().map(new Function<DerSchema, T>() {

                @Override
                public T apply(final DerSchema schema) {
                    return (T) binder.getDerSchemaTO(schema);
                }
            }).collect(Collectors.toList());
            break;
        case PLAIN:
        default:
            result = (classes.isEmpty() ? keyword == null ? plainSchemaDAO.findAll() : plainSchemaDAO.findByKeyword(keyword) : plainSchemaDAO.findByAnyTypeClasses(classes)).stream().map(new Function<PlainSchema, T>() {

                @Override
                public T apply(final PlainSchema schema) {
                    return (T) binder.getPlainSchemaTO(schema);
                }
            }).collect(Collectors.toList());
    }
    return result;
}
Also used : DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) ArrayList(java.util.ArrayList) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with DerSchema

use of org.apache.syncope.core.persistence.api.entity.DerSchema in project syncope by apache.

the class JPAAnyTypeClassDAO method delete.

@Override
public void delete(final String key) {
    AnyTypeClass anyTypeClass = find(key);
    if (anyTypeClass == null) {
        return;
    }
    for (PlainSchema schema : plainSchemaDAO.findByAnyTypeClasses(Collections.singletonList(anyTypeClass))) {
        schema.setAnyTypeClass(null);
    }
    for (DerSchema schema : derSchemaDAO.findByAnyTypeClasses(Collections.singletonList(anyTypeClass))) {
        schema.setAnyTypeClass(null);
    }
    for (VirSchema schema : virSchemaDAO.findByAnyTypeClasses(Collections.singletonList(anyTypeClass))) {
        schema.setAnyTypeClass(null);
    }
    for (AnyType type : anyTypeDAO.findByTypeClass(anyTypeClass)) {
        type.getClasses().remove(anyTypeClass);
    }
    for (TypeExtension typeExt : groupDAO.findTypeExtensions(anyTypeClass)) {
        typeExt.getAuxClasses().remove(anyTypeClass);
        if (typeExt.getAuxClasses().isEmpty()) {
            typeExt.getGroup().getTypeExtensions().remove(typeExt);
            typeExt.setGroup(null);
        }
    }
    for (Provision provision : resourceDAO.findProvisionsByAuxClass(anyTypeClass)) {
        provision.getAuxClasses().remove(anyTypeClass);
    }
    entityManager().remove(anyTypeClass);
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) TypeExtension(org.apache.syncope.core.persistence.api.entity.group.TypeExtension) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyTypeClass(org.apache.syncope.core.persistence.api.entity.AnyTypeClass) JPAAnyTypeClass(org.apache.syncope.core.persistence.jpa.entity.JPAAnyTypeClass) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType)

Example 20 with DerSchema

use of org.apache.syncope.core.persistence.api.entity.DerSchema in project syncope by apache.

the class AbstractAnyDAO method findByDerAttrValue.

@Override
public List<A> findByDerAttrValue(final String schemaKey, final String value) {
    DerSchema schema = derSchemaDAO().find(schemaKey);
    if (schema == null) {
        LOG.error("Invalid schema name '{}'", schemaKey);
        return Collections.<A>emptyList();
    }
    // query string
    StringBuilder querystring = new StringBuilder();
    boolean subquery = false;
    for (String clause : getWhereClause(schema.getExpression(), value)) {
        if (querystring.length() > 0) {
            subquery = true;
            querystring.append(" AND a.owner_id IN ( ");
        }
        querystring.append("SELECT a.owner_id ").append("FROM ").append(anyUtils().plainAttrClass().getSimpleName().substring(3)).append(" a, ").append(anyUtils().plainAttrValueClass().getSimpleName().substring(3)).append(" v, ").append(PlainSchema.class.getSimpleName()).append(" s ").append("WHERE ").append(clause);
        if (subquery) {
            querystring.append(')');
        }
    }
    List<A> result = new ArrayList<>();
    if (querystring.length() > 0) {
        Query query = entityManager().createNativeQuery(querystring.toString());
        for (Object anyKey : query.getResultList()) {
            A any = find(anyKey.toString());
            if (!result.contains(any)) {
                result.add(any);
            }
        }
    }
    return result;
}
Also used : DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject)

Aggregations

DerSchema (org.apache.syncope.core.persistence.api.entity.DerSchema)22 VirSchema (org.apache.syncope.core.persistence.api.entity.VirSchema)10 PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)8 Test (org.junit.jupiter.api.Test)6 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)5 ArrayList (java.util.ArrayList)4 AnyTypeClass (org.apache.syncope.core.persistence.api.entity.AnyTypeClass)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 List (java.util.List)3 DerSchemaTO (org.apache.syncope.common.lib.to.DerSchemaTO)3 PlainSchemaTO (org.apache.syncope.common.lib.to.PlainSchemaTO)3 VirSchemaTO (org.apache.syncope.common.lib.to.VirSchemaTO)3 User (org.apache.syncope.core.persistence.api.entity.user.User)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ParseException (java.text.ParseException)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 JexlContext (org.apache.commons.jexl3.JexlContext)2 MapContext (org.apache.commons.jexl3.MapContext)2 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)2