Skip to content

Commit

Permalink
feat: add new methods to allow get/set list of custom attributes from (
Browse files Browse the repository at this point in the history
…#2105)

entry #2104

Co-authored-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>
  • Loading branch information
2 people authored and ossdhaval committed Aug 16, 2022
1 parent aa01486 commit 9af620e
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package io.jans.orm;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand All @@ -24,6 +25,7 @@
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.CriteriaUpdate;
import jakarta.persistence.metamodel.Metamodel;
import io.jans.orm.annotation.AttributesList;
import io.jans.orm.event.DeleteNotifier;
import io.jans.orm.extension.PersistenceExtension;
import io.jans.orm.model.AttributeData;
Expand Down Expand Up @@ -140,6 +142,13 @@ <T> Map<T, List<T>> groupListByProperties(Class<T> entryClass, List<T> entries,

<T> AttributeType getAttributeType(String primaryKey, Class<T> entryClass, String propertyName);

Class<?> getCustomAttributesListItemType(Object entry, AttributesList attributesList,
String propertyName);
List<AttributeData> getAttributeDataListFromCustomAttributesList(Object entry, AttributesList attributesList,
String propertyName);
List<Object> getCustomAttributesListFromAttributeDataList(Object entry, AttributesList attributesList,
String propertyName, Collection<AttributeData> attributes);

boolean destroy();

default void clear() {
Expand Down
163 changes: 103 additions & 60 deletions jans-orm/core/src/main/java/io/jans/orm/impl/BaseEntryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
Expand Down Expand Up @@ -48,10 +49,10 @@
import io.jans.orm.extension.PersistenceExtension;
import io.jans.orm.model.AttributeData;
import io.jans.orm.model.AttributeDataModification;
import io.jans.orm.model.AttributeDataModification.AttributeModificationType;
import io.jans.orm.model.AttributeType;
import io.jans.orm.model.SearchScope;
import io.jans.orm.model.base.LocalizedString;
import io.jans.orm.model.AttributeDataModification.AttributeModificationType;
import io.jans.orm.operation.PersistenceOperationService;
import io.jans.orm.reflect.property.Getter;
import io.jans.orm.reflect.property.PropertyAnnotation;
Expand Down Expand Up @@ -642,8 +643,8 @@ protected <T> Map<String, PropertyAnnotation> getAttributesMap(T entry, List<Pro
if (entry == null) {
return null;
} else {
List<AttributeData> attributesList = getAttributesFromAttributesList(entry,
ldapAttribute, propertyName);
List<AttributeData> attributesList = getAttributeDataListFromCustomAttributesList(entry,
(AttributesList) ldapAttribute, propertyName);
for (AttributeData attributeData : attributesList) {
String ldapAttributeName = attributeData.getName();
if (!attributes.containsKey(ldapAttributeName)) {
Expand Down Expand Up @@ -1061,35 +1062,9 @@ protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotatio
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()) {
// Process objectClass first
for (Entry<String, AttributeData> attributeEntry : attributesMap.entrySet()) {
AttributeData entryAttribute = attributeEntry.getValue();
if (OBJECT_CLASS.equalsIgnoreCase(entryAttribute.getName())) {
String[] objectClasses = entryAttribute.getStringValues();
if (ArrayHelper.isEmpty(objectClasses)) {
Expand All @@ -1112,39 +1087,29 @@ public int compare(String o1, String o2) {
}
}

attributesMap.remove(attributeEntry.getKey());
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");
}
}
List<Object> propertyValue = getCustomAttributesListFromAttributeData(entryClass, (AttributesList) ldapAttribute, propertyName,
attributesMap.values(), ldapAttributesConfiguration);

Object listItem = getListItem(propertyName, entryPropertyNameSetter, entryPropertyValueSetter,
entryPropertyMultivaluedSetter, entryItemType, entryAttribute);
if (listItem != null) {
propertyValue.add(listItem);
}
Setter setter = getSetter(entryClass, propertyName);
if (setter == null) {
throw new MappingException("Entry should has setter for property " + propertyName);
}

if (doSort) {
Class<?> entryItemType = ReflectHelper.getListType(setter);
if (entryItemType == null) {
throw new MappingException(
"Entry property " + propertyName + " should has setter with specified element type");
}
sortAttributesListIfNeeded((AttributesList) ldapAttribute, entryItemType,
propertyValue);
}
setter.set(entry, propertyValue);
}
}

Expand All @@ -1156,6 +1121,78 @@ public int compare(String o1, String o2) {
return results;
}

private <T> List<Object> getCustomAttributesListFromAttributeData(Class<T> entryClass, AttributesList attributesList,
String propertyName, Collection<AttributeData> attributes, Map<String, AttributeName> ldapAttributesConfiguration) {
List<Object> resultList = new ArrayList<Object>();

Setter setter = getSetter(entryClass, propertyName);
if (setter == null) {
throw new MappingException("Entry should has setter for property " + propertyName);
}

Class<?> entryItemType = ReflectHelper.getListType(setter);
if (entryItemType == null) {
throw new MappingException(
"Entry property " + propertyName + " should has setter with specified element type");
}

String entryPropertyName = attributesList.name();
Setter entryPropertyNameSetter = getSetter(entryItemType, entryPropertyName);
if (entryPropertyNameSetter == null) {
throw new MappingException(
"Entry should has setter for property " + propertyName + "." + entryPropertyName);
}

String entryPropertyValue = attributesList.value();
Setter entryPropertyValueSetter = getSetter(entryItemType, entryPropertyValue);
if (entryPropertyValueSetter == null) {
throw new MappingException(
"Entry should has getter for property " + propertyName + "." + entryPropertyValue);
}

String entryPropertyMultivalued = attributesList.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");
}
}

for (AttributeData entryAttribute : attributes) {
if (ldapAttributesConfiguration != null) {
AttributeName ldapAttributeConfiguration = ldapAttributesConfiguration
.get(entryAttribute.getName());
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringRead()) {
continue;
}
}

Object listItem = getListItem(propertyName, entryPropertyNameSetter, entryPropertyValueSetter,
entryPropertyMultivaluedSetter, entryItemType, entryAttribute);
if (listItem != null) {
resultList.add(listItem);
}
}

return resultList;
}

public Class<?> getCustomAttributesListItemType(Object entry, AttributesList attributesList, String propertyName) {
Class<?> entryClass = entry.getClass();
Setter setter = getSetter(entryClass, propertyName);
if (setter == null) {
throw new MappingException("Entry should has setter for property " + propertyName);
}

return ReflectHelper.getListType(setter);
}

protected void loadLocalizedString(Map<String, AttributeData> attributesMap, LocalizedString localizedString, Map<String, AttributeData> filteredAttrs) {
filteredAttrs.forEach((key, value) -> {
AttributeData data = attributesMap.get(key);
Expand Down Expand Up @@ -1564,7 +1601,7 @@ protected List<AttributeData> getAttributesListForPersist(Object entry,
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(),
AttributesList.class);
if (ldapAttribute != null) {
List<AttributeData> listAttributes = getAttributesFromAttributesList(entry, ldapAttribute,
List<AttributeData> listAttributes = getAttributeDataListFromCustomAttributesList(entry, (AttributesList) ldapAttribute,
propertyName);
if (listAttributes != null) {
attributes.addAll(listAttributes);
Expand Down Expand Up @@ -1636,7 +1673,7 @@ protected List<AttributeData> getAttributeDataFromLocalizedString(String ldapAtt
return listAttributes;
}

private List<AttributeData> getAttributesFromAttributesList(Object entry, Annotation ldapAttribute,
public List<AttributeData> getAttributeDataListFromCustomAttributesList(Object entry, AttributesList attributesList,
String propertyName) {
Class<?> entryClass = entry.getClass();
List<AttributeData> listAttributes = new ArrayList<AttributeData>();
Expand All @@ -1657,21 +1694,21 @@ private List<AttributeData> getAttributesFromAttributesList(Object entry, Annota

Class<?> elementType = ReflectHelper.getListType(getter);

String entryPropertyName = ((AttributesList) ldapAttribute).name();
String entryPropertyName = attributesList.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();
String entryPropertyValue = attributesList.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();
String entryPropertyMultivalued = attributesList.multiValued();
Getter entryPropertyMultivaluedGetter = null;
if (StringHelper.isNotEmpty(entryPropertyMultivalued)) {
entryPropertyMultivaluedGetter = getGetter(elementType, entryPropertyMultivalued);
Expand Down Expand Up @@ -1706,6 +1743,12 @@ private List<AttributeData> getAttributesFromAttributesList(Object entry, Annota
return listAttributes;
}

public List<Object> getCustomAttributesListFromAttributeDataList(Object entry, AttributesList attributesList,
String propertyName, Collection<AttributeData> attributes) {
Class<?> entryClass = entry.getClass();
return getCustomAttributesListFromAttributeData(entryClass, attributesList, propertyName, attributes, null);
}

protected <T> List<PropertyAnnotation> getEntryPropertyAnnotations(Class<T> entryClass) {
final List<PropertyAnnotation> annotations = getEntryClassAnnotations(entryClass, "property_", LDAP_ENTRY_PROPERTY_ANNOTATIONS);
// KeyShortcuter.initIfNeeded(entryClass, annotations);
Expand Down

0 comments on commit 9af620e

Please sign in to comment.