Search in sources :

Example 1 with RLookupTableRow

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);
    }
}
Also used : RLookupTableRow(com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) LookupTableRowType(com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType)

Example 2 with RLookupTableRow

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);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) RelationalValueSearchQuery(com.evolveum.midpoint.schema.RelationalValueSearchQuery) Criteria(org.hibernate.Criteria) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) RLookupTableRow(com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) ObjectPaging(com.evolveum.midpoint.prism.query.ObjectPaging) LookupTableRowType(com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType) LookupTableType(com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType)

Example 3 with RLookupTableRow

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

Example 4 with RLookupTableRow

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);
        }
    }
}
Also used : RLookupTableRow(com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow) LookupTableRowType(com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType)

Aggregations

RLookupTableRow (com.evolveum.midpoint.repo.sql.data.common.other.RLookupTableRow)4 LookupTableRowType (com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableRowType)4 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)1 ContainerDelta (com.evolveum.midpoint.prism.delta.ContainerDelta)1 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 ObjectPaging (com.evolveum.midpoint.prism.query.ObjectPaging)1 RContainerId (com.evolveum.midpoint.repo.sql.data.common.id.RContainerId)1 GetOperationOptions (com.evolveum.midpoint.schema.GetOperationOptions)1 RelationalValueSearchQuery (com.evolveum.midpoint.schema.RelationalValueSearchQuery)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 LookupTableType (com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 QName (javax.xml.namespace.QName)1 Criteria (org.hibernate.Criteria)1