Search in sources :

Example 1 with ParticipationRecord

use of org.ehrbase.jooq.pg.tables.records.ParticipationRecord in project ehrbase by ehrbase.

the class ContextAccess method update.

/**
 * @throws InternalServerException when update failed
 */
@Override
public Boolean update(Timestamp transactionTime, boolean force) {
    if (force) {
        eventContextRecord.changed(true);
        // jOOQ limited support of TSTZRANGE, exclude sys_period from updateComposition!
        eventContextRecord.changed(EVENT_CONTEXT.SYS_PERIOD, false);
        for (ParticipationRecord participationRecord : participations) {
            participationRecord.changed(true);
            // jOOQ limited support of TSTZRANGE, exclude sys_period from updateComposition!
            participationRecord.changed(PARTICIPATION.SYS_PERIOD, false);
        }
    }
    return update(transactionTime);
}
Also used : ParticipationRecord(org.ehrbase.jooq.pg.tables.records.ParticipationRecord)

Example 2 with ParticipationRecord

use of org.ehrbase.jooq.pg.tables.records.ParticipationRecord in project ehrbase by ehrbase.

the class ContextAccess method update.

/**
 * @throws InternalServerException if DB inconsistency or other problem with updating DB entry
 */
@Override
public Boolean update(Timestamp transactionTime) {
    // updateComposition participations
    for (ParticipationRecord participationRecord : participations) {
        participationRecord.setSysTransaction(transactionTime);
        if (participationRecord.changed()) {
            // check if commit or updateComposition (exists or not...)
            try {
                if (getContext().fetchExists(PARTICIPATION, PARTICIPATION.ID.eq(participationRecord.getId()))) {
                    participationRecord.update();
                } else {
                    participationRecord.setId(UUID.randomUUID());
                    participationRecord.store();
                }
            } catch (DataAccessException e) {
                // generalize DB exceptions
                throw new InternalServerException(DB_INCONSISTENCY, e);
            }
        }
    }
    // ignore the temporal field since it is maintained by an external trigger!
    eventContextRecord.changed(EVENT_CONTEXT.SYS_PERIOD, false);
    eventContextRecord.setSysTransaction(transactionTime);
    UpdateQuery<?> updateQuery = getContext().updateQuery(EVENT_CONTEXT);
    updateQuery.addValue(EVENT_CONTEXT.COMPOSITION_ID, eventContextRecord.getCompositionId());
    updateQuery.addValue(EVENT_CONTEXT.START_TIME, eventContextRecord.getStartTime());
    updateQuery.addValue(EVENT_CONTEXT.START_TIME_TZID, eventContextRecord.getStartTimeTzid());
    updateQuery.addValue(EVENT_CONTEXT.END_TIME, eventContextRecord.getEndTime());
    updateQuery.addValue(EVENT_CONTEXT.END_TIME_TZID, eventContextRecord.getEndTimeTzid());
    updateQuery.addValue(EVENT_CONTEXT.FACILITY, eventContextRecord.getFacility());
    updateQuery.addValue(EVENT_CONTEXT.LOCATION, eventContextRecord.getLocation());
    if (eventContextRecord.getOtherContext() != null)
        updateQuery.addValue(EVENT_CONTEXT.OTHER_CONTEXT, eventContextRecord.getOtherContext());
    updateQuery.addValue(EVENT_CONTEXT.SETTING, eventContextRecord.getSetting());
    updateQuery.addValue(EVENT_CONTEXT.SYS_TRANSACTION, eventContextRecord.getSysTransaction());
    updateQuery.addConditions(EVENT_CONTEXT.ID.eq(getId()));
    boolean result;
    try {
        result = updateQuery.execute() > 0;
    } catch (DataAccessException e) {
        // generalize DB exceptions
        throw new InternalServerException("Problem when updating DB entry", e);
    }
    return result;
}
Also used : ParticipationRecord(org.ehrbase.jooq.pg.tables.records.ParticipationRecord) InternalServerException(org.ehrbase.api.exception.InternalServerException) DataAccessException(org.jooq.exception.DataAccessException)

Example 3 with ParticipationRecord

use of org.ehrbase.jooq.pg.tables.records.ParticipationRecord 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

ParticipationRecord (org.ehrbase.jooq.pg.tables.records.ParticipationRecord)3 DvDateTime (com.nedap.archie.rm.datavalues.quantity.datetime.DvDateTime)1 Participation (com.nedap.archie.rm.generic.Participation)1 PartyIdentified (com.nedap.archie.rm.generic.PartyIdentified)1 PartyProxy (com.nedap.archie.rm.generic.PartyProxy)1 UUID (java.util.UUID)1 InternalServerException (org.ehrbase.api.exception.InternalServerException)1 PersistedPartyProxy (org.ehrbase.dao.access.jooq.party.PersistedPartyProxy)1 RawJson (org.ehrbase.serialisation.dbencoding.RawJson)1 RecordedDvCodedText (org.ehrbase.service.RecordedDvCodedText)1 RecordedDvDateTime (org.ehrbase.service.RecordedDvDateTime)1 RecordedDvText (org.ehrbase.service.RecordedDvText)1 DataAccessException (org.jooq.exception.DataAccessException)1