use of io.jans.orm.annotation.JsonObject in project jans by JanssenProject.
the class BaseEntryManager method createEntities.
protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Map<String, List<AttributeData>> entriesAttributes, boolean doSort) {
// Check if entry has DN property
String dnProperty = getDNPropertyName(entryClass);
// Get DN value
Setter dnSetter = getSetter(entryClass, dnProperty);
if (dnSetter == null) {
throw new MappingException("Entry should has getter for property " + dnProperty);
}
// Type object classes
String[] typeObjectClasses = getTypeObjectClasses(entryClass);
Arrays.sort(typeObjectClasses);
List<T> results = new ArrayList<T>(entriesAttributes.size());
for (Entry<String, List<AttributeData>> entryAttributes : entriesAttributes.entrySet()) {
String dn = entryAttributes.getKey();
List<AttributeData> attributes = entryAttributes.getValue();
Map<String, AttributeData> attributesMap = getAttributesMap(attributes);
T entry;
List<String> customObjectClasses = null;
try {
entry = ReflectHelper.createObjectByDefaultConstructor(entryClass);
} catch (Exception ex) {
throw new MappingException(String.format("Entry %s should has default constructor", entryClass));
}
results.add(entry);
dnSetter.set(entry, dn);
// Remove processed DN attribute
attributesMap.remove(dnProperty);
// Process properties with AttributeName annotation
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Annotation ldapAttribute;
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributeName.class);
if (ldapAttribute != null) {
String ldapAttributeName = ((AttributeName) ldapAttribute).name();
if (StringHelper.isEmpty(ldapAttributeName)) {
ldapAttributeName = propertyName;
}
ldapAttributeName = ldapAttributeName.toLowerCase();
AttributeData attributeData = attributesMap.get(ldapAttributeName);
// Remove processed attributes
attributesMap.remove(ldapAttributeName);
if (((AttributeName) ldapAttribute).ignoreDuringRead()) {
continue;
}
Setter setter = getSetter(entryClass, propertyName);
if (setter == null) {
throw new MappingException("Entry should has setter for property " + propertyName);
}
Annotation ldapJsonObject = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), JsonObject.class);
boolean jsonObject = ldapJsonObject != null;
setPropertyValue(propertyName, setter, entry, attributeData, jsonObject);
}
}
// Process properties with @AttributesList annotation
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Annotation ldapAttribute;
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributesList.class);
if (ldapAttribute != null) {
Map<String, AttributeName> ldapAttributesConfiguration = new HashMap<String, AttributeName>();
for (AttributeName ldapAttributeConfiguration : ((AttributesList) ldapAttribute).attributesConfiguration()) {
ldapAttributesConfiguration.put(ldapAttributeConfiguration.name(), ldapAttributeConfiguration);
}
Setter setter = getSetter(entryClass, propertyName);
if (setter == null) {
throw new MappingException("Entry should has setter for property " + propertyName);
}
List<Object> propertyValue = new ArrayList<Object>();
setter.set(entry, propertyValue);
Class<?> entryItemType = ReflectHelper.getListType(setter);
if (entryItemType == null) {
throw new MappingException("Entry property " + propertyName + " should has setter with specified element type");
}
String entryPropertyName = ((AttributesList) ldapAttribute).name();
Setter entryPropertyNameSetter = getSetter(entryItemType, entryPropertyName);
if (entryPropertyNameSetter == null) {
throw new MappingException("Entry should has setter for property " + propertyName + "." + entryPropertyName);
}
String entryPropertyValue = ((AttributesList) ldapAttribute).value();
Setter entryPropertyValueSetter = getSetter(entryItemType, entryPropertyValue);
if (entryPropertyValueSetter == null) {
throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyValue);
}
for (AttributeData entryAttribute : attributesMap.values()) {
if (OBJECT_CLASS.equalsIgnoreCase(entryAttribute.getName())) {
String[] objectClasses = entryAttribute.getStringValues();
if (ArrayHelper.isEmpty(objectClasses)) {
continue;
}
if (customObjectClasses == null) {
customObjectClasses = new ArrayList<String>();
}
for (String objectClass : objectClasses) {
int idx = Arrays.binarySearch(typeObjectClasses, objectClass, new Comparator<String>() {
public int compare(String o1, String o2) {
return o1.toLowerCase().compareTo(o2.toLowerCase());
}
});
if (idx < 0) {
customObjectClasses.add(objectClass);
}
}
continue;
}
AttributeName ldapAttributeConfiguration = ldapAttributesConfiguration.get(entryAttribute.getName());
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringRead()) {
continue;
}
String entryPropertyMultivalued = ((AttributesList) ldapAttribute).multiValued();
Setter entryPropertyMultivaluedSetter = null;
if (StringHelper.isNotEmpty(entryPropertyMultivalued)) {
entryPropertyMultivaluedSetter = getSetter(entryItemType, entryPropertyMultivalued);
}
if (entryPropertyMultivaluedSetter != null) {
Class<?> parameterType = ReflectHelper.getSetterType(entryPropertyMultivaluedSetter);
if (!parameterType.equals(Boolean.TYPE)) {
throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyMultivalued + " with boolean type");
}
}
Object listItem = getListItem(propertyName, entryPropertyNameSetter, entryPropertyValueSetter, entryPropertyMultivaluedSetter, entryItemType, entryAttribute);
if (listItem != null) {
propertyValue.add(listItem);
}
}
if (doSort) {
sortAttributesListIfNeeded((AttributesList) ldapAttribute, entryItemType, propertyValue);
}
}
}
if ((customObjectClasses != null) && (customObjectClasses.size() > 0)) {
setCustomObjectClasses(entry, entryClass, customObjectClasses.toArray(new String[0]));
}
}
return results;
}
use of io.jans.orm.annotation.JsonObject 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.JsonObject in project jans by JanssenProject.
the class BaseEntryManager method getAttributeData.
private AttributeData getAttributeData(String propertyName, String ldapAttributeName, Getter propertyValueGetter, Object entry, boolean multiValued, boolean jsonObject) {
Object propertyValue = propertyValueGetter.get(entry);
if (propertyValue == null) {
return null;
}
Object[] attributeValues = getAttributeValues(propertyName, jsonObject, propertyValue, multiValued);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Property: %s, LdapProperty: %s, PropertyValue: %s", propertyName, ldapAttributeName, Arrays.toString(attributeValues)));
}
if (attributeValues.length == 0) {
attributeValues = new String[] {};
} else if ((attributeValues.length == 1) && (attributeValues[0] == null)) {
return null;
}
return new AttributeData(ldapAttributeName, attributeValues, multiValued);
}
Aggregations