From fad05b03085c5b43fc10c2f573c1f23d8dc21dd3 Mon Sep 17 00:00:00 2001 From: AndyKilmory Date: Tue, 29 Oct 2024 16:25:53 +0000 Subject: [PATCH] coding to allow batch updating of leases for chosen rights category --- kahuna/public/js/common/usageRightsUtils.js | 23 +++++++++++++ kahuna/public/js/edits/image-editor.js | 32 ++++++++++++++++++- kahuna/public/js/leases/leases.js | 23 +++++++++++-- .../js/usage-rights/usage-rights-editor.js | 17 ++++++++-- 4 files changed, 88 insertions(+), 7 deletions(-) diff --git a/kahuna/public/js/common/usageRightsUtils.js b/kahuna/public/js/common/usageRightsUtils.js index d8d3bec9da..f04179ac26 100644 --- a/kahuna/public/js/common/usageRightsUtils.js +++ b/kahuna/public/js/common/usageRightsUtils.js @@ -49,3 +49,26 @@ export function createCategoryLeases(leaseDefs, image) { }); return leases; } + +/* ****************************************************************************** +Remove any leases from image that have same type as any rights-cat applied leases +********************************************************************************* */ +export function removeCategoryLeases(categories, image, removeRights) { + const mtchCats = categories.filter(cat => cat.value === removeRights); + if (mtchCats.length === 0) { + return []; + } + const removeCat = mtchCats[0]; + if (removeCat.leases.length === 0) { + return []; + } + const removeLeases = []; + image.data.leases.data.leases.forEach(lease => { + const mtches = removeCat.leases.filter(catLease => catLease.type === lease.access); + if (mtches.length > 0) { + removeLeases.push(lease); + } + }); + + return removeLeases; +} diff --git a/kahuna/public/js/edits/image-editor.js b/kahuna/public/js/edits/image-editor.js index cbd43a5356..f0cda74a64 100644 --- a/kahuna/public/js/edits/image-editor.js +++ b/kahuna/public/js/edits/image-editor.js @@ -7,7 +7,7 @@ import {imageService} from '../image/service'; import '../services/label'; import {imageAccessor} from '../services/image-accessor'; import {usageRightsEditor} from '../usage-rights/usage-rights-editor'; -import { createCategoryLeases } from '../common/usageRightsUtils.js'; +import { createCategoryLeases, removeCategoryLeases } from '../common/usageRightsUtils.js'; import {metadataTemplates} from "../metadata-templates/metadata-templates"; import {leases} from '../leases/leases'; import {archiver} from '../components/gr-archiver-status/gr-archiver-status'; @@ -274,6 +274,7 @@ imageEditor.controller('ImageEditorCtrl', [ const image = ctrl.image; const resource = image.data.userMetadata.data.usageRights; editsService.update(resource, data, image); + batchSetLeasesFromUsageRights(image, data.category); }); } @@ -317,6 +318,35 @@ imageEditor.controller('ImageEditorCtrl', [ ctrl.showUsageRights = ctrl.usageRightsCategory === undefined; } + function batchSetLeasesFromUsageRights(image, rightsCat) { + const category = ctrl.categories.find(cat => cat.value === rightsCat); + if (!category || (image.data.usageRights.category && image.data.usageRights.category === rightsCat )) { + return; + } + if (category.leases.length === 0) { + // possibility of removal only + if(!image.data.usageRights.category) { + return; + } + const removeLeases = removeCategoryLeases(ctrl.categories, image, image.data.usageRights.category); + if (removeLeases && removeLeases.length > 0) { + $rootScope.$broadcast('events:rights-category:delete-leases', { + catLeases: removeLeases, + batch: false + }); + } + return; + } + const catLeases = createCategoryLeases(category.leases, image); + if (catLeases.length === 0) { + return; + } + $rootScope.$broadcast('events:rights-category:add-leases', { + catLeases: catLeases, + batch: false + }); + } + function batchApplyUsageRights() { $rootScope.$broadcast(batchApplyUsageRightsEvent, { data: ctrl.usageRights.data diff --git a/kahuna/public/js/leases/leases.js b/kahuna/public/js/leases/leases.js index 7fa4c846cd..67b06ce13b 100644 --- a/kahuna/public/js/leases/leases.js +++ b/kahuna/public/js/leases/leases.js @@ -123,14 +123,31 @@ leases.controller('LeasesCtrl', [ const batchAddLeasesEvent = 'events:batch-apply:add-leases'; const batchRemoveLeasesEvent = 'events:batch-apply:remove-leases'; const rightsCatAddLeasesEvent = 'events:rights-category:add-leases'; + const rightsCatDeleteLeasesEvent = 'events:rights-category:delete-leases'; //-handle rights cat assigned lease- $scope.$on(rightsCatAddLeasesEvent, (e, payload) => { - if (payload.catLeases[0].mediaId === ctrl.images[0].data.id || payload.batch) { - leaseService.replace(ctrl.images[0], payload.catLeases); + let matchImages = ctrl.images.filter(img => img.data.id === payload.catLeases[0].mediaId); + if (angular.isDefined(matchImages.toArray)) { + matchImages = matchImages.toArray(); + }; + if (matchImages.length || payload.batch) { + leaseService.replace(matchImages[0], payload.catLeases); } - }); + } + ); + + //-handle deletion of leases from previous rights category- + $scope.$on(rightsCatDeleteLeasesEvent, + (e, payload) => { + if (payload.catLeases && 0 < payload.catLeases.length) { + payload.catLeases.forEach(lease => { + leaseService.deleteLease(lease, ctrl.images); + }); + } + } + ); if (Boolean(ctrl.withBatch)) { $scope.$on(batchAddLeasesEvent, diff --git a/kahuna/public/js/usage-rights/usage-rights-editor.js b/kahuna/public/js/usage-rights/usage-rights-editor.js index 2eae7d5033..79c48c2eb5 100644 --- a/kahuna/public/js/usage-rights/usage-rights-editor.js +++ b/kahuna/public/js/usage-rights/usage-rights-editor.js @@ -8,7 +8,7 @@ import {List} from 'immutable'; import '../services/image-list'; -import { createCategoryLeases } from '../common/usageRightsUtils.js'; +import { createCategoryLeases, removeCategoryLeases } from '../common/usageRightsUtils.js'; import template from './usage-rights-editor.html'; import './usage-rights-editor.css'; @@ -204,7 +204,10 @@ usageRightsEditor.controller( const resource = image.data.userMetadata.data.usageRights; return editsService.update(resource, data, image, true); }, - ({ image }) => setLeasesFromUsageRights(image), + ({ image }) => { + const prevRights = (0 < ctrl.usageRights.size) ? ctrl.usageRights.first().data.category : ""; + return setLeasesFromUsageRights(image, prevRights); + }, ({ image }) => setMetadataFromUsageRights(image, true), ({ image }) => image.get() ],'images-updated'); @@ -230,8 +233,16 @@ usageRightsEditor.controller( 'Unexpected error'; } - function setLeasesFromUsageRights(image) { + function setLeasesFromUsageRights(image, prevRights) { if (ctrl.category.leases.length === 0) { + // possibility of removal only + const removeLeases = removeCategoryLeases(ctrl.categories, image, prevRights); + if (removeLeases && removeLeases.length > 0) { + $rootScope.$broadcast('events:rights-category:delete-leases', { + catLeases: removeLeases, + batch: false + }); + } return; } const catLeases = createCategoryLeases(ctrl.category.leases, image);