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