use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.
the class ConfigurationLogic method delete.
@PreAuthorize("hasRole('" + StandardEntitlement.CONFIGURATION_DELETE + "')")
public void delete(final String schema) {
Optional<? extends CPlainAttr> conf = confDAO.find(schema);
if (!conf.isPresent()) {
PlainSchema plainSchema = plainSchemaDAO.find(schema);
if (plainSchema == null) {
throw new NotFoundException("Configuration schema " + schema);
}
}
confDAO.delete(schema);
}
use of org.apache.syncope.core.persistence.api.entity.PlainSchema 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);
}
use of org.apache.syncope.core.persistence.api.entity.PlainSchema 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);
}
use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.
the class AbstractAnyDAO method getWhereClause.
/**
* Generate one where clause for each different attribute schema into the derived schema expression provided.
*
* @param expression derived schema expression
* @param value derived attribute value
* @param attrUtils USER / GROUP
* @return where clauses to use to build the query
*/
private Set<String> getWhereClause(final String expression, final String value) {
Parser parser = new Parser(new StringReader(expression));
// Schema names
List<String> identifiers = new ArrayList<>();
// Literals
List<String> literals = new ArrayList<>();
// Get schema names and literals
for (Token token = parser.getNextToken(); token != null && StringUtils.isNotBlank(token.toString()); token = parser.getNextToken()) {
if (token.kind == ParserConstants.STRING_LITERAL) {
literals.add(token.toString().substring(1, token.toString().length() - 1));
}
if (token.kind == ParserConstants.IDENTIFIER) {
identifiers.add(token.toString());
}
}
// Sort literals in order to process later literals included into others
Collections.sort(literals, (final String t, final String t1) -> {
if (t == null && t1 == null) {
return 0;
} else if (t != null && t1 == null) {
return -1;
} else if (t == null && t1 != null) {
return 1;
} else if (t.length() == t1.length()) {
return 0;
} else if (t.length() > t1.length()) {
return -1;
} else {
return 1;
}
});
// Split value on provided literals
List<String> attrValues = split(value, literals);
if (attrValues.size() != identifiers.size()) {
LOG.error("Ambiguous JEXL expression resolution: literals and values have different size");
return Collections.emptySet();
}
// clauses to be used with INTERSECTed queries
Set<String> clauses = new HashSet<>();
// builder to build the clauses
StringBuilder bld = new StringBuilder();
// Contains used identifiers in order to avoid replications
Set<String> used = new HashSet<>();
// Create several clauses: one for eanch identifiers
for (int i = 0; i < identifiers.size(); i++) {
if (!used.contains(identifiers.get(i))) {
// verify schema existence and get schema type
PlainSchema schema = plainSchemaDAO().find(identifiers.get(i));
if (schema == null) {
LOG.error("Invalid schema '{}', ignoring", identifiers.get(i));
} else {
// clear builder
bld.delete(0, bld.length());
bld.append("(");
// set schema name
bld.append("s.id = '").append(identifiers.get(i)).append("'");
bld.append(" AND ");
bld.append("s.id = a.schema_id").append(" AND ");
bld.append("a.id = v.attribute_id");
bld.append(" AND ");
// use a value clause different for eanch different schema type
switch(schema.getType()) {
case Boolean:
bld.append("v.booleanValue = '").append(attrValues.get(i)).append("'");
break;
case Long:
bld.append("v.longValue = ").append(attrValues.get(i));
break;
case Double:
bld.append("v.doubleValue = ").append(attrValues.get(i));
break;
case Date:
bld.append("v.dateValue = '").append(attrValues.get(i)).append("'");
break;
default:
bld.append("v.stringValue = '").append(attrValues.get(i)).append("'");
}
bld.append(")");
used.add(identifiers.get(i));
clauses.add(bld.toString());
}
}
}
LOG.debug("Generated where clauses {}", clauses);
return clauses;
}
use of org.apache.syncope.core.persistence.api.entity.PlainSchema in project syncope by apache.
the class AnyTypeClassTest method delete.
@Test
public void delete() {
AnyTypeClass minimalUser = anyTypeClassDAO.find("minimal user");
assertNotNull(minimalUser);
PlainSchema surname = plainSchemaDAO.find("surname");
assertNotNull(surname);
assertTrue(minimalUser.getPlainSchemas().contains(surname));
int before = minimalUser.getPlainSchemas().size();
plainSchemaDAO.delete("surname");
anyTypeClassDAO.flush();
minimalUser = anyTypeClassDAO.find("minimal user");
assertNotNull(minimalUser);
assertEquals(before, minimalUser.getPlainSchemas().size() + 1);
}
Aggregations