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