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.mods.classification;
20  
21  import org.mycore.datamodel.classifications2.MCRCategoryDAOFactory;
22  import org.mycore.datamodel.classifications2.MCRCategoryID;
23  import org.w3c.dom.Element;
24  
25  /**
26   * Authority information that is a static mapping for mods:typeOfResource. This element is always mapped to a
27   * classification with the ID typeOfResource.
28   * 
29   * @author Frank L\u00FCtzenkirchen
30   */
31  class MCRTypeOfResource extends MCRAuthorityInfo {
32  
33      /**
34       * The name of the MODS element typeOfResource, which is same as the classification ID used to map the codes to
35       * categories.
36       */
37      public static final String TYPE_OF_RESOURCE = "typeOfResource";
38  
39      /**
40       * The mods:typeOfResource code, which is same as the category ID
41       */
42      private String code;
43  
44      MCRTypeOfResource(String code) {
45          this.code = code;
46      }
47  
48      /**
49       * If the given element is mods:typeOfResource, returns the MCRTypeOfResource mapping.
50       */
51      public static MCRTypeOfResource getAuthorityInfo(org.jdom2.Element modsElement) {
52          if (modsElement == null) {
53              return null;
54          }
55          String name = modsElement.getName();
56          String code = modsElement.getTextTrim();
57          return getTypeOfResource(name, code);
58      }
59  
60      /**
61       * If the given element is mods:typeOfResource, returns the MCRTypeOfResource mapping.
62       */
63      public static MCRTypeOfResource getAuthorityInfo(Element modsElement) {
64          if (modsElement == null) {
65              return null;
66          }
67          String name = modsElement.getLocalName();
68          String code = MCRMODSClassificationSupport.getText(modsElement).trim();
69          return getTypeOfResource(name, code);
70      }
71  
72      /**
73       * If the given element name is typeOfResource, returns the MCRTypeOfResource mapping.
74       */
75      private static MCRTypeOfResource getTypeOfResource(String name, String code) {
76          return (name.equals(TYPE_OF_RESOURCE) && isClassificationPresent()) ? new MCRTypeOfResource(code) : null;
77      }
78  
79      @Override
80      public String toString() {
81          return TYPE_OF_RESOURCE + "#" + code;
82      }
83  
84      @Override
85      protected MCRCategoryID lookupCategoryID() {
86          return new MCRCategoryID(TYPE_OF_RESOURCE, code.replace(" ", "_")); // Category IDs can not contain spaces
87      }
88  
89      @Override
90      public void setInElement(org.jdom2.Element element) {
91          element.setText(code);
92  
93      }
94  
95      @Override
96      public void setInElement(Element element) {
97          element.setTextContent(code);
98      }
99  
100     public static boolean isClassificationPresent() {
101         return MCRCategoryDAOFactory.getInstance().exist(MCRCategoryID.rootID(TYPE_OF_RESOURCE));
102     }
103 }