Skip to content

Commit 2cf79d8

Browse files
authored
setting option to enable/disable backup during delete record (geonetwork#8728)
* setting option to enable/disable backup during delete record * Change default of withBackup to allow null and default with setting value. * System setting of delete record backup options * Change no preference to Use API parameter option * update translation
1 parent f478ab1 commit 2cf79d8

File tree

7 files changed

+76
-2
lines changed

7 files changed

+76
-2
lines changed

core/src/main/java/org/fao/geonet/kernel/setting/Settings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public class Settings {
144144
public static final String METADATA_HISTORY_ENABLED = "metadata/history/enabled";
145145
public static final String METADATA_HISTORY_ACCESS_LEVEL = "metadata/history/accesslevel";
146146
public static final String METADATA_PUBLISHED_DELETE_USERPROFILE = "metadata/delete/profilePublishedMetadata";
147+
public static final String METADATA_DELETE_BACKUPOPTIONS = "metadata/delete/backupOptions";
147148
public static final String METADATA_PUBLISH_USERPROFILE = "metadata/publication/profilePublishMetadata";
148149
public static final String METADATA_UNPUBLISH_USERPROFILE = "metadata/publication/profileUnpublishMetadata";
149150
public static final String METADATA_BACKUPARCHIVE_ENABLE = "metadata/backuparchive/enable";

core/src/test/resources/org/fao/geonet/api/Messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ api.metadata.share.ErrorUserNotAllowedToPublish=User not allowed to publish the
246246
api.metadata.share.strategy.groupOwnerOnly=You need to be administrator, or reviewer of the metadata group.
247247
api.metadata.share.strategy.reviewerInGroup=You need to be administrator, or reviewer of the metadata group or reviewer with edit privilege on the metadata.
248248
api.metadata.status.errorSetStatusNotAllowed=Only the owner of the metadata can set the status of this record. User is not the owner of the metadata.
249+
api.metadata.delete.errorForceBackup=Cannot force the backup during deletion process because the client specified with backup parameter as: '%s'.
250+
api.metadata.delete.errorForceNoBackup=Cannot force the no-backup during deletion process because the client specified with backup parameter as: '%s'.
249251

250252
feedback_subject_userFeedback=User feedback
251253

services/src/main/java/org/fao/geonet/api/records/MetadataInsertDeleteApi.java

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ public class MetadataInsertDeleteApi {
215215
@ResponseStatus(HttpStatus.NO_CONTENT)
216216
public void deleteRecord(
217217
@Parameter(description = API_PARAM_RECORD_UUID, required = true) @PathVariable String metadataUuid,
218-
@Parameter(description = API_PARAM_BACKUP_FIRST, required = false) @RequestParam(required = false, defaultValue = "true") boolean withBackup,
218+
@Parameter(description = API_PARAM_BACKUP_FIRST, required = false) @RequestParam(required = false) Boolean withBackup,
219219
HttpServletRequest request) throws Exception {
220+
boolean doBackup = withDeleteBackup(withBackup, request);
221+
220222
AbstractMetadata metadata = ApiUtils.canEditRecord(metadataUuid, request);
221223
ServiceContext context = ApiUtils.createServiceContext(request);
222224
Store store = context.getBean("resourceStore", Store.class);
@@ -225,7 +227,7 @@ public void deleteRecord(
225227
ApplicationContextHolder.get().publishEvent(preRemoveEvent);
226228

227229
if (metadata.getDataInfo().getType() != MetadataType.SUB_TEMPLATE
228-
&& metadata.getDataInfo().getType() != MetadataType.TEMPLATE_OF_SUB_TEMPLATE && withBackup) {
230+
&& metadata.getDataInfo().getType() != MetadataType.TEMPLATE_OF_SUB_TEMPLATE && doBackup) {
229231
MEFLib.backupRecord(metadata, context);
230232
}
231233

@@ -1026,4 +1028,41 @@ private Pair<Integer, String> loadRecord(MetadataType metadataType, Element xmlE
10261028
return Pair.read(Integer.valueOf(id.get(0)), uuid);
10271029
}
10281030

1031+
/**
1032+
* Determine if the metadata backup is needed during delete API.<br/>
1033+
* 1) The withBackup flag missing from the API, will only overide if setting as ForceNoBackup, otherwise default to true<br/>
1034+
* 2) The withBackup flag designated as true from the API, will throw an error if system setting for ForceNoBackup
1035+
* 3) The withbackup flag designated as false from the API, will throw an error if system setting for ForceBackup
1036+
*
1037+
* @param withBackup API parameter from the client side
1038+
* @param request HTTP Request
1039+
* @return boolean of metadata backup and defaulted to true
1040+
*/
1041+
private boolean withDeleteBackup (Boolean withBackup, HttpServletRequest request) {
1042+
ResourceBundle messages = ApiUtils.getMessagesResourceBundle(request.getLocales());
1043+
String mdDeleteBackupOption = settingManager.getValue(Settings.METADATA_DELETE_BACKUPOPTIONS);
1044+
if (withBackup == null) {
1045+
if (StringUtils.equals(mdDeleteBackupOption, "ForceNoBackup")) {
1046+
return false;
1047+
}
1048+
// Will default to true if setting set to UseAPIParameter, ForceBackup or empty
1049+
return true;
1050+
} else if (withBackup == true) {
1051+
if (StringUtils.equals(mdDeleteBackupOption, "ForceNoBackup")) {
1052+
throw new IllegalArgumentException(
1053+
String.format(messages.getString("api.metadata.delete.errorForceBackup"), withBackup));
1054+
}
1055+
return true;
1056+
} else if (withBackup = false) {
1057+
if (StringUtils.equals(mdDeleteBackupOption, "ForceBackup")) {
1058+
throw new IllegalArgumentException(
1059+
String.format(messages.getString("api.metadata.delete.errorForceNoBackup"), withBackup));
1060+
}
1061+
return false;
1062+
}
1063+
1064+
return true;
1065+
1066+
}
1067+
10291068
}

web-ui/src/main/resources/catalog/locales/en-admin.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,10 @@
907907
"metadata/delete": "Metadata delete",
908908
"metadata/delete/profilePublishedMetadata": "Minimum user profile allowed to delete published metadata",
909909
"metadata/delete/profilePublishedMetadata-help": "Minimum user profile allowed to delete published metadata (Editor, Reviewer or Administrator). The default value is Editor.",
910+
"metadata/delete/enablebackup": "Enable Backup",
911+
"metadata/delete/enablebackup-help": "Backup metadata on the server before deleting process.",
912+
"metadata/delete/backupOptions": "Backup Options",
913+
"metadata/delete/backupOptions-help": "Set to override backup option from the API client parameter during deletion process. Force backup: always makes a backup. Ignores the API parameter; Force no backup: never makes a backup. Ignores the API parameter; Use API parameter: If provided, uses the value provided. If not provided, it makes a backup (current default behaviour).",
910914
"metadata/batchediting": "Metadata Batch Editing",
911915
"metadata/batchediting/accesslevel": "Select the minimum user profile allowed to access batch editing",
912916
"metadata/batchediting/accesslevel-help": "Select the minimum user profile allowed to access batch editing (Editor, Reviewer or Administrator). The default value is Editor.",
@@ -1562,6 +1566,9 @@
15621566
"doiserver-euApiText": "Publication Office of the European Union",
15631567
"confirmDoiServerDelete": "Are you sure you want to delete this DOI server?",
15641568
"NoTranslationProvider": "No translation provider",
1569+
"ForceBackup": "Force Backup",
1570+
"ForceNoBackup": "Force No Backup",
1571+
"UseAPIParameter": "Use API Parameter",
15651572
"LibreTranslate": "Libretranslate",
15661573
"userHistory": "User history",
15671574
"userHistoryRevision": "Updated by {{revisionUser}} on {{revisionDate}}:",

web-ui/src/main/resources/catalog/templates/admin/settings/system.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,28 @@ <h3>{{section2.name | translate}}</h3>
222222
</option>
223223
</select>
224224

225+
<select
226+
data-ng-switch-when="metadata/delete/backupOptions"
227+
class="form-control"
228+
name="{{s.name}}"
229+
>
230+
<option
231+
value="ForceBackup"
232+
ng-selected="'ForceBackup' == s.value"
233+
>
234+
{{'ForceBackup' | translate}}
235+
</option>
236+
<option
237+
value="ForceNoBackup"
238+
ng-selected="'ForceNoBackup' == s.value"
239+
>
240+
{{'ForceNoBackup' | translate}}
241+
</option>
242+
<option value="UseAPIParameter" ng-selected="'UseAPIParameter' == s.value">
243+
{{'UseAPIParameter' | translate}}
244+
</option>
245+
</select>
246+
225247
<select
226248
data-ng-switch-when="metadata/publication/profilePublishMetadata"
227249
class="form-control"

web/src/main/webapp/WEB-INF/classes/setup/sql/data/data-db-default.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metada
709709
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/history/accesslevel', 'Editor', 0, 12021, 'n');
710710

711711
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/delete/profilePublishedMetadata', 'Editor', 0, 12011, 'n');
712+
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/delete/backupOptions', 'UseAPIParameter', 0, 12012, 'n');
712713

713714
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/publication/profilePublishMetadata', 'Reviewer', 0, 12021, 'n');
714715
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/publication/profileUnpublishMetadata', 'Reviewer', 0, 12022, 'n');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
UPDATE Settings SET value='4.4.8' WHERE name='system/platform/version';
22
UPDATE Settings SET value='SNAPSHOT' WHERE name='system/platform/subVersion';
3+
4+
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/delete/backupOptions', 'UseAPIParameter', 0, 12012, 'n');

0 commit comments

Comments
 (0)