Search in sources :

Example 6 with DtoTranslationException

use of com.evolveum.midpoint.repo.sql.util.DtoTranslationException in project midpoint by Evolveum.

the class RAnyConverter method convertFromRValue.

public void convertFromRValue(RAnyValue value, PrismContainerValue any) throws DtoTranslationException {
    Validate.notNull(value, "Value for converting must not be null.");
    Validate.notNull(any, "Parent prism container value must not be null.");
    try {
        Item<?, ?> item = any.findOrCreateItem(RUtil.stringToQName(value.getName()), value.getValueType().getItemClass());
        if (item == null) {
            throw new DtoTranslationException("Couldn't create item for value '" + value.getName() + "'.");
        }
        addValueToItem(value, item);
    } catch (Exception ex) {
        if (ex instanceof DtoTranslationException) {
            throw (DtoTranslationException) ex;
        }
        throw new DtoTranslationException(ex.getMessage(), ex);
    }
}
Also used : DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QueryException(com.evolveum.midpoint.repo.sql.query.QueryException) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException)

Example 7 with DtoTranslationException

use of com.evolveum.midpoint.repo.sql.util.DtoTranslationException in project midpoint by Evolveum.

the class AuditTest method getAuditEventRecord.

private AuditEventRecord getAuditEventRecord(int expectedCount, int index) {
    Session session = getFactory().openSession();
    try {
        session.beginTransaction();
        Query query = session.createQuery("from " + RAuditEventRecord.class.getSimpleName() + " order by id");
        List<RAuditEventRecord> records = query.list();
        assertEquals(expectedCount, records.size());
        AuditEventRecord eventRecord = RAuditEventRecord.fromRepo(records.get(index), prismContext);
        session.getTransaction().commit();
        return eventRecord;
    } catch (DtoTranslationException e) {
        throw new SystemException(e);
    } finally {
        session.close();
    }
}
Also used : DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) Query(org.hibernate.Query) RAuditEventRecord(com.evolveum.midpoint.repo.sql.data.audit.RAuditEventRecord) SystemException(com.evolveum.midpoint.util.exception.SystemException) RAuditEventRecord(com.evolveum.midpoint.repo.sql.data.audit.RAuditEventRecord) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord) Session(org.hibernate.Session)

Example 8 with DtoTranslationException

use of com.evolveum.midpoint.repo.sql.util.DtoTranslationException in project midpoint by Evolveum.

the class RObject method copyFromJAXB.

public static void copyFromJAXB(PrismContainerValue containerValue, RObject repo, RepositoryContext repositoryContext, RObjectExtensionType ownerType) throws DtoTranslationException {
    RAnyConverter converter = new RAnyConverter(repositoryContext.prismContext);
    Set<RAnyValue> values = new HashSet<RAnyValue>();
    try {
        List<Item<?, ?>> items = containerValue.getItems();
        //TODO: is this ehought??should we try items without definitions??
        if (items != null) {
            for (Item item : items) {
                values.addAll(converter.convertToRValue(item, false));
            }
        }
    } catch (Exception ex) {
        throw new DtoTranslationException(ex.getMessage(), ex);
    }
    for (RAnyValue value : values) {
        ROExtValue ex = (ROExtValue) value;
        ex.setOwner(repo);
        ex.setOwnerType(ownerType);
        if (value instanceof ROExtDate) {
            repo.getDates().add(value);
        } else if (value instanceof ROExtLong) {
            repo.getLongs().add(value);
        } else if (value instanceof ROExtReference) {
            repo.getReferences().add(value);
        } else if (value instanceof ROExtString) {
            repo.getStrings().add(value);
        } else if (value instanceof ROExtPolyString) {
            repo.getPolys().add(value);
        } else if (value instanceof ROExtBoolean) {
            repo.getBooleans().add(value);
        }
    }
    repo.setStringsCount((short) repo.getStrings().size());
    repo.setDatesCount((short) repo.getDates().size());
    repo.setPolysCount((short) repo.getPolys().size());
    repo.setReferencesCount((short) repo.getReferences().size());
    repo.setLongsCount((short) repo.getLongs().size());
    repo.setBooleansCount((short) repo.getBooleans().size());
}
Also used : ROExtLong(com.evolveum.midpoint.repo.sql.data.common.any.ROExtLong) ROExtString(com.evolveum.midpoint.repo.sql.data.common.any.ROExtString) ROExtReference(com.evolveum.midpoint.repo.sql.data.common.any.ROExtReference) ROExtDate(com.evolveum.midpoint.repo.sql.data.common.any.ROExtDate) RAnyConverter(com.evolveum.midpoint.repo.sql.data.common.any.RAnyConverter) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) ROExtValue(com.evolveum.midpoint.repo.sql.data.common.any.ROExtValue) Item(com.evolveum.midpoint.prism.Item) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) ROExtBoolean(com.evolveum.midpoint.repo.sql.data.common.any.ROExtBoolean) ROExtPolyString(com.evolveum.midpoint.repo.sql.data.common.any.ROExtPolyString) RAnyValue(com.evolveum.midpoint.repo.sql.data.common.any.RAnyValue) HashSet(java.util.HashSet)

Example 9 with DtoTranslationException

use of com.evolveum.midpoint.repo.sql.util.DtoTranslationException in project midpoint by Evolveum.

the class SqlAuditServiceImpl method listRecordsIterativeAttempt.

private void listRecordsIterativeAttempt(String query, Map<String, Object> params, AuditResultHandler handler) {
    Session session = null;
    int count = 0;
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("List records attempt\n  query: {}\n params:\n{}", query, DebugUtil.debugDump(params, 2));
    }
    try {
        session = baseHelper.beginReadOnlyTransaction();
        Query q;
        if (StringUtils.isBlank(query)) {
            query = "from RAuditEventRecord as aer where 1=1 order by aer.timestamp desc";
            q = session.createQuery(query);
            setParametersToQuery(q, params);
        } else {
            q = session.createQuery(query);
            setParametersToQuery(q, params);
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("List records attempt\n  processed query: {}", q);
        }
        ScrollableResults resultList = q.scroll();
        while (resultList.next()) {
            Object o = resultList.get(0);
            if (!(o instanceof RAuditEventRecord)) {
                throw new DtoTranslationException("Unexpected object in result set. Expected audit record, but got " + o.getClass().getSimpleName());
            }
            RAuditEventRecord raudit = (RAuditEventRecord) o;
            AuditEventRecord audit = RAuditEventRecord.fromRepo(raudit, getPrismContext());
            // TODO what if original name (in audit log) differs from the current one (in repo) ?
            audit.setInitiator(resolve(session, raudit.getInitiatorOid(), raudit.getInitiatorName(), RObjectType.USER));
            audit.setTarget(resolve(session, raudit.getTargetOid(), raudit.getTargetName(), raudit.getTargetType()));
            audit.setTargetOwner(resolve(session, raudit.getTargetOwnerOid(), raudit.getTargetOwnerName(), RObjectType.USER));
            count++;
            if (!handler.handle(audit)) {
                LOGGER.trace("Skipping handling of objects after {} was handled. ", audit);
                break;
            }
        }
        session.getTransaction().commit();
    } catch (DtoTranslationException | SchemaException ex) {
        baseHelper.handleGeneralCheckedException(ex, session, null);
    } catch (RuntimeException ex) {
        baseHelper.handleGeneralRuntimeException(ex, session, null);
    } finally {
        baseHelper.cleanupSessionAndResult(session, null);
    }
    LOGGER.trace("List records iterative attempt processed {} records", count);
}
Also used : DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SQLQuery(org.hibernate.SQLQuery) Query(org.hibernate.Query) PrismObject(com.evolveum.midpoint.prism.PrismObject) ScrollableResults(org.hibernate.ScrollableResults) AuditEventRecord(com.evolveum.midpoint.audit.api.AuditEventRecord) Session(org.hibernate.Session)

Example 10 with DtoTranslationException

use of com.evolveum.midpoint.repo.sql.util.DtoTranslationException in project midpoint by Evolveum.

the class RAuditEventRecord method toRepo.

public static RAuditEventRecord toRepo(AuditEventRecord record, PrismContext prismContext) throws DtoTranslationException {
    Validate.notNull(record, "Audit event record must not be null.");
    Validate.notNull(prismContext, "Prism context must not be null.");
    RAuditEventRecord repo = new RAuditEventRecord();
    if (record.getRepoId() != null) {
        repo.setId(record.getRepoId());
    }
    repo.setChannel(record.getChannel());
    if (record.getTimestamp() != null) {
        repo.setTimestamp(new Timestamp(record.getTimestamp()));
    }
    repo.setEventStage(RAuditEventStage.toRepo(record.getEventStage()));
    repo.setEventType(RAuditEventType.toRepo(record.getEventType()));
    repo.setSessionIdentifier(record.getSessionIdentifier());
    repo.setEventIdentifier(record.getEventIdentifier());
    repo.setHostIdentifier(record.getHostIdentifier());
    repo.setRemoteHostAddress(record.getRemoteHostAddress());
    repo.setNodeIdentifier(record.getNodeIdentifier());
    repo.setParameter(record.getParameter());
    repo.setMessage(RUtil.trimString(record.getMessage(), AuditService.MAX_MESSAGE_SIZE));
    if (record.getOutcome() != null) {
        repo.setOutcome(RUtil.getRepoEnumValue(record.getOutcome().createStatusType(), ROperationResultStatus.class));
    }
    repo.setTaskIdentifier(record.getTaskIdentifier());
    repo.setTaskOID(record.getTaskOID());
    repo.setResult(record.getResult());
    try {
        if (record.getTarget() != null) {
            PrismReferenceValue target = record.getTarget();
            repo.setTargetName(getOrigName(target));
            repo.setTargetOid(target.getOid());
            repo.setTargetType(ClassMapper.getHQLTypeForQName(target.getTargetType()));
        }
        if (record.getTargetOwner() != null) {
            PrismObject targetOwner = record.getTargetOwner();
            repo.setTargetOwnerName(getOrigName(targetOwner));
            repo.setTargetOwnerOid(targetOwner.getOid());
        }
        if (record.getInitiator() != null) {
            PrismObject<UserType> initiator = record.getInitiator();
            repo.setInitiatorName(getOrigName(initiator));
            repo.setInitiatorOid(initiator.getOid());
        }
        for (ObjectDeltaOperation<?> delta : record.getDeltas()) {
            if (delta == null) {
                continue;
            }
            ObjectDelta<?> objectDelta = delta.getObjectDelta();
            for (ItemDelta<?, ?> itemDelta : objectDelta.getModifications()) {
                ItemPath path = itemDelta.getPath();
                if (path != null) {
                    // TODO what if empty?
                    CanonicalItemPath canonical = CanonicalItemPath.create(path, objectDelta.getObjectTypeClass(), prismContext);
                    for (int i = 0; i < canonical.size(); i++) {
                        RAuditItem changedItem = RAuditItem.toRepo(repo, canonical.allUpToIncluding(i).asString());
                        repo.getChangedItems().add(changedItem);
                    }
                }
            }
            RObjectDeltaOperation rDelta = RObjectDeltaOperation.toRepo(repo, delta, prismContext);
            rDelta.setTransient(true);
            rDelta.setRecord(repo);
            repo.getDeltas().add(rDelta);
        }
        for (Map.Entry<String, Set<String>> propertyEntry : record.getProperties().entrySet()) {
            for (String propertyValue : propertyEntry.getValue()) {
                repo.getPropertyValues().add(RAuditPropertyValue.toRepo(repo, propertyEntry.getKey(), RUtil.trimString(propertyValue, AuditService.MAX_PROPERTY_SIZE)));
            }
        }
        for (Map.Entry<String, Set<AuditReferenceValue>> referenceEntry : record.getReferences().entrySet()) {
            for (AuditReferenceValue referenceValue : referenceEntry.getValue()) {
                repo.getReferenceValues().add(RAuditReferenceValue.toRepo(repo, referenceEntry.getKey(), referenceValue));
            }
        }
    } catch (Exception ex) {
        throw new DtoTranslationException(ex.getMessage(), ex);
    }
    return repo;
}
Also used : PolyString(com.evolveum.midpoint.prism.polystring.PolyString) AuditReferenceValue(com.evolveum.midpoint.audit.api.AuditReferenceValue) Timestamp(java.sql.Timestamp) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) PrismObject(com.evolveum.midpoint.prism.PrismObject) DtoTranslationException(com.evolveum.midpoint.repo.sql.util.DtoTranslationException) PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) ROperationResultStatus(com.evolveum.midpoint.repo.sql.data.common.enums.ROperationResultStatus) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

DtoTranslationException (com.evolveum.midpoint.repo.sql.util.DtoTranslationException)20 Session (org.hibernate.Session)8 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)7 RObject (com.evolveum.midpoint.repo.sql.data.common.RObject)4 AuditEventRecord (com.evolveum.midpoint.audit.api.AuditEventRecord)3 PrismObject (com.evolveum.midpoint.prism.PrismObject)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 SystemException (com.evolveum.midpoint.util.exception.SystemException)3 Query (org.hibernate.Query)3 Item (com.evolveum.midpoint.prism.Item)2 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)2 QueryException (com.evolveum.midpoint.repo.sql.query.QueryException)2 ObjectDeltaOperation (com.evolveum.midpoint.schema.ObjectDeltaOperation)2 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)2 SequenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.SequenceType)2 Timestamp (java.sql.Timestamp)2 HashSet (java.util.HashSet)2 SQLQuery (org.hibernate.SQLQuery)2 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)2 AuditReferenceValue (com.evolveum.midpoint.audit.api.AuditReferenceValue)1