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 /** 27 * This class implements all method for handling with the MCRMetaAccessRule part 28 * of a metadata object. The MCRMetaAccessRule class present a single item, 29 * which hold an ACL condition for a defined permission. 30 * 31 * @author Jens Kupferschmidt 32 * @version $Revision$ $Date$ 33 */ 34 public class MCRMetaAccessRule extends MCRMetaDefault { 35 // MCRMetaAccessRule data 36 protected Element condition; 37 38 protected String permission; 39 40 private static final Logger LOGGER = LogManager.getLogger(); 41 42 /** 43 * This is the constructor. <br> 44 * The constructor of the MCRMetaDefault runs. The <em>permission</em> Attribute 45 * is set to 'READ'. The <b>condition</b> is set to 'null'. 46 */ 47 public MCRMetaAccessRule() { 48 super(); 49 condition = null; 50 permission = "READ"; 51 } 52 53 /** 54 * This is the constructor. <br> 55 * The language element was set. If the value of <em>default_lang</em> is 56 * null, empty or false <b>en</b> was set. This is not use in other methods 57 * of this class. The subtag element was set to the value of 58 * <em>subtag</em>. If the value of <em>subtag</em> 59 * is null or empty an exception will be throwed. The type element was set to 60 * the value of <em>type</em>, if it is null, an empty string was set 61 * to the type element. The condition element was set to the value of 62 * <em>condition</em>, if it is null, an exception will be throwed. 63 * @param subtag the name of the subtag 64 * @param type the optional type string 65 * @param inherted a value >= 0 66 * @param permission permission 67 * @param condition the JDOM Element included the condition tree 68 * @exception MCRException if the subtag value or condition is null or empty 69 */ 70 public MCRMetaAccessRule(String subtag, String type, int inherted, String permission, Element condition) 71 throws MCRException { 72 super(subtag, null, type, inherted); 73 if (condition == null || !condition.getName().equals("condition")) { 74 throw new MCRException("The condition Element of MCRMetaAccessRule is null."); 75 } 76 set(permission, condition); 77 } 78 79 /** 80 * This method set the permission and the condition. 81 * 82 * @param permission the format string, if it is empty 'READ' will be set. 83 * @param condition 84 * the JDOM Element included the condition tree 85 * @exception MCRException 86 * if the condition is null or empty 87 */ 88 public final void set(String permission, Element condition) throws MCRException { 89 setCondition(condition); 90 setPermission(permission); 91 } 92 93 /** 94 * This method set the condition. 95 * 96 * @param condition 97 * the JDOM Element included the condition tree 98 * @exception MCRException 99 * if the condition is null or empty 100 */ 101 public final void setCondition(Element condition) throws MCRException { 102 if (condition == null || !condition.getName().equals("condition")) { 103 throw new MCRException("The condition Element of MCRMetaAccessRule is null."); 104 } 105 this.condition = condition.clone(); 106 } 107 108 /** 109 * This method set the permission attribute. 110 * 111 * @param permission 112 * the new permission string. 113 */ 114 public final void setPermission(String permission) { 115 this.permission = permission; 116 } 117 118 /** 119 * This method get the condition. 120 * 121 * @return the condition as JDOM Element 122 */ 123 public final Element getCondition() { 124 return condition; 125 } 126 127 /** 128 * This method get the permission attribute. 129 * 130 * @return the permission attribute 131 */ 132 public final String getPermission() { 133 return permission; 134 } 135 136 /** 137 * This method read the XML input stream part from a DOM part for the 138 * metadata of the document. 139 * 140 * @param element 141 * a relevant JDOM element for the metadata 142 */ 143 @Override 144 public void setFromDOM(Element element) { 145 super.setFromDOM(element); 146 147 setCondition(element.getChild("condition")); 148 setPermission(element.getAttributeValue("permission")); 149 } 150 151 /** 152 * This method create a XML stream for all data in this class, defined by 153 * the MyCoRe XML MCRMetaAccessRule definition for the given subtag. 154 * 155 * @exception MCRException 156 * if the content of this class is not valid 157 * @return a JDOM Element with the XML MCRMetaAccessRule part 158 */ 159 @Override 160 public Element createXML() throws MCRException { 161 Element elm = super.createXML(); 162 elm.setAttribute("permission", permission); 163 elm.addContent(condition.clone()); 164 return elm; 165 } 166 167 /** 168 * Validates this MCRMetaAccessRule. This method throws an exception if: 169 * <ul> 170 * <li>the subtag is not null or empty</li> 171 * <li>the lang value was supported</li> 172 * <li>the inherited value is lower than zero</li> 173 * <li>the condition is null</li> 174 * <li>the permission is null or empty</li> 175 * </ul> 176 * 177 * @throws MCRException the MCRMetaAccessRule is invalid 178 */ 179 @Override 180 public void validate() throws MCRException { 181 super.validate(); 182 if (condition == null) { 183 throw new MCRException(getSubTag() + ": condition is null"); 184 } 185 if (permission == null || permission.length() == 0) { 186 throw new MCRException(getSubTag() + ": permission is null or empty"); 187 } 188 } 189 190 /** 191 * This method make a clone of this class. 192 */ 193 @Override 194 public MCRMetaAccessRule clone() { 195 MCRMetaAccessRule clone = (MCRMetaAccessRule) super.clone(); 196 197 clone.permission = this.permission; 198 clone.condition = this.condition.clone(); 199 200 return clone; 201 } 202 203 /** 204 * This method put debug data to the logger (for the debug mode). 205 */ 206 @Override 207 public final void debug() { 208 if (LOGGER.isDebugEnabled()) { 209 super.debugDefault(); 210 LOGGER.debug("Permission = {}", permission); 211 LOGGER.debug("Rule = " + "condition"); 212 LOGGER.debug(" "); 213 } 214 } 215 }