@@ -35,7 +35,21 @@ public static Set<String> getThumbnailsFilesIds(JsonObject jsonDocument) {
35
35
return thumbIds ;
36
36
}
37
37
38
- static void replaceAll (JsonObject jsonDocument , Map <String , String > oldFileIdForNewFileId ) {
38
+ public static void replaceAll (JsonObject jsonDocument , Map <String , String > oldFileIdForNewFileId ) {
39
+ replaceAll (jsonDocument , oldFileIdForNewFileId , false );
40
+ }
41
+
42
+ /**
43
+ * Map all files ID in a Document to another "new" ID.
44
+ * Useful when cloning a document.
45
+ * @param jsonDocument
46
+ * @param oldFileIdForNewFileId Map of (old,new) IDs
47
+ * @param skipMissingThumbnails When truthy, drop thumbnail files that are not in the map.
48
+ */
49
+ public static void replaceAll (
50
+ JsonObject jsonDocument , Map <String , String > oldFileIdForNewFileId ,
51
+ boolean skipMissingThumbnails
52
+ ) {
39
53
// replace file id
40
54
Set <String > fileIds = getFileId (jsonDocument );
41
55
for (String fileId : fileIds ) {
@@ -44,32 +58,17 @@ static void replaceAll(JsonObject jsonDocument, Map<String, String> oldFileIdFor
44
58
}
45
59
setFileId (jsonDocument , oldFileIdForNewFileId .get (fileId ));
46
60
}
47
- // replace thumb values
61
+ // replace or drop thumb values
48
62
fileIds = getThumbnailsFilesIds (jsonDocument );
49
63
for (String fileId : fileIds ) {
50
64
if (!oldFileIdForNewFileId .containsKey (fileId )) {
51
- throw new IllegalStateException ("Could not found newFileId of the file:" + fileId );
52
- }
53
- replaceThumbnailFileId (jsonDocument , fileId , oldFileIdForNewFileId .get (fileId ));
54
- }
55
- }
56
-
57
- /**
58
- * Clean a document, by removing any reference to a file (id is given),
59
- * in the `file` and `thumbnails` fields.
60
- *
61
- * @param jsonDocument document to clean.
62
- * @param fileIds IDs of file to be removed from `file` or `thumbnails` fields.
63
- */
64
- static void removeAll (JsonObject jsonDocument , Collection <String > fileIds ) {
65
- final JsonObject thumbnails = jsonDocument .getJsonObject ("thumbnails" , null );
66
- final Set <Map .Entry <String ,Object >> thumbnailEntries = thumbnails ==null ? null : thumbnails .getMap ().entrySet ();
67
- for (String fileId : fileIds ) {
68
- if ( fileId == null ) continue ;
69
- if (getFileId (jsonDocument ).contains (fileId )) {
70
- jsonDocument .remove ("file" );
71
- } else if (thumbnailEntries !=null ) {
72
- thumbnailEntries .removeIf (entry -> entry !=null && entry .getValue ()!=null && fileId .equals (entry .getValue ().toString ()));
65
+ if (skipMissingThumbnails ) {
66
+ dropThumbnailFileId (jsonDocument , fileId );
67
+ } else {
68
+ throw new IllegalStateException ("Could not found newFileId of the file:" + fileId );
69
+ }
70
+ } else {
71
+ replaceThumbnailFileId (jsonDocument , fileId , oldFileIdForNewFileId .get (fileId ));
73
72
}
74
73
}
75
74
}
@@ -95,6 +94,18 @@ static void replaceThumbnailFileId(JsonObject jsonDocument, String fileId, Strin
95
94
jsonDocument .put ("thumbnails" , meta );
96
95
}
97
96
97
+ static void dropThumbnailFileId (JsonObject jsonDocument , String fileId ) {
98
+ JsonObject meta = jsonDocument .getJsonObject ("thumbnails" , new JsonObject ());
99
+ for (String key : meta .fieldNames ()) {
100
+ String value = meta .getString (key );
101
+ if (value != null && value .equals (fileId )) {
102
+ meta .remove (key );
103
+ break ;
104
+ }
105
+ }
106
+ jsonDocument .put ("thumbnails" , meta );
107
+ }
108
+
98
109
public static List <String > getListOfFileIds (JsonObject jsonDocument ) {
99
110
List <String > listOfFilesIds = new ArrayList <>(getFileId (jsonDocument ));
100
111
listOfFilesIds .addAll (getThumbnailsFilesIds (jsonDocument ));
0 commit comments