Search in sources :

Example 31 with PropertyAnnotation

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

Example 32 with PropertyAnnotation

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;
}
Also used : AttributesList(io.jans.orm.annotation.AttributesList) ArrayList(java.util.ArrayList) AttributeName(io.jans.orm.annotation.AttributeName) AttributeData(io.jans.orm.model.AttributeData) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Example 33 with PropertyAnnotation

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;
}
Also used : AttributeDataModification(io.jans.orm.model.AttributeDataModification) AttributeModificationType(io.jans.orm.model.AttributeDataModification.AttributeModificationType) AttributesList(io.jans.orm.annotation.AttributesList) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) AttributeName(io.jans.orm.annotation.AttributeName) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Annotation(java.lang.annotation.Annotation) AttributeData(io.jans.orm.model.AttributeData) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Example 34 with PropertyAnnotation

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);
    }
}
Also used : SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) JsonObject(com.couchbase.client.java.document.json.JsonObject) MappingException(io.jans.orm.exception.MappingException) DateTimeException(java.time.DateTimeException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) DateTimeParseException(java.time.format.DateTimeParseException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) ISO_INSTANT(java.time.format.DateTimeFormatter.ISO_INSTANT) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.couchbase.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) Sort(com.couchbase.client.java.query.dsl.Sort)

Example 35 with PropertyAnnotation

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;
}
Also used : PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Aggregations

PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)42 MappingException (io.jans.orm.exception.MappingException)28 Filter (io.jans.orm.search.filter.Filter)19 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)18 AuthenticationException (io.jans.orm.exception.AuthenticationException)17 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)17 SearchException (io.jans.orm.exception.operation.SearchException)17 AttributeData (io.jans.orm.model.AttributeData)14 ParsedKey (io.jans.orm.impl.model.ParsedKey)9 DateTimeParseException (java.time.format.DateTimeParseException)9 ArrayList (java.util.ArrayList)9 AttributeName (io.jans.orm.annotation.AttributeName)8 JsonObject (io.jans.orm.annotation.JsonObject)8 EntryData (io.jans.orm.model.EntryData)8 Annotation (java.lang.annotation.Annotation)6 JsonObject (com.couchbase.client.java.document.json.JsonObject)5 AttributesList (io.jans.orm.annotation.AttributesList)5 ConvertedExpression (io.jans.orm.couchbase.model.ConvertedExpression)5 DateTimeException (java.time.DateTimeException)5 ConvertedExpression (io.jans.orm.cloud.spanner.model.ConvertedExpression)4