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.common.MCRException;
24  import org.mycore.mets.model.simple.MCRMetsAltoLink;
25  
26  import com.google.gson.TypeAdapter;
27  import com.google.gson.stream.JsonReader;
28  import com.google.gson.stream.JsonWriter;
29  
30  /**
31   * This is a helper class to help GSON to convert simple model to JSON.
32   *
33   * @author Sebastian Hofmann(mcrshofm)
34   */
35  public class MCRAltoLinkTypeAdapter extends TypeAdapter<MCRMetsAltoLink> {
36  
37      @Override
38      public void write(JsonWriter jsonWriter, MCRMetsAltoLink altoLink) throws IOException {
39          jsonWriter.beginObject();
40          jsonWriter.name("altoFile").value(altoLink.getFile().getId());
41          jsonWriter.name("begin").value(altoLink.getBegin());
42          jsonWriter.name("end").value(altoLink.getEnd());
43          jsonWriter.endObject();
44      }
45  
46      @Override
47      public MCRMetsAltoLink read(JsonReader jsonReader) throws IOException {
48          String fileID = null;
49          String begin = null;
50          String end = null;
51  
52          jsonReader.beginObject();
53          while (jsonReader.hasNext()) {
54              switch (jsonReader.nextName()) {
55                  case "altoFile":
56                      fileID = jsonReader.nextString();
57                      break;
58                  case "begin":
59                      begin = jsonReader.nextString();
60                      break;
61                  case "end":
62                      end = jsonReader.nextString();
63                      break;
64              }
65          }
66          jsonReader.endObject();
67  
68          if (fileID == null || begin == null || end == null) {
69              throw new MCRException("Cannot read MCRMetsAltoLink! FileID && begin && end expected!");
70          }
71          return new MCRAltoLinkPlaceHolder(fileID, begin, end);
72      }
73  
74      /**
75       * Created by sebastian on 25.11.15.
76       */
77      public static class MCRAltoLinkPlaceHolder extends MCRMetsAltoLink {
78          public static final String PLACEHOLDER_EXCEPTION_MESSAGE = "this is a placeholder class";
79  
80          private String fileID;
81  
82          public MCRAltoLinkPlaceHolder(String fileID, String begin, String end) {
83              super(null, begin, end);
84              this.fileID = fileID;
85          }
86  
87          public String getFileID() {
88              return fileID;
89          }
90  
91          public void setFileID(String fileID) {
92              this.fileID = fileID;
93          }
94      }
95  }