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