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);
}
}
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);
}
}
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;
}
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);
}
}
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);
}
}
Aggregations