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.datamodel.metadata;
20  
21  import java.util.Objects;
22  import java.util.Optional;
23  
24  import org.jdom2.Element;
25  import org.mycore.common.MCRException;
26  
27  import com.google.gson.JsonObject;
28  
29  /**
30   * This class implements all method for handling the IFS metadata. The
31   * MCRMetaIFS class present all informations to store and retrieve derivates to
32   * the IFS.
33   * <p>
34   * &lt;tag class="MCRMetaIFS" &gt; <br>
35   * &lt;subtag sourcepath="..." maindoc="..." ifsid="..." /&gt; <br>
36   * &lt;/tag&gt; <br>
37   * 
38   * @author Jens Kupferschmidt
39   * @version $Revision$ $Date$
40   */
41  public final class MCRMetaIFS extends MCRMetaDefault {
42      // MCRMetaIFS data
43      private String sourcePath;
44  
45      private String maindoc;
46  
47      /**
48       * This is the constructor. <br>
49       * The language element was set to <b>en </b>. All other data was set to
50       * empty.
51       */
52      public MCRMetaIFS() {
53          super();
54          sourcePath = "";
55          maindoc = "";
56      }
57  
58      /**
59       * This is the constructor. <br>
60       * The language element was set to <b>en </b>. The subtag element was set to
61       * the value of <em>subtag</em>. If the
62       * value of <em>subtag</em> is null or empty an exception was throwed.
63       * The type element was set empty.
64       * The sourcepath must be NOT null or empty.
65       * @param subtag       the name of the subtag
66       * @param sourcePath   the sourcepath attribute
67       * @exception MCRException if the subtag value, the set_classid value or
68       * the set_categid are null, empty, too long or not a MCRObjectID
69       */
70      public MCRMetaIFS(String subtag, String sourcePath) throws MCRException {
71          super(subtag, null, null, 0);
72          setSourcePath(sourcePath);
73          maindoc = "";
74      }
75  
76      /**
77       * The method return the derivate source path.
78       * 
79       * @return the sourcepath
80       */
81      public String getSourcePath() {
82          return sourcePath;
83      }
84  
85      /**
86       * The method return the derivate main document name.
87       * 
88       * @return the main document name.
89       */
90      public String getMainDoc() {
91          return maindoc;
92      }
93  
94      /**
95       * The method return the derivate IFS ID.
96       * 
97       * @return the IFS ID.
98       * @deprecated will always return empty String
99       */
100     @Deprecated
101     public String getIFSID() {
102         return "";
103     }
104 
105     /**
106      * This method set the value of derivate source path.
107      * 
108      * @param sourcePath
109      *            the derivate source path
110      */
111     public void setSourcePath(String sourcePath) {
112         this.sourcePath = sourcePath;
113     }
114 
115     /**
116      * This method set the value of derivate main document.
117      * 
118      * @param mainDoc
119      *            the derivate main document name
120      */
121     public void setMainDoc(String mainDoc) {
122         if (mainDoc == null) {
123             maindoc = "";
124         } else {
125             maindoc = mainDoc.startsWith("/") ? mainDoc.substring(1) : mainDoc;
126         }
127     }
128 
129     /**
130      * This method set the value of derivate IFS ID.
131      * 
132      * @param ifsId
133      *            the derivate IFS ID
134      * @deprecated out of use
135      */
136     @Deprecated
137     public void setIFSID(String ifsId) {
138     }
139 
140     /**
141      * This method read the XML input stream part from a DOM part for the
142      * metadata of the document.
143      * 
144      * @param element
145      *            a relevant JDOM element for the metadata
146      * @exception MCRException
147      *                if the set_sourcepath value is null or empty
148      */
149     @Override
150     public void setFromDOM(Element element) throws MCRException {
151         super.setFromDOM(element);
152         setSourcePath(element.getAttributeValue("sourcepath"));
153         setMainDoc(element.getAttributeValue("maindoc"));
154     }
155 
156     /**
157      * This method create a XML stream for all data in this class, defined by
158      * the MyCoRe XML MCRMetaIFS definition for the given subtag.
159      * 
160      * @exception MCRException
161      *                if the content of this class is not valid
162      * @return a JDOM Element with the XML MCRClassification part
163      */
164     @Override
165     public Element createXML() throws MCRException {
166         Element elm = super.createXML();
167         if (sourcePath != null) {
168             elm.setAttribute("sourcepath", sourcePath);
169         }
170         elm.setAttribute("maindoc", maindoc);
171 
172         return elm;
173     }
174 
175     /**
176      * Creates the JSON representation. Extends the {@link MCRMetaDefault#createJSON()} method
177      * with the following data.
178      * 
179      * <pre>
180      *   {
181      *     sourcepath: "...",
182      *     maindoc: "image.tif",
183      *     ifsid: "ve3s8a3j00xsfk8z"
184      *   }
185      * </pre>
186      * 
187      */
188     @Override
189     public JsonObject createJSON() {
190         JsonObject obj = super.createJSON();
191         if (sourcePath != null) {
192             obj.addProperty("sourcepath", sourcePath);
193         }
194         obj.addProperty("maindoc", maindoc);
195         return obj;
196     }
197 
198     /**
199      * Validates this MCRMetaIFS. This method throws an exception if:
200      * <ul>
201      * <li>the subtag is not null or empty</li>
202      * <li>the lang value was supported</li>
203      * <li>the inherited value is lower than zero</li>
204      * <li>the trimmed sourcepath is null empty</li>
205      * </ul>
206      * 
207      * @throws MCRException the MCRMetaIFS is invalid
208      */
209     public void validate() throws MCRException {
210         super.validate();
211         if (sourcePath == null) {
212             return;
213         }
214         sourcePath = Optional.of(sourcePath)
215             .map(String::trim)
216             .orElseThrow(() -> new MCRException(getSubTag() + ": sourcepath is empty"));
217     }
218 
219     @Override
220     public MCRMetaIFS clone() {
221         MCRMetaIFS clone = (MCRMetaIFS) super.clone();
222 
223         clone.maindoc = this.maindoc;
224         clone.sourcePath = this.sourcePath;
225 
226         return clone;
227     }
228 
229     @Override
230     public boolean equals(Object obj) {
231         if (!super.equals(obj)) {
232             return false;
233         }
234         final MCRMetaIFS other = (MCRMetaIFS) obj;
235         return Objects.equals(sourcePath, other.sourcePath) && Objects.equals(maindoc, other.maindoc);
236     }
237 
238 }