1 4 package com.nightlabs.ipanema.security; 5 6 import java.io.Serializable ; 7 import java.util.Collection ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 12 import javax.jdo.PersistenceManager; 13 import javax.jdo.spi.PersistenceCapable; 14 15 import org.apache.log4j.Logger; 16 17 import com.nightlabs.jdo.BaseObjectID; 18 19 22 23 31 public class Role implements Serializable 32 { 33 public static Logger LOGGER = Logger.getLogger(Role.class); 34 35 39 private String roleID; 40 41 57 private Map names = new HashMap (); 58 59 75 private Map descriptions = new HashMap (); 76 77 93 private Map roleGroups = new HashMap (); 94 95 96 public Role() { } 97 98 public Role(String _roleID) 99 { 100 if (!BaseObjectID.isValidIDString(_roleID)) 101 throw new IllegalArgumentException ("roleID \""+_roleID+"\" is not a valid id!"); 102 this.roleID = _roleID; 103 } 104 105 protected static String EMPTYSTRING = ""; 106 107 110 public String getName() { 111 return (String )names.get(EMPTYSTRING); 112 } 113 116 public String getName(String languageID) { 117 if (languageID == null) 118 languageID = EMPTYSTRING; 119 String res = (String )names.get(languageID); 120 if (res == null) 121 res = (String )names.get(EMPTYSTRING); 122 return res; 123 } 124 127 public void setName(String languageID, String name) { 128 if (languageID == null) 129 languageID = EMPTYSTRING; 130 names.put(languageID, name); 131 } 132 133 136 public String getDescription() { 137 return (String )descriptions.get(EMPTYSTRING); 138 } 139 142 public String getDescription(String languageID) { 143 if (languageID == null) 144 languageID = EMPTYSTRING; 145 String res = (String )descriptions.get(languageID); 146 if (res == null) 147 res = (String )descriptions.get(EMPTYSTRING); 148 return res; 149 } 150 153 public void setDescription(String languageID, String description) { 154 if (languageID == null) 155 languageID = EMPTYSTRING; 156 descriptions.put(languageID, description); 157 } 158 159 162 public String getRoleID() 163 { 164 return roleID; 165 } 166 167 170 public Collection getRoleGroups() { 171 return roleGroups.values(); 172 } 173 174 179 protected void _addRoleGroup(RoleGroup roleGroup) 180 { 181 String roleGroupID = roleGroup.getRoleGroupID(); 182 if (roleGroups.containsKey(roleGroupID)) { 183 LOGGER.warn("_addRoleGroup(\""+roleGroupID+"\"): roleGroup was already added."); 184 return; 185 } 186 roleGroups.put(roleGroupID, roleGroup); 187 } 188 189 193 protected void _removeRoleGroup(RoleGroup roleGroup) 194 { 195 if (roleGroups.remove(roleGroup.getRoleGroupID()) == null) 196 LOGGER.warn("_removeRoleGroup(\""+roleGroup.getRoleGroupID()+"\"): roleGroup was not existing."); 197 } 198 199 202 public boolean equals(Object obj) { 203 if (obj == this) 204 return true; 205 206 if (!(obj instanceof Role)) 207 return false; 208 209 Role other = (Role) obj; 210 return this.getRoleID().equals(other.getRoleID()); 211 } 212 215 public int hashCode() { 216 return getRoleID().hashCode(); 217 } 218 219 protected transient String thisString = null; 220 223 public String toString() { 224 if (thisString == null) { 225 StringBuffer sb = new StringBuffer (); 226 sb.append(this.getClass().getName()); 227 sb.append('{'); 228 sb.append(getRoleID()); 229 sb.append(','); 230 sb.append(getDescription()); 231 sb.append('}'); 232 thisString = sb.toString(); 233 } 234 return thisString; 235 } 236 237 238 public static final int INCLUDE_NONE = 0; 239 public static final int INCLUDE_ROLEGROUPS = 0x1; 240 public static final int INCLUDE_ROLEGROUPS_ROLES = 0x2; 241 243 public void makeTransient(int includeMask) 244 { 245 PersistenceManager pm = ((PersistenceCapable)this).jdoGetPersistenceManager(); 246 if (pm == null) 247 return; 248 249 pm.retrieve(this); 250 251 Map tmpRoleGroups = new HashMap (); 252 if ((includeMask & INCLUDE_ROLEGROUPS) != 0) { 253 int roleGroupIncludeMask = 0; 254 if ((includeMask & INCLUDE_ROLEGROUPS_ROLES) != 0) 255 roleGroupIncludeMask = roleGroupIncludeMask | RoleGroup.INCLUDE_ROLES; 256 259 for (Iterator it = getRoleGroups().iterator(); it.hasNext(); ) { 260 RoleGroup rg = (RoleGroup)it.next(); 261 rg.makeTransient(roleGroupIncludeMask); 262 tmpRoleGroups.put(rg.getRoleGroupID(), rg); 263 } 264 } 265 266 pm.makeTransient(this); 267 268 roleGroups = tmpRoleGroups; 269 } 270 271 } 272 | Popular Tags |