use of org.gluu.persist.exception.mapping.MappingException 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;
}
use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.
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)) {
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(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(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");
}
}
use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.
the class BaseEntryManager method checkEntryClass.
protected boolean checkEntryClass(Class<?> entryClass, boolean isAllowSchemaEntry) {
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);
Annotation ldapSchemaEntry = ReflectHelper.getAnnotationByType(entryAnnotations, LdapSchemaEntry.class);
Annotation ldapEntry = ReflectHelper.getAnnotationByType(entryAnnotations, LdapEntry.class);
if (isAllowSchemaEntry) {
if ((ldapSchemaEntry == null) && (ldapEntry == null)) {
throw new MappingException("Entry should has LdapEntry or LdapSchemaEntry annotation");
}
} else {
if (ldapEntry == null) {
throw new MappingException("Entry should has LdapEntry annotation");
}
}
return true;
}
use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.
the class BaseEntryManager method getEntrySortBy.
protected String[] getEntrySortBy(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);
Annotation annotation = ReflectHelper.getAnnotationByType(entryAnnotations, LdapEntry.class);
if (annotation == null) {
return null;
}
return ((LdapEntry) annotation).sortBy();
}
use of org.gluu.persist.exception.mapping.MappingException in project oxCore by GluuFederation.
the class BaseEntryManager method getListItem.
private Object getListItem(String propertyName, Setter propertyNameSetter, Setter propertyValueSetter, Class<?> classType, AttributeData attribute) {
if (attribute == null) {
return null;
}
Object result;
try {
result = ReflectHelper.createObjectByDefaultConstructor(classType);
} catch (Exception ex) {
throw new MappingException(String.format("Entry %s should has default constructor", classType));
}
propertyNameSetter.set(result, attribute.getName());
setPropertyValue(propertyName, propertyValueSetter, result, attribute, false);
return result;
}
Aggregations