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.apache.logging.log4j.LogManager; 24 import org.apache.logging.log4j.Logger; 25 import org.jdom2.Element; 26 import org.mycore.common.MCRException; 27 import org.mycore.datamodel.classifications2.MCRCategoryID; 28 29 import com.google.gson.JsonObject; 30 31 /** 32 * This class implements all method for handling with the MCRMetaClassification 33 * part of a metadata object. The MCRMetaClassification class present a link to 34 * a category of a classification. 35 * <p> 36 * <tag class="MCRMetaClassification" heritable="..."> <br> 37 * <subtag classid="..." categid="..." /> <br> 38 * </tag> <br> 39 * 40 * @author Jens Kupferschmidt 41 * @version $Revision$ $Date: 2008-03-18 22:53:44 +0000 (Di, 18 Mrz 42 * 2008) $ 43 */ 44 public class MCRMetaClassification extends MCRMetaDefault { 45 private static final Logger LOGGER = LogManager.getLogger(); 46 47 // MCRMetaClassification data 48 protected MCRCategoryID category; 49 50 /** 51 * This is the constructor. <br> 52 * The language element was set to <b>en </b>. The classid and categid value 53 * was set to an empty string. 54 */ 55 public MCRMetaClassification() { 56 super(); 57 } 58 59 /** 60 * This is the constructor. <br> 61 * The language element was set to <b>en </b>. The subtag element was set to 62 * the value of <em>subtag</em>. If the 63 * value of <em>subtag</em> is null or empty an exception was throwed. 64 * The type element was set to an empty string. 65 * the <em>classid</em> and the <em>categid</em> must be not null 66 * or empty! 67 * @param subtag the name of the subtag 68 * @param inherted a value >= 0 69 * @param type the type attribute 70 * @param classid the classification ID 71 * @param categid the category ID 72 * 73 * @exception MCRException if the subtag value, the classid value or 74 * the categid are null, empty, too long or not a MCRObjectID 75 */ 76 public MCRMetaClassification(String subtag, int inherted, String type, String classid, 77 String categid) throws MCRException { 78 super(subtag, null, type, inherted); 79 setValue(classid, categid); 80 } 81 82 /** 83 * This is the constructor. <br> 84 * The language element was set to <b>en </b>. The subtag element was set to 85 * the value of <em>subtag</em>. If the 86 * value of <em>subtag</em> is null or empty an exception was throwed. 87 * The type element was set to an empty string. 88 * the <em>classid</em> and the <em>categid</em> must be not null 89 * or empty! 90 * @param subtag the name of the subtag 91 * @param inherted a value >= 0 92 * @param type the type attribute 93 * @param category a category id 94 * 95 * @exception MCRException if the subtag value is empty, too long or not a MCRObjectID 96 */ 97 public MCRMetaClassification(String subtag, int inherted, String type, MCRCategoryID category) 98 throws MCRException { 99 super(subtag, null, type, inherted); 100 if (category == null) { 101 throw new MCRException("Category is not set in " + getSubTag()); 102 } 103 this.category = category; 104 } 105 106 /** 107 * The method return the classification ID. 108 * 109 * @return the classId 110 */ 111 public final String getClassId() { 112 return category.getRootID(); 113 } 114 115 /** 116 * The method return the category ID. 117 * 118 * @return the categId 119 */ 120 public final String getCategId() { 121 return category.getID(); 122 } 123 124 /** 125 * This method set values of classid and categid. 126 * 127 * @param classid 128 * the classification ID 129 * @param categid 130 * the category ID 131 * @exception MCRException 132 * if the classid value or the categid are null, 133 * empty, too long or not a MCRObjectID 134 */ 135 public final void setValue(String classid, String categid) throws MCRException { 136 category = new MCRCategoryID(classid, categid); 137 } 138 139 /** 140 * This method read the XML input stream part from a DOM part for the 141 * metadata of the document. 142 * 143 * @param element 144 * a relevant JDOM element for the metadata 145 * @exception MCRException 146 * if the classid value or the categid are null, 147 * empty, too long or not a MCRObjectID 148 */ 149 @Override 150 public void setFromDOM(Element element) throws MCRException { 151 super.setFromDOM(element); 152 153 String classid = element.getAttributeValue("classid"); 154 String categid = element.getAttributeValue("categid"); 155 setValue(classid, categid); 156 } 157 158 /** 159 * This method create a XML stream for all data in this class, defined by 160 * the MyCoRe XML MCRMetaClassification definition for the given subtag. 161 * 162 * @exception MCRException 163 * if the content of this class is not valid 164 * @return a JDOM Element with the XML MCRClassification part 165 */ 166 @Override 167 public Element createXML() throws MCRException { 168 Element elm = super.createXML(); 169 elm.setAttribute("classid", getClassId()); 170 elm.setAttribute("categid", getCategId()); 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 * classid: "mycore_class_00000001", 182 * categid: "category1" 183 * } 184 * </pre> 185 * 186 */ 187 @Override 188 public JsonObject createJSON() { 189 JsonObject obj = super.createJSON(); 190 obj.addProperty("classid", category.getRootID()); 191 obj.addProperty("categid", category.getID()); 192 return obj; 193 } 194 195 /** 196 * Validates this MCRMetaClassification. This method throws an exception if: 197 * <ul> 198 * <li>the subtag is not null or empty</li> 199 * <li>the lang value was supported</li> 200 * <li>the inherited value is lower than zero</li> 201 * <li>the category is null</li> 202 * </ul> 203 * 204 * @throws MCRException the MCRMetaClassification is invalid 205 */ 206 public void validate() throws MCRException { 207 super.validate(); 208 if (category == null) { 209 throw new MCRException(getSubTag() + ": category is not yet set"); 210 } 211 } 212 213 /** 214 * clone of this instance 215 * 216 * you will get a (deep) clone of this element 217 * 218 * @see java.lang.Object#clone() 219 */ 220 @Override 221 public MCRMetaClassification clone() { 222 MCRMetaClassification clone = (MCRMetaClassification) super.clone(); 223 224 clone.category = this.category; // immutable so shallow copy ok 225 226 return clone; 227 } 228 229 /** 230 * This method put debug data to the logger (for the debug mode). 231 */ 232 @Override 233 public void debug() { 234 if (LOGGER.isDebugEnabled()) { 235 super.debugDefault(); 236 LOGGER.debug("Category = {}", category); 237 LOGGER.debug(" "); 238 } 239 } 240 241 @Override 242 public boolean equals(Object obj) { 243 if (!super.equals(obj)) { 244 return false; 245 } 246 final MCRMetaClassification other = (MCRMetaClassification) obj; 247 return Objects.equals(this.category, other.category); 248 } 249 250 }