use of io.jans.orm.exception.MappingException in project jans by JanssenProject.
the class BaseEntryManager method countEntries.
@SuppressWarnings("unchecked")
public <T> int countEntries(Object entry) {
if (entry == null) {
throw new MappingException("Entry to count is null");
}
// Check entry class
Class<T> entryClass = (Class<T>) entry.getClass();
checkEntryClass(entryClass, false);
List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
Object dnValue = getDNValue(entry, entryClass);
List<AttributeData> attributes = getAttributesListForPersist(entry, propertiesAnnotations);
Filter searchFilter = createFilterByEntry(entry, entryClass, attributes);
return countEntries(dnValue.toString(), entryClass, searchFilter);
}
use of io.jans.orm.exception.MappingException in project jans by JanssenProject.
the class BaseEntryManager method isConfigurationEntry.
protected boolean isConfigurationEntry(Class<?> entryClass) {
if (entryClass == null) {
throw new MappingException("Entry class is null");
}
// Check if entry is LDAP Entry
List<Annotation> entryAnnotations = ReflectHelper.getClassAnnotations(entryClass, LDAP_ENTRY_TYPE_ANNOTATIONS);
DataEntry dataEntry = (DataEntry) ReflectHelper.getAnnotationByType(entryAnnotations, DataEntry.class);
if (dataEntry == null) {
return false;
}
return dataEntry.configurationDefinition();
}
use of io.jans.orm.exception.MappingException 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.exception.MappingException in project jans by JanssenProject.
the class BaseEntryManager method getAttributeDataFromAttribute.
private AttributeData getAttributeDataFromAttribute(Object entry, Annotation ldapAttribute, PropertyAnnotation propertiesAnnotation, String propertyName) {
Class<?> entryClass = entry.getClass();
String ldapAttributeName = ((AttributeName) ldapAttribute).name();
if (StringHelper.isEmpty(ldapAttributeName)) {
ldapAttributeName = propertyName;
}
Getter getter = getGetter(entryClass, propertyName);
if (getter == null) {
throw new MappingException("Entry should has getter for property " + propertyName);
}
Class<?> parameterType = getSetterPropertyType(entryClass, propertyName);
boolean multiValued = isMultiValued(parameterType);
Annotation ldapJsonObject = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), JsonObject.class);
boolean jsonObject = ldapJsonObject != null;
AttributeData attribute = getAttributeData(propertyName, ldapAttributeName, getter, entry, multiValued, jsonObject);
return attribute;
}
use of io.jans.orm.exception.MappingException in project jans by JanssenProject.
the class BaseEntryManager method contains.
@Override
public boolean contains(Object entry) {
if (entry == null) {
throw new MappingException("Entry to persist is null");
}
// Check entry class
Class<?> entryClass = entry.getClass();
checkEntryClass(entryClass, false);
String[] objectClasses = getObjectClasses(entry, entryClass);
List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
Object dnValue = getDNValue(entry, entryClass);
List<AttributeData> attributes = getAttributesListForPersist(entry, propertiesAnnotations);
String[] ldapReturnAttributes = getAttributes(null, propertiesAnnotations, false);
return contains(dnValue.toString(), entryClass, propertiesAnnotations, attributes, objectClasses, ldapReturnAttributes);
}
Aggregations