Search in sources :

Example 6 with AttributeData

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

the class PersonImportAction method convertTableToPersons.

protected List<GluuCustomPerson> convertTableToPersons(Table table, List<ImportAttribute> importAttributes) throws Exception {
    // Prepare for conversion to list of GluuCustomPerson and check data
    // type
    Map<String, List<AttributeData>> entriesAttributes = new HashMap<String, List<AttributeData>>();
    int rows = table.getCountRows();
    boolean validTable = true;
    for (int i = 1; i <= rows; i++) {
        List<AttributeData> attributeDataList = new ArrayList<AttributeData>();
        for (ImportAttribute importAttribute : importAttributes) {
            if (importAttribute.getCol() == -1) {
                continue;
            }
            GluuAttribute attribute = importAttribute.getAttribute();
            String cellValue = table.getCellValue(importAttribute.getCol(), i);
            if (StringHelper.isEmpty(cellValue)) {
                if (attribute.isRequred()) {
                    facesMessages.add(FacesMessage.SEVERITY_ERROR, "Import failed. Empty '%s' not allowed", attribute.getDisplayName());
                    validTable = false;
                }
                continue;
            }
            String ldapValue = getTypedValue(attribute, cellValue);
            if (StringHelper.isEmpty(ldapValue)) {
                facesMessages.add(FacesMessage.SEVERITY_ERROR, "Invalid value '%s' in column '%s' at row %s were specified", cellValue, attribute.getDisplayName(), i + 1);
                validTable = false;
                continue;
            }
            AttributeData attributeData = new AttributeData(attribute.getName(), ldapValue);
            attributeDataList.add(attributeData);
        }
        entriesAttributes.put(Integer.toString(i), attributeDataList);
    }
    if (!validTable) {
        return null;
    }
    // Convert to GluuCustomPerson and set right DN
    List<GluuCustomPerson> persons = personService.createEntities(entriesAttributes);
    log.info("Found {} persons in input Excel file", persons.size());
    for (GluuCustomPerson person : persons) {
        for (String key : entriesAttributes.keySet()) {
            boolean flag = false;
            for (AttributeData AttributeData : entriesAttributes.get(key)) {
                if (AttributeData.getName().equalsIgnoreCase("uid")) {
                    if (person.getUid().equalsIgnoreCase(AttributeData.getValue())) {
                        for (AttributeData AttributeData1 : entriesAttributes.get(key)) {
                            if (AttributeData1.getName().equalsIgnoreCase("userPassword")) {
                                person.setUserPassword(AttributeData1.getValue());
                                flag = true;
                                break;
                            } else if (AttributeData1.getName().equalsIgnoreCase("gluuStatus")) {
                                person.setStatus(GluuStatus.getByValue(AttributeData1.getValue()));
                                flag = true;
                                break;
                            }
                        }
                    }
                } else {
                    if (flag)
                        break;
                }
            }
            if (flag)
                break;
        }
    }
    return persons;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GluuAttribute(org.xdi.model.GluuAttribute) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) ArrayList(java.util.ArrayList) List(java.util.List) AttributeData(org.gluu.persist.model.AttributeData)

Example 7 with AttributeData

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

the class BaseEntryManager method createAttributesFilter.

protected Filter[] createAttributesFilter(List<AttributeData> attributes) {
    if ((attributes == null) || (attributes.size() == 0)) {
        return null;
    }
    List<Filter> results = new ArrayList<Filter>(attributes.size());
    for (AttributeData attribute : attributes) {
        String attributeName = attribute.getName();
        for (String value : attribute.getValues()) {
            Filter filter = Filter.createEqualityFilter(attributeName, value);
            results.add(filter);
        }
    }
    return results.toArray(new Filter[results.size()]);
}
Also used : Filter(org.gluu.search.filter.Filter) ArrayList(java.util.ArrayList) AttributeData(org.gluu.persist.model.AttributeData)

Example 8 with AttributeData

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

the class BaseEntryManager method contains.

@Override
public boolean contains(Object entry) {
    if (entry == null) {
        throw new MappingException("Entry to persist is null");
    }
    // Check entry class
    Class<?> entryClass = entry.getClass();
    checkEntryClass(entryClass, false);
    String[] objectClasses = getObjectClasses(entry, entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Object dnValue = getDNValue(entry, entryClass);
    List<AttributeData> attributes = getAttributesListForPersist(entry, propertiesAnnotations);
    String[] ldapReturnAttributes = getLdapAttributes(null, propertiesAnnotations, false);
    return contains(dnValue.toString(), attributes, objectClasses, ldapReturnAttributes);
}
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 9 with AttributeData

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

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);
        // 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>() {

                                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 : HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) MappingException(org.gluu.persist.exception.mapping.MappingException) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) LdapAttribute(org.gluu.site.ldap.persistence.annotation.LdapAttribute) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) ArrayList(java.util.ArrayList) List(java.util.List) MappingException(org.gluu.persist.exception.mapping.MappingException) InvalidArgumentException(org.gluu.persist.exception.mapping.InvalidArgumentException) EntryPersistenceException(org.gluu.persist.exception.mapping.EntryPersistenceException) PropertyAnnotation(org.gluu.persist.model.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) LdapAttributesList(org.gluu.site.ldap.persistence.annotation.LdapAttributesList) Setter(org.gluu.persist.reflect.property.Setter) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) AttributeData(org.gluu.persist.model.AttributeData)

Example 10 with AttributeData

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

the class BaseEntryManager 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.persist.reflect.property.Getter) 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) MappingException(org.gluu.persist.exception.mapping.MappingException)

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