Search in sources :

Example 11 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

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(io.jans.orm.search.filter.Filter) CustomObjectClass(io.jans.orm.annotation.CustomObjectClass) ObjectClass(io.jans.orm.annotation.ObjectClass) JsonObject(io.jans.orm.annotation.JsonObject) AttributeData(io.jans.orm.model.AttributeData) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Example 12 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class BaseEntryManager method isConfigurationEntry.

protected boolean isConfigurationEntry(Class<?> entryClass) {
    if (entryClass == null) {
        throw new MappingException("Entry class is null");
    }
    // Check if entry is LDAP Entry
    List<Annotation> entryAnnotations = ReflectHelper.getClassAnnotations(entryClass, LDAP_ENTRY_TYPE_ANNOTATIONS);
    DataEntry dataEntry = (DataEntry) ReflectHelper.getAnnotationByType(entryAnnotations, DataEntry.class);
    if (dataEntry == null) {
        return false;
    }
    return dataEntry.configurationDefinition();
}
Also used : DataEntry(io.jans.orm.annotation.DataEntry) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) MappingException(io.jans.orm.exception.MappingException)

Example 13 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class BaseEntryManager method setPropertyValue.

private void setPropertyValue(String propertyName, Setter propertyValueSetter, Object entry, AttributeData attribute, boolean jsonObject) {
    if (attribute == null) {
        return;
    }
    LOG.debug(String.format("LdapProperty: %s, AttributeName: %s, AttributeValue: %s", propertyName, attribute.getName(), Arrays.toString(attribute.getValues())));
    Class<?> parameterType = ReflectHelper.getSetterType(propertyValueSetter);
    if (parameterType.equals(String.class)) {
        Object value = attribute.getValue();
        if (value instanceof Date) {
            value = encodeTime((Date) value);
        }
        propertyValueSetter.set(entry, String.valueOf(value));
    } else if (parameterType.equals(Boolean.class) || parameterType.equals(Boolean.TYPE)) {
        propertyValueSetter.set(entry, toBooleanValue(attribute));
    } else if (parameterType.equals(Integer.class) || parameterType.equals(Integer.TYPE)) {
        propertyValueSetter.set(entry, toIntegerValue(attribute));
    } else if (parameterType.equals(Long.class) || parameterType.equals(Long.TYPE)) {
        propertyValueSetter.set(entry, toLongValue(attribute));
    } else if (parameterType.equals(Date.class)) {
        if (attribute.getValue() == null) {
            propertyValueSetter.set(entry, null);
        } else {
            propertyValueSetter.set(entry, attribute.getValue() instanceof Date ? (Date) attribute.getValue() : decodeTime(String.valueOf(attribute.getValue())));
        }
    } else if (parameterType.equals(String[].class)) {
        propertyValueSetter.set(entry, attribute.getStringValues());
    } else if (ReflectHelper.assignableFrom(parameterType, List.class)) {
        if (jsonObject) {
            Object[] values = attribute.getValues();
            List<Object> jsonValues = new ArrayList<Object>(values.length);
            for (Object value : values) {
                Object jsonValue = convertJsonToValue(ReflectHelper.getListType(propertyValueSetter), value);
                jsonValues.add(jsonValue);
            }
            propertyValueSetter.set(entry, jsonValues);
        } else {
            List<?> resultValues = attributeToTypedList(ReflectHelper.getListType(propertyValueSetter), attribute);
            propertyValueSetter.set(entry, resultValues);
        }
    } else if (ReflectHelper.assignableFrom(parameterType, AttributeEnum.class)) {
        try {
            propertyValueSetter.set(entry, parameterType.getMethod("resolveByValue", String.class).invoke(parameterType.getEnumConstants()[0], attribute.getValue()));
        } catch (Exception ex) {
            throw new MappingException("Failed to resolve Enum '" + parameterType + "' by value '" + attribute.getValue() + "'", ex);
        }
    } else if (ReflectHelper.assignableFrom(parameterType, AttributeEnum[].class)) {
        Class<?> itemType = parameterType.getComponentType();
        Method enumResolveByValue;
        try {
            enumResolveByValue = itemType.getMethod("resolveByValue", String.class);
        } catch (Exception ex) {
            throw new MappingException("Failed to resolve Enum '" + parameterType + "' by value '" + Arrays.toString(attribute.getValues()) + "'", ex);
        }
        Object[] attributeValues = attribute.getValues();
        AttributeEnum[] ldapEnums = (AttributeEnum[]) ReflectHelper.createArray(itemType, attributeValues.length);
        for (int i = 0; i < attributeValues.length; i++) {
            try {
                ldapEnums[i] = (AttributeEnum) enumResolveByValue.invoke(itemType.getEnumConstants()[0], attributeValues[i]);
            } catch (Exception ex) {
                throw new MappingException("Failed to resolve Enum '" + parameterType + "' by value '" + Arrays.toString(attribute.getValues()) + "'", ex);
            }
        }
        propertyValueSetter.set(entry, ldapEnums);
    } else if (jsonObject) {
        Object stringValue = attribute.getValue();
        Object jsonValue = convertJsonToValue(parameterType, stringValue);
        propertyValueSetter.set(entry, jsonValue);
    } else {
        throw new MappingException("Entry property '" + propertyName + "' should has setter with String, Boolean, Integer, Long, Date, String[], List<String>, AttributeEnum or AttributeEnum[]" + " parameter type or has annotation JsonObject");
    }
}
Also used : AttributeEnum(io.jans.orm.annotation.AttributeEnum) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Date(java.util.Date) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) InvalidArgumentException(io.jans.orm.exception.InvalidArgumentException) MappingException(io.jans.orm.exception.MappingException) JsonObject(io.jans.orm.annotation.JsonObject)

Example 14 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class BaseEntryManager method getAttributeDataFromAttribute.

private AttributeData getAttributeDataFromAttribute(Object entry, Annotation ldapAttribute, PropertyAnnotation propertiesAnnotation, String propertyName) {
    Class<?> entryClass = entry.getClass();
    String ldapAttributeName = ((AttributeName) 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);
    }
    Class<?> parameterType = getSetterPropertyType(entryClass, propertyName);
    boolean multiValued = isMultiValued(parameterType);
    Annotation ldapJsonObject = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), JsonObject.class);
    boolean jsonObject = ldapJsonObject != null;
    AttributeData attribute = getAttributeData(propertyName, ldapAttributeName, getter, entry, multiValued, jsonObject);
    return attribute;
}
Also used : Getter(io.jans.orm.reflect.property.Getter) AttributeName(io.jans.orm.annotation.AttributeName) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) AttributeData(io.jans.orm.model.AttributeData) MappingException(io.jans.orm.exception.MappingException)

Example 15 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

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 = getAttributes(null, propertiesAnnotations, false);
    return contains(dnValue.toString(), entryClass, propertiesAnnotations, attributes, objectClasses, ldapReturnAttributes);
}
Also used : JsonObject(io.jans.orm.annotation.JsonObject) AttributeData(io.jans.orm.model.AttributeData) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Aggregations

MappingException (io.jans.orm.exception.MappingException)43 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)27 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)19 SearchException (io.jans.orm.exception.operation.SearchException)18 AuthenticationException (io.jans.orm.exception.AuthenticationException)16 Filter (io.jans.orm.search.filter.Filter)16 AttributeData (io.jans.orm.model.AttributeData)15 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)14 JsonObject (io.jans.orm.annotation.JsonObject)12 ArrayList (java.util.ArrayList)11 SearchScopeException (io.jans.orm.exception.operation.SearchScopeException)7 Annotation (java.lang.annotation.Annotation)7 SearchResult (com.unboundid.ldap.sdk.SearchResult)6 ConnectionException (io.jans.orm.exception.operation.ConnectionException)6 EntryData (io.jans.orm.model.EntryData)6 DateTimeParseException (java.time.format.DateTimeParseException)6 List (java.util.List)6 Getter (io.jans.orm.reflect.property.Getter)5 ParseException (java.text.ParseException)5 DataEntry (io.jans.orm.annotation.DataEntry)4