Search in sources :

Example 1 with LdapEnum

use of org.gluu.site.ldap.persistence.annotation.LdapEnum in project oxCore by GluuFederation.

the class AbstractEntryManager 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)) {
        propertyValueSetter.set(entry, attribute.getValue());
    } else if (parameterType.equals(Boolean.class) || parameterType.equals(Boolean.TYPE)) {
        propertyValueSetter.set(entry, attribute.getValue() == null ? null : Boolean.valueOf(attribute.getValue()));
    } else if (parameterType.equals(Integer.class) || parameterType.equals(Integer.TYPE)) {
        propertyValueSetter.set(entry, attribute.getValue() == null ? null : Integer.valueOf(attribute.getValue()));
    } else if (parameterType.equals(Long.class) || parameterType.equals(Long.TYPE)) {
        propertyValueSetter.set(entry, attribute.getValue() == null ? null : Long.valueOf(attribute.getValue()));
    } else if (parameterType.equals(Date.class)) {
        propertyValueSetter.set(entry, decodeGeneralizedTime(attribute.getValue()));
    } else if (parameterType.equals(String[].class)) {
        propertyValueSetter.set(entry, attribute.getValues());
    } else if (ReflectHelper.assignableFrom(parameterType, List.class)) {
        if (jsonObject) {
            String[] stringValues = attribute.getValues();
            List<Object> jsonValues = new ArrayList<Object>(stringValues.length);
            for (String stringValue : stringValues) {
                Object jsonValue = convertStringToJson(entry, ReflectHelper.getListType(propertyValueSetter), stringValue);
                jsonValues.add(jsonValue);
            }
            propertyValueSetter.set(entry, jsonValues);
        } else {
            propertyValueSetter.set(entry, Arrays.asList(attribute.getValues()));
        }
    } else if (ReflectHelper.assignableFrom(parameterType, LdapEnum.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 by value " + attribute.getValue(), ex);
        }
    } else if (ReflectHelper.assignableFrom(parameterType, LdapEnum[].class)) {
        Class<?> itemType = parameterType.getComponentType();
        Method enumResolveByValue;
        try {
            enumResolveByValue = itemType.getMethod("resolveByValue", String.class);
        } catch (Exception ex) {
            throw new MappingException("Failed to resolve Enum by value " + Arrays.toString(attribute.getValues()), ex);
        }
        String[] attributeValues = attribute.getValues();
        LdapEnum[] ldapEnums = (LdapEnum[]) ReflectHelper.createArray(itemType, attributeValues.length);
        for (int i = 0; i < attributeValues.length; i++) {
            try {
                ldapEnums[i] = (LdapEnum) enumResolveByValue.invoke(itemType.getEnumConstants()[0], attributeValues[i]);
            } catch (Exception ex) {
                throw new MappingException("Failed to resolve Enum by value " + Arrays.toString(attribute.getValues()), ex);
            }
        }
        propertyValueSetter.set(entry, ldapEnums);
    } else if (jsonObject) {
        String stringValue = attribute.getValue();
        Object jsonValue = convertStringToJson(entry, parameterType, stringValue);
        propertyValueSetter.set(entry, jsonValue);
    } else {
        throw new MappingException("Entry property '" + propertyName + "' should has setter with String, Boolean, Integer, Long, Date, String[], List, LdapEnum or LdapEnum[] parameter type or has annotation LdapJsonObject");
    }
}
Also used : Method(java.lang.reflect.Method) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) MappingException(org.gluu.site.ldap.persistence.exception.MappingException) LdapEnum(org.gluu.site.ldap.persistence.annotation.LdapEnum) LdapJsonObject(org.gluu.site.ldap.persistence.annotation.LdapJsonObject)

Example 2 with LdapEnum

use of org.gluu.site.ldap.persistence.annotation.LdapEnum in project oxCore by GluuFederation.

the class AbstractEntryManager 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 : MappingException(org.gluu.site.ldap.persistence.exception.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)

Aggregations

LdapEnum (org.gluu.site.ldap.persistence.annotation.LdapEnum)2 LdapJsonObject (org.gluu.site.ldap.persistence.annotation.LdapJsonObject)2 MappingException (org.gluu.site.ldap.persistence.exception.MappingException)2 Method (java.lang.reflect.Method)1 LdapAttributesList (org.gluu.site.ldap.persistence.annotation.LdapAttributesList)1 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)1