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);
}
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;
}
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();
}
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;
}
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;
}
Aggregations