Skip to content

Commit ca07d19

Browse files
committed
feat: add UNCHANGED state handling in SettingsServiceBean operations #11639
- Introduced `UNCHANGED` as a new operation type for settings. - Updated `convertToJson` method to include unchanged settings in JSON output. - Enhanced `replaceAllSettings` logic to track unchanged settings when content remains identical. - The reason for this is idempotency: if nothing changed, nothing should be done. Replacing the present content of an existing setting is unnecessary, wasting CPU cycles, and we also want the admin to be informed about what actually is getting changed. Before, all of these unchanged elements would be counted as "updated".
1 parent 069ec88 commit ca07d19

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/main/java/edu/harvard/iq/dataverse/settings/SettingsServiceBean.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,28 +1191,32 @@ static Set<Setting> convertJsonToSettings(JsonObject settings) {
11911191
static enum Op {
11921192
UPDATED,
11931193
CREATED,
1194-
DELETED;
1194+
DELETED,
1195+
UNCHANGED;
11951196

11961197
static JsonObjectBuilder convertToJson(Map<Setting, Op> operationalDetails) {
11971198
// Create a nice represenation of what happened as Json
11981199
JsonObjectBuilder jbo = Json.createObjectBuilder();
11991200
JsonArrayBuilder created = Json.createArrayBuilder();
12001201
JsonArrayBuilder updated = Json.createArrayBuilder();
12011202
JsonArrayBuilder deleted = Json.createArrayBuilder();
1203+
JsonArrayBuilder unchanged = Json.createArrayBuilder();
12021204

12031205
operationalDetails.forEach((setting, op) -> {
12041206
String name = convertToJsonKey(setting);
12051207
switch (op) {
12061208
case CREATED -> created.add(name);
12071209
case UPDATED -> updated.add(name);
12081210
case DELETED -> deleted.add(name);
1211+
case UNCHANGED -> unchanged.add(name);
12091212
}
12101213
});
12111214

12121215
return jbo
12131216
.add("created", created)
12141217
.add("updated", updated)
1215-
.add("deleted", deleted);
1218+
.add("deleted", deleted)
1219+
.add("unchanged", unchanged);
12161220
}
12171221
}
12181222

@@ -1268,10 +1272,14 @@ public Map<Setting, Op> replaceAllSettings(Set<Setting> newSettings) {
12681272
// Setting exists in both - update with new values
12691273
} else {
12701274
Setting newSetting = newByKey.get(key);
1271-
// We use the already managed entity and update it with the content of the new setting.
1272-
// (This means we don't need to call em.merge(), the ORM will track and execute it for us.)
1273-
existingSetting.setContent(newSetting.getContent());
1274-
opsTracking.put(existingSetting, Op.UPDATED);
1275+
if (existingSetting.getContent().equals(newSetting.getContent())) {
1276+
opsTracking.put(existingSetting, Op.UNCHANGED);
1277+
} else {
1278+
// We use the already managed entity and update it with the content of the new setting.
1279+
// (This means we don't need to call em.merge(), the ORM will track and execute it for us.)
1280+
existingSetting.setContent(newSetting.getContent());
1281+
opsTracking.put(existingSetting, Op.UPDATED);
1282+
}
12751283
}
12761284
}
12771285

0 commit comments

Comments
 (0)