diff --git a/core/src/main/java/org/fao/geonet/kernel/setting/Settings.java b/core/src/main/java/org/fao/geonet/kernel/setting/Settings.java index f5ee47213660..be9c8253d779 100644 --- a/core/src/main/java/org/fao/geonet/kernel/setting/Settings.java +++ b/core/src/main/java/org/fao/geonet/kernel/setting/Settings.java @@ -144,6 +144,7 @@ public class Settings { public static final String METADATA_HISTORY_ENABLED = "metadata/history/enabled"; public static final String METADATA_HISTORY_ACCESS_LEVEL = "metadata/history/accesslevel"; public static final String METADATA_PUBLISHED_DELETE_USERPROFILE = "metadata/delete/profilePublishedMetadata"; + public static final String METADATA_DELETE_BACKUPOPTIONS = "metadata/delete/backupOptions"; public static final String METADATA_PUBLISH_USERPROFILE = "metadata/publication/profilePublishMetadata"; public static final String METADATA_UNPUBLISH_USERPROFILE = "metadata/publication/profileUnpublishMetadata"; public static final String METADATA_BACKUPARCHIVE_ENABLE = "metadata/backuparchive/enable"; diff --git a/core/src/test/resources/org/fao/geonet/api/Messages.properties b/core/src/test/resources/org/fao/geonet/api/Messages.properties index f8bf6b86e795..f6851d1431a2 100644 --- a/core/src/test/resources/org/fao/geonet/api/Messages.properties +++ b/core/src/test/resources/org/fao/geonet/api/Messages.properties @@ -246,6 +246,8 @@ api.metadata.share.ErrorUserNotAllowedToPublish=User not allowed to publish the api.metadata.share.strategy.groupOwnerOnly=You need to be administrator, or reviewer of the metadata group. api.metadata.share.strategy.reviewerInGroup=You need to be administrator, or reviewer of the metadata group or reviewer with edit privilege on the metadata. 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. +api.metadata.delete.errorForceBackup=Cannot force the backup during deletion process because the client specified with backup parameter as: '%s'. +api.metadata.delete.errorForceNoBackup=Cannot force the no-backup during deletion process because the client specified with backup parameter as: '%s'. feedback_subject_userFeedback=User feedback diff --git a/services/src/main/java/org/fao/geonet/api/records/MetadataInsertDeleteApi.java b/services/src/main/java/org/fao/geonet/api/records/MetadataInsertDeleteApi.java index ad25403313c6..a4f7a56f2519 100644 --- a/services/src/main/java/org/fao/geonet/api/records/MetadataInsertDeleteApi.java +++ b/services/src/main/java/org/fao/geonet/api/records/MetadataInsertDeleteApi.java @@ -215,8 +215,10 @@ public class MetadataInsertDeleteApi { @ResponseStatus(HttpStatus.NO_CONTENT) public void deleteRecord( @Parameter(description = API_PARAM_RECORD_UUID, required = true) @PathVariable String metadataUuid, - @Parameter(description = API_PARAM_BACKUP_FIRST, required = false) @RequestParam(required = false, defaultValue = "true") boolean withBackup, + @Parameter(description = API_PARAM_BACKUP_FIRST, required = false) @RequestParam(required = false) Boolean withBackup, HttpServletRequest request) throws Exception { + boolean doBackup = withDeleteBackup(withBackup, request); + AbstractMetadata metadata = ApiUtils.canEditRecord(metadataUuid, request); ServiceContext context = ApiUtils.createServiceContext(request); Store store = context.getBean("resourceStore", Store.class); @@ -225,7 +227,7 @@ public void deleteRecord( ApplicationContextHolder.get().publishEvent(preRemoveEvent); if (metadata.getDataInfo().getType() != MetadataType.SUB_TEMPLATE - && metadata.getDataInfo().getType() != MetadataType.TEMPLATE_OF_SUB_TEMPLATE && withBackup) { + && metadata.getDataInfo().getType() != MetadataType.TEMPLATE_OF_SUB_TEMPLATE && doBackup) { MEFLib.backupRecord(metadata, context); } @@ -1026,4 +1028,41 @@ private Pair loadRecord(MetadataType metadataType, Element xmlE return Pair.read(Integer.valueOf(id.get(0)), uuid); } + /** + * Determine if the metadata backup is needed during delete API.
+ * 1) The withBackup flag missing from the API, will only overide if setting as ForceNoBackup, otherwise default to true
+ * 2) The withBackup flag designated as true from the API, will throw an error if system setting for ForceNoBackup + * 3) The withbackup flag designated as false from the API, will throw an error if system setting for ForceBackup + * + * @param withBackup API parameter from the client side + * @param request HTTP Request + * @return boolean of metadata backup and defaulted to true + */ + private boolean withDeleteBackup (Boolean withBackup, HttpServletRequest request) { + ResourceBundle messages = ApiUtils.getMessagesResourceBundle(request.getLocales()); + String mdDeleteBackupOption = settingManager.getValue(Settings.METADATA_DELETE_BACKUPOPTIONS); + if (withBackup == null) { + if (StringUtils.equals(mdDeleteBackupOption, "ForceNoBackup")) { + return false; + } + // Will default to true if setting set to UseAPIParameter, ForceBackup or empty + return true; + } else if (withBackup == true) { + if (StringUtils.equals(mdDeleteBackupOption, "ForceNoBackup")) { + throw new IllegalArgumentException( + String.format(messages.getString("api.metadata.delete.errorForceBackup"), withBackup)); + } + return true; + } else if (withBackup = false) { + if (StringUtils.equals(mdDeleteBackupOption, "ForceBackup")) { + throw new IllegalArgumentException( + String.format(messages.getString("api.metadata.delete.errorForceNoBackup"), withBackup)); + } + return false; + } + + return true; + + } + } diff --git a/web-ui/src/main/resources/catalog/locales/en-admin.json b/web-ui/src/main/resources/catalog/locales/en-admin.json index 92dc8474aea1..f077757b12f6 100644 --- a/web-ui/src/main/resources/catalog/locales/en-admin.json +++ b/web-ui/src/main/resources/catalog/locales/en-admin.json @@ -881,6 +881,10 @@ "metadata/delete": "Metadata delete", "metadata/delete/profilePublishedMetadata": "Minimum user profile allowed to delete published metadata", "metadata/delete/profilePublishedMetadata-help": "Minimum user profile allowed to delete published metadata (Editor, Reviewer or Administrator). The default value is Editor.", + "metadata/delete/enablebackup": "Enable Backup", + "metadata/delete/enablebackup-help": "Backup metadata on the server before deleting process.", + "metadata/delete/backupOptions": "Backup Options", + "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).", "metadata/batchediting": "Metadata Batch Editing", "metadata/batchediting/accesslevel": "Select the minimum user profile allowed to access batch editing", "metadata/batchediting/accesslevel-help": "Select the minimum user profile allowed to access batch editing (Editor, Reviewer or Administrator). The default value is Editor.", @@ -1536,6 +1540,9 @@ "doiserver-euApiText": "Publication Office of the European Union", "confirmDoiServerDelete": "Are you sure you want to delete this DOI server?", "NoTranslationProvider": "No translation provider", + "ForceBackup": "Force Backup", + "ForceNoBackup": "Force No Backup", + "UseAPIParameter": "Use API Parameter", "LibreTranslate": "Libretranslate", "userHistory": "User history", "userHistoryRevision": "Updated by {{revisionUser}} on {{revisionDate}}:", diff --git a/web-ui/src/main/resources/catalog/templates/admin/settings/system.html b/web-ui/src/main/resources/catalog/templates/admin/settings/system.html index bedc6b202550..d24f2d42f298 100755 --- a/web-ui/src/main/resources/catalog/templates/admin/settings/system.html +++ b/web-ui/src/main/resources/catalog/templates/admin/settings/system.html @@ -222,6 +222,28 @@

{{section2.name | translate}}

+ +