Search in sources :

Example 11 with PropertyAnnotation

use of org.gluu.persist.model.PropertyAnnotation 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 12 with PropertyAnnotation

use of org.gluu.persist.model.PropertyAnnotation 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 13 with PropertyAnnotation

use of org.gluu.persist.model.PropertyAnnotation 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 14 with PropertyAnnotation

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

the class BaseEntryManager method getHashCode.

@Override
public int getHashCode(Object entry) {
    if (entry == null) {
        throw new MappingException("Entry to persist is null");
    }
    // Check entry class
    Class<?> entryClass = entry.getClass();
    checkEntryClass(entryClass, false);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Object dnValue = getDNValue(entry, entryClass);
    List<AttributeData> attributes = getAttributesListForPersist(entry, propertiesAnnotations);
    String key = getEntryKey(dnValue, false, propertiesAnnotations, attributes);
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Entry key HashCode is: %s", key.hashCode()));
    }
    return key.hashCode();
}
Also used : 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 15 with PropertyAnnotation

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

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) 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(), LdapAttribute.class);
        if (ldapAttribute == null) {
            continue;
        }
        String ldapAttributeName = ((LdapAttribute) 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.getValues().clone();
            Arrays.sort(values);
        }
        addPropertyWithValuesToKey(sb, ldapAttributeName, values);
    }
    for (AttributeData attributeData : attributesData) {
        if (processedProperties.contains(attributeData.getName())) {
            continue;
        }
        addPropertyWithValuesToKey(sb, attributeData.getName(), attributeData.getValues());
    }
    if (caseSensetive) {
        return sb.toString();
    } else {
        return sb.toString().toLowerCase();
    }
}
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)

Aggregations

PropertyAnnotation (org.gluu.persist.model.PropertyAnnotation)18 AttributeData (org.gluu.persist.model.AttributeData)14 MappingException (org.gluu.persist.exception.mapping.MappingException)13 ArrayList (java.util.ArrayList)9 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)7 Annotation (java.lang.annotation.Annotation)6 LdapAttribute (org.gluu.site.ldap.persistence.annotation.LdapAttribute)6 Filter (org.gluu.search.filter.Filter)5 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)5 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)4 SearchResult (com.unboundid.ldap.sdk.SearchResult)3 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)3 ParseException (java.text.ParseException)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 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 List (java.util.List)2