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_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_PARENTID;
26  import static org.mycore.frontend.classeditor.json.MCRJSONCategoryHelper.PROP_POSITION;
27  import static org.mycore.frontend.classeditor.json.MCRJSONCategoryHelper.PROP_URISTR;
28  
29  import java.lang.reflect.Type;
30  import java.net.URI;
31  import java.util.List;
32  import java.util.Map;
33  import java.util.SortedSet;
34  
35  import org.mycore.common.MCRJSONTypeAdapter;
36  import org.mycore.common.config.MCRConfiguration2;
37  import org.mycore.datamodel.classifications2.MCRCategLinkService;
38  import org.mycore.datamodel.classifications2.MCRCategLinkServiceFactory;
39  import org.mycore.datamodel.classifications2.MCRCategory;
40  import org.mycore.datamodel.classifications2.MCRCategoryID;
41  import org.mycore.datamodel.classifications2.MCRLabel;
42  import org.mycore.frontend.classeditor.json.MCRJSONCategory;
43  import org.mycore.frontend.classeditor.wrapper.MCRCategoryListWrapper;
44  import org.mycore.frontend.classeditor.wrapper.MCRLabelSetWrapper;
45  
46  import com.google.gson.JsonDeserializationContext;
47  import com.google.gson.JsonElement;
48  import com.google.gson.JsonObject;
49  import com.google.gson.JsonParseException;
50  import com.google.gson.JsonSerializationContext;
51  
52  public class MCRCategoryTypeAdapter extends MCRJSONTypeAdapter<MCRJSONCategory> {
53      private MCRCategLinkService linkService;
54  
55      @Override
56      public MCRJSONCategory deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
57          throws JsonParseException {
58          JsonObject categJsonObject = json.getAsJsonObject();
59          MCRJSONCategory deserializedCateg = new MCRJSONCategory();
60  
61          JsonElement idJsonElement = categJsonObject.get(PROP_ID);
62          if (idJsonElement != null) {
63              MCRCategoryID id = context.deserialize(idJsonElement, MCRCategoryID.class);
64              deserializedCateg.setId(id);
65          }
66  
67          JsonElement parentIdJsonElement = categJsonObject.get(PROP_PARENTID);
68          if (parentIdJsonElement != null) {
69              MCRCategoryID parentId = context.deserialize(parentIdJsonElement, MCRCategoryID.class);
70              deserializedCateg.setParentID(parentId);
71          }
72  
73          JsonElement positionJsonElem = categJsonObject.get(PROP_POSITION);
74          if (positionJsonElem != null) {
75              deserializedCateg.setPositionInParent(positionJsonElem.getAsInt());
76          }
77  
78          JsonElement labelSetWrapperElem = categJsonObject.get(PROP_LABELS);
79          if (labelSetWrapperElem != null) {
80              MCRLabelSetWrapper labelSetWrapper = context.deserialize(labelSetWrapperElem, MCRLabelSetWrapper.class);
81              deserializedCateg.setLabels(labelSetWrapper.getSet());
82          }
83  
84          JsonElement uriJsonElement = categJsonObject.get(PROP_URISTR);
85          if (uriJsonElement != null) {
86              String uriStr = uriJsonElement.getAsString();
87              deserializedCateg.setURI(URI.create(uriStr));
88          }
89  
90          return deserializedCateg;
91      }
92  
93      @Override
94      public JsonElement serialize(MCRJSONCategory category, Type arg1, JsonSerializationContext contextSerialization) {
95          JsonObject rubricJsonObject = new JsonObject();
96          MCRCategoryID id = category.getId();
97          if (id != null) {
98              rubricJsonObject.add(PROP_ID, contextSerialization.serialize(id));
99          }
100 
101         SortedSet<MCRLabel> labels = category.getLabels();
102         rubricJsonObject.add(PROP_LABELS, contextSerialization.serialize(new MCRLabelSetWrapper(labels)));
103         URI uri = category.getURI();
104         if (uri != null) {
105             rubricJsonObject.addProperty(PROP_URISTR, uri.toString());
106         }
107 
108         if (category.hasChildren()) {
109             List<MCRCategory> children = category.getChildren();
110             Map<MCRCategoryID, Boolean> linkMap = getLinkService().hasLinks(category);
111             if (linkMap.containsValue(true)) {
112                 rubricJsonObject.addProperty(PROP_HAS_LINK, true);
113             }
114             rubricJsonObject.add(PROP_CHILDREN,
115                 contextSerialization.serialize(new MCRCategoryListWrapper(children, linkMap)));
116         }
117 
118         return rubricJsonObject;
119     }
120 
121     private MCRCategLinkService getLinkService() {
122         if (linkService == null) {
123             linkService = MCRConfiguration2.<MCRCategLinkService>getInstanceOf("Category.Link.Service")
124                 .orElseGet(MCRCategLinkServiceFactory::getInstance);
125         }
126 
127         return linkService;
128     }
129 }