use of org.ehrbase.jooq.pg.tables.records.FolderHierarchyRecord in project ehrbase by ehrbase.
the class FolderAccess method delete.
/**
* Deletes the FOLDER identified with the Folder.id provided and all its subfolders recursively.
*
* @param folderId of the {@link com.nedap.archie.rm.directory.Folder} to delete.
* @param contribution Optional contribution. Provide null to create a new one.
* @param systemId System ID for audit
* @param committerId Committer ID for audit
* @param description Optional description for audit
* @return number of the total folders deleted recursively.
*/
private Integer delete(final UUID folderId, UUID contribution, UUID systemId, UUID committerId, String description) {
if (folderId == null) {
throw new IllegalArgumentException("The folder UID provided for performing a delete operation cannot be null.");
}
// create new deletion audit
var delAudit = I_AuditDetailsAccess.getInstance(this, systemId, committerId, I_ConceptAccess.ContributionChangeType.DELETED, description);
UUID delAuditId = delAudit.commit();
// Collect directly linked entities before applying changes:
// Collect all linked hierarchy entries and linked (children) folders
var hierarchyRecord = getContext().fetch(FOLDER_HIERARCHY, FOLDER_HIERARCHY.PARENT_FOLDER.eq(folderId).or(FOLDER_HIERARCHY.CHILD_FOLDER.eq(folderId)));
// Collect all linked item entries
var itemsRecord = getContext().fetch(FOLDER_ITEMS, FOLDER_ITEMS.FOLDER_ID.eq(folderId));
var result = 0;
for (FolderHierarchyRecord rec : hierarchyRecord) {
// Delete child folder, and actual children only. While later removing all hierarchies anyway.
if (rec.getParentFolder().equals(folderId)) {
result += delete(rec.getChildFolder(), contribution, systemId, committerId, description);
}
// Delete whole hierarchy entity
rec.delete();
}
// Delete each linked items entity
for (FolderItemsRecord rec : itemsRecord) {
rec.delete();
}
// .delete() moves the old version to _history table.
var folderRec = getContext().fetchOne(FOLDER, FOLDER.ID.eq(folderId));
result += folderRec.delete();
// create new, BUT already moved to _history, version documenting the deletion
createAndCommitNewDeletedVersionAsHistory(folderRec, delAuditId, contribution);
return result;
}
use of org.ehrbase.jooq.pg.tables.records.FolderHierarchyRecord in project ehrbase by ehrbase.
the class FolderAccess method buildFolderHierarchyRecord.
/**
* @param parentFolder identifier.
* @param childFolder identifier to define the {@link FolderHierarchyRecord} from.
* @param inContribution contribution that the {@link com.nedap.archie.rm.directory.Folder}
* refers to.
* @param sysTransaction date of the transaction.
* @param sysPeriod period of validity of the entity persisted.
* @return the {@link FolderHierarchyRecord} for persisting the folder identified by the
* childFolder param.
*/
private final FolderHierarchyRecord buildFolderHierarchyRecord(final UUID parentFolder, final UUID childFolder, final UUID inContribution, final Timestamp sysTransaction, final Timestamp sysPeriod) {
FolderHierarchyRecord fhRecord = getContext().newRecord(FolderHierarchy.FOLDER_HIERARCHY);
fhRecord.setParentFolder(parentFolder);
fhRecord.setChildFolder(childFolder);
fhRecord.setInContribution(inContribution);
fhRecord.setSysTransaction(sysTransaction);
// fhRecord.setSysPeriod(sysPeriod); sys period can be left to null so the system sets it for the temporal tables.
return fhRecord;
}
use of org.ehrbase.jooq.pg.tables.records.FolderHierarchyRecord in project ehrbase by ehrbase.
the class FolderAccess method commit.
/**
* {@inheritDoc} Additional commit method to store a new entry of folder to the database and get
* all of inserted sub folders connected by one contribution which has been created before.
*
* @param transactionTime - Timestamp which will be applied to all folder sys_transaction values
* @param contributionId - ID of contribution for CREATE applied to all folders that will be
* created
* @return UUID of the new created root folder
*/
@Override
public UUID commit(LocalDateTime transactionTime, UUID contributionId) {
this.getFolderRecord().setInContribution(contributionId);
var inputContributionAccess = I_ContributionAccess.retrieveInstance(this.getDataAccess(), contributionId);
// create new folder audit with given values
auditDetailsAccess = new AuditDetailsAccess(this);
auditDetailsAccess.setSystemId(inputContributionAccess.getAuditsSystemId());
auditDetailsAccess.setCommitter(inputContributionAccess.getAuditsCommitter());
auditDetailsAccess.setDescription(inputContributionAccess.getAuditsDescription());
auditDetailsAccess.setChangeType(I_ConceptAccess.fetchContributionChangeType(this, I_ConceptAccess.ContributionChangeType.CREATION));
UUID auditId = this.auditDetailsAccess.commit();
this.setAudit(auditId);
// Save the folder record to database
this.getFolderRecord().store();
// Save folder items
this.saveFolderItems(this.getFolderRecord().getId(), contributionId, contributionId, Timestamp.valueOf(transactionTime), getContext());
// Save list of sub folders to database with parent <-> child ID relations
this.getSubfoldersList().values().forEach(child -> {
child.commit(transactionTime, contributionId);
FolderHierarchyRecord fhRecord = this.buildFolderHierarchyRecord(this.getFolderRecord().getId(), ((FolderAccess) child).getFolderRecord().getId(), contributionId, Timestamp.valueOf(transactionTime), null);
fhRecord.store();
});
return this.getFolderRecord().getId();
}
use of org.ehrbase.jooq.pg.tables.records.FolderHierarchyRecord in project ehrbase by ehrbase.
the class FolderAccess method internalUpdate.
private Boolean internalUpdate(final Timestamp transactionTime, boolean rootFolder, UUID parentFolder, UUID oldContribution, UUID newContribution, UUID systemId, UUID committerId, String description, ContributionChangeType contributionChangeType) {
boolean result;
// Set new Contribution for MODIFY
this.setInContribution(newContribution);
// create new folder audit with given values
auditDetailsAccess = new AuditDetailsAccess(this);
auditDetailsAccess.setSystemId(systemId);
auditDetailsAccess.setCommitter(committerId);
auditDetailsAccess.setDescription(description);
auditDetailsAccess.setChangeType(I_ConceptAccess.fetchContributionChangeType(this, contributionChangeType));
UUID auditId = this.auditDetailsAccess.commit();
if (rootFolder) {
// if it is the root folder preserve the original id, otherwise let the DB provide a new one for the overridden subfolders.
folderRecord.setInContribution(newContribution);
folderRecord.setSysTransaction(transactionTime);
getContext().attach(folderRecord);
result = folderRecord.update() > 0;
} else {
// Copy into new instance and attach to DB context.
var updatedFolderRecord = new FolderRecord();
updatedFolderRecord.setInContribution(newContribution);
updatedFolderRecord.setName(this.getFolderName());
updatedFolderRecord.setArchetypeNodeId(this.getFolderArchetypeNodeId());
updatedFolderRecord.setActive(this.isFolderActive());
updatedFolderRecord.setDetails(this.getFolderDetails());
updatedFolderRecord.setSysTransaction(transactionTime);
updatedFolderRecord.setSysPeriod(this.getFolderSysPeriod());
updatedFolderRecord.setHasAudit(auditId);
// attach to context DB
getContext().attach(updatedFolderRecord);
// Save new Folder entry to the database
result = updatedFolderRecord.insert() > 0;
// Finally overwrite original FolderRecord on this FolderAccess instance to have the
// new data available at service layer. Thus we do not need to re-fetch the updated folder
// tree from DB
this.folderRecord = updatedFolderRecord;
// Create FolderHierarchy entries this sub folder instance
var updatedFhR = new FolderHierarchyRecord();
updatedFhR.setParentFolder(parentFolder);
updatedFhR.setChildFolder(updatedFolderRecord.getId());
updatedFhR.setInContribution(newContribution);
updatedFhR.setSysTransaction(transactionTime);
updatedFhR.setSysPeriod(folderRecord.getSysPeriod());
getContext().attach(updatedFhR);
updatedFhR.store();
}
// Get new folder id for folder items and hierarchy
UUID updatedFolderId = this.folderRecord.getId();
// Update items -> Save new list of all items in this folder
this.saveFolderItems(updatedFolderId, oldContribution, newContribution, transactionTime, getContext());
boolean anySubfolderModified = // Map of sub folders with UUID
this.getSubfoldersList().values().stream().map(subfolder -> (// Update each entry and return if there has been at least one entry updated
((FolderAccess) subfolder).internalUpdate(transactionTime, false, updatedFolderId, oldContribution, newContribution, systemId, committerId, description, contributionChangeType))).reduce((b1, b2) -> b1 || b2).orElse(false);
return result || anySubfolderModified;
}
Aggregations