001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.right; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.cocoon.xml.AttributesImpl; 022import org.apache.cocoon.xml.XMLUtils; 023import org.xml.sax.ContentHandler; 024import org.xml.sax.SAXException; 025 026import org.ametys.runtime.i18n.I18nizableText; 027 028/** 029 * A right in a runtime application.<br> 030 * A right can also be considered as a boolean permission : a given user has or 031 * does not have the right to do something. 032 */ 033public class Right 034{ 035 private final String _id; 036 037 private final I18nizableText _label; 038 039 private final I18nizableText _description; 040 041 private final I18nizableText _category; 042 043 private final String _declaration; 044 045 /** 046 * Constructor. 047 * 048 * @param id the unique Id of this right 049 * @param label the i18n label of this Right 050 * @param description the i18n description of the usage of this right 051 * @param category the i18n cateogry of the usage of this right 052 * @param declaration the declaration source (for debug purposes) 053 */ 054 Right(String id, I18nizableText label, I18nizableText description, I18nizableText category, String declaration) 055 { 056 _id = id; 057 _label = label; 058 _description = description; 059 _category = category; 060 _declaration = declaration; 061 } 062 063 /** 064 * Returns the unique Id of this Right 065 * 066 * @return the unique Id of this Right 067 */ 068 public String getId() 069 { 070 return _id; 071 } 072 073 /** 074 * Returns the i18n label of this Right 075 * 076 * @return the i18n label of this Right 077 */ 078 public I18nizableText getLabel() 079 { 080 return _label; 081 } 082 083 /** 084 * Returns the i18n description of this Right 085 * 086 * @return the i18n description of this Right 087 */ 088 public I18nizableText getDescription() 089 { 090 return _description; 091 } 092 093 /** 094 * Returns the i18n category of this Right 095 * 096 * @return the i18n category of this Right 097 */ 098 public I18nizableText getCategory() 099 { 100 return _category; 101 } 102 103 /** 104 * Returns the declaration of this Right 105 * 106 * @return the declaration 107 */ 108 public String getDeclaration() 109 { 110 return _declaration; 111 } 112 113 /** 114 * Represents this Right as SAX events 115 * 116 * @param ch the ContentHandler to process SAX events 117 * @throws SAXException if an error occurs 118 */ 119 public void toSAX(ContentHandler ch) throws SAXException 120 { 121 AttributesImpl atts = new AttributesImpl(); 122 atts.addCDATAAttribute("id", _id); 123 XMLUtils.startElement(ch, "right", atts); 124 _label.toSAX(ch, "label"); 125 _description.toSAX(ch, "description"); 126 127 AttributesImpl attrs = new AttributesImpl(); 128 attrs.addCDATAAttribute("id", _category.toString().replaceAll("[^a-zA-Z0-9]", "_")); 129 XMLUtils.startElement(ch, "category", attrs); 130 _category.toSAX(ch); 131 XMLUtils.endElement(ch, "category"); 132 133 XMLUtils.endElement(ch, "right"); 134 } 135 136 /** 137 * Represents this Right as JSON 138 * @return the right in JSON format 139 */ 140 public Map<String, Object> toJSON() 141 { 142 Map<String, Object> right = new HashMap<>(); 143 144 right.put("id", _id); 145 right.put("label", _label); 146 right.put("description", _description); 147 right.put("category", _category); 148 149 return right; 150 } 151 152 @Override 153 public int hashCode() 154 { 155 return _id.hashCode(); 156 } 157 158 @Override 159 public boolean equals(Object object) 160 { 161 if (object == null || !(object instanceof Right)) 162 { 163 return false; 164 } 165 166 return _id.equals(((Right) object).getId()); 167 } 168}