Search in sources :

Example 16 with MappingException

use of org.gluu.persist.exception.mapping.MappingException 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 17 with MappingException

use of org.gluu.persist.exception.mapping.MappingException 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)

Example 18 with MappingException

use of org.gluu.persist.exception.mapping.MappingException 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 19 with MappingException

use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.

the class BaseEntryManager method groupListByPropertiesImpl.

private <T> Map<T, List<T>> groupListByPropertiesImpl(Class<T> entryClass, List<T> entries, boolean caseSensetive, Getter[] groupPropertyGetters, Setter[] groupPropertySetters, Getter[] sumProperyGetters, Setter[] sumPropertySetter) {
    Map<String, T> keys = new HashMap<String, T>();
    Map<T, List<T>> groups = new IdentityHashMap<T, List<T>>();
    for (T entry : entries) {
        String key = getEntryKey(entry, caseSensetive, groupPropertyGetters);
        T entryKey = keys.get(key);
        if (entryKey == null) {
            try {
                entryKey = ReflectHelper.createObjectByDefaultConstructor(entryClass);
            } catch (Exception ex) {
                throw new MappingException(String.format("Entry %s should has default constructor", entryClass), ex);
            }
            try {
                ReflectHelper.copyObjectPropertyValues(entry, entryKey, groupPropertyGetters, groupPropertySetters);
            } catch (Exception ex) {
                throw new MappingException("Failed to set values in group Entry", ex);
            }
            keys.put(key, entryKey);
        }
        List<T> groupValues = groups.get(entryKey);
        if (groupValues == null) {
            groupValues = new ArrayList<T>();
            groups.put(entryKey, groupValues);
        }
        try {
            if (sumProperyGetters != null) {
                ReflectHelper.sumObjectPropertyValues(entryKey, entry, sumProperyGetters, sumPropertySetter);
            }
        } catch (Exception ex) {
            throw new MappingException("Failed to sum values in group Entry", ex);
        }
        groupValues.add(entry);
    }
    return groups;
}
Also used : HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) IdentityHashMap(java.util.IdentityHashMap) 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) MappingException(org.gluu.persist.exception.mapping.MappingException)

Example 20 with MappingException

use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.

the class BaseEntryManager method getDNValue.

protected <T> Object getDNValue(Object entry, Class<T> entryClass) {
    // Check if entry has DN property
    String dnProperty = getDNPropertyName(entryClass);
    // Get DN value
    Getter dnGetter = getGetter(entryClass, dnProperty);
    if (dnGetter == null) {
        throw new MappingException("Entry should has getter for property " + dnProperty);
    }
    Object dnValue = dnGetter.get(entry);
    if (StringHelper.isEmptyString(dnValue)) {
        throw new MappingException("Entry should has not null base DN property value");
    }
    return dnValue;
}
Also used : Getter(org.gluu.persist.reflect.property.Getter) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject) MappingException(org.gluu.persist.exception.mapping.MappingException)

Aggregations

MappingException (org.gluu.persist.exception.mapping.MappingException)23 PropertyAnnotation (org.gluu.persist.model.PropertyAnnotation)15 AttributeData (org.gluu.persist.model.AttributeData)12 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)12 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)9 ArrayList (java.util.ArrayList)8 Filter (org.gluu.search.filter.Filter)7 SearchException (org.gluu.persist.exception.operation.SearchException)5 SearchResult (com.unboundid.ldap.sdk.SearchResult)4 Annotation (java.lang.annotation.Annotation)4 ParseException (java.text.ParseException)4 List (java.util.List)4 InvalidArgumentException (org.gluu.persist.exception.mapping.InvalidArgumentException)4 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)4 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)4 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)4 Getter (org.gluu.persist.reflect.property.Getter)4 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)4 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)3 HashMap (java.util.HashMap)2