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;
}
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();
}
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;
}
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);
}
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());
}
Aggregations