Search in sources :

Example 6 with AttributeData

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

the class SpannerEntryManager 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(SpannerOperationService.OBJECT_CLASS, attributeName)) {
                if (!ArrayHelper.isEmpty(realValues)) {
                    realValues = new Object[] { realValues[0] };
                    multiValued = false;
                }
            }
            // Process userPassword
            if (StringHelper.equalsIgnoreCase(SpannerOperationService.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(SpannerOperationService.DN, dn));
        resultAttributes.add(new AttributeData(SpannerOperationService.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) AuthenticationException(io.jans.orm.exception.AuthenticationException)

Example 7 with AttributeData

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

the class SpannerEntryManager method exportEntry.

@Override
public List<AttributeData> exportEntry(String dn, String objectClass) {
    if (StringHelper.isEmpty(objectClass)) {
        throw new MappingException("Object class isn't defined!");
    }
    try {
        // Load entry
        ParsedKey keyWithInum = toSQLKey(dn);
        List<AttributeData> entry = getOperationService().lookup(keyWithInum.getKey(), objectClass);
        if (entry != null) {
            return entry;
        }
        return null;
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entry: '%s'", dn), ex);
    }
}
Also used : 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) AuthenticationException(io.jans.orm.exception.AuthenticationException) MappingException(io.jans.orm.exception.MappingException)

Example 8 with AttributeData

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

the class SpannerOperationServiceImpl method addEntryImpl.

private boolean addEntryImpl(TableMapping tableMapping, String key, Collection<AttributeData> attributes) throws PersistenceException {
    try {
        MessageDigest messageDigest = getMessageDigestInstance();
        Map<String, StructField> columTypes = tableMapping.getColumTypes();
        WriteBuilder mutationBuilder = Mutation.newInsertOrUpdateBuilder(tableMapping.getTableName());
        List<Mutation> mutations = new LinkedList<>();
        for (AttributeData attribute : attributes) {
            String attributeName = attribute.getName();
            StructField attributeType = columTypes.get(attributeName.toLowerCase());
            // If column not inside table we should check if there is child table
            if (attributeType == null) {
                TableMapping childTableMapping = connectionProvider.getChildTableMappingByKey(key, tableMapping, attributeName);
                if (childTableMapping == null) {
                    throw new PersistenceException(String.format("Failed to add entry. Column '%s' is undefined", attributeName));
                }
                Map<String, StructField> childColumTypes = childTableMapping.getColumTypes();
                if (childColumTypes == null) {
                    throw new PersistenceException(String.format("Failed to add entry. Column '%s' is undefined", attributeName));
                }
                StructField childAttributeType = childColumTypes.get(attributeName.toLowerCase());
                // Build Mutation for child table
                for (Object value : attribute.getValues()) {
                    // Build Mutation for child table
                    String dictDocId = getStringUniqueKey(messageDigest, value);
                    WriteBuilder childMutationBuilder = Mutation.newInsertOrUpdateBuilder(childTableMapping.getTableName());
                    childMutationBuilder.set(SpannerOperationService.DOC_ID).to(key).set(SpannerOperationService.DICT_DOC_ID).to(dictDocId);
                    setMutationBuilderValue(childMutationBuilder, childAttributeType, value);
                    mutations.add(childMutationBuilder.build());
                }
            } else {
                setMutationBuilderValue(mutationBuilder, attributeType, attribute.getValues());
            }
        }
        mutations.add(0, mutationBuilder.build());
        databaseClient.write(mutations);
        return true;
    } catch (SpannerException | IllegalStateException ex) {
        throw new PersistenceException("Failed to add entry", ex);
    }
}
Also used : TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) LinkedList(java.util.LinkedList) ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) PersistenceException(io.jans.orm.exception.operation.PersistenceException) Mutation(com.google.cloud.spanner.Mutation) SpannerException(com.google.cloud.spanner.SpannerException) MessageDigest(java.security.MessageDigest) AttributeData(io.jans.orm.model.AttributeData)

Example 9 with AttributeData

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

the class CouchbaseEntryManager method merge.

@Override
public void merge(String dn, String[] objectClasses, List<AttributeDataModification> attributeDataModifications, Integer expirationValue) {
    // Update entry
    try {
        List<MutationSpec> modifications = new ArrayList<MutationSpec>(attributeDataModifications.size());
        for (AttributeDataModification attributeDataModification : attributeDataModifications) {
            AttributeData attribute = attributeDataModification.getAttribute();
            AttributeData oldAttribute = attributeDataModification.getOldAttribute();
            String attributeName = null;
            Object[] attributeValues = null;
            Boolean multiValued = null;
            if (attribute != null) {
                attributeName = attribute.getName();
                attributeValues = attribute.getValues();
                multiValued = attribute.getMultiValued();
            }
            String oldAttributeName = null;
            Object[] oldAttributeValues = null;
            if (oldAttribute != null) {
                oldAttributeName = oldAttribute.getName();
                oldAttributeValues = oldAttribute.getValues();
            }
            MutationSpec modification = null;
            if (AttributeModificationType.ADD.equals(attributeDataModification.getModificationType())) {
                modification = createModification(Mutation.DICT_ADD, toInternalAttribute(attributeName), multiValued, attributeValues);
            } else {
                if (AttributeModificationType.REMOVE.equals(attributeDataModification.getModificationType())) {
                    modification = createModification(Mutation.DELETE, toInternalAttribute(oldAttributeName), multiValued, oldAttributeValues);
                } else if (AttributeModificationType.REPLACE.equals(attributeDataModification.getModificationType())) {
                    modification = createModification(Mutation.REPLACE, toInternalAttribute(attributeName), multiValued, attributeValues);
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = getOperationService().updateEntry(toCouchbaseKey(dn).getKey(), modifications, expirationValue);
            if (!result) {
                throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn));
            }
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to update entry: %s", dn), ex);
    }
}
Also used : AttributeDataModification(io.jans.orm.model.AttributeDataModification) ArrayList(java.util.ArrayList) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) JsonObject(com.couchbase.client.java.document.json.JsonObject) MutationSpec(com.couchbase.client.java.subdoc.MutationSpec) AttributeData(io.jans.orm.model.AttributeData) MappingException(io.jans.orm.exception.MappingException) DateTimeException(java.time.DateTimeException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) DateTimeParseException(java.time.format.DateTimeParseException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException)

Example 10 with AttributeData

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

the class CouchbaseEntryManager method exportEntry.

@Override
public List<AttributeData> exportEntry(String dn) {
    try {
        // Load entry
        ParsedKey keyWithInum = toCouchbaseKey(dn);
        JsonObject entry = getOperationService().lookup(keyWithInum.getKey(), null);
        List<AttributeData> result = getAttributeDataList(entry);
        if (result != null) {
            return result;
        }
        return null;
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn), ex);
    }
}
Also used : ParsedKey(io.jans.orm.impl.model.ParsedKey) JsonObject(com.couchbase.client.java.document.json.JsonObject) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) AttributeData(io.jans.orm.model.AttributeData) MappingException(io.jans.orm.exception.MappingException) DateTimeException(java.time.DateTimeException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) DateTimeParseException(java.time.format.DateTimeParseException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) SearchException(io.jans.orm.exception.operation.SearchException) AuthenticationException(io.jans.orm.exception.AuthenticationException)

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