1 package com.calipso.reportgenerator.usermanager; 2 import com.calipso.reportgenerator.common.InfoException; 3 4 import java.util.*; 5 import java.io.IOException ; 6 7 import com.calipso.reportgenerator.common.*; 8 import com.calipso.reportgenerator.reportmanager.RolsRepository; 9 import com.calipso.reportgenerator.reportmanager.RolDataRepository; 10 import com.calipso.reportgenerator.reportmanager.UserDataRepository; 11 import com.calipso.reportgenerator.reportmanager.UsersRepository; 12 13 20 public class UserManager { 21 public Map mapObjects = new HashMap(); 22 public Map map = new HashMap(); 23 public Set listUsers = new TreeSet(); 24 public List listRols = new ArrayList(); 25 private Collection listeners; 26 private ReportGeneratorConfiguration reportGeneratorConfiguration; 27 28 33 public UserManager(ReportGeneratorConfiguration reportGeneratorConfiguration) throws InfoException { 34 this.reportGeneratorConfiguration = reportGeneratorConfiguration; 35 this.refreshListUsers(); 36 this.refreshListRols(); 37 this.refreshMap(); 38 this.fillMapToMapRolsRepository(); 39 } 40 48 public void addUsersToCollectionRol(Rol role,User us) throws InfoException { 49 if (!(this.isValid(us))){ 50 throw new InfoException(LanguageTraslator.traslate("453")); 51 } 52 if (!(this.isValid(role))){ 53 throw new InfoException(LanguageTraslator.traslate("454")); 54 } 55 addUserRol( us.getId(),role.getId(),reportGeneratorConfiguration.getRolsRepositoryPath()); 56 fireModelChange(); 57 } 58 66 67 private void addUserRol(String userName, String rol, String rolsRepositoryPath) throws InfoException { 68 RolsRepository repository = new RolsRepository(rolsRepositoryPath); 69 repository.addUserRol(userName, rol); 70 refreshMap(); 71 fireModelChange(); 72 } 73 78 public Set getUsers() throws InfoException { 79 this.refreshListUsers(); 80 return listUsers; 81 } 82 83 91 public void addRol(Rol rol) throws InfoException { 92 RolDataRepository rolsDataRepository; 93 rolsDataRepository = new RolDataRepository(reportGeneratorConfiguration.getRolDataRepositoryPath()); 94 rolsDataRepository.addNewRol(rol.getId(),rol.getDescription()); 95 listRols.add(rol); 96 fireModelChange(); 97 } 98 107 public void addUser(User us) throws InfoException { 108 UserDataRepository userDataRepository; 109 userDataRepository = new UserDataRepository(reportGeneratorConfiguration.getUserDataRepositoryPath()); 110 userDataRepository.addNewUser(us.getId(),us.getUserName(),us.getCompany()); 111 listUsers.add(us); 112 fireModelChange(); 113 } 114 119 120 private void refreshMap() throws InfoException { 121 RolsRepository repository = new RolsRepository(reportGeneratorConfiguration.getRolsRepositoryPath()); 122 map = repository.getMap(); 123 fillMapToMapRolsRepository(); 124 } 125 132 public List getRolsByUser(User us) throws InfoException { 133 fillMapToMapRolsRepository(); 134 Iterator it = getMap().entrySet().iterator(); 135 List list = new ArrayList(); 136 while (it.hasNext()) { 137 Map.Entry e =(Map.Entry) it.next(); 138 Rol role = (Rol)e.getKey(); 139 List listUsers =(List) e.getValue(); 140 if( listUsers.contains(us)) { 141 list.add(role); 142 } 143 } 144 return list; 145 } 146 153 public List getUsersByRol(Rol role) throws InfoException { 154 fillMapToMapRolsRepository(); 155 List list = new ArrayList(); 156 Set keySet = (Set)getMap().keySet(); 157 if( keySet.contains(role) ) { 158 list = (List)getMap().get(role); 159 } 160 return list; 161 } 162 168 private boolean isValid(Rol role) throws InfoException { 169 this.refreshListRols(); 170 return listRols.contains(role); 171 } 172 178 private boolean isValid(User us) throws InfoException { 179 this.refreshListUsers(); 180 return listUsers.contains(us); 181 } 182 183 189 public List getUsersWithRols()throws InfoException { 190 this.refreshMap(); 191 this.refreshListUsers(); 192 this.fillMapToMapRolsRepository(); 193 List list = new ArrayList(); 194 Iterator it = getMap().entrySet().iterator(); 195 while (it.hasNext()) { 196 Map.Entry e =(Map.Entry) it.next(); 197 List list2= (List) e.getValue(); 198 Iterator it2 = list2.iterator(); 199 while (it2.hasNext()) { 200 User us = (User) it2.next(); 201 if(!(list.contains(us))){ 202 list.add(us); 203 } 204 } 205 } 206 return list; 207 } 208 212 private void refreshListUsers() throws InfoException { 213 UserDataRepository userDataRepository = new UserDataRepository(reportGeneratorConfiguration.getUserDataRepositoryPath()); 214 listUsers = userDataRepository.getAllUsers(); 215 } 216 217 221 222 public void refreshListRols()throws InfoException{ 223 RolDataRepository rolsDataRepository = new RolDataRepository(reportGeneratorConfiguration.getRolDataRepositoryPath()); 224 listRols = (List) rolsDataRepository.getAllRols(); 225 } 226 227 231 232 public List getRols() throws InfoException { 233 this.refreshListRols(); 234 return listRols; 235 } 236 242 public void modifyUser(User us) throws InfoException, IOException { 243 if (!(this.isValidUserId(us.getId()))){ 244 throw new InfoException(LanguageTraslator.traslate("455")); 245 } 246 Iterator it = listUsers.iterator(); 247 while (it.hasNext()) { 248 User us1 = (User)it.next(); 249 if (us1.getId().equals(us.getId())){ 250 UserDataRepository userDataRepository = new UserDataRepository(reportGeneratorConfiguration.getUserDataRepositoryPath()); 251 userDataRepository.removeUser(us.getId()); 252 this.addUser(us); 253 this.fillMapToMapRolsRepository(); 254 break; 255 } 256 } 257 fireModelChange(); 258 } 259 265 private boolean isValidUserId(String idUser){ 266 Iterator it = listUsers.iterator(); 267 while (it.hasNext()) { 268 User us = (User)it.next(); 269 if (us.getId().equals(idUser)){ 270 return true; 271 } 272 } 273 return false; 274 } 275 281 private boolean isValidRolId(String idRol){ 282 Iterator it = listRols.iterator(); 283 while (it.hasNext()) { 284 Rol role = (Rol)it.next(); 285 if (role.getId().equals(idRol)){ 286 return true; 287 } 288 } 289 return false; 290 } 291 292 300 public boolean removeUser(User us) throws InfoException, IOException { 301 if (!(this.isValid(us))){ 302 throw new InfoException(LanguageTraslator.traslate("453")); 303 } 304 UserDataRepository userDataRepository = new UserDataRepository(reportGeneratorConfiguration.getUserDataRepositoryPath()); 305 userDataRepository.removeUser(us.getId()); 306 refreshListUsers(); 307 if (!(hasRol(us))) { 308 return false; 309 } 310 RolsRepository rolsRepository = new RolsRepository(reportGeneratorConfiguration.getRolsRepositoryPath()); 311 rolsRepository.removeUser(us.getId()); 312 refreshMap(); 313 fireModelChange(); 314 return true; 315 } 316 322 private boolean hasRol(User us){ 323 Iterator it = map.entrySet().iterator(); 324 while (it.hasNext()) { 325 Map.Entry e =(Map.Entry) it.next(); 326 Vector vector = (Vector)e.getValue(); 327 if (vector.contains(us.getId())) 328 return true; 329 330 } 331 return false; 332 } 333 341 342 public boolean removeRol(Rol role) throws InfoException, IOException { 343 if (!(this.isValid(role))){ 344 throw new InfoException(LanguageTraslator.traslate("454")); 345 } 346 RolDataRepository rolsDataRepository = new RolDataRepository(reportGeneratorConfiguration.getRolDataRepositoryPath()); 347 rolsDataRepository.removeRol(role.getId()); 348 refreshListRols(); 349 350 351 if (!(map.containsKey(role.getId()))) { 352 return false; 353 } 354 RolsRepository rolsRepository = new RolsRepository(reportGeneratorConfiguration.getRolsRepositoryPath()); 355 rolsRepository.removeRol(role.getId()); 356 refreshMap(); 357 fireModelChange(); 358 return true; 359 } 360 364 365 private void fireModelChange() { 366 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 367 UserManagerListener userManagerListener = (UserManagerListener) iterator.next(); 368 userManagerListener.userManagerChange(this); 369 } 370 } 371 375 public void addListener(UserManagerListener userManagerListener) { 376 getListeners().add(userManagerListener); 377 } 378 382 private Collection getListeners() { 383 if(listeners==null){ 384 listeners = new Vector(); 385 } 386 return listeners; 387 } 388 394 public void fillMapToMapRolsRepository() throws InfoException { 395 map = new HashMap(); 396 mapObjects = new HashMap(); 397 RolsRepository repository = new RolsRepository(reportGeneratorConfiguration.getRolsRepositoryPath()); 398 map = repository.getMap(); 399 Iterator it = map.entrySet().iterator(); 400 401 while (it.hasNext()) { 402 Map.Entry e =(Map.Entry) it.next(); 403 String idRol = (String )e.getKey(); 404 405 if (!(isValidRolId(idRol))) { 406 throw new InfoException(LanguageTraslator.traslate("457")); 407 } 408 Rol role = getObjectRolFrom(idRol); 409 Vector vector = (Vector)e.getValue(); 410 List users = new ArrayList(); 411 for (int i = 0; i < vector.size(); i++) { 412 String idUser = (String )vector.get(i); 413 if (!(isValidUserId(idUser))) { 414 throw new InfoException(LanguageTraslator.traslate("457")); 415 } 416 User us = getObjectUserFrom(idUser); 417 users.add(us); 418 } 419 mapObjects.put(role,users); 420 } 421 } 422 427 private Rol getObjectRolFrom(String idRol){ 428 Iterator it = listRols.iterator(); 429 while (it.hasNext()) { 430 Rol role = (Rol)it.next(); 431 if (role.getId().equals(idRol)){ 432 return role; 433 } 434 } 435 return null; 436 } 437 442 private User getObjectUserFrom(String idUser){ 443 Iterator it = listUsers.iterator(); 444 while (it.hasNext()) { 445 User us = (User)it.next(); 446 if (us.getId().equals(idUser)){ 447 return us; 448 } 449 } 450 return null; 451 } 452 456 public Map getMap(){ 457 return mapObjects; 458 } 459 466 public boolean validate(String userName, String password) throws InfoException { 467 UsersRepository usersRepository = new UsersRepository(reportGeneratorConfiguration.getUsersRepositoryPath()); 468 return usersRepository.validate(userName,password); 469 } 470 477 public void removeRolToUser(Rol role, User us) throws InfoException{ 478 refreshMap(); 479 List list; 480 if (!(this.isValid(role))){ 481 throw new InfoException(LanguageTraslator.traslate("454")); 482 } 483 if (!(this.isValid(us))){ 484 throw new InfoException(LanguageTraslator.traslate("453")); 485 } 486 list =((List)getMap().get(role)); 487 if (!(list.remove(us))){ 488 throw new InfoException(LanguageTraslator.traslate("456")); 489 } 490 RolsRepository rolsRepository = new RolsRepository(reportGeneratorConfiguration.getRolsRepositoryPath()); 491 rolsRepository.removeUser(us.getId()); 492 refreshMap(); 493 fireModelChange(); 494 } 495 496 } 497 498 499 | Popular Tags |