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 org.apache.logging.log4j.LogManager; 22 import org.apache.logging.log4j.Logger; 23 import org.jdom2.Element; 24 import org.mycore.common.MCRException; 25 26 import com.google.gson.JsonObject; 27 28 /** 29 * This class implements all method for handling with the MCRMetaBoolean part of 30 * a metadata object. The MCRMetaBoolean class present a logical value of true 31 * or false and optional a type. 32 * <p> 33 * <tag class="MCRMetaBoolean" heritable="..."> <br> 34 * <subtag type="..."> <br> 35 * true|false<br> 36 * </subtag> <br> 37 * </tag> <br> 38 * 39 * @author Jens Kupferschmidt 40 */ 41 public final class MCRMetaBoolean extends MCRMetaDefault { 42 43 private boolean value; 44 45 private static final Logger LOGGER = LogManager.getLogger(); 46 47 /** 48 * This is the constructor. <br> 49 * The language element was set to <b>en </b>. The boolean value was set to 50 * false. 51 */ 52 public MCRMetaBoolean() { 53 super(); 54 value = false; 55 } 56 57 /** 58 * This is the constructor. <br> 59 * The language element was set to <b>en </b>. The subtag element was set to 60 * the value of <em>subtag</em>. If the value of <em>subtag</em> 61 * is null or empty an exception was thrown. The type element was set to 62 * the value of <em>type</em>, if it is null, an empty string was set 63 * to the type element. The boolean string <em>value</em> 64 * was set to a boolean element, if it is null, false was set. 65 * @param subtag the name of the subtag 66 * @param lang the language 67 * @param type the optional type string 68 * @param inherted a value >= 0 69 * @param value the boolean value (true or false) as string 70 * 71 * @exception MCRException if the subtag value is null or empty 72 */ 73 @Deprecated 74 public MCRMetaBoolean(String subtag, String lang, String type, int inherted, String value) 75 throws MCRException { 76 this(subtag, type, inherted, false); 77 setValue(value); 78 } 79 80 /** 81 * This is the constructor. <br> 82 * The language element was set to <b>en </b>. The subtag element was set to 83 * the value of <em>subtag</em>. If the value of <em>subtag</em> 84 * is null or empty an exception was thrown. The type element was set to 85 * the value of <em>type</em>, if it is null, an empty string was set 86 * to the type element. The boolean string <em>value</em> 87 * was set to a boolean element, if it is null, false was set. 88 * @param subtag the name of the subtag 89 * @param type the optional type string 90 * @param inherted a value >= 0 91 * @param value the boolean value (true or false) as string 92 * 93 * @exception MCRException if the subtag value is null or empty 94 */ 95 public MCRMetaBoolean(String subtag, String type, int inherted, String value) 96 throws MCRException { 97 super(subtag, null, type, inherted); 98 setValue(value); 99 } 100 101 /** 102 * This is the constructor. <br> 103 * The language element was set to <b>en </b>. The subtag element was set to 104 * the value of <em>subtag</em>. If the value of <em>subtag</em> 105 * is null or empty an exception was thrown. The type element was set to 106 * the value of <em>type</em>, if it is null, an empty string was set 107 * to the type element. The boolean string <em>value</em> 108 * was set to a boolean element, if it is null, false was set. 109 * @param subtag the name of the subtag 110 * @param type the optional type string 111 * @param inherted a value >= 0 112 * @param value the boolean value (true or false) 113 * @exception MCRException if the subtag value is null or empty 114 */ 115 public MCRMetaBoolean(String subtag, String type, int inherted, boolean value) throws MCRException { 116 super(subtag, null, type, inherted); 117 setValue(value); 118 } 119 120 /** 121 * This method set value. It set false if the string is corrupt. 122 * 123 * @param value 124 * the boolean value (true or false) as string 125 */ 126 public void setValue(String value) { 127 this.value = Boolean.parseBoolean(value); 128 } 129 130 /** 131 * This method set the value. 132 * 133 * @param value 134 * the boolean value 135 */ 136 public void setValue(boolean value) { 137 this.value = value; 138 } 139 140 /** 141 * This method get the value element. 142 * 143 * @return the value as Boolean 144 */ 145 public boolean getValue() { 146 return value; 147 } 148 149 /** 150 * This method get the value element as String. 151 * 152 * @return the value as String 153 */ 154 public String getValueToString() { 155 return String.valueOf(value); 156 } 157 158 /** 159 * This method read the XML input stream part from a DOM part for the 160 * metadata of the document. 161 * 162 * @param element 163 * a relevant JDOM element for the metadata 164 */ 165 @Override 166 public void setFromDOM(Element element) { 167 super.setFromDOM(element); 168 setValue(element.getTextTrim()); 169 } 170 171 /** 172 * This method create a XML stream for all data in this class, defined by 173 * the MyCoRe XML MCRBoolean definition for the given subtag. 174 * 175 * @exception MCRException 176 * if the content of this class is not valid 177 * @return a JDOM Element with the XML MCRBoolean part 178 */ 179 @Override 180 public Element createXML() throws MCRException { 181 Element elm = super.createXML(); 182 elm.addContent(getValueToString()); 183 return elm; 184 } 185 186 /** 187 * Creates the JSON representation. Extends the {@link MCRMetaDefault#createJSON()} method 188 * with the following data. 189 * 190 * <pre> 191 * { 192 * value: true|false 193 * } 194 * </pre> 195 * 196 */ 197 @Override 198 public JsonObject createJSON() { 199 JsonObject obj = super.createJSON(); 200 obj.addProperty("value", getValue()); 201 return obj; 202 } 203 204 /** 205 * clone of this instance 206 * 207 * you will get a (deep) clone of this element 208 * 209 * @see java.lang.Object#clone() 210 */ 211 @Override 212 public MCRMetaBoolean clone() { 213 MCRMetaBoolean clone = (MCRMetaBoolean) super.clone(); 214 215 clone.value = this.value; 216 217 return clone; 218 } 219 220 /** 221 * This method put debug data to the logger (for the debug mode). 222 */ 223 @Override 224 public void debug() { 225 if (LOGGER.isDebugEnabled()) { 226 super.debugDefault(); 227 LOGGER.debug("Value = {}", Boolean.toString(value)); 228 LOGGER.debug(" "); 229 } 230 } 231 232 @Override 233 public boolean equals(Object obj) { 234 if (!super.equals(obj)) { 235 return false; 236 } 237 final MCRMetaBoolean other = (MCRMetaBoolean) obj; 238 return this.value == other.value; 239 } 240 241 }