Search in sources :

Example 1 with AttributeName

use of io.jans.orm.annotation.AttributeName in project jans by JanssenProject.

the class ScimResourcesUpdatedWebService method getNonCustomAttributes.

private Map<String, List<Object>> getNonCustomAttributes(ScimCustomPerson person) {
    Map<String, List<Object>> map = new HashMap<>();
    Field[] fields = ScimCustomPerson.class.getDeclaredFields();
    for (Field field : fields) {
        try {
            AttributeName annotation = field.getAnnotation(AttributeName.class);
            if (annotation != null) {
                String fieldName = field.getName();
                String attribute = StringUtils.isEmpty(annotation.name()) ? fieldName : annotation.name();
                Method getter = IntrospectUtil.getGetter(fieldName, ScimCustomPerson.class);
                if (getter != null) {
                    Object value = getter.invoke(person);
                    if (value != null) {
                        map.put(attribute, new ArrayList<>(Collections.singletonList(value)));
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
    return map;
}
Also used : Field(java.lang.reflect.Field) AttributeName(io.jans.orm.annotation.AttributeName) Method(java.lang.reflect.Method)

Example 2 with AttributeName

use of io.jans.orm.annotation.AttributeName in project jans by JanssenProject.

the class CouchbaseEntryManager method getScanConsistency.

private ScanConsistency getScanConsistency(String attributeName, Map<String, PropertyAnnotation> propertiesAnnotationsMap) {
    if (StringHelper.isEmpty(attributeName)) {
        return null;
    }
    PropertyAnnotation propertyAnnotation = propertiesAnnotationsMap.get(attributeName);
    if ((propertyAnnotation == null) || (propertyAnnotation.getParameterType() == null)) {
        return null;
    }
    AttributeName attributeNameAnnotation = (AttributeName) ReflectHelper.getAnnotationByType(propertyAnnotation.getAnnotations(), AttributeName.class);
    if (attributeNameAnnotation.consistency()) {
        return ScanConsistency.REQUEST_PLUS;
    }
    return null;
}
Also used : AttributeName(io.jans.orm.annotation.AttributeName) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Example 3 with AttributeName

use of io.jans.orm.annotation.AttributeName 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;
}
Also used : HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) AttributesList(io.jans.orm.annotation.AttributesList) ArrayList(java.util.ArrayList) AttributesList(io.jans.orm.annotation.AttributesList) List(java.util.List) AttributeName(io.jans.orm.annotation.AttributeName) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) InvalidArgumentException(io.jans.orm.exception.InvalidArgumentException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) Setter(io.jans.orm.reflect.property.Setter) JsonObject(io.jans.orm.annotation.JsonObject) AttributeData(io.jans.orm.model.AttributeData)

Example 4 with AttributeName

use of io.jans.orm.annotation.AttributeName 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;
}
Also used : Getter(io.jans.orm.reflect.property.Getter) AttributeName(io.jans.orm.annotation.AttributeName) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) AttributeData(io.jans.orm.model.AttributeData) MappingException(io.jans.orm.exception.MappingException)

Example 5 with AttributeName

use of io.jans.orm.annotation.AttributeName in project jans by JanssenProject.

the class BaseEntryManager method getEntryKey.

private String getEntryKey(Object dnValue, boolean caseSensetive, List<PropertyAnnotation> propertiesAnnotations, List<AttributeData> attributesData) {
    StringBuilder sb = new StringBuilder("_HASH__").append((String.valueOf(dnValue)).toLowerCase()).append("__");
    List<String> processedProperties = new ArrayList<String>();
    Map<String, AttributeData> attributesDataMap = getAttributesDataMap(attributesData);
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        Annotation ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributeName.class);
        if (ldapAttribute == null) {
            continue;
        }
        String ldapAttributeName = ((AttributeName) ldapAttribute).name();
        if (StringHelper.isEmpty(ldapAttributeName)) {
            ldapAttributeName = propertiesAnnotation.getPropertyName();
        }
        processedProperties.add(ldapAttributeName);
        String[] values = null;
        AttributeData attributeData = attributesDataMap.get(ldapAttributeName);
        if ((attributeData != null) && (attributeData.getValues() != null)) {
            values = attributeData.getStringValues();
            Arrays.sort(values);
        }
        addPropertyWithValuesToKey(sb, ldapAttributeName, values);
    }
    for (AttributeData attributeData : attributesData) {
        if (processedProperties.contains(attributeData.getName())) {
            continue;
        }
        addPropertyWithValuesToKey(sb, attributeData.getName(), attributeData.getStringValues());
    }
    if (caseSensetive) {
        return sb.toString();
    } else {
        return sb.toString().toLowerCase();
    }
}
Also used : ArrayList(java.util.ArrayList) AttributeName(io.jans.orm.annotation.AttributeName) AttributeData(io.jans.orm.model.AttributeData) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Aggregations

AttributeName (io.jans.orm.annotation.AttributeName)9 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)8 AttributeData (io.jans.orm.model.AttributeData)6 Annotation (java.lang.annotation.Annotation)6 AttributesList (io.jans.orm.annotation.AttributesList)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 IdentityHashMap (java.util.IdentityHashMap)3 MappingException (io.jans.orm.exception.MappingException)2 JsonObject (io.jans.orm.annotation.JsonObject)1 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)1 InvalidArgumentException (io.jans.orm.exception.InvalidArgumentException)1 AttributeDataModification (io.jans.orm.model.AttributeDataModification)1 AttributeModificationType (io.jans.orm.model.AttributeDataModification.AttributeModificationType)1 Getter (io.jans.orm.reflect.property.Getter)1 Setter (io.jans.orm.reflect.property.Setter)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 List (java.util.List)1