Search in sources :

Example 16 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class BaseEntryManager method getCustomObjectClasses.

protected String[] getCustomObjectClasses(Object entry, Class<?> entryClass) {
    List<String> result = new ArrayList<String>();
    List<PropertyAnnotation> customObjectAnnotations = getEntryCustomObjectClassAnnotations(entryClass);
    for (PropertyAnnotation propertiesAnnotation : customObjectAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Getter getter = getGetter(entryClass, propertyName);
        if (getter == null) {
            throw new MappingException("Entry should has getter for property " + propertyName);
        }
        AttributeData attribute = getAttribute(propertyName, propertyName, getter, entry, false);
        if (attribute != null) {
            for (String objectClass : attribute.getValues()) {
                if (objectClass != null) {
                    result.add(objectClass);
                }
            }
        }
        break;
    }
    return result.toArray(new String[0]);
}
Also used : Getter(org.gluu.persist.reflect.property.Getter) ArrayList(java.util.ArrayList) AttributeData(org.gluu.persist.model.AttributeData) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) MappingException(org.gluu.persist.exception.mapping.MappingException)

Example 17 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class BaseEntryManager method collectAttributeModifications.

protected List<AttributeDataModification> collectAttributeModifications(List<PropertyAnnotation> propertiesAnnotations, Map<String, AttributeData> attributesToPersistMap, Map<String, AttributeData> attributesFromLdapMap, boolean isSchemaUpdate, AttributeModificationType schemaModificationType) {
    List<AttributeDataModification> attributeDataModifications = new ArrayList<AttributeDataModification>();
    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 attributeToPersist = attributesToPersistMap.get(ldapAttributeName);
            AttributeData attributeFromLdap = attributesFromLdapMap.get(ldapAttributeName);
            // Remove processed attributes
            attributesToPersistMap.remove(ldapAttributeName);
            attributesFromLdapMap.remove(ldapAttributeName);
            LdapAttribute ldapAttributeAnnotation = (LdapAttribute) 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 String[] {}))) {
                    continue;
                }
                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) && (attributeToPersist == null)) {
                // or updateOnly = true
                if (!ldapAttributeAnnotation.ignoreDuringRead() && !ldapAttributeAnnotation.updateOnly()) {
                    attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
                }
            }
        }
    }
    // Process properties with LdapAttributesList annotation
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        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);
            }
            // Prepare attributes for removal
            for (AttributeData attributeFromLdap : attributesFromLdapMap.values()) {
                String attributeName = attributeFromLdap.getName();
                if (OBJECT_CLASS.equalsIgnoreCase(attributeName)) {
                    continue;
                }
                LdapAttribute ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
                if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
                    continue;
                }
                if (!attributesToPersistMap.containsKey(attributeName.toLowerCase())) {
                    // 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();
                LdapAttribute 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) && (Arrays.equals(attributeToPersist.getValues(), new String[] {}))) {
                    attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
                } else {
                    if (!attributeFromLdap.equals(attributeToPersist)) {
                        if (isEmptyAttributeValues(attributeToPersist) && !ldapAttributeConfiguration.updateOnly()) {
                            attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
                        } else {
                            attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
                        }
                    }
                }
            }
        }
    }
    return attributeDataModifications;
}
Also used : AttributeDataModification(org.gluu.persist.model.AttributeDataModification) AttributeModificationType(org.gluu.persist.model.AttributeDataModification.AttributeModificationType) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) 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 18 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class BaseEntryManager method countEntries.

@SuppressWarnings("unchecked")
public <T> int countEntries(Object entry) {
    if (entry == null) {
        throw new MappingException("Entry to count is null");
    }
    // Check entry class
    Class<T> entryClass = (Class<T>) entry.getClass();
    checkEntryClass(entryClass, false);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Object dnValue = getDNValue(entry, entryClass);
    List<AttributeData> attributes = getAttributesListForPersist(entry, propertiesAnnotations);
    Filter searchFilter = createFilterByEntry(entry, entryClass, attributes);
    return countEntries(dnValue.toString(), entryClass, searchFilter);
}
Also used : Filter(org.gluu.search.filter.Filter) LdapObjectClass(org.gluu.site.ldap.persistence.annotation.LdapObjectClass) LdapCustomObjectClass(org.gluu.site.ldap.persistence.annotation.LdapCustomObjectClass) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) AttributeData(org.gluu.persist.model.AttributeData) MappingException(org.gluu.persist.exception.mapping.MappingException) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation)

Example 19 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class BaseEntryManager method getAttribute.

private AttributeData getAttribute(String propertyName, String ldapAttributeName, Getter propertyValueGetter, Object entry, boolean jsonObject) {
    Object propertyValue = propertyValueGetter.get(entry);
    if (propertyValue == null) {
        return null;
    }
    String[] attributeValues = new String[1];
    if (propertyValue instanceof String) {
        attributeValues[0] = StringHelper.toString(propertyValue);
    } else if (propertyValue instanceof Boolean) {
        attributeValues[0] = propertyValue.toString();
    } else if (propertyValue instanceof Integer) {
        attributeValues[0] = propertyValue.toString();
    } else if (propertyValue instanceof Long) {
        attributeValues[0] = propertyValue.toString();
    } else if (propertyValue instanceof Date) {
        attributeValues[0] = encodeGeneralizedTime((Date) propertyValue);
    } else if (propertyValue instanceof String[]) {
        attributeValues = (String[]) propertyValue;
    } else if (propertyValue instanceof List<?>) {
        attributeValues = new String[((List<?>) propertyValue).size()];
        int index = 0;
        for (Object tmpPropertyValue : (List<?>) propertyValue) {
            if (jsonObject) {
                attributeValues[index++] = convertJsonToString(tmpPropertyValue);
            } else {
                attributeValues[index++] = StringHelper.toString(tmpPropertyValue);
            }
        }
    } else if (propertyValue instanceof LdapEnum) {
        attributeValues[0] = ((LdapEnum) propertyValue).getValue();
    } else if (propertyValue instanceof LdapEnum[]) {
        LdapEnum[] propertyValues = (LdapEnum[]) propertyValue;
        attributeValues = new String[propertyValues.length];
        for (int i = 0; i < propertyValues.length; i++) {
            attributeValues[i] = (propertyValues[i] == null) ? null : propertyValues[i].getValue();
        }
    } else if (jsonObject) {
        attributeValues[0] = convertJsonToString(propertyValue);
    } else {
        throw new MappingException("Entry property '" + propertyName + "' should has getter with String, String[], Boolean, Integer, Long, Date, List, LdapEnum or LdapEnum[]" + " return type or has annotation LdapJsonObject");
    }
    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) && StringHelper.isEmpty(attributeValues[0])) {
        return null;
    }
    return new AttributeData(ldapAttributeName, attributeValues);
}
Also used : Date(java.util.Date) MappingException(org.gluu.persist.exception.mapping.MappingException) LdapEnum(org.gluu.site.ldap.persistence.annotation.LdapEnum) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) ArrayList(java.util.ArrayList) List(java.util.List) AttributeData(org.gluu.persist.model.AttributeData)

Example 20 with AttributeData

use of org.gluu.persist.model.AttributeData in project oxCore by GluuFederation.

the class BaseEntryManager method getAttributesFromLdapAttributesList.

private List<AttributeData> getAttributesFromLdapAttributesList(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 = ((LdapAttributesList) ldapAttribute).name();
    Getter entryPropertyNameGetter = getGetter(elementType, entryPropertyName);
    if (entryPropertyNameGetter == null) {
        throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyName);
    }
    String entryPropertyValue = ((LdapAttributesList) ldapAttribute).value();
    Getter entryPropertyValueGetter = getGetter(elementType, entryPropertyValue);
    if (entryPropertyValueGetter == null) {
        throw new MappingException("Entry should has getter for property " + propertyName + "." + entryPropertyValue);
    }
    for (Object entryAttribute : (List<?>) propertyValue) {
        AttributeData attribute = getAttribute(propertyName, entryPropertyNameGetter, entryPropertyValueGetter, entryAttribute, false);
        if (attribute != null) {
            listAttributes.add(attribute);
        }
    }
    return listAttributes;
}
Also used : Getter(org.gluu.persist.reflect.property.Getter) ArrayList(java.util.ArrayList) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) ArrayList(java.util.ArrayList) List(java.util.List) AttributeData(org.gluu.persist.model.AttributeData) MappingException(org.gluu.persist.exception.mapping.MappingException) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList)

Aggregations

AttributeData (org.gluu.persist.model.AttributeData)25 ArrayList (java.util.ArrayList)15 MappingException (org.gluu.persist.exception.mapping.MappingException)15 PropertyAnnotation (org.gluu.persist.model.PropertyAnnotation)14 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)9 List (java.util.List)7 Annotation (java.lang.annotation.Annotation)6 LdapAttribute (org.gluu.site.ldap.persistence.annotation.LdapAttribute)6 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)6 HashMap (java.util.HashMap)4 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)4 AttributeDataModification (org.gluu.persist.model.AttributeDataModification)4 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)3 ParseException (java.text.ParseException)3 LinkedList (java.util.LinkedList)3 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)3 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)3 SearchException (org.gluu.persist.exception.operation.SearchException)3 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)3 Getter (org.gluu.persist.reflect.property.Getter)3