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  
23  import org.jdom2.Element;
24  import org.mycore.common.MCRException;
25  
26  import com.fasterxml.jackson.annotation.JsonClassDescription;
27  import com.fasterxml.jackson.annotation.JsonIgnore;
28  
29  /**
30   * This class implements all method for special handling with the MCRMetaLink
31   * part of a metadata object. The MCRMetaLinkID class present two types. At once
32   * a reference only of a other MCRObject. At second a bidirectional link between
33   * two MCRObject's. Optional you can append the reference with the label
34   * attribute. See to W3C XLink Standard for more informations.
35   * <p>
36   * &lt;tag class="MCRMetaLinkID"&gt;
37   * <br>
38   * &lt;subtag xlink:type="locator" xlink:href=" <em>MCRObjectID</em>"
39   * xlink:label="..." xlink:title="..."/&gt; <br>
40   * &lt;subtag xlink:type="arc" xlink:from=" <em>MCRObjectID</em>"
41   * xlink:to="MCRObjectID"/&gt; <br>
42   * &lt;/tag&gt; <br>
43   *
44   * @author Jens Kupferschmidt
45   * @version $Revision$ $Date$
46   */
47  @JsonClassDescription("Links to other objects or derivates")
48  public class MCRMetaLinkID extends MCRMetaLink {
49      /**
50       * initializes with empty values.
51       */
52      public MCRMetaLinkID() {
53          super();
54      }
55  
56      /**
57       * initializes with given values.
58       * @see MCRMetaLink#MCRMetaLink(String, int)
59       */
60      public MCRMetaLinkID(String subtag, int inherted) {
61          super(subtag, inherted);
62      }
63  
64      /**
65       * initializes with all values needed to link to an MCRObject.
66       *
67       *  This is the same as running {@link #MCRMetaLinkID(String, int)} and
68       *  {@link #setReference(MCRObjectID, String, String)}.
69       */
70      public MCRMetaLinkID(String subtag, MCRObjectID id, String label, String title) {
71          this(subtag, id, label, title, null);
72      }
73  
74      /**
75       * initializes with all values needed to link to an MCRObject.
76       *
77       */
78      public MCRMetaLinkID(String subtag, MCRObjectID id, String label, String title, String role) {
79          this(subtag, 0);
80          setReference(id, label, title);
81          setXLinkRole(role);
82      }
83  
84      /**
85       * This method set a reference with xlink:href, xlink:label and xlink:title.
86       *
87       * @param href
88       *            the reference as MCRObjectID string
89       * @param label
90       *            the new label string
91       * @param title
92       *            the new title string
93       * @exception MCRException
94       *                if the set_href value is null, empty or not a MCRObjectID
95       */
96      @Override
97      public final void setReference(String href, String label, String title) throws MCRException {
98          try {
99              MCRObjectID hrefid = MCRObjectID.getInstance(href);
100             super.setReference(hrefid.toString(), label, title);
101         } catch (Exception e) {
102             throw new MCRException("The href value is not a MCRObjectID: " + href, e);
103         }
104     }
105 
106     /**
107      * This method set a reference with xlink:href, xlink:label and xlink:title.
108      *
109      * @param href
110      *            the reference as MCRObjectID
111      * @param label
112      *            the new label string
113      * @param title
114      *            the new title string
115      * @exception MCRException
116      *                if the href value is null, empty or not a MCRObjectID
117      */
118     public final void setReference(MCRObjectID href, String label, String title) throws MCRException {
119         if (href == null) {
120             throw new MCRException("The href value is null.");
121         }
122 
123         super.setReference(href.toString(), label, title);
124     }
125 
126     /**
127      * This method set a bidirectional link with xlink:from, xlink:to and
128      * xlink:title.
129      *
130      * @param from
131      *            the source MCRObjectID string
132      * @param to
133      *            the target MCRObjectID string
134      * @param title
135      *            the new title string
136      * @exception MCRException
137      *                if the from or to element is not a MCRObjectId
138      */
139     @Override
140     public final void setBiLink(String from, String to, String title) throws MCRException {
141         try {
142             MCRObjectID fromid = MCRObjectID.getInstance(from);
143             MCRObjectID toid = MCRObjectID.getInstance(to);
144             super.setBiLink(fromid.toString(), toid.toString(), title);
145         } catch (Exception e) {
146             linktype = null;
147             throw new MCRException("The from/to value is not a MCRObjectID.");
148         }
149     }
150 
151     /**
152      * This method set a bidirectional link with xlink:from, xlink:to and
153      * xlink:title.
154      *
155      * @param from
156      *            the source MCRObjectID
157      * @param to
158      *            the target MCRObjectID
159      * @param title
160      *            the new title string
161      * @exception MCRException
162      *                if the from or to element is not a MCRObjectId
163      */
164     public final void setBiLink(MCRObjectID from, MCRObjectID to, String title) throws MCRException {
165         if (from == null || to == null) {
166             throw new MCRException("The from/to value is null.");
167         }
168 
169         super.setBiLink(from.toString(), to.toString(), title);
170     }
171 
172     /**
173      * This method get the xlink:href element as MCRObjectID.
174      *
175      * @return the xlink:href element as MCRObjectID
176      */
177     @JsonIgnore
178     public final MCRObjectID getXLinkHrefID() {
179         return MCRObjectID.getInstance(href);
180     }
181 
182     /**
183      * This method get the xlink:from element as MCRObjectID.
184      *
185      * @return the xlink:from element as MCRObjectID
186      */
187     @JsonIgnore
188     public final MCRObjectID getXLinkFromID() {
189         return MCRObjectID.getInstance(from);
190     }
191 
192     /**
193      * This method get the xlink:to element as MCRObjectID.
194      *
195      * @return the xlink:to element as MCRObjectID
196      */
197     @JsonIgnore
198     public final MCRObjectID getXLinkToID() {
199         return MCRObjectID.getInstance(to);
200     }
201 
202     /**
203      * This method read the XML input stream part from a DOM part for the
204      * metadata of the document.
205      *
206      * @param element
207      *            a relevant DOM element for the metadata
208      * @exception MCRException
209      *                if the xlink:type is not locator or arc or if href or from
210      *                and to are not a MCRObjectID
211      */
212     @Override
213     public void setFromDOM(Element element) {
214         super.setFromDOM(element);
215 
216         if (linktype.equals("locator")) {
217             try {
218                 MCRObjectID hrefid = MCRObjectID.getInstance(href);
219                 href = hrefid.toString();
220             } catch (Exception e) {
221                 throw new MCRException("The xlink:href is not a MCRObjectID.");
222             }
223         } else {
224             try {
225                 MCRObjectID fromid = MCRObjectID.getInstance(from);
226                 from = fromid.toString();
227             } catch (Exception e) {
228                 throw new MCRException("The xlink:from is not a MCRObjectID.");
229             }
230 
231             try {
232                 MCRObjectID toid = MCRObjectID.getInstance(to);
233                 to = toid.toString();
234             } catch (Exception e) {
235                 throw new MCRException("The xlink:to is not a MCRObjectID.");
236             }
237         }
238     }
239 
240     @Override
241     public boolean equals(Object obj) {
242         if (!super.equals(obj)) {
243             return false;
244         }
245         MCRMetaLinkID other = (MCRMetaLinkID) obj;
246         if (!Objects.equals(from, other.from)) {
247             return false;
248         } else if (!Objects.equals(href, other.href)) {
249             return false;
250         } else if (!Objects.equals(label, other.label)) {
251             return false;
252         } else if (!Objects.equals(linktype, other.linktype)) {
253             return false;
254         } else if (!Objects.equals(role, other.role)) {
255             return false;
256         } else if (!Objects.equals(title, other.title)) {
257             return false;
258         } else {
259             return Objects.equals(to, other.to);
260         }
261     }
262 
263     @Override
264     public MCRMetaLinkID clone() {
265         return (MCRMetaLinkID) super.clone();
266     }
267 
268     @Override
269     public final String toString() {
270         return getXLinkHref();
271     }
272 }