1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.common.InfoException; 4 5 import java.util.*; 6 import java.io.*; 7 8 import com.calipso.reportgenerator.common.LanguageTraslator; 9 10 17 public class RolsRepository { 18 private HashMap roles; 19 private String repositoryPath; 20 21 25 public RolsRepository(String repositoryPath) throws InfoException { 26 this.repositoryPath = repositoryPath; 27 initialize(new File(repositoryPath)); 28 } 29 30 36 private void initialize(File repositoryFile) throws InfoException { 37 if(repositoryFile.exists()) { 38 fillRolsMapFrom(repositoryFile); 39 } 40 } 41 42 51 private void fillRolsMapFrom(File repositoryFile) throws InfoException { 52 try { 53 FileReader fileReader = new FileReader(repositoryFile); 54 BufferedReader bufferedReader = new BufferedReader(fileReader); 55 String line = bufferedReader.readLine(); 56 roles = new HashMap(); 57 while(line != null) { 58 if(!line.equals("")) { 59 StringTokenizer tokenizer = new StringTokenizer(line, "="); 60 String user = tokenizer.nextToken(); 61 String rol = tokenizer.nextToken(); 62 if(roles.containsKey(rol)){ 63 Vector vec = ((Vector)roles.get(rol)); 64 if(vec != null && !(vec.contains(user))) { 65 ((Vector)roles.get(rol)).add(user); 66 }else{ 67 throw new InfoException(LanguageTraslator.traslate("394")); 68 69 } 70 }else{ 71 Vector users = new Vector(); 72 users.add(user); 73 roles.put(rol, users); 74 } 75 } 76 line = bufferedReader.readLine(); 77 } 78 bufferedReader.close(); 79 fileReader.close(); 80 } catch(FileNotFoundException e) { 81 82 } catch(IOException e) { 83 84 } 85 } 86 87 93 public boolean validateRol(String userName, String rol) { 94 boolean returnVal = false; 95 if(getMap().containsKey(rol)) { 96 returnVal = ((Vector)getMap().get(rol)).contains(userName); 97 } 98 return returnVal; 99 } 100 101 106 public HashMap getMap() { 107 if(roles == null) { 108 roles = new HashMap(); 109 } 110 return roles; 111 } 112 122 public void addUserRol(String user, String rol) throws InfoException{ 123 if(getMap().containsKey(rol)){ 124 Vector vec = ((Vector)getMap().get(rol)); 125 if(vec != null && !(vec.contains(user))) { 126 ((Vector)getMap().get(rol)).add(user); 127 }else{ 128 throw new InfoException(LanguageTraslator.traslate("394")); 129 } 130 }else{ 131 Vector users = new Vector(); 132 users.add(user); 133 getMap().put(rol, users); 134 } 135 addToFile(user, rol); 136 } 137 143 private void addToFile(String userName, String rol) throws InfoException { 144 try { 145 File outputFile = new File(repositoryPath); 146 FileWriter fileWriter = null; 147 fileWriter = new FileWriter(outputFile.getPath(), true); 148 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 149 bufferedWriter.write(userName + "=" + rol + '\n'); 150 bufferedWriter.close(); 151 fileWriter.close(); 152 } catch(IOException e) { 153 throw new InfoException(LanguageTraslator.traslate("571"), e); 154 } 155 } 156 160 public List getRols(){ 161 Iterator it = getMap().entrySet().iterator(); 162 List allRols = new ArrayList(); 163 while (it.hasNext()){ 164 Map.Entry e =(Map.Entry) it.next(); 165 allRols.add(e.getKey()) ; 166 } 167 return allRols; 168 } 169 174 public void removeUser(String us) throws InfoException { 175 removeUserToMap(us); 176 removeToFileRolsRepository(); 177 178 } 179 183 private void removeUserToMap(String usId){ 184 Iterator it = getMap().entrySet().iterator(); 185 while (it.hasNext()){ 186 Map.Entry e =(Map.Entry) it.next(); 187 Vector vector =(Vector)e.getValue(); 188 vector.remove(usId); 189 } 190 } 191 195 196 private void removeToFileRolsRepository() throws InfoException { 197 try{ 198 Iterator it = getMap().entrySet().iterator(); 199 File outputFile = new File(repositoryPath); 200 FileWriter fileWriter = null; 201 fileWriter = new FileWriter(outputFile.getPath()); 202 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 203 while (it.hasNext()) { 204 Map.Entry list = (Map.Entry) it.next(); 205 String idRol = (String )list.getKey(); 206 Vector vector = (Vector)list.getValue(); 207 for (int i = 0; i < vector.size(); i++) { 208 String s = (String ) vector.elementAt(i); 209 bufferedWriter.write (s + "=" + idRol + '\n'); 210 } 211 } 212 bufferedWriter.flush(); 213 bufferedWriter.close(); 214 fileWriter.close(); 215 } catch(IOException e) { 216 throw new InfoException(LanguageTraslator.traslate("477"),e); 217 } 218 } 219 224 public void removeRol(String roleId) throws InfoException { 225 removeRolToMap(roleId); 226 removeToFileRolsRepository(); 227 } 228 232 private void removeRolToMap(String roleId){ 233 roles.remove(roleId); 234 } 235 236 237 } 238 | Popular Tags |