Search in sources :

Example 1 with EntryConvertationException

use of io.jans.orm.exception.operation.EntryConvertationException in project jans by JanssenProject.

the class SpannerOperationServiceImpl method authenticateImpl.

private boolean authenticateImpl(String key, String password, String objectClass) throws SearchException {
    Instant startTime = OperationDurationUtil.instance().now();
    boolean result = false;
    if (password != null) {
        try {
            List<AttributeData> attributes = lookup(key, objectClass, USER_PASSWORD);
            Object userPasswordObj = null;
            for (AttributeData attribute : attributes) {
                if (StringHelper.equalsIgnoreCase(attribute.getName(), USER_PASSWORD)) {
                    userPasswordObj = attribute.getValue();
                }
            }
            String userPassword = null;
            if (userPasswordObj instanceof String) {
                userPassword = (String) userPasswordObj;
            }
            if (userPassword != null) {
                if (persistenceExtension == null) {
                    result = PasswordEncryptionHelper.compareCredentials(password, userPassword);
                } else {
                    result = persistenceExtension.compareHashedPasswords(password, userPassword);
                }
            }
        } catch (EntryConvertationException ex) {
            throw new SearchException(String.format("Failed to get '%s' attribute", USER_PASSWORD), ex);
        }
    }
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    OperationDurationUtil.instance().logDebug("Spanner operation: bind, duration: {}, table: {}, key: {}", duration, tableMapping.getTableName(), key);
    return result;
}
Also used : Instant(java.time.Instant) SearchException(io.jans.orm.exception.operation.SearchException) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) Duration(java.time.Duration) AttributeData(io.jans.orm.model.AttributeData)

Example 2 with EntryConvertationException

use of io.jans.orm.exception.operation.EntryConvertationException 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;
}
Also used : EntryData(io.jans.orm.model.EntryData) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SearchException(io.jans.orm.exception.operation.SearchException) Function(net.sf.jsqlparser.expression.Function) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) ResultSet(com.google.cloud.spanner.ResultSet) IncompatibleTypeException(io.jans.orm.exception.operation.IncompatibleTypeException) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) PagedResult(io.jans.orm.model.PagedResult) Table(net.sf.jsqlparser.schema.Table) Statement(com.google.cloud.spanner.Statement) LinkedList(java.util.LinkedList) Offset(net.sf.jsqlparser.statement.select.Offset) Alias(net.sf.jsqlparser.expression.Alias) LongValue(net.sf.jsqlparser.expression.LongValue) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) Limit(net.sf.jsqlparser.statement.select.Limit) SpannerException(com.google.cloud.spanner.SpannerException) Builder(com.google.cloud.spanner.Statement.Builder)

Example 3 with EntryConvertationException

use of io.jans.orm.exception.operation.EntryConvertationException in project jans by JanssenProject.

the class SpannerOperationServiceImpl method removeMutationBuilderValue.

private void removeMutationBuilderValue(WriteBuilder mutation, AttributeData attribute, StructField attributeType) throws EntryConvertationException {
    ValueBinder<WriteBuilder> valueBinder = mutation.set(attributeType.getName());
    Code typeCode = attributeType.getType().getCode();
    if (Code.BOOL == typeCode) {
        valueBinder.to((Boolean) null);
    } else if (Code.DATE == typeCode) {
        valueBinder.to((com.google.cloud.Date) null);
    } else if (Code.TIMESTAMP == typeCode) {
        valueBinder.to((com.google.cloud.Timestamp) null);
    } else if (Code.INT64 == typeCode) {
        valueBinder.to((Long) null);
    } else if (Code.NUMERIC == typeCode) {
        valueBinder.to((BigDecimal) null);
    } else if (Code.STRING == typeCode) {
        valueBinder.to((String) null);
    } else if (Code.ARRAY == typeCode) {
        Code arrayCode = attributeType.getType().getArrayElementType().getCode();
        if (Code.BOOL == arrayCode) {
            valueBinder.toBoolArray((boolean[]) null);
        } else if (Code.DATE == arrayCode) {
            valueBinder.toDateArray((List<com.google.cloud.Date>) null);
        } else if (Code.TIMESTAMP == arrayCode) {
            valueBinder.toTimestampArray((List<com.google.cloud.Timestamp>) null);
        } else if (Code.INT64 == arrayCode) {
            valueBinder.toInt64Array((long[]) null);
        } else if (Code.NUMERIC == arrayCode) {
            valueBinder.toNumericArray((List<BigDecimal>) null);
        } else if (Code.STRING == arrayCode) {
            valueBinder.toStringArray((List<String>) null);
        }
    } else {
        throw new EntryConvertationException(String.format("Array column with name '%s' does not contain supported type '%s'", attribute.getName(), attributeType));
    }
}
Also used : Code(com.google.cloud.spanner.Type.Code) Date(java.util.Date) BigDecimal(java.math.BigDecimal) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) List(java.util.List) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) LinkedList(java.util.LinkedList) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException)

Example 4 with EntryConvertationException

use of io.jans.orm.exception.operation.EntryConvertationException in project jans by JanssenProject.

the class SqlOperationServiceImpl method searchImpl.

private <O> PagedResult<EntryData> searchImpl(TableMapping tableMapping, String key, ConvertedExpression expression, SearchScope scope, String[] attributes, OrderSpecifier<?>[] orderBy, SqlBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
    BatchOperation<O> batchOperation = null;
    if (batchOperationWraper != null) {
        batchOperation = (BatchOperation<O>) batchOperationWraper.getBatchOperation();
    }
    RelationalPathBase<Object> tableRelationalPath = buildTableRelationalPath(tableMapping);
    Expression<?> attributesExp = buildSelectAttributes(attributes);
    SQLQuery<?> sqlSelectQuery;
    if (expression == null) {
        sqlSelectQuery = sqlQueryFactory.select(attributesExp).from(tableRelationalPath);
    } else {
        Predicate whereExp = (Predicate) expression.expression();
        sqlSelectQuery = sqlQueryFactory.select(attributesExp).from(tableRelationalPath).where(whereExp);
    }
    SQLQuery<?> baseQuery = sqlSelectQuery;
    if (orderBy != null) {
        baseQuery = sqlSelectQuery.orderBy(orderBy);
    }
    List<EntryData> searchResultList = new LinkedList<EntryData>();
    String queryStr = null;
    if ((SearchReturnDataType.SEARCH == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
        List<EntryData> lastResult = null;
        if (pageSize > 0) {
            boolean collectSearchResult;
            SQLQuery<?> query;
            int currentLimit;
            try {
                int resultCount = 0;
                int lastCountRows = 0;
                do {
                    collectSearchResult = true;
                    currentLimit = pageSize;
                    if (count > 0) {
                        currentLimit = Math.min(pageSize, count - resultCount);
                    }
                    query = baseQuery.limit(currentLimit).offset(start + resultCount);
                    queryStr = query.getSQL().getSQL();
                    LOG.debug("Executing query: '" + queryStr + "'");
                    try (ResultSet resultSet = query.getResults()) {
                        lastResult = getEntryDataList(tableMapping, 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)) || (lastCountRows < currentLimit)) {
                        break;
                    }
                } while (lastCountRows > 0);
            } catch (QueryException ex) {
                throw new SearchException(String.format("Failed to build search entries query. Key: '%s', expression: '%s'", key, expression.expression()), ex);
            } catch (SQLException | EntryConvertationException ex) {
                throw new SearchException(String.format("Failed to execute query '%s'  with key: '%s'", queryStr, key), ex);
            }
        } else {
            try {
                SQLQuery<?> query = baseQuery;
                if (count > 0) {
                    query = query.limit(count);
                }
                if (start > 0) {
                    query = query.offset(start);
                }
                queryStr = query.getSQL().getSQL();
                LOG.debug("Execution query: '" + queryStr + "'");
                try (ResultSet resultSet = query.getResults()) {
                    lastResult = getEntryDataList(tableMapping, resultSet);
                    searchResultList.addAll(lastResult);
                }
            } catch (QueryException ex) {
                String sqlExpression = queryStr;
                if (StringHelper.isNotEmpty(sqlExpression)) {
                    sqlExpression = expression.expression().toString();
                }
                throw new SearchException(String.format("Failed to build search entries query. Key: '%s', expression: '%s'", key, sqlExpression), ex);
            } catch (SQLException | EntryConvertationException ex) {
                throw new SearchException("Failed to search entries. Query: '" + queryStr + "'", 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)) {
        SQLQuery<?> sqlCountSelectQuery;
        if (expression == null) {
            sqlCountSelectQuery = sqlQueryFactory.select(Expressions.as(ExpressionUtils.count(Wildcard.all), "TOTAL")).from(tableRelationalPath);
        } else {
            Predicate whereExp = (Predicate) expression.expression();
            sqlCountSelectQuery = sqlQueryFactory.select(Expressions.as(ExpressionUtils.count(Wildcard.all), "TOTAL")).from(tableRelationalPath).where(whereExp);
        }
        try {
            queryStr = sqlCountSelectQuery.getSQL().getSQL();
            LOG.debug("Calculating count. Execution query: '" + queryStr + "'");
            try (ResultSet countResult = sqlCountSelectQuery.getResults()) {
                if (!countResult.next()) {
                    throw new SearchException("Failed to calculate count entries. Query: '" + queryStr + "'");
                }
                result.setTotalEntriesCount(countResult.getInt("TOTAL"));
            }
        } catch (QueryException ex) {
            throw new SearchException(String.format("Failed to build count search entries query. Key: '%s', expression: '%s'", key, expression.expression()), ex);
        } catch (SQLException ex) {
            throw new SearchException("Failed to calculate count entries. Query: '" + queryStr + "'", ex);
        }
    }
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) SQLException(java.sql.SQLException) SearchException(io.jans.orm.exception.operation.SearchException) LinkedList(java.util.LinkedList) Predicate(com.querydsl.core.types.Predicate) QueryException(com.querydsl.core.QueryException) ResultSet(java.sql.ResultSet) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) PagedResult(io.jans.orm.model.PagedResult)

Example 5 with EntryConvertationException

use of io.jans.orm.exception.operation.EntryConvertationException in project jans by JanssenProject.

the class SpannerOperationServiceImpl method getAttributeDataList.

private List<AttributeData> getAttributeDataList(String objectClass, ResultSet resultSet, boolean skipDn) throws EntryConvertationException {
    try {
        if ((resultSet == null)) {
            return null;
        }
        if (!resultSet.next()) {
            return null;
        }
        List<AttributeData> result = new ArrayList<AttributeData>();
        // TODO: Include child table columns
        Set<String> nullableColumns = connectionProvider.getTableNullableColumns(objectClass);
        List<StructField> structFields = resultSet.getType().getStructFields();
        int columnsCount = resultSet.getColumnCount();
        for (int i = 0; i < columnsCount; i++) {
            StructField structField = structFields.get(i);
            String attributeName = structField.getName();
            Code columnTypeCode = structField.getType().getCode();
            boolean isNullable = nullableColumns.contains(attributeName.toLowerCase());
            if (SpannerOperationService.DOC_ID.equalsIgnoreCase(attributeName) || SpannerOperationService.ID.equalsIgnoreCase(attributeName)) {
                // Skip internal attributes
                continue;
            }
            if (skipDn && SpannerOperationService.DN.equalsIgnoreCase(attributeName)) {
                // Skip DN attribute
                continue;
            }
            Boolean multiValued = Boolean.FALSE;
            Object[] attributeValueObjects;
            if (resultSet.isNull(i)) {
                attributeValueObjects = NO_OBJECTS;
                if (isNullable) {
                    // Ignore columns with default NULL values
                    continue;
                }
            } else {
                if (Code.ARRAY == columnTypeCode) {
                    attributeValueObjects = convertDbArrayToValue(resultSet, structField.getType().getArrayElementType(), i, attributeName);
                    multiValued = Boolean.TRUE;
                } else if (Code.BOOL == columnTypeCode) {
                    attributeValueObjects = new Object[] { resultSet.getBoolean(i) };
                } else if (Code.DATE == columnTypeCode) {
                    attributeValueObjects = new Object[] { com.google.cloud.Date.toJavaUtilDate(resultSet.getDate(i)) };
                } else if (Code.TIMESTAMP == columnTypeCode) {
                    attributeValueObjects = new Object[] { resultSet.getTimestamp(i).toDate() };
                } else if (Code.INT64 == columnTypeCode) {
                    attributeValueObjects = new Object[] { resultSet.getLong(i) };
                } else if (Code.NUMERIC == columnTypeCode) {
                    attributeValueObjects = new Object[] { resultSet.getBigDecimal(i).longValue() };
                } else if (Code.STRING == columnTypeCode) {
                    Object value = resultSet.getString(i);
                    try {
                        value = com.google.cloud.Timestamp.parseTimestamp(value.toString());
                    } catch (Exception ex) {
                    }
                    attributeValueObjects = new Object[] { value };
                } else {
                    throw new EntryConvertationException(String.format("Column with name '%s' does not contain unsupported type '%s'", attributeName, columnTypeCode));
                }
            }
            unescapeValues(attributeValueObjects);
            AttributeData tmpAttribute = new AttributeData(attributeName, attributeValueObjects, multiValued);
            if (multiValued != null) {
                tmpAttribute.setMultiValued(multiValued);
            }
            result.add(tmpAttribute);
        }
        return result;
    } catch (SpannerException ex) {
        throw new EntryConvertationException("Failed to convert entry!", ex);
    }
}
Also used : ArrayList(java.util.ArrayList) Code(com.google.cloud.spanner.Type.Code) DeleteException(io.jans.orm.exception.operation.DeleteException) IncompatibleTypeException(io.jans.orm.exception.operation.IncompatibleTypeException) PersistenceException(io.jans.orm.exception.operation.PersistenceException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) DuplicateEntryException(io.jans.orm.exception.operation.DuplicateEntryException) SpannerException(com.google.cloud.spanner.SpannerException) SearchException(io.jans.orm.exception.operation.SearchException) EntryNotFoundException(io.jans.orm.exception.operation.EntryNotFoundException) ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) SpannerException(com.google.cloud.spanner.SpannerException) AttributeData(io.jans.orm.model.AttributeData)

Aggregations

EntryConvertationException (io.jans.orm.exception.operation.EntryConvertationException)7 SearchException (io.jans.orm.exception.operation.SearchException)6 AttributeData (io.jans.orm.model.AttributeData)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 SpannerException (com.google.cloud.spanner.SpannerException)2 Code (com.google.cloud.spanner.Type.Code)2 QueryException (com.querydsl.core.QueryException)2 DeleteException (io.jans.orm.exception.operation.DeleteException)2 DuplicateEntryException (io.jans.orm.exception.operation.DuplicateEntryException)2 EntryNotFoundException (io.jans.orm.exception.operation.EntryNotFoundException)2 IncompatibleTypeException (io.jans.orm.exception.operation.IncompatibleTypeException)2 PersistenceException (io.jans.orm.exception.operation.PersistenceException)2 EntryData (io.jans.orm.model.EntryData)2 PagedResult (io.jans.orm.model.PagedResult)2 SQLException (java.sql.SQLException)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)1 ResultSet (com.google.cloud.spanner.ResultSet)1