Skip to content

Commit 2a6e4b5

Browse files
ianwallenwangf1122
andauthored
[Backport 4.2.x] setting option to enable/disable backup during delete record (#8728) (#8784)
* 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 Co-authored-by: wangf1122 <74916635+wangf1122@users.noreply.github.com>
1 parent 16c14a0 commit 2a6e4b5

7 files changed

Lines changed: 76 additions & 2 deletions

File tree

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
@@ -145,6 +145,7 @@ public class Settings {
145145
public static final String METADATA_HISTORY_ENABLED = "metadata/history/enabled";
146146
public static final String METADATA_HISTORY_ACCESS_LEVEL = "metadata/history/accesslevel";
147147
public static final String METADATA_PUBLISHED_DELETE_USERPROFILE = "metadata/delete/profilePublishedMetadata";
148+
public static final String METADATA_DELETE_BACKUPOPTIONS = "metadata/delete/backupOptions";
148149
public static final String METADATA_PUBLISH_USERPROFILE = "metadata/publication/profilePublishMetadata";
149150
public static final String METADATA_UNPUBLISH_USERPROFILE = "metadata/publication/profileUnpublishMetadata";
150151
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
@@ -243,5 +243,7 @@ api.metadata.share.ErrorUserNotAllowedToPublish=User not allowed to publish the
243243
api.metadata.share.strategy.groupOwnerOnly=You need to be administrator, or reviewer of the metadata group.
244244
api.metadata.share.strategy.reviewerInGroup=You need to be administrator, or reviewer of the metadata group or reviewer with edit privilege on the metadata.
245245
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.
246+
api.metadata.delete.errorForceBackup=Cannot force the backup during deletion process because the client specified with backup parameter as: '%s'.
247+
api.metadata.delete.errorForceNoBackup=Cannot force the no-backup during deletion process because the client specified with backup parameter as: '%s'.
246248

247249
feedback_subject_userFeedback=User feedback

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
@@ -213,8 +213,10 @@ public class MetadataInsertDeleteApi {
213213
@ResponseStatus(HttpStatus.NO_CONTENT)
214214
public void deleteRecord(
215215
@Parameter(description = API_PARAM_RECORD_UUID, required = true) @PathVariable String metadataUuid,
216-
@Parameter(description = API_PARAM_BACKUP_FIRST, required = false) @RequestParam(required = false, defaultValue = "true") boolean withBackup,
216+
@Parameter(description = API_PARAM_BACKUP_FIRST, required = false) @RequestParam(required = false) Boolean withBackup,
217217
HttpServletRequest request) throws Exception {
218+
boolean doBackup = withDeleteBackup(withBackup, request);
219+
218220
AbstractMetadata metadata = ApiUtils.canEditRecord(metadataUuid, request);
219221
ServiceContext context = ApiUtils.createServiceContext(request);
220222
Store store = context.getBean("resourceStore", Store.class);
@@ -223,7 +225,7 @@ public void deleteRecord(
223225
ApplicationContextHolder.get().publishEvent(preRemoveEvent);
224226

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

@@ -1017,4 +1019,41 @@ private Pair<Integer, String> loadRecord(MetadataType metadataType, Element xmlE
10171019
return Pair.read(Integer.valueOf(id.get(0)), uuid);
10181020
}
10191021

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

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,10 @@
890890
"metadata/delete": "Metadata delete",
891891
"metadata/delete/profilePublishedMetadata": "Minimum user profile allowed to delete published metadata",
892892
"metadata/delete/profilePublishedMetadata-help": "Minimum user profile allowed to delete published metadata (Editor, Reviewer or Administrator). The default value is Editor.",
893+
"metadata/delete/enablebackup": "Enable Backup",
894+
"metadata/delete/enablebackup-help": "Backup metadata on the server before deleting process.",
895+
"metadata/delete/backupOptions": "Backup Options",
896+
"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).",
893897
"metadata/batchediting": "Metadata Batch Editing",
894898
"metadata/batchediting/accesslevel": "Select the minimum user profile allowed to access batch editing",
895899
"metadata/batchediting/accesslevel-help": "Select the minimum user profile allowed to access batch editing (Editor, Reviewer or Administrator). The default value is Editor.",
@@ -1505,6 +1509,9 @@
15051509
"es.index": "Index name",
15061510
"systemPropertiesProxyConfiguration": "Using http proxy settings in system properties.",
15071511
"NoTranslationProvider": "No translation provider",
1512+
"ForceBackup": "Force Backup",
1513+
"ForceNoBackup": "Force No Backup",
1514+
"UseAPIParameter": "Use API Parameter",
15081515
"LibreTranslate": "Libretranslate"
15091516
}
15101517

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
@@ -710,6 +710,7 @@ INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metada
710710
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/history/accesslevel', 'Editor', 0, 12021, 'n');
711711

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

714715
INSERT INTO Settings (name, value, datatype, position, internal) VALUES ('metadata/publication/profilePublishMetadata', 'Reviewer', 0, 12021, 'n');
715716
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.2.13' 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)