Search in sources :

Example 21 with PropertyAnnotation

use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.

the class BaseEntryManager method getCustomObjectClasses.

protected String[] getCustomObjectClasses(Object entry, Class<?> entryClass) {
    List<String> result = new ArrayList<String>();
    List<PropertyAnnotation> customObjectAnnotations = getEntryCustomObjectClassAnnotations(entryClass);
    for (PropertyAnnotation propertiesAnnotation : customObjectAnnotations) {
        String propertyName = propertiesAnnotation.getPropertyName();
        Getter getter = getGetter(entryClass, propertyName);
        if (getter == null) {
            throw new MappingException("Entry should has getter for property " + propertyName);
        }
        Class<?> parameterType = getSetterPropertyType(entryClass, propertyName);
        boolean multiValued = isMultiValued(parameterType);
        AttributeData attribute = getAttributeData(propertyName, propertyName, getter, entry, multiValued, false);
        if (attribute != null) {
            for (String objectClass : attribute.getStringValues()) {
                if (objectClass != null) {
                    result.add(objectClass);
                }
            }
        }
        break;
    }
    return result.toArray(new String[0]);
}
Also used : Getter(io.jans.orm.reflect.property.Getter) ArrayList(java.util.ArrayList) AttributeData(io.jans.orm.model.AttributeData) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) MappingException(io.jans.orm.exception.MappingException)

Example 22 with PropertyAnnotation

use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.

the class BaseEntryManager method getExpirationValue.

protected <T> Integer getExpirationValue(Object entry, Class<T> entryClass, boolean merge) {
    // Check if entry has Expiration property
    PropertyAnnotation expirationProperty = getExpirationProperty(entryClass);
    if (expirationProperty == null) {
        return null;
    }
    String expirationPropertyName = expirationProperty.getPropertyName();
    Expiration expirationAnnotation = (Expiration) ReflectHelper.getAnnotationByType(expirationProperty.getAnnotations(), Expiration.class);
    if (merge && expirationAnnotation.ignoreDuringUpdate()) {
        return null;
    }
    if (expirationPropertyName == null) {
        // No entry expiration property
        return null;
    }
    // Get Expiration value
    Getter expirationGetter = getGetter(entryClass, expirationPropertyName);
    if (expirationGetter == null) {
        throw new MappingException("Entry should has getter for property " + expirationGetter);
    }
    Class<?> propertyType = expirationGetter.getReturnType();
    if (!((propertyType == Integer.class) || (propertyType == Integer.TYPE))) {
        throw new MappingException("Entry expiration property should has Integer type. Property: '" + expirationGetter + "'");
    }
    Object expirationValue = expirationGetter.get(entry);
    if (expirationValue == null) {
        // No entry expiration or null
        return null;
    }
    Integer resultExpirationValue;
    if (expirationValue instanceof Integer) {
        resultExpirationValue = (Integer) expirationValue;
    } else {
        resultExpirationValue = Integer.valueOf((int) expirationValue);
    }
    // TTL can't be negative
    if (resultExpirationValue < 0) {
        resultExpirationValue = 0;
    }
    return resultExpirationValue;
}
Also used : Getter(io.jans.orm.reflect.property.Getter) Expiration(io.jans.orm.annotation.Expiration) JsonObject(io.jans.orm.annotation.JsonObject) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) MappingException(io.jans.orm.exception.MappingException)

Example 23 with PropertyAnnotation

use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.

the class BaseEntryManager method getEntryKey.

private String getEntryKey(Object dnValue, boolean caseSensetive, List<PropertyAnnotation> propertiesAnnotations, List<AttributeData> attributesData) {
    StringBuilder sb = new StringBuilder("_HASH__").append((String.valueOf(dnValue)).toLowerCase()).append("__");
    List<String> processedProperties = new ArrayList<String>();
    Map<String, AttributeData> attributesDataMap = getAttributesDataMap(attributesData);
    for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
        Annotation ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributeName.class);
        if (ldapAttribute == null) {
            continue;
        }
        String ldapAttributeName = ((AttributeName) ldapAttribute).name();
        if (StringHelper.isEmpty(ldapAttributeName)) {
            ldapAttributeName = propertiesAnnotation.getPropertyName();
        }
        processedProperties.add(ldapAttributeName);
        String[] values = null;
        AttributeData attributeData = attributesDataMap.get(ldapAttributeName);
        if ((attributeData != null) && (attributeData.getValues() != null)) {
            values = attributeData.getStringValues();
            Arrays.sort(values);
        }
        addPropertyWithValuesToKey(sb, ldapAttributeName, values);
    }
    for (AttributeData attributeData : attributesData) {
        if (processedProperties.contains(attributeData.getName())) {
            continue;
        }
        addPropertyWithValuesToKey(sb, attributeData.getName(), attributeData.getStringValues());
    }
    if (caseSensetive) {
        return sb.toString();
    } else {
        return sb.toString().toLowerCase();
    }
}
Also used : 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 24 with PropertyAnnotation

use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.

the class SqlEntryManager method findEntriesImpl.

protected <T> PagedResult<EntryData> 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
    OrderSpecifier<?>[] defaultSort = getDefaultSort(entryClass);
    if (StringHelper.isNotEmpty(sortBy)) {
        OrderSpecifier<?> requestedSort = buildSort(sortBy, sortOrder);
        if (ArrayHelper.isEmpty(defaultSort)) {
            defaultSort = new OrderSpecifier[] { requestedSort };
        } else {
            defaultSort = ArrayHelper.arrayMerge(new OrderSpecifier[] { requestedSort }, defaultSort);
        }
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    ParsedKey keyWithInum = toSQLKey(baseDN);
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilter(searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
    }
    PagedResult<EntryData> searchResult = null;
    try {
        SqlBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new SqlBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        }
        searchResult = searchImpl(keyWithInum.getKey(), objectClasses[0], convertedExpression, 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 (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s'", keyWithInum.getKey()), ex);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression), ex);
    }
}
Also used : EntryData(io.jans.orm.model.EntryData) SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) DateTimeParseException(java.time.format.DateTimeParseException) AuthenticationException(io.jans.orm.exception.AuthenticationException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) OrderSpecifier(com.querydsl.core.types.OrderSpecifier)

Example 25 with PropertyAnnotation

use of io.jans.orm.reflect.property.PropertyAnnotation in project jans by JanssenProject.

the class LdapEntryManager method findEntriesVirtualListView.

@Deprecated
public <T> List<T> findEntriesVirtualListView(String baseDN, Class<T> entryClass, Filter filter, int start, int count, String sortBy, SortOrder sortOrder, PagedResult vlvResponse, String[] ldapReturnAttributes) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to find entries is null");
    }
    // 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);
    }
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchResult searchResult = null;
    try {
        searchResult = getOperationService().searchVirtualListView(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(SearchScope.SUB), start, count, sortBy, sortOrder, vlvResponse, currentLdapReturnAttributes);
        if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    if (searchResult.getEntryCount() == 0) {
        return new ArrayList<T>(0);
    }
    List<T> entries = createEntitiesVirtualListView(entryClass, propertiesAnnotations, searchResult.getSearchEntries().toArray(new SearchResultEntry[searchResult.getSearchEntries().size()]));
    return entries;
}
Also used : ArrayList(java.util.ArrayList) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchResult(com.unboundid.ldap.sdk.SearchResult) MappingException(io.jans.orm.exception.MappingException) ParseException(java.text.ParseException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

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