View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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 }