use of io.jans.orm.annotation.AttributeEnum in project jans by JanssenProject.
the class BaseEntryManager method setPropertyValue.
private void setPropertyValue(String propertyName, Setter propertyValueSetter, Object entry, AttributeData attribute, boolean jsonObject) {
if (attribute == null) {
return;
}
LOG.debug(String.format("LdapProperty: %s, AttributeName: %s, AttributeValue: %s", propertyName, attribute.getName(), Arrays.toString(attribute.getValues())));
Class<?> parameterType = ReflectHelper.getSetterType(propertyValueSetter);
if (parameterType.equals(String.class)) {
Object value = attribute.getValue();
if (value instanceof Date) {
value = encodeTime((Date) value);
}
propertyValueSetter.set(entry, String.valueOf(value));
} else if (parameterType.equals(Boolean.class) || parameterType.equals(Boolean.TYPE)) {
propertyValueSetter.set(entry, toBooleanValue(attribute));
} else if (parameterType.equals(Integer.class) || parameterType.equals(Integer.TYPE)) {
propertyValueSetter.set(entry, toIntegerValue(attribute));
} else if (parameterType.equals(Long.class) || parameterType.equals(Long.TYPE)) {
propertyValueSetter.set(entry, toLongValue(attribute));
} else if (parameterType.equals(Date.class)) {
if (attribute.getValue() == null) {
propertyValueSetter.set(entry, null);
} else {
propertyValueSetter.set(entry, attribute.getValue() instanceof Date ? (Date) attribute.getValue() : decodeTime(String.valueOf(attribute.getValue())));
}
} else if (parameterType.equals(String[].class)) {
propertyValueSetter.set(entry, attribute.getStringValues());
} else if (ReflectHelper.assignableFrom(parameterType, List.class)) {
if (jsonObject) {
Object[] values = attribute.getValues();
List<Object> jsonValues = new ArrayList<Object>(values.length);
for (Object value : values) {
Object jsonValue = convertJsonToValue(ReflectHelper.getListType(propertyValueSetter), value);
jsonValues.add(jsonValue);
}
propertyValueSetter.set(entry, jsonValues);
} else {
List<?> resultValues = attributeToTypedList(ReflectHelper.getListType(propertyValueSetter), attribute);
propertyValueSetter.set(entry, resultValues);
}
} else if (ReflectHelper.assignableFrom(parameterType, AttributeEnum.class)) {
try {
propertyValueSetter.set(entry, parameterType.getMethod("resolveByValue", String.class).invoke(parameterType.getEnumConstants()[0], attribute.getValue()));
} catch (Exception ex) {
throw new MappingException("Failed to resolve Enum '" + parameterType + "' by value '" + attribute.getValue() + "'", ex);
}
} else if (ReflectHelper.assignableFrom(parameterType, AttributeEnum[].class)) {
Class<?> itemType = parameterType.getComponentType();
Method enumResolveByValue;
try {
enumResolveByValue = itemType.getMethod("resolveByValue", String.class);
} catch (Exception ex) {
throw new MappingException("Failed to resolve Enum '" + parameterType + "' by value '" + Arrays.toString(attribute.getValues()) + "'", ex);
}
Object[] attributeValues = attribute.getValues();
AttributeEnum[] ldapEnums = (AttributeEnum[]) ReflectHelper.createArray(itemType, attributeValues.length);
for (int i = 0; i < attributeValues.length; i++) {
try {
ldapEnums[i] = (AttributeEnum) enumResolveByValue.invoke(itemType.getEnumConstants()[0], attributeValues[i]);
} catch (Exception ex) {
throw new MappingException("Failed to resolve Enum '" + parameterType + "' by value '" + Arrays.toString(attribute.getValues()) + "'", ex);
}
}
propertyValueSetter.set(entry, ldapEnums);
} else if (jsonObject) {
Object stringValue = attribute.getValue();
Object jsonValue = convertJsonToValue(parameterType, stringValue);
propertyValueSetter.set(entry, jsonValue);
} else {
throw new MappingException("Entry property '" + propertyName + "' should has setter with String, Boolean, Integer, Long, Date, String[], List<String>, AttributeEnum or AttributeEnum[]" + " parameter type or has annotation JsonObject");
}
}
use of io.jans.orm.annotation.AttributeEnum in project jans by JanssenProject.
the class SqlFilterConverter method convertToSqlFilterImpl.
private ConvertedExpression convertToSqlFilterImpl(Filter genericFilter, Map<String, PropertyAnnotation> propertiesAnnotationsMap, Map<String, Class<?>> jsonAttributes, Function<? super Filter, Boolean> processor, boolean skipAlias) throws SearchException {
if (genericFilter == null) {
return null;
}
Filter currentGenericFilter = genericFilter;
FilterType type = currentGenericFilter.getType();
if (FilterType.RAW == type) {
LOG.warn("RAW Ldap filter to SQL convertion will be removed in new version!!!");
currentGenericFilter = ldapFilterConverter.convertRawLdapFilterToFilter(currentGenericFilter.getFilterString());
type = currentGenericFilter.getType();
}
if (processor != null) {
processor.apply(currentGenericFilter);
}
if ((FilterType.NOT == type) || (FilterType.AND == type) || (FilterType.OR == type)) {
Filter[] genericFilters = currentGenericFilter.getFilters();
Predicate[] expFilters = new Predicate[genericFilters.length];
if (genericFilters != null) {
// We can replace only multiple OR with IN
boolean canJoinOrFilters = FilterType.OR == type;
List<Filter> joinOrFilters = new ArrayList<Filter>();
String joinOrAttributeName = null;
for (int i = 0; i < genericFilters.length; i++) {
Filter tmpFilter = genericFilters[i];
expFilters[i] = (Predicate) convertToSqlFilterImpl(tmpFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias).expression();
// Check if we can replace OR with IN
if (!canJoinOrFilters) {
continue;
}
if (tmpFilter.getMultiValued() != null) {
canJoinOrFilters = false;
continue;
}
if ((FilterType.EQUALITY != tmpFilter.getType()) || (tmpFilter.getFilters() != null)) {
canJoinOrFilters = false;
continue;
}
Boolean isMultiValuedDetected = determineMultiValuedByType(tmpFilter.getAttributeName(), propertiesAnnotationsMap);
if (!Boolean.FALSE.equals(isMultiValuedDetected)) {
if (!Boolean.FALSE.equals(currentGenericFilter.getMultiValued())) {
canJoinOrFilters = false;
continue;
}
}
if (joinOrAttributeName == null) {
joinOrAttributeName = tmpFilter.getAttributeName();
joinOrFilters.add(tmpFilter);
continue;
}
if (!joinOrAttributeName.equals(tmpFilter.getAttributeName())) {
canJoinOrFilters = false;
continue;
}
joinOrFilters.add(tmpFilter);
}
if (FilterType.NOT == type) {
return ConvertedExpression.build(ExpressionUtils.predicate(Ops.NOT, expFilters[0]), jsonAttributes);
} else if (FilterType.AND == type) {
return ConvertedExpression.build(ExpressionUtils.allOf(expFilters), jsonAttributes);
} else if (FilterType.OR == type) {
if (canJoinOrFilters) {
List<Object> rightObjs = new ArrayList<>(joinOrFilters.size());
Filter lastEqFilter = null;
for (Filter eqFilter : joinOrFilters) {
lastEqFilter = eqFilter;
Object assertionValue = eqFilter.getAssertionValue();
if (assertionValue instanceof AttributeEnum) {
assertionValue = ((AttributeEnum) assertionValue).getValue();
}
rightObjs.add(assertionValue);
}
return ConvertedExpression.build(ExpressionUtils.in(buildTypedPath(lastEqFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), rightObjs), jsonAttributes);
} else {
return ConvertedExpression.build(ExpressionUtils.anyOf(expFilters), jsonAttributes);
}
}
}
}
if (FilterType.EQUALITY == type) {
if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
Expression expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_CONTAINS, expression, buildTypedExpression(currentGenericFilter, true), Expressions.constant("$.v"));
return ConvertedExpression.build(operation, jsonAttributes);
} else {
Filter usedFilter = currentGenericFilter;
Expression expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
return ConvertedExpression.build(ExpressionUtils.eq(expression, buildTypedExpression(usedFilter)), jsonAttributes);
}
}
if (FilterType.LESS_OR_EQUAL == type) {
if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
if (currentGenericFilter.getMultiValuedCount() > 1) {
Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
Predicate predicate = Expressions.asComparable(operation).loe(buildTypedExpression(currentGenericFilter));
expressions.add(predicate);
}
Expression expression = ExpressionUtils.anyOf(expressions);
return ConvertedExpression.build(expression, jsonAttributes);
}
Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
Expression expression = Expressions.asComparable(operation).loe(buildTypedExpression(currentGenericFilter));
return ConvertedExpression.build(expression, jsonAttributes);
} else {
return ConvertedExpression.build(Expressions.asComparable(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)).loe(buildTypedExpression(currentGenericFilter)), jsonAttributes);
}
}
if (FilterType.GREATER_OR_EQUAL == type) {
if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
if (currentGenericFilter.getMultiValuedCount() > 1) {
Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
Predicate predicate = Expressions.asComparable(operation).goe(buildTypedExpression(currentGenericFilter));
expressions.add(predicate);
}
Expression expression = ExpressionUtils.anyOf(expressions);
return ConvertedExpression.build(expression, jsonAttributes);
}
Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
Expression expression = Expressions.asComparable(operation).goe(buildTypedExpression(currentGenericFilter));
return ConvertedExpression.build(expression, jsonAttributes);
} else {
return ConvertedExpression.build(Expressions.asComparable(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)).goe(buildTypedExpression(currentGenericFilter)), jsonAttributes);
}
}
if (FilterType.PRESENCE == type) {
Expression expression;
if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
if (currentGenericFilter.getMultiValuedCount() > 1) {
Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
Predicate predicate = ExpressionUtils.isNotNull(ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]")));
expressions.add(predicate);
}
Predicate predicate = ExpressionUtils.anyOf(expressions);
return ConvertedExpression.build(predicate, jsonAttributes);
}
expression = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
} else {
expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
}
return ConvertedExpression.build(ExpressionUtils.isNotNull(expression), jsonAttributes);
}
if (FilterType.APPROXIMATE_MATCH == type) {
throw new SearchException("Convertion from APPROXIMATE_MATCH LDAP filter to SQL filter is not implemented");
}
if (FilterType.SUBSTRING == type) {
StringBuilder like = new StringBuilder();
if (currentGenericFilter.getSubInitial() != null) {
like.append(currentGenericFilter.getSubInitial());
}
like.append("%");
String[] subAny = currentGenericFilter.getSubAny();
if ((subAny != null) && (subAny.length > 0)) {
for (String any : subAny) {
like.append(any);
like.append("%");
}
}
if (currentGenericFilter.getSubFinal() != null) {
like.append(currentGenericFilter.getSubFinal());
}
Expression expression;
if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
if (currentGenericFilter.getMultiValuedCount() > 1) {
Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
Predicate predicate = Expressions.booleanOperation(Ops.LIKE, operation, Expressions.constant(like.toString()));
expressions.add(predicate);
}
Predicate predicate = ExpressionUtils.anyOf(expressions);
return ConvertedExpression.build(predicate, jsonAttributes);
}
expression = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
} else {
expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
}
return ConvertedExpression.build(Expressions.booleanOperation(Ops.LIKE, expression, Expressions.constant(like.toString())), jsonAttributes);
}
if (FilterType.LOWERCASE == type) {
return ConvertedExpression.build(ExpressionUtils.toLower(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)), jsonAttributes);
}
throw new SearchException(String.format("Unknown filter type '%s'", type));
}
Aggregations