Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantic link propagation #491

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import uk.ac.ox.softeng.maurodatamapper.core.diff.bidirectional.ObjectDiff
import uk.ac.ox.softeng.maurodatamapper.core.facet.EditService
import uk.ac.ox.softeng.maurodatamapper.core.facet.EditTitle
import uk.ac.ox.softeng.maurodatamapper.core.facet.Rule
import uk.ac.ox.softeng.maurodatamapper.core.facet.SemanticLink
import uk.ac.ox.softeng.maurodatamapper.core.facet.SemanticLinkType
import uk.ac.ox.softeng.maurodatamapper.core.model.CatalogueItem
import uk.ac.ox.softeng.maurodatamapper.core.model.Container
import uk.ac.ox.softeng.maurodatamapper.core.model.ContainerService
import uk.ac.ox.softeng.maurodatamapper.core.model.CopyPassType
import uk.ac.ox.softeng.maurodatamapper.core.model.Model
Expand Down Expand Up @@ -352,16 +355,18 @@ class FolderService extends ContainerService<Folder> {
UserSecurityPolicyManager userSecurityPolicyManager) {
log.debug('Copying folder {}[{}]', original.id, original.label)
long start = System.currentTimeMillis()
Map<CatalogueItem, CatalogueItem> oldNewItemMap = [:]
copyFolderPass(CopyPassType.FIRST_PASS, original, copiedFolder, label, copier, copyPermissions, modelBranchName, modelCopyDocVersion,
throwErrors, userSecurityPolicyManager)
throwErrors, userSecurityPolicyManager, oldNewItemMap)
sessionFactory.currentSession.flush()
if (clearSession) sessionFactory.currentSession.clear()
getAllSemanticLinks(oldNewItemMap, copier)
copyFolderPass(CopyPassType.SECOND_PASS, original, copiedFolder, label, copier, copyPermissions, modelBranchName, modelCopyDocVersion,
throwErrors, userSecurityPolicyManager)
throwErrors, userSecurityPolicyManager, oldNewItemMap)
sessionFactory.currentSession.flush()
if (clearSession) sessionFactory.currentSession.clear()
copyFolderPass(CopyPassType.THIRD_PASS, original, copiedFolder, label, copier, copyPermissions, modelBranchName, modelCopyDocVersion,
throwErrors, userSecurityPolicyManager)
throwErrors, userSecurityPolicyManager, oldNewItemMap)
log.debug('Folder copy complete in {}', Utils.timeTaken(start))
get(copiedFolder.id)
}
Expand All @@ -370,7 +375,8 @@ class FolderService extends ContainerService<Folder> {
String label, User copier, boolean copyPermissions,
String modelBranchName,
Version modelCopyDocVersion, boolean throwErrors,
UserSecurityPolicyManager userSecurityPolicyManager) {
UserSecurityPolicyManager userSecurityPolicyManager,
Map<CatalogueItem, CatalogueItem> oldNewItemMap) {
log.debug('{} performing copy folder pass for {}[{}]', copyPassType, original.id, original.label)
long start = System.currentTimeMillis()
if (copyPassType == CopyPassType.FIRST_PASS) {
Expand Down Expand Up @@ -400,7 +406,7 @@ class FolderService extends ContainerService<Folder> {
} else throw new ApiInvalidModelException('FS01', 'Copied Folder is invalid', copiedFolder.errors, messageSource)
}
copyFolderContents(original, copiedFolder, copier, copyPassType, copyPermissions, modelCopyDocVersion, modelBranchName, throwErrors,
userSecurityPolicyManager)
userSecurityPolicyManager, oldNewItemMap)

log.debug('{} folder copy complete for {}[{}] in {}', copyPassType, original.id, original.label, Utils.timeTaken(start))
copiedFolder
Expand Down Expand Up @@ -437,13 +443,14 @@ class FolderService extends ContainerService<Folder> {
boolean copyPermissions,
Version copyDocVersion,
String branchName,
boolean throwErrors, UserSecurityPolicyManager userSecurityPolicyManager) {
boolean throwErrors, UserSecurityPolicyManager userSecurityPolicyManager,
Map<CatalogueItem, CatalogueItem> oldNewItemMap) {

// If changing label then we need to prefix all the new models so the names dont introduce label conflicts as this situation arises in forking
String labelSuffix = folderCopy.label == original.label ? '' : " (${folderCopy.label})"

copyModelsInFolder(original, folderCopy, copier, copyPassType, labelSuffix, copyPermissions, copyDocVersion, branchName,
throwErrors, userSecurityPolicyManager)
throwErrors, userSecurityPolicyManager, oldNewItemMap)

List<Folder> folders = findAllByParentId(original.id)
log.debug('{} copying {} sub folders inside folder', copyPassType, folders.size())
Expand All @@ -459,7 +466,7 @@ class FolderService extends ContainerService<Folder> {
}
}
copyFolderPass(copyPassType, childFolder, childCopy, childFolder.label, copier, copyPermissions, branchName, copyDocVersion,
throwErrors, userSecurityPolicyManager)
throwErrors, userSecurityPolicyManager, oldNewItemMap)
}
}

Expand All @@ -469,7 +476,8 @@ class FolderService extends ContainerService<Folder> {
boolean copyPermissions,
Version copyDocVersion,
String branchName,
boolean throwErrors, UserSecurityPolicyManager userSecurityPolicyManager) {
boolean throwErrors, UserSecurityPolicyManager userSecurityPolicyManager,
Map<CatalogueItem, CatalogueItem> oldNewItemMap, boolean addRefinementLinks = true) {
modelServices.each { service ->

if (service.countByContainerId(originalFolder.id)) {
Expand All @@ -489,7 +497,8 @@ class FolderService extends ContainerService<Folder> {
Model copiedModel = service.copyModel(workingModel, workingFolder, copier, copyPermissions,
"${workingModel.label}${labelSuffix}",
copyDocVersion, branchName, throwErrors,
userSecurityPolicyManager)
userSecurityPolicyManager, oldNewItemMap, addRefinementLinks)
oldNewItemMap[originalModel] = copiedModel
log.debug('Validating and saving model copy {}', copiedModel.path)
service.validate(copiedModel)
if (copiedModel.hasErrors()) {
Expand Down Expand Up @@ -664,4 +673,41 @@ class FolderService extends ContainerService<Folder> {
if (parentCache) parentCache.addDiffCache(folder.path, fDiffCache)
fDiffCache
}

List<SemanticLink> getAllSemanticLinks(Map<CatalogueItem, CatalogueItem> oldNewItemMap, User copier) {
log.error('{}', oldNewItemMap)
Map<UUID, CatalogueItem> idMap = [:]
oldNewItemMap.keySet().each {
idMap[it.id] = it
}
List<SemanticLink> semanticLinks = []
Utils.executeInBatches(oldNewItemMap.keySet().id, {ids ->
semanticLinks.addAll(semanticLinkService.findAllByMultiFacetAwareItemIdInList(ids))
})
log.error('{}', semanticLinks.size())

List<SemanticLink> newSemanticLinks = []
semanticLinks.each {oldSemanticLink ->
UUID oldSourceItemId = oldSemanticLink.multiFacetAwareItemId
UUID oldTargetItemId = oldSemanticLink.targetMultiFacetAwareItemId

CatalogueItem newSourceItem = oldNewItemMap[idMap[oldSourceItemId]]

if(idMap[oldTargetItemId]) {
CatalogueItem newTargetItem = oldNewItemMap[idMap[oldTargetItemId]]
newSemanticLinks.add(semanticLinkService.createSemanticLink(copier, newSourceItem, newTargetItem, oldSemanticLink.linkType))
} else {
newSemanticLinks.add(new SemanticLink(createdBy: copier.emailAddress, linkType: oldSemanticLink.linkType,
targetMultiFacetAwareItemId: oldSemanticLink.targetMultiFacetAwareItemId,
targetMultiFacetAwareItemDomainType: oldSemanticLink.targetMultiFacetAwareItemDomainType,
).with {
setMultiFacetAwareItem(newSourceItem)
it
})

}
}
SemanticLink.saveAll(newSemanticLinks)
newSemanticLinks
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ class VersionedFolderService extends ContainerService<VersionedFolder> implement
getModelIdentifier(targetVersionedFolder)) as Folder
def copiedModel = modelService.copyModelAndValidateAndSave(modelToCopy, parentFolder, userSecurityPolicyManager.user, true, modelToCopy.label,
modelToCopy.documentationVersion,
targetVersionedFolder.branchName, false, userSecurityPolicyManager)
targetVersionedFolder.branchName, false, userSecurityPolicyManager, [:])

copiedModel.addMergeEdit(userSecurityPolicyManager.user, mergeEditDescription)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ enum ApiPropertyEnum {
SECURITY_RESTRICT_CLASSIFIER_CREATE('security.restrict.classifier.create'),
SECURITY_HIDE_EXCEPTIONS('security.hide.exception'),
FEATURE_COPY_ANNOTATIONS_TO_NEW_VERSION('feature.copy_annotations_to_new_version'),
FEATURE_ATTACHMENT_SIZE_LIMIT('feature.attachment_size_limit_mb')
FEATURE_ATTACHMENT_SIZE_LIMIT('feature.attachment_size_limit_mb'),
FEATURE_CREATE_REFINEMENT_LINKS_BETWEEN_VERSIONS('feature.create_refinement_links')

String key

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,12 @@ abstract class ModelController<T extends Model> extends CatalogueItemController<
AsyncJob asyncJob = getModelService().asyncCreateNewDocumentationVersion(instance,
currentUser,
createNewVersionData.copyPermissions,
currentUserSecurityPolicyManager)
currentUserSecurityPolicyManager, [:])
return respond(asyncJob, view: '/asyncJob/show', status: HttpStatus.ACCEPTED)
}

T copy = getModelService().
createNewDocumentationVersion(instance, currentUser, createNewVersionData.copyPermissions, currentUserSecurityPolicyManager) as T
createNewDocumentationVersion(instance, currentUser, createNewVersionData.copyPermissions, currentUserSecurityPolicyManager, [:]) as T

if (!validateResource(copy, 'create')) return

Expand Down Expand Up @@ -842,6 +842,12 @@ abstract class ModelController<T extends Model> extends CatalogueItemController<

if (result.any {it.hasErrors()}) {
log.debug('Errors found in imported models')
result.each {
if(it.hasErrors()) {
System.err.println(it.label)
System.err.println(it.errors)
}
}
transactionStatus.setRollbackOnly()
respond(getMultiErrorResponseMap(result), view: '/error', status: UNPROCESSABLE_ENTITY)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ abstract class CatalogueItemService<K extends CatalogueItem> implements MdmDomai
}
copy.addToRules(copiedRule)
}
semanticLinks.each {link ->

/* This will be carried out in another pass now

semanticLinks.each {link ->
if (link.targetMultiFacetAwareItem) {
copy.addToSemanticLinks(createdBy: copier.emailAddress, linkType: link.linkType,
targetMultiFacetAwareItem: link.targetMultiFacetAwareItem,
Expand All @@ -228,6 +231,8 @@ abstract class CatalogueItemService<K extends CatalogueItem> implements MdmDomai
}

}

*/
copy
}

Expand Down Expand Up @@ -345,8 +350,11 @@ abstract class CatalogueItemService<K extends CatalogueItem> implements MdmDomai
// default no-op
}

void setCatalogueItemRefinesCatalogueItem(CatalogueItem source, CatalogueItem target, User catalogueUser) {
source.addToSemanticLinks(linkType: SemanticLinkType.REFINES, createdBy: catalogueUser.emailAddress, targetMultiFacetAwareItem: target)
void setCatalogueItemRefinesCatalogueItem(CatalogueItem source, CatalogueItem target, User catalogueUser, boolean addRefinementLinks = true) {
// Temporarily disable this
if(addRefinementLinks) {
source.addToSemanticLinks(linkType: SemanticLinkType.REFINES, createdBy: catalogueUser.emailAddress, targetMultiFacetAwareItem: target)
}
}

void checkBreadcrumbTreeAfterSavingCatalogueItem(K catalogueItem) {
Expand Down
Loading
Loading