use of io.jans.orm.annotation.AttributesList 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.AttributesList in project jans by JanssenProject.
the class BaseEntryManager method getAttributesMap.
protected <T> Map<String, PropertyAnnotation> getAttributesMap(T entry, List<PropertyAnnotation> propertiesAnnotations, boolean isIgnoreAttributesList) {
Map<String, PropertyAnnotation> attributes = new HashMap<String, PropertyAnnotation>();
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Annotation ldapAttribute;
if (!isIgnoreAttributesList) {
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributesList.class);
if (ldapAttribute != null) {
if (entry == null) {
return null;
} else {
List<AttributeData> attributesList = getAttributesFromAttributesList(entry, ldapAttribute, propertyName);
for (AttributeData attributeData : attributesList) {
String ldapAttributeName = attributeData.getName();
if (!attributes.containsKey(ldapAttributeName)) {
attributes.put(ldapAttributeName, propertiesAnnotation);
}
}
}
}
}
// Process properties with AttributeName annotation
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributeName.class);
if (ldapAttribute != null) {
String ldapAttributeName = ((AttributeName) ldapAttribute).name();
if (StringHelper.isEmpty(ldapAttributeName)) {
ldapAttributeName = propertyName;
}
if (!attributes.containsKey(ldapAttributeName)) {
attributes.put(ldapAttributeName, propertiesAnnotation);
}
}
}
if (attributes.size() == 0) {
return null;
}
return attributes;
}
use of io.jans.orm.annotation.AttributesList in project jans by JanssenProject.
the class BaseEntryManager method getAttributesFromAttributesList.
private List<AttributeData> getAttributesFromAttributesList(Object entry, Annotation ldapAttribute, String propertyName) {
Class<?> entryClass = entry.getClass();
List<AttributeData> listAttributes = new ArrayList<AttributeData>();
Getter getter = getGetter(entryClass, propertyName);
if (getter == null) {
throw new MappingException("Entry should has getter for property " + propertyName);
}
Object propertyValue = getter.get(entry);
if (propertyValue == null) {
return null;
}
if (!(propertyValue instanceof List<?>)) {
throw new MappingException("Entry property should has List base type");
}
Class<?> elementType = ReflectHelper.getListType(getter);
String entryPropertyName = ((AttributesList) ldapAttribute).name();
Getter entryPropertyNameGetter = getGetter(elementType, entryPropertyName);
if (entryPropertyNameGetter == null) {
throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyName);
}
String entryPropertyValue = ((AttributesList) ldapAttribute).value();
Getter entryPropertyValueGetter = getGetter(elementType, entryPropertyValue);
if (entryPropertyValueGetter == null) {
throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyValue);
}
String entryPropertyMultivalued = ((AttributesList) ldapAttribute).multiValued();
Getter entryPropertyMultivaluedGetter = null;
if (StringHelper.isNotEmpty(entryPropertyMultivalued)) {
entryPropertyMultivaluedGetter = getGetter(elementType, entryPropertyMultivalued);
}
if (entryPropertyMultivaluedGetter != null) {
Class<?> propertyType = entryPropertyMultivaluedGetter.getReturnType();
if (!propertyType.equals(Boolean.TYPE)) {
throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyMultivalued + " with boolean type");
}
}
for (Object entryAttribute : (List<?>) propertyValue) {
Boolean multiValued = null;
if (entryPropertyMultivaluedGetter != null) {
multiValued = (boolean) entryPropertyMultivaluedGetter.get(entryAttribute);
}
AttributeData attribute = getAttributeData(propertyName, entryPropertyNameGetter, entryPropertyValueGetter, entryAttribute, Boolean.TRUE.equals(multiValued), false);
if (attribute != null) {
if (multiValued == null) {
// Detect if attribute has more than one value
multiValued = attribute.getValues().length > 1;
}
attribute.setMultiValued(multiValued);
listAttributes.add(attribute);
}
}
return listAttributes;
}
use of io.jans.orm.annotation.AttributesList in project jans by JanssenProject.
the class BaseEntryManager method getAttributesListForPersist.
protected List<AttributeData> getAttributesListForPersist(Object entry, List<PropertyAnnotation> propertiesAnnotations) {
// Prepare list of properties to persist
List<AttributeData> attributes = new ArrayList<AttributeData>();
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Annotation ldapAttribute;
// Process properties with AttributeName annotation
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributeName.class);
if (ldapAttribute != null) {
AttributeData attribute = getAttributeDataFromAttribute(entry, ldapAttribute, propertiesAnnotation, propertyName);
if (attribute != null) {
attributes.add(attribute);
}
continue;
}
// Process properties with @AttributesList annotation
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributesList.class);
if (ldapAttribute != null) {
List<AttributeData> listAttributes = getAttributesFromAttributesList(entry, ldapAttribute, propertyName);
if (listAttributes != null) {
attributes.addAll(listAttributes);
}
continue;
}
}
return attributes;
}
use of io.jans.orm.annotation.AttributesList in project jans by JanssenProject.
the class BaseEntryManager method collectAttributeModifications.
protected List<AttributeDataModification> collectAttributeModifications(List<PropertyAnnotation> propertiesAnnotations, Map<String, AttributeData> attributesToPersistMap, Map<String, AttributeData> attributesFromLdapMap, boolean isSchemaUpdate, AttributeModificationType schemaModificationType, boolean forceUpdate) {
List<AttributeDataModification> attributeDataModifications = new ArrayList<AttributeDataModification>();
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 attributeToPersist = attributesToPersistMap.get(ldapAttributeName);
AttributeData attributeFromLdap = attributesFromLdapMap.get(ldapAttributeName);
// Remove processed attributes
attributesToPersistMap.remove(ldapAttributeName);
attributesFromLdapMap.remove(ldapAttributeName);
AttributeName ldapAttributeAnnotation = (AttributeName) ldapAttribute;
if (ldapAttributeAnnotation.ignoreDuringUpdate()) {
continue;
}
if (attributeFromLdap != null && attributeToPersist != null) {
// Modify DN entry attribute in DS
if (!attributeFromLdap.equals(attributeToPersist)) {
if (isEmptyAttributeValues(attributeToPersist) && !ldapAttributeAnnotation.updateOnly()) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
}
}
} else if ((attributeFromLdap == null) && (attributeToPersist != null)) {
// Add entry attribute or change schema
if (isSchemaUpdate && (attributeToPersist.getValue() == null && Arrays.equals(attributeToPersist.getValues(), new Object[] {}))) {
continue;
}
AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
if (AttributeModificationType.ADD == modType) {
if (isEmptyAttributeValues(attributeToPersist)) {
if (forceUpdate) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else {
modType = forceUpdate ? AttributeModificationType.FORCE_UPDATE : modType;
attributeDataModifications.add(new AttributeDataModification(modType, attributeToPersist));
}
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else if ((attributeFromLdap != null) && (attributeToPersist == null)) {
// or updateOnly = true
if (!ldapAttributeAnnotation.ignoreDuringRead() && !ldapAttributeAnnotation.updateOnly()) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
}
} else if (forceUpdate && (attributeFromLdap == null) && (attributeToPersist == null)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, new AttributeData(ldapAttributeName, null)));
}
}
}
// Process properties with @AttributesList annotation
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
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);
}
// Prepare attributes for removal
for (AttributeData attributeFromLdap : attributesFromLdapMap.values()) {
String attributeName = attributeFromLdap.getName();
if (OBJECT_CLASS.equalsIgnoreCase(attributeName)) {
continue;
}
AttributeName ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
continue;
}
if (!attributesToPersistMap.containsKey(attributeName.toLowerCase())) {
// Remove if attribute not marked as ignoreDuringRead = true
if ((ldapAttributeConfiguration == null) || ((ldapAttributeConfiguration != null) && !ldapAttributeConfiguration.ignoreDuringRead())) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
}
}
}
// Prepare attributes for adding and replace
for (AttributeData attributeToPersist : attributesToPersistMap.values()) {
String attributeName = attributeToPersist.getName();
AttributeName ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
continue;
}
AttributeData attributeFromLdap = attributesFromLdapMap.get(attributeName.toLowerCase());
if (attributeFromLdap == null) {
// Add entry attribute or change schema
AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
if (AttributeModificationType.ADD.equals(modType)) {
if (!isEmptyAttributeValues(attributeToPersist)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.ADD, attributeToPersist));
}
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else if ((attributeFromLdap != null) && isEmptyAttributeValues(attributeToPersist)) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
if (!attributeFromLdap.equals(attributeToPersist)) {
if (isEmptyAttributeValues(attributeToPersist) && (ldapAttributeConfiguration == null || !ldapAttributeConfiguration.updateOnly())) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
}
}
}
}
}
}
return attributeDataModifications;
}
Aggregations