|
16 | 16 | import jakarta.persistence.EntityManager; |
17 | 17 | import jakarta.persistence.PersistenceContext; |
18 | 18 |
|
| 19 | +import jakarta.transaction.Transactional; |
19 | 20 | import org.json.JSONArray; |
20 | 21 | import org.json.JSONException; |
21 | 22 | import org.json.JSONObject; |
22 | 23 |
|
| 24 | +import java.util.ArrayList; |
23 | 25 | import java.util.Arrays; |
24 | 26 | import java.util.Collections; |
25 | 27 | import java.util.HashMap; |
@@ -1077,6 +1079,31 @@ public JsonObject listAllAsJson() { |
1077 | 1079 | return response.build(); |
1078 | 1080 | } |
1079 | 1081 |
|
| 1082 | + /** |
| 1083 | + * Updates all settings by replacing them with the settings provided in the given JSON object. |
| 1084 | + * This method validates the keys and values from the JSON object, converts them into |
| 1085 | + * a list of Setting objects, and performs an atomic update of the internal settings. |
| 1086 | + * |
| 1087 | + * @param settings a JsonObject containing the new settings to apply. |
| 1088 | + * Each key corresponds to a setting name, and each value corresponds |
| 1089 | + * to its respective value. The keys and values will be validated before |
| 1090 | + * applying the updates. |
| 1091 | + * @throws IllegalArgumentException if the JSON object contains invalid keys or invalid settings. |
| 1092 | + */ |
| 1093 | + public void setAllFromJson(JsonObject settings) { |
| 1094 | + // Validate the input |
| 1095 | + List<String> invalidKeys = validateKeys(settings); |
| 1096 | + if (!invalidKeys.isEmpty()) { |
| 1097 | + throw new IllegalArgumentException("Invalid key(s): " + String.join(", ", invalidKeys)); |
| 1098 | + } |
| 1099 | + |
| 1100 | + // Convert JSON to Setting objects |
| 1101 | + List<Setting> newSettings = convertJsonToSettings(settings); |
| 1102 | + |
| 1103 | + // Perform atomic update (replace all settings) |
| 1104 | + replaceAllSettings(newSettings); |
| 1105 | + } |
| 1106 | + |
1080 | 1107 | /** |
1081 | 1108 | * Converts a JSON object representing settings into a list of Setting objects. |
1082 | 1109 | * Each entry in the JSON object is processed to create a Setting instance. |
@@ -1112,6 +1139,21 @@ static List<Setting> convertJsonToSettings(JsonObject settings) { |
1112 | 1139 | .collect(Collectors.toList()); |
1113 | 1140 | } |
1114 | 1141 |
|
| 1142 | + /** |
| 1143 | + * Replaces all existing settings with a new list of settings within a single transaction. |
| 1144 | + * The operation is atomic, ensuring that either all changes are applied or none in case of a failure. |
| 1145 | + * |
| 1146 | + * @param newSettings the list of new {@link Setting} objects to replace the existing settings. |
| 1147 | + * Must not be null; an empty list clears all settings. |
| 1148 | + */ |
| 1149 | + @Transactional |
| 1150 | + public void replaceAllSettings(List<Setting> newSettings) { |
| 1151 | + // Implementation for atomic replacement |
| 1152 | + // This would involve clearing existing settings and inserting new ones |
| 1153 | + // within the same transaction |
| 1154 | + throw new IllegalStateException("Not yet implemented"); |
| 1155 | + } |
| 1156 | + |
1115 | 1157 | public Map<String, String> getBaseMetadataLanguageMap(Map<String,String> languageMap, boolean refresh) { |
1116 | 1158 | if (languageMap == null || refresh) { |
1117 | 1159 | languageMap = new HashMap<String, String>(); |
|
0 commit comments