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.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.jdom2.Element;
27  import org.mycore.datamodel.classifications2.MCRCategoryID;
28  
29  /**
30   * This holds information about file metadata that is stored in derivate xml
31   * 
32   * @author Thomas Scheffler (yagee)
33   * @author shermann
34   * @see MCRObjectDerivate
35   */
36  public class MCRFileMetadata implements Comparable<MCRFileMetadata> {
37  
38      private Collection<MCRCategoryID> categories;
39  
40      private String urn, handle;
41  
42      private String name;
43  
44      public MCRFileMetadata(Element file) {
45          this.name = file.getAttributeValue("name");
46          this.urn = file.getChildText("urn");
47          this.handle = file.getChildText("handle");
48  
49          List<Element> categoryElements = file.getChildren("category");
50          this.categories = Collections.emptySet();
51          if (!categoryElements.isEmpty()) {
52              categories = new ArrayList<>(categoryElements.size());
53              for (Element categElement : categoryElements) {
54                  MCRCategoryID categId = MCRCategoryID.fromString(categElement.getAttributeValue("id"));
55                  categories.add(categId);
56              }
57          }
58      }
59  
60      public MCRFileMetadata() {
61          this(null, null, null);
62      }
63  
64      public MCRFileMetadata(String name, String urn, Collection<MCRCategoryID> categories) {
65          super();
66          setName(name);
67          setUrn(urn);
68          setHandle(null);
69          setCategories(categories);
70      }
71  
72      public MCRFileMetadata(String name, String urn, String handle, Collection<MCRCategoryID> categories) {
73          this(name, urn, categories);
74          setHandle(handle);
75      }
76  
77      public Element createXML() {
78          Element file = new Element("file");
79          file.setAttribute("name", name);
80          if (urn != null) {
81              Element urn = new Element("urn");
82              urn.setText(this.urn);
83              file.addContent(urn);
84          }
85          if (handle != null) {
86              Element handle = new Element("handle");
87              handle.setText(this.handle);
88              file.addContent(handle);
89          }
90          for (MCRCategoryID categid : categories) {
91              Element category = new Element("category");
92              category.setAttribute("id", categid.toString());
93              file.addContent(category);
94          }
95          return file;
96      }
97  
98      public Collection<MCRCategoryID> getCategories() {
99          return Collections.unmodifiableCollection(categories);
100     }
101 
102     public void setCategories(Collection<MCRCategoryID> categories) {
103         if (categories == null || categories.isEmpty()) {
104             this.categories = Collections.emptySet();
105         } else {
106             this.categories = new ArrayList<>(categories);
107         }
108     }
109 
110     public String getUrn() {
111         return urn;
112     }
113 
114     public void setUrn(String urn) {
115         this.urn = urn;
116     }
117 
118     /**
119      * @return the handle
120      */
121     public String getHandle() {
122         return handle;
123     }
124 
125     /**
126      * @param handle the handle to set
127      */
128     public void setHandle(String handle) {
129         this.handle = handle;
130     }
131 
132     public String getName() {
133         return name;
134     }
135 
136     public void setName(String name) {
137         this.name = name;
138     }
139 
140     @Override
141     public int compareTo(MCRFileMetadata o) {
142         return this.name.compareTo(o.name);
143     }
144 }