use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.
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 io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.
the class BaseEntryManager method getAttributesListForPersist.
protected List<AttributeData> getAttributesListForPersist(Object entry, List<PropertyAnnotation> propertiesAnnotations) {
// Prepare list of properties to persist
List<AttributeData> attributes = new ArrayList<AttributeData>();
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Annotation ldapAttribute;
// Process properties with AttributeName annotation
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributeName.class);
if (ldapAttribute != null) {
AttributeData attribute = getAttributeDataFromAttribute(entry, ldapAttribute, propertiesAnnotation, propertyName);
if (attribute != null) {
attributes.add(attribute);
}
continue;
}
// Process properties with @AttributesList annotation
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributesList.class);
if (ldapAttribute != null) {
List<AttributeData> listAttributes = getAttributesFromAttributesList(entry, ldapAttribute, propertyName);
if (listAttributes != null) {
attributes.addAll(listAttributes);
}
continue;
}
}
return attributes;
}
use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.
the class BaseEntryManager method collectAttributeModifications.
protected List<AttributeDataModification> collectAttributeModifications(List<PropertyAnnotation> propertiesAnnotations, Map<String, AttributeData> attributesToPersistMap, Map<String, AttributeData> attributesFromLdapMap, boolean isSchemaUpdate, AttributeModificationType schemaModificationType, boolean forceUpdate) {
List<AttributeDataModification> attributeDataModifications = new ArrayList<AttributeDataModification>();
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Annotation ldapAttribute;
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributeName.class);
if (ldapAttribute != null) {
String ldapAttributeName = ((AttributeName) ldapAttribute).name();
if (StringHelper.isEmpty(ldapAttributeName)) {
ldapAttributeName = propertyName;
}
ldapAttributeName = ldapAttributeName.toLowerCase();
AttributeData attributeToPersist = attributesToPersistMap.get(ldapAttributeName);
AttributeData attributeFromLdap = attributesFromLdapMap.get(ldapAttributeName);
// Remove processed attributes
attributesToPersistMap.remove(ldapAttributeName);
attributesFromLdapMap.remove(ldapAttributeName);
AttributeName ldapAttributeAnnotation = (AttributeName) ldapAttribute;
if (ldapAttributeAnnotation.ignoreDuringUpdate()) {
continue;
}
if (attributeFromLdap != null && attributeToPersist != null) {
// Modify DN entry attribute in DS
if (!attributeFromLdap.equals(attributeToPersist)) {
if (isEmptyAttributeValues(attributeToPersist) && !ldapAttributeAnnotation.updateOnly()) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
}
}
} else if ((attributeFromLdap == null) && (attributeToPersist != null)) {
// Add entry attribute or change schema
if (isSchemaUpdate && (attributeToPersist.getValue() == null && Arrays.equals(attributeToPersist.getValues(), new Object[] {}))) {
continue;
}
AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
if (AttributeModificationType.ADD == modType) {
if (isEmptyAttributeValues(attributeToPersist)) {
if (forceUpdate) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else {
modType = forceUpdate ? AttributeModificationType.FORCE_UPDATE : modType;
attributeDataModifications.add(new AttributeDataModification(modType, attributeToPersist));
}
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else if ((attributeFromLdap != null) && (attributeToPersist == null)) {
// or updateOnly = true
if (!ldapAttributeAnnotation.ignoreDuringRead() && !ldapAttributeAnnotation.updateOnly()) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
}
} else if (forceUpdate && (attributeFromLdap == null) && (attributeToPersist == null)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, new AttributeData(ldapAttributeName, null)));
}
}
}
// Process properties with @AttributesList annotation
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
Annotation ldapAttribute;
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributesList.class);
if (ldapAttribute != null) {
Map<String, AttributeName> ldapAttributesConfiguration = new HashMap<String, AttributeName>();
for (AttributeName ldapAttributeConfiguration : ((AttributesList) ldapAttribute).attributesConfiguration()) {
ldapAttributesConfiguration.put(ldapAttributeConfiguration.name(), ldapAttributeConfiguration);
}
// Prepare attributes for removal
for (AttributeData attributeFromLdap : attributesFromLdapMap.values()) {
String attributeName = attributeFromLdap.getName();
if (OBJECT_CLASS.equalsIgnoreCase(attributeName)) {
continue;
}
AttributeName ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
continue;
}
if (!attributesToPersistMap.containsKey(attributeName.toLowerCase())) {
// Remove if attribute not marked as ignoreDuringRead = true
if ((ldapAttributeConfiguration == null) || ((ldapAttributeConfiguration != null) && !ldapAttributeConfiguration.ignoreDuringRead())) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
}
}
}
// Prepare attributes for adding and replace
for (AttributeData attributeToPersist : attributesToPersistMap.values()) {
String attributeName = attributeToPersist.getName();
AttributeName ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
continue;
}
AttributeData attributeFromLdap = attributesFromLdapMap.get(attributeName.toLowerCase());
if (attributeFromLdap == null) {
// Add entry attribute or change schema
AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
if (AttributeModificationType.ADD.equals(modType)) {
if (!isEmptyAttributeValues(attributeToPersist)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.ADD, attributeToPersist));
}
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else if ((attributeFromLdap != null) && isEmptyAttributeValues(attributeToPersist)) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
if (!attributeFromLdap.equals(attributeToPersist)) {
if (isEmptyAttributeValues(attributeToPersist) && (ldapAttributeConfiguration == null || !ldapAttributeConfiguration.updateOnly())) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
}
}
}
}
}
}
return attributeDataModifications;
}
use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.
the class CouchbaseEntryManager method findEntriesImpl.
protected <T> PagedResult<JsonObject> findEntriesImpl(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, BatchOperation<T> batchOperation, SearchReturnDataType returnDataType, int start, int count, int chunkSize) {
// Check entry class
checkEntryClass(entryClass, false);
String[] objectClasses = getTypeObjectClasses(entryClass);
List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
String[] currentLdapReturnAttributes = ldapReturnAttributes;
if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
currentLdapReturnAttributes = getAttributes(null, propertiesAnnotations, false);
}
Filter searchFilter;
if (objectClasses.length > 0) {
LOG.trace("Filter: {}", filter);
searchFilter = addObjectClassFilter(filter, objectClasses);
} else {
searchFilter = filter;
}
// Find entries
LOG.trace("-------------------------------------------------------");
LOG.trace("Filter: {}", filter);
LOG.trace("objectClasses count: {} ", objectClasses.length);
LOG.trace("objectClasses: {}", ArrayHelper.toString(objectClasses));
LOG.trace("Search filter: {}", searchFilter);
// Prepare default sort
Sort[] defaultSort = getDefaultSort(entryClass);
if (StringHelper.isNotEmpty(sortBy)) {
Sort requestedSort = buildSort(sortBy, sortOrder);
if (ArrayHelper.isEmpty(defaultSort)) {
defaultSort = new Sort[] { requestedSort };
} else {
defaultSort = ArrayHelper.arrayMerge(new Sort[] { requestedSort }, defaultSort);
}
}
// Prepare properties types to allow build filter properly
Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
ParsedKey keyWithInum = toCouchbaseKey(baseDN);
ConvertedExpression convertedExpression;
try {
convertedExpression = toCouchbaseFilter(searchFilter, propertiesAnnotationsMap);
} catch (SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to convert filter %s to expression", searchFilter));
}
PagedResult<JsonObject> searchResult = null;
try {
CouchbaseBatchOperationWraper<T> batchOperationWraper = null;
if (batchOperation != null) {
batchOperationWraper = new CouchbaseBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
}
searchResult = searchImpl(keyWithInum.getKey(), getScanConsistency(convertedExpression), convertedExpression.expression(), scope, currentLdapReturnAttributes, defaultSort, batchOperationWraper, returnDataType, start, count, chunkSize);
if (searchResult == null) {
throw new EntryPersistenceException(String.format("Failed to find entries with key: %s, expression: %s", keyWithInum.getKey(), convertedExpression));
}
return searchResult;
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entries with key: %s, expression: %s", keyWithInum.getKey(), convertedExpression), ex);
}
}
use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.
the class CouchbaseFilterConverter method determineMultiValuedByType.
private Boolean determineMultiValuedByType(String attributeName, Map<String, PropertyAnnotation> propertiesAnnotationsMap) {
if ((attributeName == null) || (propertiesAnnotationsMap == null)) {
return null;
}
if (StringHelper.equalsIgnoreCase(attributeName, CouchbaseEntryManager.OBJECT_CLASS)) {
return false;
}
PropertyAnnotation propertyAnnotation = propertiesAnnotationsMap.get(attributeName);
if ((propertyAnnotation == null) || (propertyAnnotation.getParameterType() == null)) {
return null;
}
Class<?> parameterType = propertyAnnotation.getParameterType();
boolean isMultiValued = parameterType.equals(String[].class) || ReflectHelper.assignableFrom(parameterType, List.class) || ReflectHelper.assignableFrom(parameterType, AttributeEnum[].class);
return isMultiValued;
}
Aggregations