use of io.jans.orm.model.AttributeData in project jans by JanssenProject.
the class SqlEntryManager 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(SqlOperationService.OBJECT_CLASS, attributeName)) {
if (!ArrayHelper.isEmpty(realValues)) {
realValues = new Object[] { realValues[0] };
multiValued = false;
}
}
// Process userPassword
if (StringHelper.equalsIgnoreCase(SqlOperationService.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(SqlOperationService.DN, dn));
resultAttributes.add(new AttributeData(SqlOperationService.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 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.AttributeData in project jans by JanssenProject.
the class SqlOperationServiceImpl method lookupImpl.
private List<AttributeData> lookupImpl(TableMapping tableMapping, String key, String... attributes) throws SearchException, EntryConvertationException {
try {
RelationalPathBase<Object> tableRelationalPath = buildTableRelationalPath(tableMapping);
Predicate whereExp = ExpressionUtils.eq(Expressions.stringPath(SqlOperationService.DOC_ID), Expressions.constant(key));
Expression<?> attributesExp = buildSelectAttributes(attributes);
SQLQuery<?> sqlSelectQuery = sqlQueryFactory.select(attributesExp).from(tableRelationalPath).where(whereExp).limit(1);
try (ResultSet resultSet = sqlSelectQuery.getResults()) {
List<AttributeData> result = getAttributeDataList(tableMapping, resultSet, true);
if (result != null) {
return result;
}
}
} catch (SQLException | QueryException ex) {
throw new SearchException(String.format("Failed to lookup query by key: '%s'", key), ex);
}
throw new SearchException(String.format("Failed to lookup entry by key: '%s'", key));
}
use of io.jans.orm.model.AttributeData in project jans by JanssenProject.
the class LdapEntryManager method createEntitiesVirtualListView.
@Deprecated
private <T> List<T> createEntitiesVirtualListView(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, SearchResultEntry... searchResultEntries) {
List<T> result = new LinkedList<T>();
Map<String, List<AttributeData>> entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
int count = 0;
for (int i = 0; i < searchResultEntries.length; i++) {
count++;
SearchResultEntry entry = searchResultEntries[i];
LinkedList<AttributeData> attributeDataLinkedList = new LinkedList<AttributeData>();
attributeDataLinkedList.addAll(getAttributeDataList(entry));
entriesAttributes.put(entry.getDN(), attributeDataLinkedList);
// Remove reference to allow java clean up object
searchResultEntries[i] = null;
// Allow java to clean up temporary objects
if (count >= 100) {
List<T> currentResult = new LinkedList<T>();
currentResult.addAll(createEntities(entryClass, propertiesAnnotations, entriesAttributes, false));
result.addAll(currentResult);
entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
count = 0;
}
}
List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes, false);
result.addAll(currentResult);
return result;
}
use of io.jans.orm.model.AttributeData in project jans by JanssenProject.
the class LdapEntryManager method exportEntry.
@Override
public List<AttributeData> exportEntry(String dn) {
try {
SearchResultEntry searchResultEntry = getOperationService().lookup(dn, (String[]) null);
List<AttributeData> result = getAttributeDataList(searchResultEntry);
if (result != null) {
return result;
}
return null;
} catch (ConnectionException | SearchException ex) {
throw new EntryPersistenceException(String.format("Failed to find entry: %s", dn), ex);
}
}
Aggregations