use of com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow in project midpoint by Evolveum.
the class LookupTableHelper method addLookupTableRows.
private void addLookupTableRows(Session session, String tableOid, Collection<PrismContainerValue> values, int currentId, boolean deleteBeforeAdd) throws SchemaException {
for (PrismContainerValue value : values) {
LookupTableRowType rowType = new LookupTableRowType();
rowType.setupContainerValue(value);
if (deleteBeforeAdd) {
deleteRowByKey(session, tableOid, rowType.getKey());
}
RLookupTableRow row = RLookupTableRow.toRepo(tableOid, rowType);
row.setId(currentId);
currentId++;
session.save(row);
}
}
use of com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow in project midpoint by Evolveum.
the class LookupTableHelper method updateLoadedLookupTable.
public <T extends ObjectType> void updateLoadedLookupTable(PrismObject<T> object, Collection<SelectorOptions<GetOperationOptions>> options, Session session) throws SchemaException {
if (!SelectorOptions.hasToLoadPath(LookupTableType.F_ROW, options)) {
return;
}
LOGGER.debug("Loading lookup table data.");
GetOperationOptions getOption = findLookupTableGetOption(options);
RelationalValueSearchQuery queryDef = getOption == null ? null : getOption.getRelationalValueSearchQuery();
Criteria criteria = setupLookupTableRowsQuery(session, queryDef, object.getOid());
if (queryDef != null && queryDef.getPaging() != null) {
ObjectPaging paging = queryDef.getPaging();
if (paging.getOffset() != null) {
criteria.setFirstResult(paging.getOffset());
}
if (paging.getMaxSize() != null) {
criteria.setMaxResults(paging.getMaxSize());
}
ItemPath orderByPath = paging.getOrderBy();
if (paging.getDirection() != null && orderByPath != null && !orderByPath.isEmpty()) {
if (orderByPath.size() > 1 || !(orderByPath.first() instanceof NameItemPathSegment) && !(orderByPath.first() instanceof IdentifierPathSegment)) {
throw new SchemaException("OrderBy has to consist of just one naming or identifier segment");
}
ItemPathSegment first = orderByPath.first();
String orderBy = first instanceof NameItemPathSegment ? ((NameItemPathSegment) first).getName().getLocalPart() : RLookupTableRow.ID_COLUMN_NAME;
switch(paging.getDirection()) {
case ASCENDING:
criteria.addOrder(Order.asc(orderBy));
break;
case DESCENDING:
criteria.addOrder(Order.desc(orderBy));
break;
}
}
}
List<RLookupTableRow> rows = criteria.list();
if (rows == null || rows.isEmpty()) {
return;
}
LookupTableType lookup = (LookupTableType) object.asObjectable();
List<LookupTableRowType> jaxbRows = lookup.getRow();
for (RLookupTableRow row : rows) {
LookupTableRowType jaxbRow = row.toJAXB();
jaxbRows.add(jaxbRow);
}
}
use of com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow in project midpoint by Evolveum.
the class LookupTableHelper method updateLookupTableData.
public void updateLookupTableData(Session session, String tableOid, Collection<? extends ItemDelta> modifications) throws SchemaException {
if (modifications.isEmpty()) {
return;
}
for (ItemDelta delta : modifications) {
if (delta.getPath().size() == 1) {
// whole row add/delete/replace
if (!(delta instanceof ContainerDelta)) {
throw new IllegalStateException("Wrong table delta sneaked into updateLookupTableData: class=" + delta.getClass() + ", path=" + delta.getPath());
}
// one "table" container modification
ContainerDelta containerDelta = (ContainerDelta) delta;
if (containerDelta.getValuesToDelete() != null) {
// todo do 'bulk' delete like delete from ... where oid=? and id in (...)
for (PrismContainerValue<LookupTableRowType> value : (Collection<PrismContainerValue>) containerDelta.getValuesToDelete()) {
if (value.getId() != null) {
deleteRowById(session, tableOid, value.getId());
} else if (value.asContainerable().getKey() != null) {
deleteRowByKey(session, tableOid, value.asContainerable().getKey());
} else {
// ignore (or throw an exception?)
}
}
}
if (containerDelta.getValuesToAdd() != null) {
int currentId = generalHelper.findLastIdInRepo(session, tableOid, "get.lookupTableLastId") + 1;
addLookupTableRows(session, tableOid, containerDelta.getValuesToAdd(), currentId, true);
}
if (containerDelta.getValuesToReplace() != null) {
deleteLookupTableRows(session, tableOid);
addLookupTableRows(session, tableOid, containerDelta.getValuesToReplace(), 1, false);
}
} else if (delta.getPath().size() == 3) {
// row segment modification (structure is already checked)
List<ItemPathSegment> segments = delta.getPath().getSegments();
Long rowId = ((IdItemPathSegment) segments.get(1)).getId();
QName name = ((NameItemPathSegment) segments.get(2)).getName();
RLookupTableRow row = (RLookupTableRow) session.get(RLookupTableRow.class, new RContainerId(RUtil.toInteger(rowId), tableOid));
LookupTableRowType rowType = row.toJAXB();
delta.setParentPath(ItemPath.EMPTY_PATH);
delta.applyTo(rowType.asPrismContainerValue());
if (!QNameUtil.match(name, LookupTableRowType.F_LAST_CHANGE_TIMESTAMP)) {
// in order to get filled in via toRepo call below
rowType.setLastChangeTimestamp(null);
}
RLookupTableRow rowUpdated = RLookupTableRow.toRepo(tableOid, rowType);
session.merge(rowUpdated);
}
}
}
use of com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow in project midpoint by Evolveum.
the class RLookupTable method copyFromJAXB.
public static void copyFromJAXB(LookupTableType jaxb, RLookupTable repo, RepositoryContext repositoryContext, IdGeneratorResult generatorResult) throws DtoTranslationException, SchemaException {
RObject.copyFromJAXB(jaxb, repo, repositoryContext, generatorResult);
repo.setName(RPolyString.copyFromJAXB(jaxb.getName()));
List<LookupTableRowType> rows = jaxb.getRow();
if (!rows.isEmpty()) {
repo.setRows(new HashSet<>());
for (LookupTableRowType row : rows) {
RLookupTableRow rRow = RLookupTableRow.toRepo(repo, row);
rRow.setTransient(generatorResult.isTransient(row.asPrismContainerValue()));
repo.getRows().add(rRow);
}
}
}
Aggregations