Search in sources :

Example 46 with PlainSchema

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

the class JPAAnySearchDAO method parseOrderBy.

private OrderBySupport parseOrderBy(final AnyTypeKind kind, final SearchSupport svs, final List<OrderByClause> orderBy) {
    AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
    OrderBySupport obs = new OrderBySupport();
    for (OrderByClause clause : filterOrderBy(orderBy)) {
        OrderBySupport.Item item = new OrderBySupport.Item();
        // Manage difference among external key attribute and internal JPA @Id
        String fieldName = "key".equals(clause.getField()) ? "id" : clause.getField();
        if (ReflectionUtils.findField(attrUtils.anyClass(), fieldName) == null) {
            PlainSchema schema = schemaDAO.find(fieldName);
            if (schema != null) {
                // keep track of involvement of non-mandatory schemas in the order by clauses
                obs.nonMandatorySchemas = !"true".equals(schema.getMandatoryCondition());
                if (schema.isUniqueConstraint()) {
                    obs.views.add(svs.uniqueAttr());
                    item.select = new StringBuilder().append(svs.uniqueAttr().alias).append('.').append(svs.fieldName(schema.getType())).append(" AS ").append(fieldName).toString();
                    item.where = new StringBuilder().append(svs.uniqueAttr().alias).append(".schema_id='").append(fieldName).append("'").toString();
                    item.orderBy = fieldName + " " + clause.getDirection().name();
                } else {
                    obs.views.add(svs.attr());
                    item.select = new StringBuilder().append(svs.attr().alias).append('.').append(svs.fieldName(schema.getType())).append(" AS ").append(fieldName).toString();
                    item.where = new StringBuilder().append(svs.attr().alias).append(".schema_id='").append(fieldName).append("'").toString();
                    item.orderBy = fieldName + " " + clause.getDirection().name();
                }
            }
        } else {
            // Adjust field name to column name
            fieldName = "realm".equals(fieldName) ? "realm_id" : fieldName;
            obs.views.add(svs.field());
            item.select = svs.field().alias + "." + fieldName;
            item.where = StringUtils.EMPTY;
            item.orderBy = svs.field().alias + "." + fieldName + " " + clause.getDirection().name();
        }
        if (item.isEmpty()) {
            LOG.warn("Cannot build any valid clause from {}", clause);
        } else {
            obs.items.add(item);
        }
    }
    return obs;
}
Also used : OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 47 with PlainSchema

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

the class AbstractAnyDAO method findByPlainAttrUniqueValue.

@Override
public A findByPlainAttrUniqueValue(final String schemaKey, final PlainAttrValue attrUniqueValue) {
    PlainSchema schema = plainSchemaDAO().find(schemaKey);
    if (schema == null) {
        LOG.error("Invalid schema name '{}'", schemaKey);
        return null;
    }
    if (!schema.isUniqueConstraint()) {
        LOG.error("This schema has not unique constraint: '{}'", schemaKey);
        return null;
    }
    List<A> result = findByPlainAttrValue(schemaKey, attrUniqueValue);
    return result.isEmpty() ? null : result.iterator().next();
}
Also used : PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema)

Example 48 with PlainSchema

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

the class AbstractAnyDAO method findByPlainAttrValue.

@Override
@SuppressWarnings("unchecked")
public List<A> findByPlainAttrValue(final String schemaKey, final PlainAttrValue attrValue) {
    PlainSchema schema = plainSchemaDAO().find(schemaKey);
    if (schema == null) {
        LOG.error("Invalid schema name '{}'", schemaKey);
        return Collections.<A>emptyList();
    }
    String entityName = schema.isUniqueConstraint() ? anyUtils().plainAttrUniqueValueClass().getName() : anyUtils().plainAttrValueClass().getName();
    Query query = findByPlainAttrValueQuery(entityName);
    query.setParameter("schemaKey", schemaKey);
    query.setParameter("stringValue", attrValue.getStringValue());
    query.setParameter("booleanValue", attrValue.getBooleanValue() == null ? null : ((AbstractPlainAttrValue) attrValue).getBooleanAsInteger(attrValue.getBooleanValue()));
    if (attrValue.getDateValue() == null) {
        query.setParameter("dateValue", null);
    } else {
        query.setParameter("dateValue", attrValue.getDateValue(), TemporalType.TIMESTAMP);
    }
    query.setParameter("longValue", attrValue.getLongValue());
    query.setParameter("doubleValue", attrValue.getDoubleValue());
    List<A> result = new ArrayList<>();
    ((List<PlainAttrValue>) query.getResultList()).stream().forEach(value -> {
        A any = (A) value.getAttr().getOwner();
        if (!result.contains(any)) {
            result.add(any);
        }
    });
    return result;
}
Also used : Query(javax.persistence.Query) ArrayList(java.util.ArrayList) AbstractPlainAttrValue(org.apache.syncope.core.persistence.jpa.entity.AbstractPlainAttrValue) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) AbstractPlainAttrValue(org.apache.syncope.core.persistence.jpa.entity.AbstractPlainAttrValue) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema)

Example 49 with PlainSchema

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

the class JPAPlainSchemaDAO method delete.

@Override
public void delete(final String key) {
    PlainSchema schema = find(key);
    if (schema == null) {
        return;
    }
    AnyUtilsFactory anyUtilsFactory = new JPAAnyUtilsFactory();
    for (AnyTypeKind anyTypeKind : AnyTypeKind.values()) {
        AnyUtils anyUtils = anyUtilsFactory.getInstance(anyTypeKind);
        for (PlainAttr<?> attr : findAttrs(schema, anyUtils.plainAttrClass())) {
            plainAttrDAO.delete(attr.getKey(), anyUtils.plainAttrClass());
        }
        resourceDAO().deleteMapping(key);
    }
    if (schema.getAnyTypeClass() != null) {
        schema.getAnyTypeClass().getPlainSchemas().remove(schema);
    }
    entityManager().remove(schema);
}
Also used : AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) JPAAnyUtilsFactory(org.apache.syncope.core.persistence.jpa.entity.JPAAnyUtilsFactory) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) JPAAnyUtilsFactory(org.apache.syncope.core.persistence.jpa.entity.JPAAnyUtilsFactory) 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)

Example 50 with PlainSchema

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

the class ConfTest method setAndDelete.

@Test
public void setAndDelete() {
    // 1. create CSChema
    PlainSchema useless = entityFactory.newEntity(PlainSchema.class);
    useless.setKey("useless");
    useless.setType(AttrSchemaType.Date);
    useless.setConversionPattern("yyyy-MM-dd");
    useless = plainSchemaDAO.save(useless);
    // 2. create conf
    CPlainAttr newConf = entityFactory.newEntity(CPlainAttr.class);
    newConf.setOwner(confDAO.get());
    newConf.setSchema(useless);
    add(newConf, "2014-06-20");
    confDAO.save(newConf);
    Optional<? extends CPlainAttr> actual = confDAO.find("useless");
    assertEquals(actual.get().getValuesAsStrings(), newConf.getValuesAsStrings());
    // 3. update conf
    newConf.getValues().clear();
    add(newConf, "2014-06-20");
    confDAO.save(newConf);
    actual = confDAO.find("useless");
    assertEquals(actual.get().getValuesAsStrings(), newConf.getValuesAsStrings());
    // 4. delete conf
    confDAO.delete("useless");
    assertFalse(confDAO.find("useless").isPresent());
}
Also used : CPlainAttr(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttr) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)55 Test (org.junit.jupiter.api.Test)22 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)21 AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)15 VirSchema (org.apache.syncope.core.persistence.api.entity.VirSchema)14 User (org.apache.syncope.core.persistence.api.entity.user.User)13 DerSchema (org.apache.syncope.core.persistence.api.entity.DerSchema)12 UPlainAttr (org.apache.syncope.core.persistence.api.entity.user.UPlainAttr)11 ArrayList (java.util.ArrayList)10 PlainAttrValue (org.apache.syncope.core.persistence.api.entity.PlainAttrValue)10 Transactional (org.springframework.transaction.annotation.Transactional)10 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)9 AnyTypeClass (org.apache.syncope.core.persistence.api.entity.AnyTypeClass)9 MappingItem (org.apache.syncope.core.persistence.api.entity.resource.MappingItem)9 Collections (java.util.Collections)8 HashSet (java.util.HashSet)8 List (java.util.List)8 Optional (java.util.Optional)8 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)8 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)8