1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.frontend.classeditor;
20
21 import static org.mycore.frontend.classeditor.json.MCRJSONCategoryHelper.PROP_HAS_CHILDREN;
22 import static org.mycore.frontend.classeditor.json.MCRJSONCategoryHelper.PROP_HAS_LINK;
23 import static org.mycore.frontend.classeditor.json.MCRJSONCategoryHelper.PROP_ID;
24 import static org.mycore.frontend.classeditor.json.MCRJSONCategoryHelper.PROP_LABELS;
25 import static org.mycore.frontend.classeditor.json.MCRJSONCategoryHelper.PROP_URISTR;
26
27 import java.lang.reflect.Type;
28 import java.net.URI;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.SortedSet;
33
34 import org.mycore.common.MCRJSONTypeAdapter;
35 import org.mycore.datamodel.classifications2.MCRCategory;
36 import org.mycore.datamodel.classifications2.MCRCategoryID;
37 import org.mycore.datamodel.classifications2.MCRLabel;
38 import org.mycore.frontend.classeditor.json.MCRJSONCategory;
39 import org.mycore.frontend.classeditor.wrapper.MCRCategoryListWrapper;
40 import org.mycore.frontend.classeditor.wrapper.MCRLabelSetWrapper;
41
42 import com.google.gson.JsonArray;
43 import com.google.gson.JsonDeserializationContext;
44 import com.google.gson.JsonElement;
45 import com.google.gson.JsonObject;
46 import com.google.gson.JsonParseException;
47 import com.google.gson.JsonSerializationContext;
48
49 import jakarta.ws.rs.WebApplicationException;
50
51 public class MCRCategoryListTypeAdapter extends MCRJSONTypeAdapter<MCRCategoryListWrapper> {
52 private JsonSerializationContext serializationContext;
53
54 @Override
55 public JsonElement serialize(MCRCategoryListWrapper categListWrapper, Type typeOfSrc,
56 JsonSerializationContext context) {
57 this.serializationContext = context;
58 Map<MCRCategoryID, Boolean> linkMap = categListWrapper.getLinkMap();
59
60 if (linkMap == null) {
61 throw new WebApplicationException("For serializing link map must not be null.");
62 }
63
64 return categListToJsonArray(categListWrapper.getList(), linkMap);
65 }
66
67 private JsonElement categListToJsonArray(List<MCRCategory> categList, Map<MCRCategoryID, Boolean> linkMap) {
68 JsonArray categJsonArray = new JsonArray();
69 for (MCRCategory categ : categList) {
70 Boolean hasLink = linkMap.get(categ.getId());
71 JsonElement element = createCategRefJSONObj(categ, hasLink);
72 categJsonArray.add(element);
73 }
74
75 return categJsonArray;
76 }
77
78 private JsonElement createCategRefJSONObj(MCRCategory categ, Boolean hasLink) {
79 JsonObject categRefJsonObject = new JsonObject();
80 categRefJsonObject.add(PROP_ID, serializationContext.serialize(categ.getId()));
81 SortedSet<MCRLabel> labels = categ.getLabels();
82 categRefJsonObject.add(PROP_LABELS, serializationContext.serialize(new MCRLabelSetWrapper(labels)));
83 URI uri = categ.getURI();
84 if (uri != null) {
85 categRefJsonObject.addProperty(PROP_URISTR, uri.toString());
86 }
87 categRefJsonObject.addProperty(PROP_HAS_CHILDREN, categ.hasChildren());
88 categRefJsonObject.addProperty(PROP_HAS_LINK, hasLink);
89 return categRefJsonObject;
90 }
91
92 @Override
93 public MCRCategoryListWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
94 throws JsonParseException {
95 List<MCRCategory> categList = new ArrayList<>();
96
97 for (JsonElement categRef : json.getAsJsonArray()) {
98 JsonObject categRefJsonObject = categRef.getAsJsonObject();
99
100 MCRCategory categ = context.deserialize(categRefJsonObject, MCRJSONCategory.class);
101 categList.add(categ);
102 }
103
104 return new MCRCategoryListWrapper(categList);
105 }
106 }