Search in sources :

Example 31 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class SqlEntryManager method persist.

@Override
protected void persist(String dn, String[] objectClasses, List<AttributeData> attributes, Integer expiration) {
    ArrayList<AttributeData> resultAttributes = new ArrayList<>(attributes.size() + 1);
    for (AttributeData attribute : attributes) {
        String attributeName = attribute.getName();
        Object[] attributeValues = attribute.getValues();
        Boolean multiValued = attribute.getMultiValued();
        if (ArrayHelper.isNotEmpty(attributeValues) && (attributeValues[0] != null)) {
            Object[] realValues = attributeValues;
            // We need to store only one objectClass value in SQL
            if (StringHelper.equalsIgnoreCase(SqlOperationService.OBJECT_CLASS, attributeName)) {
                if (!ArrayHelper.isEmpty(realValues)) {
                    realValues = new Object[] { realValues[0] };
                    multiValued = false;
                }
            }
            // Process userPassword
            if (StringHelper.equalsIgnoreCase(SqlOperationService.USER_PASSWORD, attributeName)) {
                realValues = getOperationService().createStoragePassword(StringHelper.toStringArray(attributeValues));
            }
            escapeValues(realValues);
            AttributeData resultAttributeData;
            if (Boolean.TRUE.equals(multiValued)) {
                resultAttributeData = new AttributeData(toInternalAttribute(attributeName), realValues, multiValued);
            } else {
                resultAttributeData = new AttributeData(toInternalAttribute(attributeName), realValues[0]);
            }
            resultAttributes.add(resultAttributeData);
        }
    }
    // Persist entry
    try {
        ParsedKey parsedKey = toSQLKey(dn);
        resultAttributes.add(new AttributeData(SqlOperationService.DN, dn));
        resultAttributes.add(new AttributeData(SqlOperationService.DOC_ID, parsedKey.getKey()));
        boolean result = getOperationService().addEntry(parsedKey.getKey(), objectClasses[0], resultAttributes);
        if (!result) {
            throw new EntryPersistenceException(String.format("Failed to persist entry: '%s'", dn));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to persist entry: '%s'", dn), ex);
    }
}
Also used : ArrayList(java.util.ArrayList) ParsedKey(io.jans.orm.impl.model.ParsedKey) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) AttributeData(io.jans.orm.model.AttributeData) 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)

Example 32 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class SqlOperationServiceImpl method updateEntryImpl.

private boolean updateEntryImpl(TableMapping tableMapping, String key, List<AttributeDataModification> mods) throws PersistenceException {
    try {
        Map<String, String> columTypes = tableMapping.getColumTypes();
        RelationalPathBase<Object> tableRelationalPath = buildTableRelationalPath(tableMapping);
        SQLUpdateClause sqlUpdateQuery = this.sqlQueryFactory.update(tableRelationalPath);
        for (AttributeDataModification attributeMod : mods) {
            AttributeData attribute = attributeMod.getAttribute();
            Path path = Expressions.stringPath(attribute.getName());
            String attributeType = columTypes.get(attribute.getName().toLowerCase());
            boolean multiValued = (attributeType != null) && isJsonColumn(tableMapping.getTableName(), attributeType);
            AttributeModificationType type = attributeMod.getModificationType();
            if ((AttributeModificationType.ADD == type) || (AttributeModificationType.FORCE_UPDATE == type)) {
                if (multiValued || Boolean.TRUE.equals(attribute.getMultiValued())) {
                    sqlUpdateQuery.set(path, convertValueToDbJson(attribute.getValues()));
                } else {
                    sqlUpdateQuery.set(path, attribute.getValue());
                }
            } else if (AttributeModificationType.REPLACE == type) {
                if (multiValued || Boolean.TRUE.equals(attribute.getMultiValued())) {
                    sqlUpdateQuery.set(path, convertValueToDbJson(attribute.getValues()));
                } else {
                    sqlUpdateQuery.set(path, attribute.getValue());
                }
            } else if (AttributeModificationType.REMOVE == type) {
                sqlUpdateQuery.setNull(path);
            } else {
                throw new UnsupportedOperationException("Operation type '" + type + "' is not implemented");
            }
        }
        Predicate whereExp = ExpressionUtils.eq(Expressions.stringPath(SqlOperationService.DOC_ID), Expressions.constant(key));
        long rowInserted = sqlUpdateQuery.where(whereExp).execute();
        return rowInserted == 1;
    } catch (QueryException ex) {
        throw new PersistenceException("Failed to update entry", ex);
    }
}
Also used : Path(com.querydsl.core.types.Path) AttributeModificationType(io.jans.orm.model.AttributeDataModification.AttributeModificationType) Predicate(com.querydsl.core.types.Predicate) QueryException(com.querydsl.core.QueryException) AttributeDataModification(io.jans.orm.model.AttributeDataModification) SQLUpdateClause(com.querydsl.sql.dml.SQLUpdateClause) PersistenceException(io.jans.orm.exception.operation.PersistenceException) AttributeData(io.jans.orm.model.AttributeData)

Example 33 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class SqlOperationServiceImpl method lookupImpl.

private List<AttributeData> lookupImpl(TableMapping tableMapping, String key, String... attributes) throws SearchException, EntryConvertationException {
    try {
        RelationalPathBase<Object> tableRelationalPath = buildTableRelationalPath(tableMapping);
        Predicate whereExp = ExpressionUtils.eq(Expressions.stringPath(SqlOperationService.DOC_ID), Expressions.constant(key));
        Expression<?> attributesExp = buildSelectAttributes(attributes);
        SQLQuery<?> sqlSelectQuery = sqlQueryFactory.select(attributesExp).from(tableRelationalPath).where(whereExp).limit(1);
        try (ResultSet resultSet = sqlSelectQuery.getResults()) {
            List<AttributeData> result = getAttributeDataList(tableMapping, resultSet, true);
            if (result != null) {
                return result;
            }
        }
    } catch (SQLException | QueryException ex) {
        throw new SearchException(String.format("Failed to lookup query by key: '%s'", key), ex);
    }
    throw new SearchException(String.format("Failed to lookup entry by key: '%s'", key));
}
Also used : QueryException(com.querydsl.core.QueryException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) SearchException(io.jans.orm.exception.operation.SearchException) AttributeData(io.jans.orm.model.AttributeData) Predicate(com.querydsl.core.types.Predicate)

Example 34 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class LdapEntryManager method createEntitiesVirtualListView.

@Deprecated
private <T> List<T> createEntitiesVirtualListView(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, SearchResultEntry... searchResultEntries) {
    List<T> result = new LinkedList<T>();
    Map<String, List<AttributeData>> entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
    int count = 0;
    for (int i = 0; i < searchResultEntries.length; i++) {
        count++;
        SearchResultEntry entry = searchResultEntries[i];
        LinkedList<AttributeData> attributeDataLinkedList = new LinkedList<AttributeData>();
        attributeDataLinkedList.addAll(getAttributeDataList(entry));
        entriesAttributes.put(entry.getDN(), attributeDataLinkedList);
        // Remove reference to allow java clean up object
        searchResultEntries[i] = null;
        // Allow java to clean up temporary objects
        if (count >= 100) {
            List<T> currentResult = new LinkedList<T>();
            currentResult.addAll(createEntities(entryClass, propertiesAnnotations, entriesAttributes, false));
            result.addAll(currentResult);
            entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
            count = 0;
        }
    }
    List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes, false);
    result.addAll(currentResult);
    return result;
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) AttributeData(io.jans.orm.model.AttributeData) LinkedHashMap(java.util.LinkedHashMap) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 35 with AttributeData

use of io.jans.orm.model.AttributeData in project jans by JanssenProject.

the class LdapEntryManager method exportEntry.

@Override
public List<AttributeData> exportEntry(String dn) {
    try {
        SearchResultEntry searchResultEntry = getOperationService().lookup(dn, (String[]) null);
        List<AttributeData> result = getAttributeDataList(searchResultEntry);
        if (result != null) {
            return result;
        }
        return null;
    } catch (ConnectionException | SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn), ex);
    }
}
Also used : SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) AttributeData(io.jans.orm.model.AttributeData) ConnectionException(io.jans.orm.exception.operation.ConnectionException) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

AttributeData (io.jans.orm.model.AttributeData)62 MappingException (io.jans.orm.exception.MappingException)29 ArrayList (java.util.ArrayList)23 SearchException (io.jans.orm.exception.operation.SearchException)22 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)17 AuthenticationException (io.jans.orm.exception.AuthenticationException)15 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)15 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)14 AttributeDataModification (io.jans.orm.model.AttributeDataModification)13 JsonObject (io.jans.orm.annotation.JsonObject)10 LinkedList (java.util.LinkedList)10 DateTimeParseException (java.time.format.DateTimeParseException)8 List (java.util.List)8 ParsedKey (io.jans.orm.impl.model.ParsedKey)7 JsonObject (com.couchbase.client.java.document.json.JsonObject)6 AttributeName (io.jans.orm.annotation.AttributeName)6 PersistenceException (io.jans.orm.exception.operation.PersistenceException)6 Annotation (java.lang.annotation.Annotation)6 AttributeModificationType (io.jans.orm.model.AttributeDataModification.AttributeModificationType)5 LinkedHashMap (java.util.LinkedHashMap)5