Skip to content

Commit

Permalink
coding to allow batch updating of leases for chosen rights category
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyKilmory committed Oct 29, 2024
1 parent b49d578 commit fad05b0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
23 changes: 23 additions & 0 deletions kahuna/public/js/common/usageRightsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
32 changes: 31 additions & 1 deletion kahuna/public/js/edits/image-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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) {

Check failure on line 328 in kahuna/public/js/edits/image-editor.js

View workflow job for this annotation

GitHub Actions / CI

Expected space(s) after "if"
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
Expand Down
23 changes: 20 additions & 3 deletions kahuna/public/js/leases/leases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 14 additions & 3 deletions kahuna/public/js/usage-rights/usage-rights-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand Down

0 comments on commit fad05b0

Please sign in to comment.