Search in sources :

Example 1 with RContainerId

use of com.evolveum.midpoint.repo.sql.data.common.id.RContainerId 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);
        }
    }
}
Also used : RLookupTableRow(com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow) ContainerDelta(com.evolveum.midpoint.prism.delta.ContainerDelta) QName(javax.xml.namespace.QName) Collection(java.util.Collection) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) ArrayList(java.util.ArrayList) List(java.util.List) RContainerId(com.evolveum.midpoint.repo.sql.data.common.id.RContainerId) LookupTableRowType(com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType)

Aggregations

ContainerDelta (com.evolveum.midpoint.prism.delta.ContainerDelta)1 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)1 RContainerId (com.evolveum.midpoint.repo.sql.data.common.id.RContainerId)1 RLookupTableRow (com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow)1 LookupTableRowType (com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 QName (javax.xml.namespace.QName)1