1 9 package org.jboss.portal.core.impl.role; 10 11 import org.apache.log4j.Logger; 12 import org.jboss.portal.common.util.Tools; 13 import org.jboss.portal.core.impl.user.UserImpl; 14 import org.jboss.portal.core.model.Role; 15 import org.jboss.portal.core.modules.AbstractModule; 16 import org.jboss.portal.core.modules.ModuleException; 17 import org.jboss.portal.core.modules.RoleModule; 18 import org.hibernate.Session; 19 import org.hibernate.Query; 20 import org.hibernate.HibernateException; 21 import org.hibernate.SessionFactory; 22 23 import javax.naming.InitialContext ; 24 import javax.naming.NamingException ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 import java.util.HashSet ; 28 29 38 public class RoleModuleImpl 39 extends AbstractModule 40 implements RoleModule 41 { 42 43 private final Logger log = Logger.getLogger(getClass()); 44 45 public RoleModuleImpl() 46 { 47 } 48 49 public Role findRoleByName(String name) throws ModuleException 50 { 51 if(name != null) 52 { 53 try 54 { 55 Session session = getSession(); 56 Query query = session.createQuery("from RoleImpl as g where g.name=?"); 57 query.setString(0, name); 58 RoleImpl role = (RoleImpl) query.uniqueResult(); 59 if(role == null) 60 { 61 throw new ModuleException("No such role " + name); 62 } 63 return role; 64 } 65 catch(HibernateException e) 66 { 67 String message = "Cannot find role by name " + name; 68 log.error(message, e); 69 throw new ModuleException(message, e); 70 } 71 } 72 else 73 { 74 throw new IllegalArgumentException ("name cannot be null"); 75 } 76 } 77 78 public Set findRolesByNames(String [] names) throws ModuleException 79 { 80 if(names != null) 81 { 82 try 83 { 84 Session session = getSession(); 85 StringBuffer queryString = new StringBuffer ("from RoleImpl as g where g.name=?"); 86 for(int i = 1; i < names.length; i++) 87 { 88 queryString.append(" or g.name=?"); 89 } 90 Query query = session.createQuery(queryString.toString()); 91 for(int i = 0; i < names.length; i++) 92 { 93 query.setString(i, names[i]); 94 } 95 Iterator iterator = query.iterate(); 96 Set result = Tools.toSet(iterator); 97 return result; 98 } 99 catch(HibernateException e) 100 { 101 String message = "Cannot find roles"; 102 log.error(message, e); 103 throw new ModuleException(message, e); 104 } 105 } 106 else 107 { 108 throw new IllegalArgumentException ("name cannot be null"); 109 } 110 } 111 112 public Role findRoleByDisplayName(String displayName) throws ModuleException 113 { 114 if(displayName != null) 115 { 116 try 117 { 118 Session session = getSession(); 119 Query query = session.createQuery("from RoleImpl as g where g.displayName=?"); 120 query.setString(0, displayName); 121 RoleImpl role = (RoleImpl) query.uniqueResult(); 122 if(role == null) 123 { 124 throw new ModuleException("No such role " + displayName); 125 } 126 return role; 127 } 128 catch(HibernateException e) 129 { 130 String message = "Cannot find role by display name " + displayName; 131 log.error(message, e); 132 throw new ModuleException(message, e); 133 } 134 } 135 else 136 { 137 throw new IllegalArgumentException ("displayname cannot be null"); 138 } 139 } 140 141 public Role findRoleByID(Integer id) throws ModuleException 142 { 143 if(id != null) 144 { 145 try 146 { 147 Session session = getSession(); 148 RoleImpl role = (RoleImpl) session.get(RoleImpl.class, id); 149 if(role == null) 150 { 151 throw new ModuleException("No role found for " + id); 152 } 153 return role; 154 } 155 catch(HibernateException e) 156 { 157 String message = "Cannot find role by id " + id; 158 log.error(message, e); 159 throw new ModuleException(message, e); 160 } 161 } 162 else 163 { 164 throw new IllegalArgumentException ("id cannot be null"); 165 } 166 } 167 168 public Role createRole(String name, String displayName) throws ModuleException 169 { 170 if(name != null) 171 { 172 try 173 { 174 RoleImpl role = new RoleImpl(name, displayName); 175 Session session = getSession(); 176 session.save(role); 177 return role; 178 } 179 catch(HibernateException e) 180 { 181 String message = "Cannot create role " + name; 182 log.error(message, e); 183 throw new ModuleException(message, e); 184 } 185 } 186 else 187 { 188 throw new IllegalArgumentException ("name cannot be null"); 189 } 190 } 191 192 public void removeRole(Integer id) throws ModuleException 193 { 194 if(id != null) 195 { 196 try 197 { 198 Session session = getSession(); 199 RoleImpl role = (RoleImpl) session.load(RoleImpl.class, id); 202 Iterator users = role.getUsers().iterator(); 204 while(users.hasNext()) 205 { 206 UserImpl user = (UserImpl) users.next(); 207 user.getRoles().remove(role); 208 } 209 session.delete(role); 210 session.flush(); 211 } 212 catch(HibernateException e) 213 { 214 String message = "Cannot remove role " + id; 215 log.error(message, e); 216 throw new ModuleException(message, e); 217 } 218 } 219 else 220 { 221 throw new IllegalArgumentException ("id cannot be null"); 222 } 223 } 224 225 public int getRolesCount() throws ModuleException 226 { 227 try 228 { 229 Session session = getSession(); 230 Query query = session.createQuery("select count(g.ID) from RoleImpl as g"); 231 return ((Integer ) query.uniqueResult()).intValue(); 232 } 233 catch(HibernateException e) 234 { 235 String message = "Cannot count roles"; 236 log.error(message, e); 237 throw new ModuleException(message, e); 238 } 239 } 240 241 public Set findRoles() throws ModuleException 242 { 243 try 244 { 245 Session session = getSession(); 246 Query query = session.createQuery("from RoleImpl"); 247 Iterator iterator = query.iterate(); 248 Set result = Tools.toSet(iterator); 249 return result; 250 } 251 catch(HibernateException e) 252 { 253 String message = "Cannot find roles"; 254 log.error(message, e); 255 throw new ModuleException(message, e); 256 } 257 } 258 259 public Set findRoleMembers(String roleName, int offset, int limit, String userNameFilter) throws ModuleException 260 { 261 if(roleName != null) 262 { 263 try 264 { 265 Session session = getSession(); 266 267 UserImpl userimpl = new UserImpl(); 268 userimpl.setEnabled(true); 269 270 Query query = null; 271 StringBuffer sQuery = new StringBuffer (); 272 sQuery.append("from UserImpl as user left join user.roles role where role.name=:name"); 273 if(userNameFilter.trim().length() != 0) 274 { 275 sQuery.append(" AND user.userName LIKE '%':filter'%'"); 276 query = session.createQuery(sQuery.toString()); 277 query.setString("filter", userNameFilter); 278 } 279 else 280 { 281 query = session.createQuery(sQuery.toString()); 282 } 283 query.setString("name", roleName); 284 query.setFirstResult(offset); 285 query.setMaxResults(limit); 286 287 Iterator iterator = query.iterate(); 288 Set result = Tools.toSet(iterator); 289 290 Set newResult = new HashSet (); 291 Iterator cleaner = result.iterator(); 292 while(cleaner.hasNext()) 293 { 294 Object [] oArr = (Object []) cleaner.next(); 295 newResult.add(oArr[0]); 296 } 297 298 return newResult; 299 } 300 catch(HibernateException e) 301 { 302 String message = "Cannot find role " + roleName; 303 log.error(message, e); 304 throw new ModuleException(message, e); 305 } 306 } 307 else 308 { 309 throw new IllegalArgumentException ("id cannot be null"); 310 } 311 } 312 313 317 protected Session getSession() 318 { 319 try 320 { 321 SessionFactory sf = (SessionFactory)new InitialContext ().lookup("java:portal/SessionFactory"); 322 Session session = sf.getCurrentSession(); 323 return session; 324 } 325 catch (NamingException e) 326 { 327 e.printStackTrace(); throw new RuntimeException ("Cannot get session"); 329 } 330 } 331 } | Popular Tags |