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.mets.model.converter;
20  
21  import java.io.IOException;
22  
23  import org.mycore.mets.model.simple.MCRMetsLink;
24  import org.mycore.mets.model.simple.MCRMetsPage;
25  import org.mycore.mets.model.simple.MCRMetsSection;
26  
27  import com.google.gson.TypeAdapter;
28  import com.google.gson.stream.JsonReader;
29  import com.google.gson.stream.JsonWriter;
30  
31  /**
32   * This is a helper class to help GSON to convert simple model to JSON.
33   * @author Sebastian Hofmann(mcrshofm)
34   */
35  public class MCRMetsLinkTypeAdapter extends TypeAdapter<MCRMetsLink> {
36  
37      @Override
38      public void write(JsonWriter jsonWriter, MCRMetsLink metsLink) throws IOException {
39          jsonWriter.beginObject();
40          jsonWriter.name("from").value(metsLink.getFrom().getId());
41          jsonWriter.name("to").value(metsLink.getTo().getId());
42          jsonWriter.endObject();
43      }
44  
45      @Override
46      public MCRMetsLink read(JsonReader jsonReader) throws IOException {
47          MCRMetsLinkPlaceholder ml = new MCRMetsLinkPlaceholder();
48  
49          jsonReader.beginObject();
50          while (jsonReader.hasNext()) {
51              switch (jsonReader.nextName()) {
52                  case "from":
53                      ml.setFromString(jsonReader.nextString());
54                      break;
55                  case "to":
56                      ml.setToString(jsonReader.nextString());
57                      break;
58              }
59          }
60          jsonReader.endObject();
61  
62          return ml;
63      }
64  
65      protected class MCRMetsLinkPlaceholder extends MCRMetsLink {
66          public static final String PLACEHOLDER_EXCEPTION_MESSAGE = "this is a placeholder class";
67  
68          private String fromString;
69  
70          private String toString;
71  
72          public String getFromString() {
73              return fromString;
74          }
75  
76          public void setFromString(String fromString) {
77              this.fromString = fromString;
78          }
79  
80          public String getToString() {
81              return toString;
82          }
83  
84          public void setToString(String toString) {
85              this.toString = toString;
86          }
87  
88          @Override
89          public MCRMetsPage getTo() {
90              throw new RuntimeException(PLACEHOLDER_EXCEPTION_MESSAGE);
91          }
92  
93          @Override
94          public MCRMetsSection getFrom() {
95              throw new RuntimeException(PLACEHOLDER_EXCEPTION_MESSAGE);
96          }
97  
98          @Override
99          public void setFrom(MCRMetsSection from) {
100             throw new RuntimeException(PLACEHOLDER_EXCEPTION_MESSAGE);
101         }
102 
103         @Override
104         public void setTo(MCRMetsPage to) {
105             throw new RuntimeException(PLACEHOLDER_EXCEPTION_MESSAGE);
106         }
107     }
108 }