use of io.jans.orm.model.AttributeDataModification in project jans by JanssenProject.
the class SqlEntryManager method createModification.
private AttributeDataModification createModification(final AttributeModificationType type, final String attributeName, final Boolean multiValued, final Object... attributeValues) {
String realAttributeName = attributeName;
Object[] realValues = attributeValues;
if (StringHelper.equalsIgnoreCase(SqlOperationService.USER_PASSWORD, realAttributeName)) {
realValues = getOperationService().createStoragePassword(StringHelper.toStringArray(attributeValues));
}
escapeValues(realValues);
if (Boolean.TRUE.equals(multiValued)) {
return new AttributeDataModification(type, new AttributeData(realAttributeName, realValues, multiValued));
} else {
if ((realValues == null) || (realValues.length == 0)) {
return new AttributeDataModification(type, new AttributeData(realAttributeName, null));
}
return new AttributeDataModification(type, new AttributeData(realAttributeName, realValues[0]));
}
}
use of io.jans.orm.model.AttributeDataModification 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.model.AttributeDataModification in project jans by JanssenProject.
the class BaseEntryManager method collectAttributeModifications.
protected List<AttributeDataModification> collectAttributeModifications(List<PropertyAnnotation> propertiesAnnotations, Map<String, AttributeData> attributesToPersistMap, Map<String, AttributeData> attributesFromLdapMap, boolean isSchemaUpdate, AttributeModificationType schemaModificationType, boolean forceUpdate) {
List<AttributeDataModification> attributeDataModifications = new ArrayList<AttributeDataModification>();
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
String propertyName = propertiesAnnotation.getPropertyName();
Annotation ldapAttribute;
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributeName.class);
if (ldapAttribute != null) {
String ldapAttributeName = ((AttributeName) ldapAttribute).name();
if (StringHelper.isEmpty(ldapAttributeName)) {
ldapAttributeName = propertyName;
}
ldapAttributeName = ldapAttributeName.toLowerCase();
AttributeData attributeToPersist = attributesToPersistMap.get(ldapAttributeName);
AttributeData attributeFromLdap = attributesFromLdapMap.get(ldapAttributeName);
// Remove processed attributes
attributesToPersistMap.remove(ldapAttributeName);
attributesFromLdapMap.remove(ldapAttributeName);
AttributeName ldapAttributeAnnotation = (AttributeName) ldapAttribute;
if (ldapAttributeAnnotation.ignoreDuringUpdate()) {
continue;
}
if (attributeFromLdap != null && attributeToPersist != null) {
// Modify DN entry attribute in DS
if (!attributeFromLdap.equals(attributeToPersist)) {
if (isEmptyAttributeValues(attributeToPersist) && !ldapAttributeAnnotation.updateOnly()) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
}
}
} else if ((attributeFromLdap == null) && (attributeToPersist != null)) {
// Add entry attribute or change schema
if (isSchemaUpdate && (attributeToPersist.getValue() == null && Arrays.equals(attributeToPersist.getValues(), new Object[] {}))) {
continue;
}
AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
if (AttributeModificationType.ADD == modType) {
if (isEmptyAttributeValues(attributeToPersist)) {
if (forceUpdate) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else {
modType = forceUpdate ? AttributeModificationType.FORCE_UPDATE : modType;
attributeDataModifications.add(new AttributeDataModification(modType, attributeToPersist));
}
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else if ((attributeFromLdap != null) && (attributeToPersist == null)) {
// or updateOnly = true
if (!ldapAttributeAnnotation.ignoreDuringRead() && !ldapAttributeAnnotation.updateOnly()) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
}
} else if (forceUpdate && (attributeFromLdap == null) && (attributeToPersist == null)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, new AttributeData(ldapAttributeName, null)));
}
}
}
// Process properties with @AttributesList annotation
for (PropertyAnnotation propertiesAnnotation : propertiesAnnotations) {
Annotation ldapAttribute;
ldapAttribute = ReflectHelper.getAnnotationByType(propertiesAnnotation.getAnnotations(), AttributesList.class);
if (ldapAttribute != null) {
Map<String, AttributeName> ldapAttributesConfiguration = new HashMap<String, AttributeName>();
for (AttributeName ldapAttributeConfiguration : ((AttributesList) ldapAttribute).attributesConfiguration()) {
ldapAttributesConfiguration.put(ldapAttributeConfiguration.name(), ldapAttributeConfiguration);
}
// Prepare attributes for removal
for (AttributeData attributeFromLdap : attributesFromLdapMap.values()) {
String attributeName = attributeFromLdap.getName();
if (OBJECT_CLASS.equalsIgnoreCase(attributeName)) {
continue;
}
AttributeName ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
continue;
}
if (!attributesToPersistMap.containsKey(attributeName.toLowerCase())) {
// Remove if attribute not marked as ignoreDuringRead = true
if ((ldapAttributeConfiguration == null) || ((ldapAttributeConfiguration != null) && !ldapAttributeConfiguration.ignoreDuringRead())) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
}
}
}
// Prepare attributes for adding and replace
for (AttributeData attributeToPersist : attributesToPersistMap.values()) {
String attributeName = attributeToPersist.getName();
AttributeName ldapAttributeConfiguration = ldapAttributesConfiguration.get(attributeName);
if ((ldapAttributeConfiguration != null) && ldapAttributeConfiguration.ignoreDuringUpdate()) {
continue;
}
AttributeData attributeFromLdap = attributesFromLdapMap.get(attributeName.toLowerCase());
if (attributeFromLdap == null) {
// Add entry attribute or change schema
AttributeModificationType modType = isSchemaUpdate ? schemaModificationType : AttributeModificationType.ADD;
if (AttributeModificationType.ADD.equals(modType)) {
if (!isEmptyAttributeValues(attributeToPersist)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.ADD, attributeToPersist));
}
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeToPersist));
}
} else if ((attributeFromLdap != null) && isEmptyAttributeValues(attributeToPersist)) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
if (!attributeFromLdap.equals(attributeToPersist)) {
if (isEmptyAttributeValues(attributeToPersist) && (ldapAttributeConfiguration == null || !ldapAttributeConfiguration.updateOnly())) {
if (isEmptyAttributeValues(attributeFromLdap) && isStoreFullEntry()) {
// It's RDBS case. We don't need to set null to already empty table cell
continue;
}
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REMOVE, null, attributeFromLdap));
} else {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, attributeToPersist, attributeFromLdap));
}
}
}
}
}
}
return attributeDataModifications;
}
use of io.jans.orm.model.AttributeDataModification in project jans by JanssenProject.
the class CouchbaseEntryManager method updateMergeChanges.
@Override
protected <T> void updateMergeChanges(String baseDn, T entry, boolean isConfigurationUpdate, Class<?> entryClass, Map<String, AttributeData> attributesFromDbMap, List<AttributeDataModification> attributeDataModifications, boolean forceUpdate) {
// Update object classes if entry contains custom object classes
if (!isConfigurationUpdate) {
String[] objectClasses = getObjectClasses(entry, entryClass);
if (ArrayHelper.isEmpty(objectClasses)) {
throw new UnsupportedOperationException(String.format("There is no attribute with objectClasses to persist! Entry is invalid: '%s'", entry));
}
AttributeData objectClassAttributeData = attributesFromDbMap.get(OBJECT_CLASS.toLowerCase());
if (objectClassAttributeData == null) {
throw new UnsupportedOperationException(String.format("There is no attribute with objectClasses in DB! Entry is invalid: '%s'", entry));
}
String[] objectClassesFromDb = objectClassAttributeData.getStringValues();
if (ArrayHelper.isEmpty(objectClassesFromDb)) {
throw new UnsupportedOperationException(String.format("There is no attribute with objectClasses in DB! Entry is invalid: '%s'", entry));
}
// We need to check only first element of each array because objectCLass in Couchbase is single value attribute
if (!StringHelper.equals(objectClassesFromDb[0], objectClasses[0])) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, new AttributeData(OBJECT_CLASS, objectClasses, false), new AttributeData(OBJECT_CLASS, objectClassesFromDb, false)));
}
}
}
use of io.jans.orm.model.AttributeDataModification in project jans by JanssenProject.
the class LdapEntryManager method updateMergeChanges.
@Override
protected <T> void updateMergeChanges(String baseDn, T entry, boolean isConfigurationUpdate, Class<?> entryClass, Map<String, AttributeData> attributesFromLdapMap, List<AttributeDataModification> attributeDataModifications, boolean forceUpdate) {
// Update object classes if entry contains custom object classes
if (getSupportedLDAPVersion() > 2) {
if (!isConfigurationUpdate) {
String[] objectClasses = getObjectClasses(entry, entryClass);
String[] objectClassesFromLdap = attributesFromLdapMap.get(OBJECT_CLASS.toLowerCase()).getStringValues();
if (!Arrays.equals(objectClassesFromLdap, objectClasses)) {
attributeDataModifications.add(new AttributeDataModification(AttributeModificationType.REPLACE, new AttributeData(OBJECT_CLASS, objectClasses), new AttributeData(OBJECT_CLASS, objectClassesFromLdap)));
}
}
}
}
Aggregations