Search in sources :

Example 1 with AttributeDataModification

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

the class SpannerEntryManager method merge.

@Override
public void merge(String dn, String[] objectClasses, List<AttributeDataModification> attributeDataModifications, Integer expirationValue) {
    // Update entry
    try {
        List<AttributeDataModification> modifications = new ArrayList<AttributeDataModification>(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();
            }
            AttributeDataModification modification = null;
            AttributeModificationType modificationType = attributeDataModification.getModificationType();
            if ((AttributeModificationType.ADD == modificationType) || (AttributeModificationType.FORCE_UPDATE == modificationType)) {
                modification = createModification(modificationType, toInternalAttribute(attributeName), multiValued, attributeValues, oldAttributeValues);
            } else {
                if ((AttributeModificationType.REMOVE == modificationType)) {
                    if ((attribute == null) && isEmptyAttributeValues(oldAttribute)) {
                        // It's RDBS case. We don't need to set null to already empty table cell
                        continue;
                    }
                    modification = createModification(AttributeModificationType.REMOVE, toInternalAttribute(oldAttributeName), multiValued, oldAttributeValues, null);
                } else if ((AttributeModificationType.REPLACE == modificationType)) {
                    modification = createModification(AttributeModificationType.REPLACE, toInternalAttribute(attributeName), multiValued, attributeValues, oldAttributeValues);
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = getOperationService().updateEntry(toSQLKey(dn).getKey(), objectClasses[0], modifications);
            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) AttributeModificationType(io.jans.orm.model.AttributeDataModification.AttributeModificationType) ArrayList(java.util.ArrayList) 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 2 with AttributeDataModification

use of io.jans.orm.model.AttributeDataModification 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 3 with AttributeDataModification

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

the class BaseEntryManager method merge.

@SuppressWarnings("unchecked")
protected Void merge(Object entry, boolean isSchemaUpdate, boolean isConfigurationUpdate, AttributeModificationType schemaModificationType) {
    if (entry == null) {
        throw new MappingException("Entry to persist is null");
    }
    Class<?> entryClass = entry.getClass();
    checkEntryClass(entryClass, isSchemaUpdate);
    // Determine entry update method
    boolean forceUpdate = isUseEntryForceUpdate(entryClass);
    String[] objectClasses = getObjectClasses(entry, entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    Object dnValue = getDNValue(entry, entryClass);
    Integer expirationValue = getExpirationValue(entry, entryClass, true);
    List<AttributeData> attributesToPersist = getAttributesListForPersist(entry, propertiesAnnotations);
    Map<String, AttributeData> attributesToPersistMap = getAttributesMap(attributesToPersist);
    // Load entry
    List<AttributeData> attributesFromLdap = null;
    if (isSchemaUpdate || forceUpdate) {
        // If it's schema modification request we don't need to load
        // attributes from LDAP
        attributesFromLdap = new ArrayList<AttributeData>();
    } else {
        List<String> currentLdapReturnAttributesList = buildAttributesListForUpdate(entry, objectClasses, propertiesAnnotations);
        if (!isConfigurationUpdate) {
            currentLdapReturnAttributesList.add("objectClass");
        }
        attributesFromLdap = find(dnValue.toString(), objectClasses, propertiesAnnotationsMap, currentLdapReturnAttributesList.toArray(EMPTY_STRING_ARRAY));
    }
    if (LOG.isTraceEnabled()) {
        dumpAttributes("attributesFromLdap", attributesFromLdap);
        dumpAttributes("attributesToPersist", attributesToPersist);
    }
    Map<String, AttributeData> attributesFromLdapMap = getAttributesMap(attributesFromLdap);
    // Prepare list of modifications
    // Process properties with Attribute annotation
    List<AttributeDataModification> attributeDataModifications = collectAttributeModifications(propertiesAnnotations, attributesToPersistMap, attributesFromLdapMap, isSchemaUpdate, schemaModificationType, forceUpdate);
    if (LOG.isTraceEnabled()) {
        dumpAttributeDataModifications("attributeDataModifications before updateMergeChanges", attributeDataModifications);
    }
    updateMergeChanges(dnValue.toString(), entry, isSchemaUpdate | isConfigurationUpdate, entryClass, attributesFromLdapMap, attributeDataModifications, forceUpdate);
    if (LOG.isTraceEnabled()) {
        dumpAttributeDataModifications("attributeDataModifications after updateMergeChanges", attributeDataModifications);
    }
    LOG.debug(String.format("LDAP attributes for merge: %s", attributeDataModifications));
    merge(dnValue.toString(), objectClasses, attributeDataModifications, expirationValue);
    return null;
}
Also used : MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) AttributeDataModification(io.jans.orm.model.AttributeDataModification) JsonObject(io.jans.orm.annotation.JsonObject) AttributeData(io.jans.orm.model.AttributeData)

Example 4 with AttributeDataModification

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

the class BaseEntryManager method dumpAttributeDataModifications.

protected void dumpAttributeDataModifications(String variableName, List<AttributeDataModification> attributeDataModifications) {
    System.out.println("\n" + variableName + ": START");
    for (AttributeDataModification modification : attributeDataModifications) {
        String newValues = "[]";
        String oldValues = "[]";
        if ((modification.getAttribute() != null) && (modification.getAttribute().getValues() != null)) {
            newValues = Arrays.toString(modification.getAttribute().getValues());
        }
        if ((modification.getOldAttribute() != null) && (modification.getOldAttribute().getValues() != null)) {
            oldValues = Arrays.toString(modification.getOldAttribute().getValues());
        }
        AttributeData attribute = modification.getAttribute();
        if (attribute == null) {
            attribute = modification.getOldAttribute();
        }
        System.out.println(String.format("%s\t\t%s\t%b\t\t%s\t->\t%s", attribute.getName(), modification.getModificationType().name(), attribute.getMultiValued(), oldValues, newValues));
    }
    System.out.println(variableName + ": END");
}
Also used : AttributeDataModification(io.jans.orm.model.AttributeDataModification) AttributeData(io.jans.orm.model.AttributeData)

Example 5 with AttributeDataModification

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

the class SqlEntryManager method merge.

@Override
public void merge(String dn, String[] objectClasses, List<AttributeDataModification> attributeDataModifications, Integer expirationValue) {
    // Update entry
    try {
        List<AttributeDataModification> modifications = new ArrayList<AttributeDataModification>(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();
            }
            AttributeDataModification modification = null;
            AttributeModificationType modificationType = attributeDataModification.getModificationType();
            if ((AttributeModificationType.ADD == modificationType) || (AttributeModificationType.FORCE_UPDATE == modificationType)) {
                modification = createModification(modificationType, toInternalAttribute(attributeName), multiValued, attributeValues);
            } else {
                if ((AttributeModificationType.REMOVE == modificationType)) {
                    if ((attribute == null) && isEmptyAttributeValues(oldAttribute)) {
                        // It's RDBS case. We don't need to set null to already empty table cell
                        continue;
                    }
                    modification = createModification(AttributeModificationType.REMOVE, toInternalAttribute(oldAttributeName), multiValued, oldAttributeValues);
                } else if ((AttributeModificationType.REPLACE == modificationType)) {
                    modification = createModification(AttributeModificationType.REPLACE, toInternalAttribute(attributeName), multiValued, attributeValues);
                }
            }
            if (modification != null) {
                modifications.add(modification);
            }
        }
        if (modifications.size() > 0) {
            boolean result = getOperationService().updateEntry(toSQLKey(dn).getKey(), objectClasses[0], modifications);
            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) AttributeModificationType(io.jans.orm.model.AttributeDataModification.AttributeModificationType) ArrayList(java.util.ArrayList) 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)

Aggregations

AttributeData (io.jans.orm.model.AttributeData)13 AttributeDataModification (io.jans.orm.model.AttributeDataModification)13 MappingException (io.jans.orm.exception.MappingException)5 AttributeModificationType (io.jans.orm.model.AttributeDataModification.AttributeModificationType)5 ArrayList (java.util.ArrayList)5 AuthenticationException (io.jans.orm.exception.AuthenticationException)4 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)4 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)4 SearchException (io.jans.orm.exception.operation.SearchException)4 PersistenceException (io.jans.orm.exception.operation.PersistenceException)2 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)2 DateTimeParseException (java.time.format.DateTimeParseException)2 LinkedList (java.util.LinkedList)2 JsonObject (com.couchbase.client.java.document.json.JsonObject)1 MutationSpec (com.couchbase.client.java.subdoc.MutationSpec)1 KeySet (com.google.cloud.spanner.KeySet)1 Mutation (com.google.cloud.spanner.Mutation)1 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)1 SpannerException (com.google.cloud.spanner.SpannerException)1 Builder (com.google.cloud.spanner.Statement.Builder)1