Search in sources :

Example 1 with CompositionRecord

use of org.ehrbase.jooq.pg.tables.records.CompositionRecord 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;
}
Also used : I_EntryAccess(org.ehrbase.dao.access.interfaces.I_EntryAccess) I_AuditDetailsAccess(org.ehrbase.dao.access.interfaces.I_AuditDetailsAccess) SQLException(java.sql.SQLException) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) I_ContributionAccess(org.ehrbase.dao.access.interfaces.I_ContributionAccess) Connection(java.sql.Connection) I_AuditDetailsAccess(org.ehrbase.dao.access.interfaces.I_AuditDetailsAccess) PreparedStatement(java.sql.PreparedStatement) EventContext(com.nedap.archie.rm.composition.EventContext) ObjectNotFoundException(org.ehrbase.api.exception.ObjectNotFoundException) ResultSet(java.sql.ResultSet) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) CompositionRecord(org.ehrbase.jooq.pg.tables.records.CompositionRecord)

Example 2 with CompositionRecord

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

the class CompositionAccess method getVersionMapOfComposition.

public static Map<Integer, I_CompositionAccess> getVersionMapOfComposition(I_DomainAccess domainAccess, UUID compositionId) {
    Map<Integer, I_CompositionAccess> versionMap = new HashMap<>();
    // create counter with highest version, to keep track of version number and allow check in the end
    Integer versionCounter = getLastVersionNumber(domainAccess, compositionId);
    // fetch matching entry
    CompositionRecord record = domainAccess.getContext().fetchOne(COMPOSITION, COMPOSITION.ID.eq(compositionId));
    if (record != null) {
        I_CompositionAccess compositionAccess = new CompositionAccess(domainAccess);
        compositionAccess.setCompositionRecord(record);
        compositionAccess.setContent(I_EntryAccess.retrieveInstanceInComposition(domainAccess, compositionAccess));
        versionMap.put(versionCounter, compositionAccess);
        versionCounter--;
    }
    // if composition was removed (i.e. from "COMPOSITION" table) *or* other versions are existing
    Result<CompositionHistoryRecord> historyRecords = domainAccess.getContext().selectFrom(COMPOSITION_HISTORY).where(COMPOSITION_HISTORY.ID.eq(compositionId)).orderBy(COMPOSITION_HISTORY.SYS_TRANSACTION.desc()).fetch();
    for (CompositionHistoryRecord historyRecord : historyRecords) {
        I_CompositionAccess historyAccess = new CompositionAccess(domainAccess);
        historyAccess.setCompositionRecord(historyRecord);
        historyAccess.setContent(I_EntryAccess.retrieveInstanceInComposition(domainAccess, historyAccess));
        versionMap.put(versionCounter, historyAccess);
        versionCounter--;
    }
    if (versionCounter != 0) {
        throw new InternalServerException("Version Map generation failed");
    }
    return versionMap;
}
Also used : HashMap(java.util.HashMap) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) InternalServerException(org.ehrbase.api.exception.InternalServerException) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) CompositionRecord(org.ehrbase.jooq.pg.tables.records.CompositionRecord) CompositionHistoryRecord(org.ehrbase.jooq.pg.tables.records.CompositionHistoryRecord)

Example 3 with CompositionRecord

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

the class CompositionAccess method retrieveInstance.

public static I_CompositionAccess retrieveInstance(I_DomainAccess domainAccess, UUID id) {
    I_CompositionAccess compositionAccess = new CompositionAccess(domainAccess);
    CompositionRecord compositionRecord = domainAccess.getContext().selectFrom(COMPOSITION).where(COMPOSITION.ID.eq(id)).fetchOne();
    if (compositionRecord == null) {
        return null;
    }
    compositionAccess.setCompositionRecord(compositionRecord);
    compositionAccess.setContent(I_EntryAccess.retrieveInstanceInComposition(domainAccess, compositionAccess));
    // retrieve the corresponding contribution
    I_ContributionAccess contributionAccess = I_ContributionAccess.retrieveInstance(domainAccess, compositionAccess.getContributionId());
    compositionAccess.setContributionAccess(contributionAccess);
    // retrieve corresponding audit
    I_AuditDetailsAccess auditAccess = new AuditDetailsAccess(domainAccess.getDataAccess()).retrieveInstance(domainAccess.getDataAccess(), compositionAccess.getAuditDetailsId());
    compositionAccess.setAuditDetailsAccess(auditAccess);
    return compositionAccess;
}
Also used : I_AuditDetailsAccess(org.ehrbase.dao.access.interfaces.I_AuditDetailsAccess) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) I_ContributionAccess(org.ehrbase.dao.access.interfaces.I_ContributionAccess) I_CompositionAccess(org.ehrbase.dao.access.interfaces.I_CompositionAccess) I_AuditDetailsAccess(org.ehrbase.dao.access.interfaces.I_AuditDetailsAccess) CompositionRecord(org.ehrbase.jooq.pg.tables.records.CompositionRecord)

Aggregations

I_CompositionAccess (org.ehrbase.dao.access.interfaces.I_CompositionAccess)3 CompositionRecord (org.ehrbase.jooq.pg.tables.records.CompositionRecord)3 I_AuditDetailsAccess (org.ehrbase.dao.access.interfaces.I_AuditDetailsAccess)2 I_ContributionAccess (org.ehrbase.dao.access.interfaces.I_ContributionAccess)2 EventContext (com.nedap.archie.rm.composition.EventContext)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 InternalServerException (org.ehrbase.api.exception.InternalServerException)1 ObjectNotFoundException (org.ehrbase.api.exception.ObjectNotFoundException)1 I_EntryAccess (org.ehrbase.dao.access.interfaces.I_EntryAccess)1 CompositionHistoryRecord (org.ehrbase.jooq.pg.tables.records.CompositionHistoryRecord)1