Search in sources :

Example 1 with RawJson

use of org.ehrbase.serialisation.dbencoding.RawJson 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 2 with RawJson

use of org.ehrbase.serialisation.dbencoding.RawJson 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 3 with RawJson

use of org.ehrbase.serialisation.dbencoding.RawJson in project ehrbase by ehrbase.

the class EntryAccess method retrieveInstanceInComposition.

/**
 * @throws IllegalArgumentException if DB is inconsistent or operation fails
 */
public static List<I_EntryAccess> retrieveInstanceInComposition(I_DomainAccess domainAccess, I_CompositionAccess compositionAccess) {
    Result<EntryRecord> entryRecords = domainAccess.getContext().selectFrom(ENTRY).where(ENTRY.COMPOSITION_ID.eq(compositionAccess.getId())).fetch();
    // build the list of parameters to recreate the composition
    Map<SystemValue, Object> values = new HashMap<>();
    values.put(SystemValue.COMPOSER, new PersistedPartyProxy(domainAccess).retrieve(compositionAccess.getComposerId()));
    // optional handling for persistent compositions that do not have a context
    Optional<I_ContextAccess> opContextAccess = compositionAccess.getContextId().map(id -> I_ContextAccess.retrieveInstance(domainAccess, id));
    opContextAccess.ifPresent(context -> values.put(SystemValue.CONTEXT, context.mapRmEventContext()));
    values.put(SystemValue.LANGUAGE, new CodePhrase(new TerminologyId("ISO_639-1"), compositionAccess.getLanguageCode()));
    String territory2letters = domainAccess.getContext().fetchOne(TERRITORY, TERRITORY.CODE.eq(compositionAccess.getTerritoryCode())).getTwoletter();
    values.put(SystemValue.TERRITORY, new CodePhrase(new TerminologyId("ISO_3166-1"), territory2letters));
    if (compositionAccess.getFeederAudit() != null) {
        values.put(SystemValue.FEEDER_AUDIT, new FeederAuditEncoding().fromDB(compositionAccess.getFeederAudit()));
    }
    if (compositionAccess.getLinks() != null) {
        values.put(SystemValue.LINKS, new LinksEncoding().fromDB(compositionAccess.getLinks()));
    }
    List<I_EntryAccess> content = new ArrayList<>();
    try {
        EntryAccess entryAccess = new EntryAccess(domainAccess);
        for (EntryRecord record : entryRecords) {
            // set the record UID in the composition with matching version number
            Integer version = I_CompositionAccess.getLastVersionNumber(domainAccess, compositionAccess.getId());
            values.put(SystemValue.UID, new ObjectVersionId(compositionAccess.getId().toString() + "::" + domainAccess.getServerConfig().getNodename() + "::" + version));
            entryAccess.entryRecord = record;
            String value = record.getEntry().data();
            entryAccess.composition = new RawJson().unmarshal(value, Composition.class);
            // continuing optional handling for persistent compositions
            opContextAccess.map(I_ContextAccess::mapRmEventContext).ifPresent(ec -> values.put(SystemValue.CONTEXT, ec));
            values.put(SystemValue.CATEGORY, new RecordedDvCodedText().fromDB(record, ENTRY.CATEGORY));
            setCompositionAttributes(entryAccess.composition, values);
            buildArchetypeDetails(entryAccess);
            content.add(entryAccess);
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(DB_INCONSISTENCY + e);
    }
    return content;
}
Also used : I_EntryAccess(org.ehrbase.dao.access.interfaces.I_EntryAccess) CodePhrase(com.nedap.archie.rm.datatypes.CodePhrase) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ObjectVersionId(com.nedap.archie.rm.support.identification.ObjectVersionId) I_EntryAccess(org.ehrbase.dao.access.interfaces.I_EntryAccess) RecordedDvCodedText(org.ehrbase.service.RecordedDvCodedText) FeederAuditEncoding(org.ehrbase.serialisation.dbencoding.rmobject.FeederAuditEncoding) LinksEncoding(org.ehrbase.serialisation.dbencoding.rmobject.LinksEncoding) TerminologyId(com.nedap.archie.rm.support.identification.TerminologyId) Composition(com.nedap.archie.rm.composition.Composition) RawJson(org.ehrbase.serialisation.dbencoding.RawJson) InternalServerException(org.ehrbase.api.exception.InternalServerException) PersistedPartyProxy(org.ehrbase.dao.access.jooq.party.PersistedPartyProxy) I_ContextAccess(org.ehrbase.dao.access.interfaces.I_ContextAccess) EntryRecord(org.ehrbase.jooq.pg.tables.records.EntryRecord)

Example 4 with RawJson

use of org.ehrbase.serialisation.dbencoding.RawJson in project ehrbase by ehrbase.

the class EntryAccess method setCompositionFields.

/**
 * set the EntryRecord with fields from composition:<br>
 * <ul>
 * <li>category</li>
 * <li>item type</li>
 * <li>archetype node Id</li>
 * <li>entry content (json)</li>
 * </ul>
 *
 * @param record      Target {@link EntryRecord}
 * @param composition input data in {@link Composition} representation
 */
private void setCompositionFields(EntryRecord record, Composition composition) {
    record.setCategory(record.getCategory());
    if (composition.getContent() != null && !composition.getContent().isEmpty()) {
        Object node = composition.getContent().get(0);
        if (node instanceof Section) {
            record.setItemType(EntryType.valueOf("section"));
        } else if (node instanceof Evaluation || node instanceof Observation || node instanceof Instruction || node instanceof Action) {
            record.setItemType(EntryType.valueOf("care_entry"));
        } else if (node instanceof AdminEntry) {
            record.setItemType(EntryType.valueOf("admin"));
        }
    } else {
        record.setItemType(EntryType.valueOf("admin"));
    }
    record.setArchetypeId(composition.getArchetypeNodeId());
    RawJson rawJson = new RawJson();
    record.setEntry(JSONB.valueOf(rawJson.marshal(composition)));
}
Also used : Evaluation(com.nedap.archie.rm.composition.Evaluation) Action(com.nedap.archie.rm.composition.Action) RawJson(org.ehrbase.serialisation.dbencoding.RawJson) AdminEntry(com.nedap.archie.rm.composition.AdminEntry) Observation(com.nedap.archie.rm.composition.Observation) Instruction(com.nedap.archie.rm.composition.Instruction) Section(com.nedap.archie.rm.composition.Section)

Example 5 with RawJson

use of org.ehrbase.serialisation.dbencoding.RawJson in project ehrbase by ehrbase.

the class ContextAccess method setRecordFields.

/**
 * setup an EventContextRecord instance based on values from an EventContext instance
 *
 * @param id
 * @param eventContext
 */
@Override
public void setRecordFields(UUID id, EventContext eventContext) {
    RecordedDvDateTime recordedDvDateTime = new RecordedDvDateTime(eventContext.getStartTime());
    eventContextRecord.setStartTime(recordedDvDateTime.toTimestamp());
    recordedDvDateTime.zoneId().ifPresent(eventContextRecord::setStartTimeTzid);
    if (eventContext.getEndTime() != null) {
        recordedDvDateTime = new RecordedDvDateTime(eventContext.getEndTime());
        eventContextRecord.setEndTime(recordedDvDateTime.toTimestamp());
        recordedDvDateTime.zoneId().ifPresent(eventContextRecord::setEndTimeTzid);
    }
    eventContextRecord.setId(id != null ? id : UUID.randomUUID());
    // Health care facility
    if (eventContext.getHealthCareFacility() != null) {
        UUID healthcareFacilityId = new PersistedPartyProxy(this).getOrCreate(eventContext.getHealthCareFacility());
        eventContextRecord.setFacility(healthcareFacilityId);
    }
    // location
    if (eventContext.getLocation() != null)
        eventContextRecord.setLocation(eventContext.getLocation());
    new RecordedDvCodedText().toDB(eventContextRecord, EVENT_CONTEXT.SETTING, eventContext.getSetting());
    if (eventContext.getParticipations() != null) {
        for (Participation participation : eventContext.getParticipations()) {
            ParticipationRecord participationRecord = getContext().newRecord(PARTICIPATION);
            participationRecord.setEventContext(eventContextRecord.getId());
            new RecordedDvText().toDB(participationRecord, PARTICIPATION.FUNCTION, participation.getFunction());
            if (participation.getMode() != null)
                new RecordedDvCodedText().toDB(participationRecord, PARTICIPATION.MODE, participation.getMode());
            if (participation.getTime() != null) {
                DvDateTime lower = participation.getTime().getLower();
                if (lower != null) {
                    recordedDvDateTime = new RecordedDvDateTime(lower);
                    participationRecord.setTimeLower(recordedDvDateTime.toTimestamp());
                    recordedDvDateTime.zoneId().ifPresent(participationRecord::setTimeLowerTz);
                }
                DvDateTime upper = participation.getTime().getUpper();
                if (upper != null) {
                    recordedDvDateTime = new RecordedDvDateTime(upper);
                    participationRecord.setTimeUpper(recordedDvDateTime.toTimestamp());
                    recordedDvDateTime.zoneId().ifPresent(participationRecord::setTimeUpperTz);
                }
            }
            // only PartyIdentified performer is supported now
            PartyIdentified performer;
            PartyProxy setPerformer = participation.getPerformer();
            if (!(setPerformer instanceof PartyIdentified)) {
                log.warn("Set performer is using unsupported type: {}", setPerformer);
                break;
            }
            performer = (PartyIdentified) setPerformer;
            UUID performerUuid = new PersistedPartyProxy(this).getOrCreate(performer);
            // set the performer
            participationRecord.setPerformer(performerUuid);
            participations.add(participationRecord);
        }
    }
    // other context
    if (eventContext.getOtherContext() != null && CollectionUtils.isNotEmpty(eventContext.getOtherContext().getItems())) {
        // set up the JSONB field other_context
        eventContextRecord.setOtherContext(JSONB.valueOf(new RawJson().marshal(eventContext.getOtherContext())));
    }
}
Also used : Participation(com.nedap.archie.rm.generic.Participation) PartyIdentified(com.nedap.archie.rm.generic.PartyIdentified) ParticipationRecord(org.ehrbase.jooq.pg.tables.records.ParticipationRecord) RawJson(org.ehrbase.serialisation.dbencoding.RawJson) PersistedPartyProxy(org.ehrbase.dao.access.jooq.party.PersistedPartyProxy) DvDateTime(com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime) RecordedDvDateTime(org.ehrbase.service.RecordedDvDateTime) RecordedDvDateTime(org.ehrbase.service.RecordedDvDateTime) RecordedDvText(org.ehrbase.service.RecordedDvText) RecordedDvCodedText(org.ehrbase.service.RecordedDvCodedText) PersistedPartyProxy(org.ehrbase.dao.access.jooq.party.PersistedPartyProxy) PartyProxy(com.nedap.archie.rm.generic.PartyProxy) UUID(java.util.UUID)

Aggregations

RawJson (org.ehrbase.serialisation.dbencoding.RawJson)5 PersistedPartyProxy (org.ehrbase.dao.access.jooq.party.PersistedPartyProxy)4 ArrayList (java.util.ArrayList)3 RecordedDvCodedText (org.ehrbase.service.RecordedDvCodedText)3 Composition (com.nedap.archie.rm.composition.Composition)2 EventContext (com.nedap.archie.rm.composition.EventContext)2 CodePhrase (com.nedap.archie.rm.datatypes.CodePhrase)2 DvDateTime (com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime)2 Participation (com.nedap.archie.rm.generic.Participation)2 PartyIdentified (com.nedap.archie.rm.generic.PartyIdentified)2 PartyProxy (com.nedap.archie.rm.generic.PartyProxy)2 ObjectVersionId (com.nedap.archie.rm.support.identification.ObjectVersionId)2 TerminologyId (com.nedap.archie.rm.support.identification.TerminologyId)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 InternalServerException (org.ehrbase.api.exception.InternalServerException)2 I_EntryAccess (org.ehrbase.dao.access.interfaces.I_EntryAccess)2 FeederAuditEncoding (org.ehrbase.serialisation.dbencoding.rmobject.FeederAuditEncoding)2 RecordedDvDateTime (org.ehrbase.service.RecordedDvDateTime)2 Action (com.nedap.archie.rm.composition.Action)1