Search in sources :

Example 1 with EventContext

use of com.nedap.archie.rm.composition.EventContext in project ehrbase by ehrbase.

the class CompositionAccess method retrieveCompositionVersion.

/**
 * @throws IllegalArgumentException when version number is not greater 0
 * @throws ObjectNotFoundException  when no composition could be found with given input
 */
public static I_CompositionAccess retrieveCompositionVersion(I_DomainAccess domainAccess, UUID id, int version) {
    if (version < 1) {
        throw new IllegalArgumentException("Version number must be > 0  please check your code");
    }
    // check if this version number matches the current version
    if (getLastVersionNumber(domainAccess, id) == version) {
        // current version
        return retrieveInstance(domainAccess, id);
    }
    // FIXME make jooq compliant
    String versionQuery = "select " + "row_id, " + "in_contribution, " + "ehr_id, " + "language, " + "territory, " + "composer, " + "sys_transaction, " + "has_audit," + "attestation_ref, " + "feeder_audit, " + "links from \n" + "  (select ROW_NUMBER() OVER (ORDER BY sys_transaction ASC ) AS row_id, * from ehr.composition_history " + "       WHERE id = ?) \n" + "    AS Version WHERE row_id = ?;";
    Connection connection = domainAccess.getConnection();
    I_CompositionAccess compositionHistoryAccess = null;
    try (PreparedStatement preparedStatement = connection.prepareStatement(versionQuery)) {
        preparedStatement.setObject(1, id);
        preparedStatement.setInt(2, version);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                CompositionRecord compositionRecord1 = domainAccess.getContext().newRecord(COMPOSITION);
                compositionRecord1.setId(id);
                compositionRecord1.setInContribution(UUID.fromString(resultSet.getString("in_contribution")));
                compositionRecord1.setEhrId(UUID.fromString(resultSet.getString("ehr_id")));
                compositionRecord1.setLanguage(resultSet.getString("language"));
                compositionRecord1.setTerritory(resultSet.getInt("territory"));
                compositionRecord1.setComposer(UUID.fromString(resultSet.getString("composer")));
                compositionRecord1.setSysTransaction(resultSet.getTimestamp("sys_transaction"));
                compositionRecord1.setHasAudit(UUID.fromString(resultSet.getString("has_audit")));
                compositionRecord1.setFeederAudit(JSONB.valueOf(resultSet.getString("feeder_audit")));
                /* TODO: uncomment when links encode/decode is fully implemented
                    compositionRecord1.setLinks(JSONB.valueOf(resultSet.getString("links")));
                     */
                compositionHistoryAccess = new CompositionAccess(domainAccess, compositionRecord1);
            }
        }
    } catch (SQLException e) {
        throw new ObjectNotFoundException(COMPOSITION_LITERAL, "Composition not found or or invalid DB content", e);
    }
    if (compositionHistoryAccess != null) {
        compositionHistoryAccess.setContent(I_EntryAccess.retrieveInstanceInCompositionVersion(domainAccess, compositionHistoryAccess, version));
        // retrieve the corresponding contribution
        I_ContributionAccess contributionAccess = I_ContributionAccess.retrieveInstance(domainAccess, compositionHistoryAccess.getContributionId());
        compositionHistoryAccess.setContributionAccess(contributionAccess);
        I_AuditDetailsAccess auditDetailsAccess = new AuditDetailsAccess(domainAccess.getDataAccess()).retrieveInstance(domainAccess.getDataAccess(), compositionHistoryAccess.getAuditDetailsId());
        compositionHistoryAccess.setAuditDetailsAccess(auditDetailsAccess);
        // retrieve versioned context
        EventContext historicalEventContext = I_ContextAccess.retrieveHistoricalEventContext(domainAccess, id, compositionHistoryAccess.getSysTransaction());
        // adjust context for entries
        if (historicalEventContext != null) {
            for (I_EntryAccess entryAccess : compositionHistoryAccess.getContent()) {
                entryAccess.getComposition().setContext(historicalEventContext);
            }
        }
    }
    domainAccess.releaseConnection(connection);
    return compositionHistoryAccess;
}
Also used : I_EntryAccess(org.ehrbase.dao.access.interfaces.I_EntryAccess) I_AuditDetailsAccess(org.ehrbase.dao.access.interfaces.I_AuditDetailsAccess) SQLException(java.sql.SQLException) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) I_ContributionAccess(org.ehrbase.dao.access.interfaces.I_ContributionAccess) Connection(java.sql.Connection) I_AuditDetailsAccess(org.ehrbase.dao.access.interfaces.I_AuditDetailsAccess) PreparedStatement(java.sql.PreparedStatement) EventContext(com.nedap.archie.rm.composition.EventContext) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) ResultSet(java.sql.ResultSet) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) CompositionRecord(org.ehrbase.jooq.pg.tables.records.CompositionRecord)

Example 2 with EventContext

use of com.nedap.archie.rm.composition.EventContext in project ehrbase by ehrbase.

the class CompositionAccess method internalCreate.

private UUID internalCreate(LocalDateTime timestamp, UUID committerId, UUID systemId, String description, UUID contribution) {
    // check if custom contribution is already set, because changing it would yield updating in DB which is not desired (creates wrong new "version")
    if (contribution != null) {
        // Retrieve audit metadata from given contribution
        var newContributionAccess = I_ContributionAccess.retrieveInstance(this.getDataAccess(), contribution);
        systemId = newContributionAccess.getAuditsSystemId();
        committerId = newContributionAccess.getAuditsCommitter();
        description = newContributionAccess.getAuditsDescription();
    } else {
        // if not set, create DB entry of contribution so it can get referenced in this composition
        // prepare contribution with given values
        contributionAccess.setDataType(ContributionDataType.composition);
        contributionAccess.setState(ContributionDef.ContributionState.COMPLETE);
        contributionAccess.setAuditDetailsValues(committerId, systemId, description, I_ConceptAccess.ContributionChangeType.CREATION);
        UUID contributionId = this.contributionAccess.commit();
        setContributionId(contributionId);
    }
    // create DB entry of prepared auditDetails so it can get referenced in this composition
    auditDetailsAccess.setChangeType(I_ConceptAccess.fetchContributionChangeType(this, I_ConceptAccess.ContributionChangeType.CREATION));
    // prepare composition audit with given values
    auditDetailsAccess.setSystemId(systemId);
    auditDetailsAccess.setCommitter(committerId);
    auditDetailsAccess.setDescription(description);
    UUID auditId = this.auditDetailsAccess.commit();
    compositionRecord.setHasAudit(auditId);
    compositionRecord.setSysTransaction(Timestamp.valueOf(timestamp));
    compositionRecord.store();
    if (content.isEmpty()) {
        logger.warn("Composition has no content:");
    }
    try {
        for (I_EntryAccess entryAccess : content) {
            entryAccess.commit(Timestamp.valueOf(timestamp));
        }
    } catch (Exception exception) {
        logger.error("Problem in committing content, rolling back, exception:" + exception);
        throw new IllegalArgumentException("Could not commit content:" + exception);
    }
    if (!composition.getCategory().getDefiningCode().getCodeString().equals("431")) {
        EventContext eventContext = composition.getContext();
        I_ContextAccess contextAccess = I_ContextAccess.getInstance(this, eventContext);
        if (!contextAccess.isVoid()) {
            contextAccess.setCompositionId(compositionRecord.getId());
            contextAccess.commit(Timestamp.valueOf(timestamp));
        }
    }
    return compositionRecord.getId();
}
Also used : EventContext(com.nedap.archie.rm.composition.EventContext) I_EntryAccess(org.ehrbase.dao.access.interfaces.I_EntryAccess) I_ContextAccess(org.ehrbase.dao.access.interfaces.I_ContextAccess) UUID(java.util.UUID) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) InternalServerException(org.ehrbase.api.exception.InternalServerException) SQLException(java.sql.SQLException)

Example 3 with EventContext

use of com.nedap.archie.rm.composition.EventContext in project ehrbase by ehrbase.

the class ContextAccess method mapRmEventContext.

/**
 * @throws InternalServerException on failure of decoding DvText or DvDateTime
 */
@Override
public EventContext mapRmEventContext() {
    // get the facility entry
    PartyIdentifiedRecord partyIdentifiedRecord = getContext().fetchOne(PARTY_IDENTIFIED, PARTY_IDENTIFIED.ID.eq(eventContextRecord.getFacility()));
    // facility identifiers
    PartyIdentified healthCareFacility = null;
    if (partyIdentifiedRecord != null) {
        List<DvIdentifier> identifiers = new ArrayList<>();
        getContext().fetch(IDENTIFIER, IDENTIFIER.PARTY.eq(partyIdentifiedRecord.getId())).forEach(record -> {
            DvIdentifier dvIdentifier = new DvIdentifier();
            dvIdentifier.setIssuer(record.getIssuer());
            dvIdentifier.setAssigner(record.getAssigner());
            dvIdentifier.setId(record.getIdValue());
            dvIdentifier.setType(record.getTypeName());
            identifiers.add(dvIdentifier);
        });
        // get PartyRef values from record
        healthCareFacility = getPartyIdentifiedFromRecord(partyIdentifiedRecord, identifiers);
    }
    List<Participation> participationList = new ArrayList<>();
    // get the participations
    getContext().fetch(PARTICIPATION, PARTICIPATION.EVENT_CONTEXT.eq(eventContextRecord.getId())).forEach(record -> {
        // retrieve performer
        PartyProxy performer = new PersistedPartyProxy(this).retrieve(record.getPerformer());
        DvInterval<DvDateTime> dvInterval = convertDvIntervalDvDateTimeFromRecord(record);
        DvCodedText mode = convertModeFromRecord(record);
        Participation participation = new Participation(performer, (DvText) new RecordedDvCodedText().fromDB(record, PARTICIPATION.FUNCTION), mode, dvInterval);
        participationList.add(participation);
    });
    DvCodedText concept = (DvCodedText) new RecordedDvCodedText().fromDB(eventContextRecord, EVENT_CONTEXT.SETTING);
    ItemStructure otherContext = null;
    if (eventContextRecord.getOtherContext() != null) {
        otherContext = new RawJson().unmarshal((eventContextRecord.getOtherContext().data()), ItemStructure.class);
    }
    return new EventContext(healthCareFacility, new RecordedDvDateTime().decodeDvDateTime(eventContextRecord.getStartTime(), eventContextRecord.getStartTimeTzid()), new RecordedDvDateTime().decodeDvDateTime(eventContextRecord.getEndTime(), eventContextRecord.getEndTimeTzid()), participationList.isEmpty() ? null : participationList, eventContextRecord.getLocation(), concept, otherContext);
}
Also used : Participation(com.nedap.archie.rm.generic.Participation) PartyIdentifiedRecord(org.ehrbase.jooq.pg.tables.records.PartyIdentifiedRecord) PartyIdentified(com.nedap.archie.rm.generic.PartyIdentified) RecordedDvCodedText(org.ehrbase.service.RecordedDvCodedText) DvCodedText(com.nedap.archie.rm.datavalues.DvCodedText) RawJson(org.ehrbase.serialisation.dbencoding.RawJson) ArrayList(java.util.ArrayList) ItemStructure(com.nedap.archie.rm.datastructures.ItemStructure) DvIdentifier(com.nedap.archie.rm.datavalues.DvIdentifier) PersistedPartyProxy(org.ehrbase.dao.access.jooq.party.PersistedPartyProxy) DvDateTime(com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime) RecordedDvDateTime(org.ehrbase.service.RecordedDvDateTime) EventContext(com.nedap.archie.rm.composition.EventContext) RecordedDvDateTime(org.ehrbase.service.RecordedDvDateTime) RecordedDvCodedText(org.ehrbase.service.RecordedDvCodedText) PersistedPartyProxy(org.ehrbase.dao.access.jooq.party.PersistedPartyProxy) PartyProxy(com.nedap.archie.rm.generic.PartyProxy)

Example 4 with EventContext

use of com.nedap.archie.rm.composition.EventContext in project ehrbase by ehrbase.

the class EntryAccess method retrieveInstanceInCompositionVersion.

public static List<I_EntryAccess> retrieveInstanceInCompositionVersion(I_DomainAccess domainAccess, I_CompositionAccess compositionHistoryAccess, int version) {
    Result<EntryHistoryRecord> entryHistoryRecords = domainAccess.getContext().selectFrom(ENTRY_HISTORY).where(ENTRY_HISTORY.COMPOSITION_ID.eq(compositionHistoryAccess.getId())).and(ENTRY_HISTORY.SYS_TRANSACTION.eq(compositionHistoryAccess.getSysTransaction())).fetch();
    // build the list of parameters to recreate the composition
    Map<SystemValue, Object> values = new HashMap<>();
    values.put(SystemValue.COMPOSER, new PersistedPartyProxy(domainAccess).retrieve(compositionHistoryAccess.getComposerId()));
    EventContext context = I_ContextAccess.retrieveHistoricalEventContext(domainAccess, compositionHistoryAccess.getId(), compositionHistoryAccess.getSysTransaction());
    if (context == null) {
        // unchanged context use the current one!
        // also optional handling of context, because persistent compositions don't have a context
        compositionHistoryAccess.getContextId().ifPresent(uuid -> I_ContextAccess.retrieveInstance(domainAccess, uuid).mapRmEventContext());
    }
    values.put(SystemValue.CONTEXT, context);
    values.put(SystemValue.LANGUAGE, new CodePhrase(new TerminologyId("ISO_639-1"), compositionHistoryAccess.getLanguageCode()));
    String territory2letters = domainAccess.getContext().fetchOne(TERRITORY, TERRITORY.CODE.eq(compositionHistoryAccess.getTerritoryCode())).getTwoletter();
    values.put(SystemValue.TERRITORY, new CodePhrase(new TerminologyId("ISO_3166-1"), territory2letters));
    values.put(SystemValue.FEEDER_AUDIT, new FeederAuditEncoding().fromDB(compositionHistoryAccess.getFeederAudit()));
    List<I_EntryAccess> content = new ArrayList<>();
    try {
        EntryAccess entryAccess = new EntryAccess(domainAccess);
        for (EntryHistoryRecord record : entryHistoryRecords) {
            // set the record UID in the composition
            UUID compositionId = compositionHistoryAccess.getId();
            values.put(SystemValue.UID, new ObjectVersionId(compositionId.toString() + "::" + domainAccess.getServerConfig().getNodename() + "::" + version));
            entryAccess.entryRecord = domainAccess.getContext().newRecord(ENTRY);
            entryAccess.entryRecord.from(record);
            entryAccess.composition = new RawJson().unmarshal(record.getEntry().data(), Composition.class);
            setCompositionAttributes(entryAccess.composition, values);
            buildArchetypeDetails(entryAccess);
            content.add(entryAccess);
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(DB_INCONSISTENCY + e);
    }
    return content;
}
Also used : TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) I_EntryAccess(org.ehrbase.dao.access.interfaces.I_EntryAccess) Composition(com.nedap.archie.rm.composition.Composition) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) HashMap(java.util.HashMap) RawJson(org.ehrbase.serialisation.dbencoding.RawJson) EntryHistoryRecord(org.ehrbase.jooq.pg.tables.records.EntryHistoryRecord) ArrayList(java.util.ArrayList) ObjectVersionId(com.nedap.archie.rm.support.identification.ObjectVersionId) InternalServerException(org.ehrbase.api.exception.InternalServerException) PersistedPartyProxy(org.ehrbase.dao.access.jooq.party.PersistedPartyProxy) I_EntryAccess(org.ehrbase.dao.access.interfaces.I_EntryAccess) EventContext(com.nedap.archie.rm.composition.EventContext) FeederAuditEncoding(org.ehrbase.serialisation.dbencoding.rmobject.FeederAuditEncoding) UUID(java.util.UUID)

Example 5 with EventContext

use of com.nedap.archie.rm.composition.EventContext in project openEHR_SDK by ehrbase.

the class EventContextUnmarshalPostprocessor method process.

/**
 * {@inheritDoc}
 */
@Override
public void process(String term, EventContext rmObject, Map<FlatPathDto, String> values, Set<String> consumedPaths, Context<Map<FlatPathDto, String>> context) {
    setValue(term + PATH_DIVIDER + "_location", null, values, rmObject::setLocation, String.class, consumedPaths);
    setValue(term + PATH_DIVIDER + "_end_time", null, values, s -> {
        if (s != null) {
            rmObject.setEndTime(new DvDateTime(s));
        }
    }, String.class, consumedPaths);
    Map<FlatPathDto, String> health_care_facilityValues = FlatHelper.filter(values, term + "/_health_care_facility", false);
    if (!health_care_facilityValues.isEmpty()) {
        rmObject.setHealthCareFacility(new PartyIdentified());
        handleRmAttribute(term, rmObject.getHealthCareFacility(), health_care_facilityValues, consumedPaths, context, "health_care_facility");
    }
    Map<Integer, Map<String, String>> other = extractMultiValued(term, "_participation", values);
    other.values().stream().map(Map::entrySet).map(s -> s.stream().collect(Collectors.toMap(e -> "ctx/" + DefaultValuePath.PARTICIPATION.getPath() + "_" + e.getKey().replace("identifiers_", "identifiers|"), e -> StringUtils.wrap(e.getValue(), '"'))).entrySet()).map(DefaultValues::buildParticipation).forEach(rmObject::addParticipation);
    consumeAllMatching(term + PATH_DIVIDER + "_participation", values, consumedPaths, false);
    // Strange Path with value true if setting = other care (238)
    consumedPaths.add(term + "/" + "setting|238");
    // Strange Path with value true if setting != other care (238)
    consumedPaths.add(term + "/" + "setting|");
    if (rmObject.getSetting() != null && (rmObject.getSetting().getDefiningCode().getTerminologyId() == null || rmObject.getSetting().getDefiningCode().getTerminologyId().getValue() == null)) {
        rmObject.getSetting().getDefiningCode().setTerminologyId(new TerminologyId("openehr"));
    }
}
Also used : Context(org.ehrbase.serialisation.walker.Context) FlatHelper(org.ehrbase.serialisation.walker.FlatHelper) PartyIdentified(com.nedap.archie.rm.generic.PartyIdentified) TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) DefaultValuePath(org.ehrbase.serialisation.walker.defaultvalues.DefaultValuePath) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) Set(java.util.Set) DefaultValues(org.ehrbase.serialisation.walker.defaultvalues.DefaultValues) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) EventContext(com.nedap.archie.rm.composition.EventContext) DvDateTime(com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime) PATH_DIVIDER(org.ehrbase.webtemplate.parser.OPTParser.PATH_DIVIDER) FlatHelper.extractMultiValued(org.ehrbase.serialisation.walker.FlatHelper.extractMultiValued) Map(java.util.Map) FlatHelper.consumeAllMatching(org.ehrbase.serialisation.walker.FlatHelper.consumeAllMatching) TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) PartyIdentified(com.nedap.archie.rm.generic.PartyIdentified) FlatPathDto(org.ehrbase.webtemplate.path.flat.FlatPathDto) Map(java.util.Map) DvDateTime(com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime)

Aggregations

EventContext (com.nedap.archie.rm.composition.EventContext)10 DvDateTime (com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime)6 DvCodedText (com.nedap.archie.rm.datavalues.DvCodedText)5 PartyIdentified (com.nedap.archie.rm.generic.PartyIdentified)5 TerminologyId (com.nedap.archie.rm.support.identification.TerminologyId)4 I_EntryAccess (org.ehrbase.dao.access.interfaces.I_EntryAccess)4 CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)3 Participation (com.nedap.archie.rm.generic.Participation)3 PartyProxy (com.nedap.archie.rm.generic.PartyProxy)3 ArrayList (java.util.ArrayList)3 UUID (java.util.UUID)3 PersistedPartyProxy (org.ehrbase.dao.access.jooq.party.PersistedPartyProxy)3 DvIdentifier (com.nedap.archie.rm.datavalues.DvIdentifier)2 GenericId (com.nedap.archie.rm.support.identification.GenericId)2 PartyRef (com.nedap.archie.rm.support.identification.PartyRef)2 SQLException (java.sql.SQLException)2 InternalServerException (org.ehrbase.api.exception.InternalServerException)2 ObjectNotFoundException (org.ehrbase.api.exception.ObjectNotFoundException)2 I_ContextAccess (org.ehrbase.dao.access.interfaces.I_ContextAccess)2 PartyIdentifiedRecord (org.ehrbase.jooq.pg.tables.records.PartyIdentifiedRecord)2