28
28
public class RestrictedFieldProcessor {
29
29
30
30
private static final String VALUE = "value" ;
31
+ private static final String ID = "id" ;
31
32
32
33
private final CaseAccessService caseAccessService ;
33
34
@@ -124,52 +125,87 @@ private JsonNode processCollectionFields(CaseFieldDefinition subFieldDefinition,
124
125
JsonNode sanitizedArrayNode ,
125
126
JsonNode existingArrayNode ,
126
127
Set <String > accessProfileNames ) {
128
+ ArrayNode sanitizedArray = initializeSanitizedArray (sanitizedArrayNode );
129
+ ArrayNode existingArray = (ArrayNode ) existingArrayNode ;
130
+
131
+ for (JsonNode existingItem : existingArray ) {
132
+ processExistingItem (subFieldDefinition , sanitizedArray , existingItem , accessProfileNames );
133
+ }
134
+
135
+ return sanitizedArray ;
136
+ }
127
137
128
- ArrayNode sanitizedArray = sanitizedArrayNode != null && sanitizedArrayNode .isArray ()
138
+ private ArrayNode initializeSanitizedArray (JsonNode sanitizedArrayNode ) {
139
+ return sanitizedArrayNode != null && sanitizedArrayNode .isArray ()
129
140
? (ArrayNode ) sanitizedArrayNode .deepCopy ()
130
141
: JsonNodeFactory .instance .arrayNode ();
142
+ }
131
143
132
- ArrayNode existingArray = (ArrayNode ) existingArrayNode ;
144
+ private void processExistingItem (CaseFieldDefinition subFieldDefinition ,
145
+ ArrayNode sanitizedArray ,
146
+ JsonNode existingItem ,
147
+ Set <String > accessProfileNames ) {
148
+ JsonNode existingItemId = existingItem .get (ID );
133
149
134
- for (JsonNode existingItem : existingArray ) {
135
- JsonNode existingItemId = existingItem .get ("id" );
150
+ Optional <JsonNode > matchingNewItem = findMatchingNewItem (sanitizedArray , existingItemId );
151
+
152
+ if (matchingNewItem .isEmpty ()) {
153
+ handleMissingItem (subFieldDefinition , sanitizedArray , existingItem , accessProfileNames , existingItemId );
154
+ } else {
155
+ processMatchingItem (subFieldDefinition , matchingNewItem .get (), existingItem , accessProfileNames );
156
+ }
157
+ }
136
158
137
- Optional <JsonNode > matchingNewItem = StreamSupport .stream (sanitizedArray .spliterator (), false )
138
- .filter (newItem -> !isNullId (newItem ) && newItem .get ("id" ).equals (existingItemId ))
139
- .findFirst ();
159
+ private Optional <JsonNode > findMatchingNewItem (ArrayNode sanitizedArray , JsonNode existingItemId ) {
160
+ return StreamSupport .stream (sanitizedArray .spliterator (), false )
161
+ .filter (newItem -> !isNullId (newItem ) && newItem .get (ID ).equals (existingItemId ))
162
+ .findFirst ();
163
+ }
140
164
141
- if (matchingNewItem .isEmpty ()) {
142
- log .debug ("Missing collection item with ID '{}' under '{}'." , existingItemId ,
143
- subFieldDefinition .getId ());
165
+ private void handleMissingItem (CaseFieldDefinition subFieldDefinition ,
166
+ ArrayNode sanitizedArray ,
167
+ JsonNode existingItem ,
168
+ Set <String > accessProfileNames ,
169
+ JsonNode existingItemId ) {
170
+ log .debug ("Missing collection item with ID '{}' under '{}'." , existingItemId ,
171
+ subFieldDefinition .getId ());
172
+
173
+ if (isCreateWithoutReadAllowed (subFieldDefinition .getAccessControlLists (), accessProfileNames )) {
174
+ log .info ("Adding missing collection item with ID '{}' under '{}'." , existingItemId ,
175
+ subFieldDefinition .getId ());
176
+ sanitizedArray .add (existingItem );
177
+ }
178
+ }
144
179
145
- if (isCreateWithoutReadAllowed (subFieldDefinition .getAccessControlLists (), accessProfileNames )) {
146
- log .info ("Adding missing collection item with ID '{}' under '{}'." , existingItemId ,
147
- subFieldDefinition .getId ());
148
- sanitizedArray .add (existingItem );
149
- }
150
- } else {
151
- JsonNode newValueField = matchingNewItem .get ().get (VALUE );
152
- JsonNode existingValueField = existingItem .get (VALUE );
153
-
154
- if (existingValueField != null ) {
155
- JsonNode processedValueField ;
156
-
157
- if (existingValueField .isObject ()) {
158
- processedValueField = processSubFieldsRecursively (subFieldDefinition ,
159
- newValueField ,
160
- existingValueField ,
161
- accessProfileNames );
162
- } else {
163
- processedValueField = processSimpleValueField (
164
- subFieldDefinition , newValueField , existingValueField , accessProfileNames );
165
- }
166
-
167
- ((ObjectNode ) matchingNewItem .get ()).set (VALUE , processedValueField );
168
- }
169
- }
180
+ private void processMatchingItem (CaseFieldDefinition subFieldDefinition ,
181
+ JsonNode matchingNewItem ,
182
+ JsonNode existingItem ,
183
+ Set <String > accessProfileNames ) {
184
+ JsonNode newValueField = matchingNewItem .get (VALUE );
185
+ JsonNode existingValueField = existingItem .get (VALUE );
186
+
187
+ if (existingValueField != null ) {
188
+ JsonNode processedValueField = processValueField (subFieldDefinition , newValueField , existingValueField ,
189
+ accessProfileNames );
190
+ ((ObjectNode ) matchingNewItem ).set (VALUE , processedValueField );
170
191
}
192
+ }
171
193
172
- return sanitizedArray ;
194
+ private JsonNode processValueField (CaseFieldDefinition subFieldDefinition ,
195
+ JsonNode newValueField ,
196
+ JsonNode existingValueField ,
197
+ Set <String > accessProfileNames ) {
198
+ if (existingValueField .isObject ()) {
199
+ return processSubFieldsRecursively (subFieldDefinition ,
200
+ newValueField ,
201
+ existingValueField ,
202
+ accessProfileNames );
203
+ } else {
204
+ return processSimpleValueField (subFieldDefinition ,
205
+ newValueField ,
206
+ existingValueField ,
207
+ accessProfileNames );
208
+ }
173
209
}
174
210
175
211
private JsonNode processSimpleValueField (CaseFieldDefinition subFieldDefinition , JsonNode newValueField ,
@@ -188,9 +224,9 @@ private JsonNode processSimpleValueField(CaseFieldDefinition subFieldDefinition,
188
224
}
189
225
190
226
private boolean isNullId (JsonNode newItem ) {
191
- return newItem .get ("id" ) == null
192
- || newItem .get ("id" ).equals (NullNode .getInstance ())
193
- || "null" .equalsIgnoreCase (newItem .get ("id" ).asText ());
227
+ return newItem .get (ID ) == null
228
+ || newItem .get (ID ).equals (NullNode .getInstance ())
229
+ || "null" .equalsIgnoreCase (newItem .get (ID ).asText ());
194
230
}
195
231
196
232
private boolean isCreateWithoutReadAllowed (List <AccessControlList > fieldAccessControlLists ,
0 commit comments