use of io.jans.orm.model.EntryData in project jans by JanssenProject.
the class SpannerOperationServiceImpl method searchImpl.
private <O> PagedResult<EntryData> searchImpl(TableMapping tableMapping, String key, ConvertedExpression expression, SearchScope scope, String[] attributes, Sort[] orderBy, SpannerBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
BatchOperation<O> batchOperation = null;
if (batchOperationWraper != null) {
batchOperation = (BatchOperation<O>) batchOperationWraper.getBatchOperation();
}
Table table = buildTable(tableMapping);
PlainSelect sqlSelectQuery = new PlainSelect();
sqlSelectQuery.setFromItem(table);
List<SelectItem> selectItems = buildSelectAttributes(tableMapping, key, attributes);
sqlSelectQuery.addSelectItems(selectItems);
if (expression != null) {
applyWhereExpression(sqlSelectQuery, expression);
}
if (orderBy != null) {
OrderByElement[] orderByElements = new OrderByElement[orderBy.length];
for (int i = 0; i < orderBy.length; i++) {
Column column = new Column(orderBy[i].getName());
orderByElements[i] = new OrderByElement();
orderByElements[i].setExpression(column);
if (orderBy[i].getSortOrder() != null) {
orderByElements[i].setAscDescPresent(true);
orderByElements[i].setAsc(SortOrder.ASCENDING == orderBy[i].getSortOrder());
}
}
sqlSelectQuery.withOrderByElements(Arrays.asList(orderByElements));
}
List<EntryData> searchResultList = new LinkedList<EntryData>();
if ((SearchReturnDataType.SEARCH == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
List<EntryData> lastResult = null;
if (pageSize > 0) {
boolean collectSearchResult;
Limit limit = new Limit();
sqlSelectQuery.setLimit(limit);
Offset offset = new Offset();
sqlSelectQuery.setOffset(offset);
int currentLimit;
try {
int resultCount = 0;
int lastCountRows = 0;
do {
collectSearchResult = true;
currentLimit = pageSize;
if (count > 0) {
currentLimit = Math.min(pageSize, count - resultCount);
}
// Change limit and offset
limit.setRowCount(new LongValue(currentLimit));
offset.setOffset(start + resultCount);
Statement.Builder statementBuilder = Statement.newBuilder(sqlSelectQuery.toString());
applyParametersBinding(statementBuilder, expression);
Statement statement = statementBuilder.build();
LOG.debug("Executing query: '{}'", statement);
try (ResultSet resultSet = databaseClient.singleUse().executeQuery(statement)) {
lastResult = getEntryDataList(tableMapping.getObjectClass(), resultSet);
}
lastCountRows = lastResult.size();
if (batchOperation != null) {
collectSearchResult = batchOperation.collectSearchResult(lastCountRows);
}
if (collectSearchResult) {
searchResultList.addAll(lastResult);
}
if (batchOperation != null) {
List<O> entries = batchOperationWraper.createEntities(lastResult);
batchOperation.performAction(entries);
}
resultCount += lastCountRows;
if ((count > 0) && (resultCount >= count)) {
break;
}
} while (lastCountRows > 0);
} catch (SpannerException | EntryConvertationException | IncompatibleTypeException ex) {
LOG.error("Failed to execute query with expression: '{}'", expression);
throw new SearchException(String.format("Failed to execute query '%s' with key: '%s'", sqlSelectQuery, key), ex);
}
} else {
try {
long currentLimit = count;
if (currentLimit <= 0) {
currentLimit = connectionProvider.getDefaultMaximumResultSize();
}
Limit limit = new Limit();
limit.setRowCount(new LongValue(currentLimit));
sqlSelectQuery.setLimit(limit);
if (start > 0) {
Offset offset = new Offset();
offset.setOffset(start);
sqlSelectQuery.setOffset(offset);
}
Statement.Builder statementBuilder = Statement.newBuilder(sqlSelectQuery.toString());
applyParametersBinding(statementBuilder, expression);
Statement statement = statementBuilder.build();
LOG.debug("Executing query: '{}'", statement);
try (ResultSet resultSet = databaseClient.singleUse().executeQuery(statement)) {
lastResult = getEntryDataList(tableMapping.getObjectClass(), resultSet);
searchResultList.addAll(lastResult);
}
} catch (SpannerException | EntryConvertationException | IncompatibleTypeException ex) {
LOG.error("Failed to execute query with expression: '{}'", expression);
throw new SearchException(String.format("Failed to execute query '%s' with key: '%s'", sqlSelectQuery, key), ex);
}
}
}
PagedResult<EntryData> result = new PagedResult<EntryData>();
result.setEntries(searchResultList);
result.setEntriesCount(searchResultList.size());
result.setStart(start);
if ((SearchReturnDataType.COUNT == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
PlainSelect sqlCountSelectQuery = new PlainSelect();
sqlCountSelectQuery.setFromItem(table);
Function countFunction = new Function();
countFunction.setName("COUNT");
countFunction.setAllColumns(true);
SelectExpressionItem selectCountItem = new SelectExpressionItem(countFunction);
selectCountItem.setAlias(new Alias("TOTAL", false));
sqlCountSelectQuery.addSelectItems(selectCountItem);
if (expression != null) {
applyWhereExpression(sqlCountSelectQuery, expression);
}
try {
Statement.Builder statementBuilder = Statement.newBuilder(sqlCountSelectQuery.toString());
applyParametersBinding(statementBuilder, expression);
Statement statement = statementBuilder.build();
LOG.debug("Calculating count. Executing query: '{}'", statement);
try (ResultSet countResult = databaseClient.singleUse().executeQuery(statement)) {
if (!countResult.next()) {
throw new SearchException(String.format("Failed to calculate count entries. Query: '%s'", statement));
}
result.setTotalEntriesCount((int) countResult.getLong("TOTAL"));
}
} catch (SpannerException | IncompatibleTypeException ex) {
LOG.error("Failed to execute query with expression: '{}'", expression);
throw new SearchException(String.format("Failed to build count search entries query. Key: '%s', expression: '%s'", key, expression.expression()), ex);
}
}
return result;
}
use of io.jans.orm.model.EntryData in project jans by JanssenProject.
the class SpannerEntryManager 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
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);
String key = toSQLKey(baseDN).getKey();
ConvertedExpression convertedExpression;
try {
convertedExpression = toSqlFilter(key, objectClasses[0], searchFilter, propertiesAnnotationsMap);
} catch (SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter), ex);
}
PagedResult<EntryData> searchResult = null;
try {
SpannerBatchOperationWraper<T> batchOperationWraper = null;
if (batchOperation != null) {
batchOperationWraper = new SpannerBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
}
searchResult = searchImpl(key, 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'", key, convertedExpression));
}
return searchResult;
} catch (SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s'", key), ex);
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", key, convertedExpression), ex);
}
}
use of io.jans.orm.model.EntryData in project jans by JanssenProject.
the class SpannerEntryManager 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;
}
// Prepare properties types to allow build filter properly
Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
String key = toSQLKey(baseDN).getKey();
ConvertedExpression convertedExpression;
try {
convertedExpression = toSqlFilter(key, objectClasses[0], searchFilter, propertiesAnnotationsMap);
} catch (SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
}
PagedResult<EntryData> searchResult = null;
try {
searchResult = searchImpl(key, objectClasses[0], convertedExpression, SearchScope.SUB, ldapReturnAttributes, null, null, SearchReturnDataType.SEARCH, 0, 1, 0);
if (searchResult == null) {
throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter));
}
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter), ex);
}
return (searchResult != null) && (searchResult.getEntriesCount() > 0);
}
use of io.jans.orm.model.EntryData in project jans by JanssenProject.
the class SpannerEntryManager 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 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;
}
// Prepare properties types to allow build filter properly
Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
String key = toSQLKey(baseDN).getKey();
ConvertedExpression convertedExpression;
try {
convertedExpression = toSqlFilter(key, objectClasses[0], searchFilter, propertiesAnnotationsMap);
} catch (SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
}
PagedResult<EntryData> searchResult;
try {
searchResult = searchImpl(toSQLKey(baseDN).getKey(), objectClasses[0], convertedExpression, scope, null, null, null, SearchReturnDataType.COUNT, 0, 0, 0);
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to calculate the number of entries with baseDN: '%s', filter: '%s'", baseDN, searchFilter), ex);
}
return searchResult.getTotalEntriesCount();
}
use of io.jans.orm.model.EntryData in project jans by JanssenProject.
the class SpannerEntryManager method createEntities.
protected <T> List<T> createEntities(String baseDN, Class<T> entryClass, PagedResult<EntryData> searchResult) {
ParsedKey keyWithInum = toSQLKey(baseDN);
List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
List<T> entries = createEntities(entryClass, propertiesAnnotations, keyWithInum, searchResult.getEntries().toArray(new EntryData[searchResult.getEntriesCount()]));
return entries;
}
Aggregations