Search in sources :

Example 11 with AttributeData

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

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

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

the class BaseEntryManager method persist.

@Override
public void persist(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);
    // Add object classes
    String[] objectClasses = getObjectClasses(entry, entryClass);
    attributes.add(new AttributeData(OBJECT_CLASS, objectClasses));
    LOG.debug(String.format("LDAP attributes for persist: %s", attributes));
    persist(dnValue.toString(), attributes);
}
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 14 with AttributeData

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

the class LdapEntryManager method updateMergeChanges.

@Override
protected <T> void updateMergeChanges(T entry, boolean isSchemaUpdate, Class<?> entryClass, Map<String, AttributeData> attributesFromLdapMap, List<AttributeDataModification> attributeDataModifications) {
    // Update object classes if entry contains custom object classes
    if (getSupportedLDAPVersion() > 2) {
        if (!isSchemaUpdate) {
            String[] objectClasses = getObjectClasses(entry, entryClass);
            String[] objectClassesFromLdap = attributesFromLdapMap.get(OBJECT_CLASS.toLowerCase()).getValues();
            if (!Arrays.equals(objectClassesFromLdap, objectClasses)) {
                attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, new AttributeData(OBJECT_CLASS, objectClasses), new AttributeData(OBJECT_CLASS, objectClassesFromLdap)));
            }
        }
    }
}
Also used : AttributeDataModification(org.gluu.persist.model.AttributeDataModification) AttributeData(org.gluu.persist.model.AttributeData)

Example 15 with AttributeData

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

the class LdapEntryManager method merge.

@Override
public void merge(String dn, List<AttributeDataModification> attributeDataModifications) {
    // Update entry
    try {
        List<Modification> modifications = new ArrayList<Modification>(attributeDataModifications.size());
        for (AttributeDataModification attributeDataModification : attributeDataModifications) {
            AttributeData attribute = attributeDataModification.getAttribute();
            AttributeData oldAttribute = attributeDataModification.getOldAttribute();
            String attributeName = null;
            String[] attributeValues = null;
            if (attribute != null) {
                attributeName = attribute.getName();
                attributeValues = attribute.getValues();
            }
            String oldAttributeName = null;
            String[] oldAttributeValues = null;
            if (oldAttribute != null) {
                oldAttributeName = oldAttribute.getName();
                oldAttributeValues = oldAttribute.getValues();
            }
            Modification modification = null;
            if (AttributeModificationType.ADD.equals(attributeDataModification.getModificationType())) {
                modification = createModification(ModificationType.ADD, attributeName, attributeValues);
            } else {
                if (AttributeModificationType.REMOVE.equals(attributeDataModification.getModificationType())) {
                    modification = createModification(ModificationType.DELETE, oldAttributeName, oldAttributeValues);
                } else if (AttributeModificationType.REPLACE.equals(attributeDataModification.getModificationType())) {
                    if (attributeValues.length == 1) {
                        modification = createModification(ModificationType.REPLACE, attributeName, attributeValues);
                    } else {
                        String[] oldValues = ArrayHelper.arrayClone(oldAttributeValues);
                        String[] newValues = ArrayHelper.arrayClone(attributeValues);
                        Arrays.sort(oldValues);
                        Arrays.sort(newValues);
                        boolean[] retainOldValues = new boolean[oldValues.length];
                        Arrays.fill(retainOldValues, false);
                        List<String> addValues = new ArrayList<String>();
                        List<String> removeValues = new ArrayList<String>();
                        // Add new values
                        for (String value : newValues) {
                            int idx = Arrays.binarySearch(oldValues, value, new Comparator<String>() {

                                @Override
                                public int compare(String o1, String o2) {
                                    return o1.toLowerCase().compareTo(o2.toLowerCase());
                                }
                            });
                            if (idx >= 0) {
                                // Old values array contains new value. Retain
                                // old value
                                retainOldValues[idx] = true;
                            } else {
                                // This is new value
                                addValues.add(value);
                            }
                        }
                        // Remove values which we don't have in new values
                        for (int i = 0; i < oldValues.length; i++) {
                            if (!retainOldValues[i]) {
                                removeValues.add(oldValues[i]);
                            }
                        }
                        if (removeValues.size() > 0) {
                            Modification removeModification = createModification(ModificationType.DELETE, attributeName, removeValues.toArray(new String[removeValues.size()]));
                            modifications.add(removeModification);
                        }
                        if (addValues.size() > 0) {
                            Modification addModification = createModification(ModificationType.ADD, attributeName, addValues.toArray(new String[addValues.size()]));
                            modifications.add(addModification);
                        }
                    }
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = this.ldapOperationService.updateEntry(dn, modifications);
            if (!result) {
                throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn));
            }
        }
    } catch (ConnectionException ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex.getCause());
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex);
    }
}
Also used : AttributeDataModification(org.gluu.persist.model.AttributeDataModification) Modification(com.unboundid.ldap.sdk.Modification) ArrayList(java.util.ArrayList) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) SearchException(org.gluu.persist.exception.operation.SearchException) AuthenticationException(org.gluu.persist.exception.operation.AuthenticationException) MappingException(org.gluu.persist.exception.mapping.MappingException) SearchScopeException(org.gluu.persist.exception.operation.SearchScopeException) ParseException(java.text.ParseException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) ConnectionException(org.gluu.persist.exception.operation.ConnectionException) Comparator(java.util.Comparator) AttributeDataModification(org.gluu.persist.model.AttributeDataModification) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttributeData(org.gluu.persist.model.AttributeData) ConnectionException(org.gluu.persist.exception.operation.ConnectionException)

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