Search in sources :

Example 26 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class LdapEntryManager method countEntries.

@Override
public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to count entries is null");
    }
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchScope searchScope = scope;
    if (searchScope == null) {
        searchScope = SearchScope.SUB;
    }
    String[] ldapReturnAttributes;
    CountBatchOperation<T> batchOperation;
    if (SearchScope.BASE == searchScope) {
        // Don't load attributes
        ldapReturnAttributes = new String[] { "numsubordinates" };
        batchOperation = null;
    } else {
        // Don't load attributes
        ldapReturnAttributes = new String[] { "" };
        batchOperation = new CountBatchOperation<T>();
    }
    SearchResult searchResult;
    try {
        LdapBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new LdapBatchOperationWraper<T>(batchOperation);
        }
        searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(searchScope), batchOperationWraper, 0, 100, 0, null, ldapReturnAttributes);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to calculate the number of entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    if (SearchScope.BASE != searchScope) {
        return batchOperation.getCountEntries();
    }
    if (searchResult.getEntryCount() != 1) {
        throw new EntryPersistenceException(String.format("Failed to calculate the number of entries due to missing result entry with baseDN: %s, filter: %s", baseDN, searchFilter));
    }
    Long result = searchResult.getSearchEntries().get(0).getAttributeValueAsLong("numsubordinates");
    if (result == null) {
        throw new EntryPersistenceException(String.format("Failed to calculate the number of entries due to missing attribute 'numsubordinates' with baseDN: %s, filter: %s", baseDN, searchFilter));
    }
    return result.intValue();
}
Also used : Filter(io.jans.orm.search.filter.Filter) SearchScope(io.jans.orm.model.SearchScope) 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)

Example 27 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class LdapEntryManager method remove.

@Override
public <T> int remove(String baseDN, Class<T> entryClass, Filter filter, int count) {
    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);
    // Find entries
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    DeleteBatchOperation batchOperation = new DeleteBatchOperation<T>(this);
    SearchResult searchResult = null;
    try {
        LdapBatchOperationWraper<T> batchOperationWraper = new LdapBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(SearchScope.SUB), batchOperationWraper, 0, 100, count, null, LdapOperationService.DN);
    } catch (Exception ex) {
        throw new EntryDeleteException(String.format("Failed to delete entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    if (!ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
        throw new EntryDeleteException(String.format("Failed to delete entries with baseDN: %s, filter: %s", baseDN, searchFilter));
    }
    return batchOperation.getCountEntries();
}
Also used : 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) EntryDeleteException(io.jans.orm.exception.EntryDeleteException)

Example 28 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class LdapEntryManager method contains.

@Override
protected <T> boolean contains(String baseDN, String[] objectClasses, Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Filter filter, String[] ldapReturnAttributes) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to check contain entries is null");
    }
    // Create filter
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    SearchScope scope = SearchScope.SUB;
    SearchResult searchResult = null;
    try {
        searchResult = getOperationService().search(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(scope), null, 0, 1, 1, null, ldapReturnAttributes);
        if ((searchResult == null) || !ResultCode.SUCCESS.equals(searchResult.getResultCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: %s, filter: %s", baseDN, searchFilter));
        }
    } catch (SearchScopeException ex) {
        throw new AuthenticationException(String.format("Failed to convert scope: %s", scope), ex);
    } catch (SearchException ex) {
        if (!(ResultCode.NO_SUCH_OBJECT_INT_VALUE == ex.getErrorCode())) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
        }
    }
    return (searchResult != null) && (searchResult.getEntryCount() > 0);
}
Also used : Filter(io.jans.orm.search.filter.Filter) AuthenticationException(io.jans.orm.exception.AuthenticationException) SearchScope(io.jans.orm.model.SearchScope) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchException(io.jans.orm.exception.operation.SearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) MappingException(io.jans.orm.exception.MappingException) SearchScopeException(io.jans.orm.exception.operation.SearchScopeException)

Example 29 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

the class LdapEntryManager method findPagedEntries.

@Override
public <T> PagedResult<T> findPagedEntries(String baseDN, Class<T> entryClass, Filter filter, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, int start, int count, int chunkSize) {
    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;
    }
    List<SearchResultEntry> searchResultEntries;
    PagedResult<T> vlvResponse = new PagedResult<T>();
    try {
        searchResultEntries = getOperationService().searchSearchResultEntryList(baseDN, toLdapFilter(searchFilter), toLdapSearchScope(SearchScope.SUB), start, count, chunkSize, sortBy, sortOrder, vlvResponse, currentLdapReturnAttributes);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
    }
    List<T> entries = new ArrayList<T>(0);
    if (searchResultEntries.size() > 0) {
        entries = createEntitiesVirtualListView(entryClass, propertiesAnnotations, searchResultEntries.toArray(new SearchResultEntry[] {}));
    }
    vlvResponse.setEntries(entries);
    return vlvResponse;
}
Also used : ArrayList(java.util.ArrayList) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) 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) PagedResult(io.jans.orm.model.PagedResult) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 30 with MappingException

use of io.jans.orm.exception.MappingException in project jans by JanssenProject.

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;
}
Also used : HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) AttributesList(io.jans.orm.annotation.AttributesList) List(java.util.List) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) InvalidArgumentException(io.jans.orm.exception.InvalidArgumentException) MappingException(io.jans.orm.exception.MappingException)

Aggregations

MappingException (io.jans.orm.exception.MappingException)43 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)27 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)19 SearchException (io.jans.orm.exception.operation.SearchException)18 AuthenticationException (io.jans.orm.exception.AuthenticationException)16 Filter (io.jans.orm.search.filter.Filter)16 AttributeData (io.jans.orm.model.AttributeData)15 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)14 JsonObject (io.jans.orm.annotation.JsonObject)12 ArrayList (java.util.ArrayList)11 SearchScopeException (io.jans.orm.exception.operation.SearchScopeException)7 Annotation (java.lang.annotation.Annotation)7 SearchResult (com.unboundid.ldap.sdk.SearchResult)6 ConnectionException (io.jans.orm.exception.operation.ConnectionException)6 EntryData (io.jans.orm.model.EntryData)6 DateTimeParseException (java.time.format.DateTimeParseException)6 List (java.util.List)6 Getter (io.jans.orm.reflect.property.Getter)5 ParseException (java.text.ParseException)5 DataEntry (io.jans.orm.annotation.DataEntry)4