Search in sources :

Example 6 with LdapAttribute

use of org.gluu.site.ldap.persistence.annotation.LdapAttribute in project oxCore by GluuFederation.

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 LdapAttribute annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
        if (ldapAttribute != null) {
            AttributeData attribute = getAttributeFromLdapAttribute(entry, ldapAttribute, propertiesAnnotation, propertyName);
            if (attribute != null) {
                attributes.add(attribute);
            }
            continue;
        }
        // Process properties with LdapAttributesList annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
        if (ldapAttribute != null) {
            List<AttributeData> listAttributes = getAttributesFromLdapAttributesList(entry, ldapAttribute, propertyName);
            if (listAttributes != null) {
                attributes.addAll(listAttributes);
            }
            continue;
        }
    }
    return attributes;
}
Also used : ArrayList(java.util.ArrayList) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) AttributeData(org.gluu.persist.model.AttributeData) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Example 7 with LdapAttribute

use of org.gluu.site.ldap.persistence.annotation.LdapAttribute in project oxCore by GluuFederation.

the class BaseEntryManager method getLdapAttributesList.

private <T> List<String> getLdapAttributesList(T entry, List<PropertyAnnotation> propertiesAnnotations, boolean isIgnoreLdapAttributesList) {
    List<String> attributes = new ArrayList<String>();
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Annotation ldapAttribute;
        if (!isIgnoreLdapAttributesList) {
            ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
            if (ldapAttribute != null) {
                if (entry == null) {
                    return null;
                } else {
                    List<AttributeData> ldapAttributesList = getAttributesFromLdapAttributesList(entry, ldapAttribute, propertyName);
                    for (AttributeData attributeData : ldapAttributesList) {
                        String ldapAttributeName = attributeData.getName();
                        attributes.add(ldapAttributeName);
                    }
                }
            }
        }
        // Process properties with LdapAttribute annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
        if (ldapAttribute != null) {
            String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
            if (StringHelper.isEmpty(ldapAttributeName)) {
                ldapAttributeName = propertyName;
            }
            attributes.add(ldapAttributeName);
        }
    }
    if (attributes.size() == 0) {
        return null;
    }
    return attributes;
}
Also used : ArrayList(java.util.ArrayList) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) AttributeData(org.gluu.persist.model.AttributeData) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Example 8 with LdapAttribute

use of org.gluu.site.ldap.persistence.annotation.LdapAttribute in project oxCore by GluuFederation.

the class AbstractEntryManager method getAttributeFromLdapAttribute.

private AttributeData getAttributeFromLdapAttribute(Object entry, Annotation ldapAttribute, PropertyAnnotation propertiesAnnotation, String propertyName) {
    Class<?> entryClass = entry.getClass();
    String ldapAttributeName = ((LdapAttribute) 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);
    }
    Annotation ldapJsonObject = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapJsonObject.class);
    boolean jsonObject = ldapJsonObject != null;
    AttributeData attribute = getAttribute(propertyName, ldapAttributeName, getter, entry, jsonObject);
    return attribute;
}
Also used : Getter(org.gluu.site.ldap.persistence.property.Getter) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) Annotation(java.lang.annotation.Annotation) MappingException(org.gluu.site.ldap.persistence.exception.MappingException)

Example 9 with LdapAttribute

use of org.gluu.site.ldap.persistence.annotation.LdapAttribute in project oxCore by GluuFederation.

the class AbstractEntryManager method getLdapAttributesList.

private <T> List<String> getLdapAttributesList(T entry, List<PropertyAnnotation> propertiesAnnotations, boolean isIgnoreLdapAttributesList) {
    List<String> attributes = new ArrayList<String>();
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Annotation ldapAttribute;
        if (!isIgnoreLdapAttributesList) {
            ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
            if (ldapAttribute != null) {
                if (entry == null) {
                    return null;
                } else {
                    List<AttributeData> ldapAttributesList = getAttributesFromLdapAttributesList(entry, ldapAttribute, propertyName);
                    for (AttributeData attributeData : ldapAttributesList) {
                        String ldapAttributeName = attributeData.getName();
                        attributes.add(ldapAttributeName);
                    }
                }
            }
        }
        // Process properties with LdapAttribute annotation
        ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
        if (ldapAttribute != null) {
            String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
            if (StringHelper.isEmpty(ldapAttributeName)) {
                ldapAttributeName = propertyName;
            }
            attributes.add(ldapAttributeName);
        }
    }
    if (attributes.size() == 0) {
        return null;
    }
    return attributes;
}
Also used : LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) Annotation(java.lang.annotation.Annotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Example 10 with LdapAttribute

use of org.gluu.site.ldap.persistence.annotation.LdapAttribute in project oxCore by GluuFederation.

the class AbstractEntryManager 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);
        // Process properties with LdapAttribute annotation
        for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
            String propertyName = propertiesAnnotation.getPropertyName();
            Annotation ldapAttribute;
            ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttribute.class);
            if (ldapAttribute != null) {
                String ldapAttributeName = ((LdapAttribute) ldapAttribute).name();
                if (StringHelper.isEmpty(ldapAttributeName)) {
                    ldapAttributeName = propertyName;
                }
                ldapAttributeName = ldapAttributeName.toLowerCase();
                AttributeData attributeData = attributesMap.get(ldapAttributeName);
                // Remove processed attributes
                attributesMap.remove(ldapAttributeName);
                if (((LdapAttribute) 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(), LdapJsonObject.class);
                boolean jsonObject = ldapJsonObject != null;
                setPropertyValue(propertyName, setter, entry, attributeData, jsonObject);
            }
        }
        // Process properties with LdapAttributesList annotation
        for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
            String propertyName = propertiesAnnotation.getPropertyName();
            Annotation ldapAttribute;
            ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), LdapAttributesList.class);
            if (ldapAttribute != null) {
                Map<String, LdapAttribute> ldapAttributesConfiguration = new HashMap<String, LdapAttribute>();
                for (LdapAttribute ldapAttributeConfiguration : ((LdapAttributesList) 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 = ((LdapAttributesList) ldapAttribute).name();
                Setter entryPropertyNameSetter = getSetter(entryItemType, entryPropertyName);
                if (entryPropertyNameSetter == null) {
                    throw new MappingException("Entry should has setter for property " + propertyName + "." + entryPropertyName);
                }
                String entryPropertyValue = ((LdapAttributesList) 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.getValues();
                        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>() {

                                @Override
                                public int compare(String o1, String o2) {
                                    return o1.toLowerCase().compareTo(o2.toLowerCase());
                                }
                            });
                            if (idx < 0) {
                                customObjectClasses.add(objectClass);
                            }
                        }
                        continue;
                    }
                    LdapAttribute ldapAttributeConfiguration = ldapAttributesConfiguration.get(entryAttribute.getName());
                    if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringRead()) {
                        continue;
                    }
                    Object listItem = getListItem(propertyName, entryPropertyNameSetter, entryPropertyValueSetter, entryItemType, entryAttribute);
                    if (listItem != null) {
                        propertyValue.add(listItem);
                    }
                }
                if (doSort) {
                    sortLdapAttributesListIfNeeded((LdapAttributesList) ldapAttribute, entryItemType, propertyValue);
                }
            }
        }
        if ((customObjectClasses != null) && (customObjectClasses.size() > 0)) {
            setCustomObjectClasses(entry, entryClass, customObjectClasses.toArray(new String[0]));
        }
    }
    return results;
}
Also used : MappingException(org.gluu.site.ldap.persistence.exception.MappingException) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) Annotation(java.lang.annotation.Annotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) Setter(org.gluu.site.ldap.persistence.property.Setter) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject)

Aggregations

Annotation (java.lang.annotation.Annotation)12 LdapAttribute (org.gluu.site.ldap.persistence.annotation.LdapAttribute)12 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)8 AttributeData (org.gluu.persist.model.AttributeData)6 PropertyAnnotation (org.gluu.persist.model.PropertyAnnotation)6 ArrayList (java.util.ArrayList)5 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)3 MappingException (org.gluu.site.ldap.persistence.exception.MappingException)3 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 MappingException (org.gluu.persist.exception.mapping.MappingException)2 List (java.util.List)1 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)1 InvalidArgumentException (org.gluu.persist.exception.mapping.InvalidArgumentException)1 AttributeDataModification (org.gluu.persist.model.AttributeDataModification)1 AttributeModificationType (org.gluu.persist.model.AttributeDataModification.AttributeModificationType)1 Getter (org.gluu.persist.reflect.property.Getter)1 Setter (org.gluu.persist.reflect.property.Setter)1 AttributeModificationType (org.gluu.site.ldap.persistence.AttributeDataModification.AttributeModificationType)1 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)1