Skip to content

Commit

Permalink
Merge pull request #48 from ehrbase/feature/44-refactor-event-context…
Browse files Browse the repository at this point in the history
…-at-persistent-comp

ehrbase/project_management#44
  • Loading branch information
jakesmolka committed Nov 22, 2019
2 parents 20aa369 + 0a2b9fe commit 929e0e7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import static org.ehrbase.jooq.pg.Tables.*;
Expand Down Expand Up @@ -326,9 +327,9 @@ static boolean isValidLanguageCode(I_DomainAccess domainAccess, String languageC
/**
* get the event context id
*
* @return {@link UUID}
* @return Optional with ID if it exists, otherwise empty Optional
*/
UUID getContextId();
Optional<UUID> getContextId();

/**
* get the contribution version id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,17 +416,17 @@ public void setComposerId(UUID composerId) {
}

@Override
public UUID getContextId() {
public Optional<UUID> getContextId() {
if (compositionRecord == null)
return null;
return Optional.empty();
if (compositionRecord.getId() == null)
return null;
return Optional.empty();
// conditional handling for persistent composition that do not have a event context
EventContextRecord eventContext = getContext().fetchOne(EVENT_CONTEXT, EVENT_CONTEXT.COMPOSITION_ID.eq(compositionRecord.getId()));
if (eventContext == null) {
return null;
return Optional.empty();
}
return eventContext.getId();
return Optional.of(eventContext.getId());
}

@Override
Expand Down Expand Up @@ -643,11 +643,8 @@ public Boolean update(Timestamp transactionTime, boolean force) {
}
}

if (force && getContextId() != null) { //updateComposition event context accordingly, if composition is not persistent (i.e. has no context)
//retrieve context and force update
I_ContextAccess contextAccess = I_ContextAccess.retrieveInstance(this, getContextId());
contextAccess.update(transactionTime, true);
}
//updateComposition event context accordingly, if composition is not persistent (i.e. has a context)
getContextId().ifPresent(id -> I_ContextAccess.retrieveInstance(this, id).update(transactionTime, force));

return result;
}
Expand Down
16 changes: 6 additions & 10 deletions service/src/main/java/org/ehrbase/dao/access/jooq/EntryAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,9 @@ public static List<I_EntryAccess> retrieveInstanceInComposition(I_DomainAccess d
Map<SystemValue, Object> values = new HashMap<>();
values.put(SystemValue.COMPOSER, I_PartyIdentifiedAccess.retrievePartyIdentified(domainAccess, compositionAccess.getComposerId()));

// conditional handling for persistent compositions that do not have a context
I_ContextAccess contextAccess = null; // stays null if persistent
if (Optional.ofNullable(compositionAccess.getContextId()).isPresent()) {
contextAccess = I_ContextAccess.retrieveInstance(domainAccess, compositionAccess.getContextId());
values.put(SystemValue.CONTEXT, contextAccess.mapRmEventContext());
}
// 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();
Expand All @@ -144,8 +141,7 @@ public static List<I_EntryAccess> retrieveInstanceInComposition(I_DomainAccess d
String value = ((PGobject) record.getEntry()).getValue();
entryAccess.composition = new RawJson().unmarshal(value, Composition.class);

// continuing conditional handling for persistent compositions
Optional<I_ContextAccess> opContextAccess = Optional.ofNullable(contextAccess);
// continuing optional handling for persistent compositions
opContextAccess.map(I_ContextAccess::mapRmEventContext).ifPresent(ec -> values.put(SystemValue.CONTEXT, ec));

setCompositionAttributes(entryAccess.composition, values);
Expand Down Expand Up @@ -183,8 +179,8 @@ public static List<I_EntryAccess> retrieveInstanceInCompositionVersion(I_DomainA

EventContext context = I_ContextAccess.retrieveHistoricalEventContext(domainAccess, compositionHistoryAccess.getId(), compositionHistoryAccess.getSysTransaction());
if (context == null) {//unchanged context use the current one!
I_ContextAccess contextAccess = I_ContextAccess.retrieveInstance(domainAccess, compositionHistoryAccess.getContextId());
context = contextAccess.mapRmEventContext();
// 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);

Expand Down

0 comments on commit 929e0e7

Please sign in to comment.