Search in sources :

Example 1 with PersistenceException

use of io.jans.orm.exception.operation.PersistenceException 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 2 with PersistenceException

use of io.jans.orm.exception.operation.PersistenceException 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 3 with PersistenceException

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

the class CouchbaseOperationServiceImpl method addEntryImpl.

private boolean addEntryImpl(BucketMapping bucketMapping, String key, JsonObject jsonObject, Integer expiration) throws PersistenceException {
    try {
        JsonDocument jsonDocument;
        if (expiration == null) {
            jsonDocument = JsonDocument.create(key, jsonObject);
        } else {
            jsonDocument = JsonDocument.create(key, expiration, jsonObject);
        }
        JsonDocument result = bucketMapping.getBucket().upsert(jsonDocument);
        if (result != null) {
            return true;
        }
    } catch (CouchbaseException ex) {
        throw new PersistenceException("Failed to add entry", ex);
    }
    return false;
}
Also used : CouchbaseException(com.couchbase.client.core.CouchbaseException) PersistenceException(io.jans.orm.exception.operation.PersistenceException) JsonDocument(com.couchbase.client.java.document.JsonDocument)

Example 4 with PersistenceException

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

the class SpannerOperationServiceImpl method updateEntryImpl.

private boolean updateEntryImpl(TableMapping tableMapping, String key, List<AttributeDataModification> mods) throws PersistenceException {
    try {
        MessageDigest messageDigest = getMessageDigestInstance();
        Map<String, StructField> columTypes = tableMapping.getColumTypes();
        WriteBuilder mutationBuilder = Mutation.newInsertOrUpdateBuilder(tableMapping.getTableName()).set(SpannerOperationService.DOC_ID).to(key);
        List<Mutation> mutations = new LinkedList<>();
        for (AttributeDataModification attributeMod : mods) {
            AttributeData attribute = attributeMod.getAttribute();
            AttributeModificationType type = attributeMod.getModificationType();
            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 update entry. Column '%s' is undefined", attributeName));
                }
                Map<String, StructField> childColumTypes = childTableMapping.getColumTypes();
                StructField childAttributeType = childColumTypes.get(attributeName.toLowerCase());
                // Build Mutation for child table
                Map<String, Object> oldValues = null;
                if ((attributeMod.getOldAttribute() != null) && (attributeMod.getOldAttribute().getValues() != null)) {
                    oldValues = new HashMap<>();
                    for (Object oldValue : attributeMod.getOldAttribute().getValues()) {
                        String dictDocId = getStringUniqueKey(messageDigest, oldValue);
                        oldValues.put(dictDocId, oldValue);
                    }
                }
                if ((AttributeModificationType.ADD == type) || (AttributeModificationType.FORCE_UPDATE == type) || (AttributeModificationType.REPLACE == type)) {
                    for (Object value : attribute.getValues()) {
                        WriteBuilder childMutationBuilder = Mutation.newInsertOrUpdateBuilder(childTableMapping.getTableName());
                        String dictDocId = getStringUniqueKey(messageDigest, value);
                        childMutationBuilder.set(SpannerOperationService.DOC_ID).to(key).set(SpannerOperationService.DICT_DOC_ID).to(dictDocId);
                        setMutationBuilderValue(childMutationBuilder, childAttributeType, value);
                        mutations.add(childMutationBuilder.build());
                        if (oldValues != null) {
                            oldValues.remove(dictDocId);
                        }
                    }
                } else if (AttributeModificationType.REMOVE == type) {
                    // Build Mutation for child table
                    com.google.cloud.spanner.KeySet.Builder keySetBuilder = KeySet.newBuilder();
                    for (Object value : attribute.getValues()) {
                        String dictDocId = getStringUniqueKey(messageDigest, value);
                        keySetBuilder.addKey(Key.of(key, dictDocId));
                    }
                    Mutation childMutation = Mutation.delete(childTableMapping.getTableName(), keySetBuilder.build());
                    mutations.add(childMutation);
                } else {
                    throw new UnsupportedOperationException("Operation type '" + type + "' is not implemented");
                }
                if ((oldValues != null) && (oldValues.size() > 0)) {
                    com.google.cloud.spanner.KeySet.Builder keySetBuilder = KeySet.newBuilder();
                    for (String removeDictDocId : oldValues.keySet()) {
                        keySetBuilder.addKey(Key.of(key, removeDictDocId));
                    }
                    Mutation childMutation = Mutation.delete(childTableMapping.getTableName(), keySetBuilder.build());
                    mutations.add(childMutation);
                }
            } else {
                if ((AttributeModificationType.ADD == type) || (AttributeModificationType.FORCE_UPDATE == type) || (AttributeModificationType.REPLACE == type)) {
                    setMutationBuilderValue(mutationBuilder, attributeType, attribute.getValues());
                } else if (AttributeModificationType.REMOVE == type) {
                    removeMutationBuilderValue(mutationBuilder, attribute, attributeType);
                } else {
                    throw new UnsupportedOperationException("Operation type '" + type + "' is not implemented");
                }
            }
        }
        mutations.add(0, mutationBuilder.build());
        databaseClient.write(mutations);
        return true;
    } catch (SpannerException | IllegalStateException ex) {
        throw new PersistenceException("Failed to update entry", ex);
    }
}
Also used : AttributeModificationType(io.jans.orm.model.AttributeDataModification.AttributeModificationType) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Builder(com.google.cloud.spanner.Statement.Builder) ValueWithStructField(io.jans.orm.cloud.spanner.model.ValueWithStructField) StructField(com.google.cloud.spanner.Type.StructField) MessageDigest(java.security.MessageDigest) KeySet(com.google.cloud.spanner.KeySet) TableMapping(io.jans.orm.cloud.spanner.model.TableMapping) LinkedList(java.util.LinkedList) AttributeDataModification(io.jans.orm.model.AttributeDataModification) 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) AttributeData(io.jans.orm.model.AttributeData)

Example 5 with PersistenceException

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

the class SqlOperationServiceImpl method addEntryImpl.

private boolean addEntryImpl(TableMapping tableMapping, String key, Collection<AttributeData> attributes) throws PersistenceException {
    try {
        Map<String, String> columTypes = tableMapping.getColumTypes();
        RelationalPathBase<Object> tableRelationalPath = buildTableRelationalPath(tableMapping);
        SQLInsertClause sqlInsertQuery = this.sqlQueryFactory.insert(tableRelationalPath);
        for (AttributeData attribute : attributes) {
            String attributeType = columTypes.get(attribute.getName().toLowerCase());
            boolean multiValued = (attributeType != null) && isJsonColumn(tableMapping.getTableName(), attributeType);
            sqlInsertQuery.columns(Expressions.stringPath(attribute.getName()));
            if (multiValued || Boolean.TRUE.equals(attribute.getMultiValued())) {
                sqlInsertQuery.values(convertValueToDbJson(attribute.getValues()));
            } else {
                sqlInsertQuery.values(attribute.getValue());
            }
        }
        long rowInserted = sqlInsertQuery.execute();
        return rowInserted == 1;
    } catch (QueryException ex) {
        throw new PersistenceException("Failed to add entry", ex);
    }
}
Also used : QueryException(com.querydsl.core.QueryException) SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) PersistenceException(io.jans.orm.exception.operation.PersistenceException) AttributeData(io.jans.orm.model.AttributeData)

Aggregations

PersistenceException (io.jans.orm.exception.operation.PersistenceException)5 AttributeData (io.jans.orm.model.AttributeData)4 Mutation (com.google.cloud.spanner.Mutation)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)2 SpannerException (com.google.cloud.spanner.SpannerException)2 StructField (com.google.cloud.spanner.Type.StructField)2 QueryException (com.querydsl.core.QueryException)2 TableMapping (io.jans.orm.cloud.spanner.model.TableMapping)2 ValueWithStructField (io.jans.orm.cloud.spanner.model.ValueWithStructField)2 AttributeDataModification (io.jans.orm.model.AttributeDataModification)2 AttributeModificationType (io.jans.orm.model.AttributeDataModification.AttributeModificationType)2 MessageDigest (java.security.MessageDigest)2 LinkedList (java.util.LinkedList)2 CouchbaseException (com.couchbase.client.core.CouchbaseException)1 JsonDocument (com.couchbase.client.java.document.JsonDocument)1 KeySet (com.google.cloud.spanner.KeySet)1 Builder (com.google.cloud.spanner.Statement.Builder)1 Path (com.querydsl.core.types.Path)1 Predicate (com.querydsl.core.types.Predicate)1 SQLInsertClause (com.querydsl.sql.dml.SQLInsertClause)1