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 import com.calipso.reportgenerator.common.Rol; 10 11 18 public class RolDataRepository { 19 private String repositoryPath; 20 private HashMap rols = new HashMap(); 21 22 26 public RolDataRepository(String repositoryPath) throws InfoException { 27 this.repositoryPath = repositoryPath; 28 initialize(new File(repositoryPath)); 29 } 30 36 private void initialize(File repositoryFile) throws InfoException { 37 if(repositoryFile.exists()) { 38 fillRolsMapFrom(repositoryFile); 39 } 40 } 41 47 public void addNewRol(String rolId, String name) throws InfoException { 48 try { 49 if(getRols().containsKey(rolId)){ 50 throw new InfoException(LanguageTraslator.traslate("452")); 51 } 52 File outputFile = new File(repositoryPath); 53 FileWriter fileWriter = null; 54 fileWriter = new FileWriter(outputFile.getPath(), true); 55 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 56 bufferedWriter.write(rolId + ";" + name + '\n'); 57 bufferedWriter.close(); 58 fileWriter.close(); 59 } catch(IOException e) { 60 throw new InfoException(LanguageTraslator.traslate("475"),e); 61 } 62 } 63 70 private void fillRolsMapFrom(File repositoryFile) throws InfoException { 71 try { 72 FileReader fileReader = new FileReader(repositoryFile); 73 BufferedReader bufferedReader = new BufferedReader(fileReader); 74 String line = bufferedReader.readLine(); 75 while(line != null) { 76 if(!line.equals("")) { 77 StringTokenizer tokenizer = new StringTokenizer(line, ";"); 78 String rolId = tokenizer.nextToken(); 79 String rolName = tokenizer.nextToken(); 80 Vector data = new Vector(1); 81 data.add(rolName); 82 getRols().put(rolId, data); 83 } 84 line = bufferedReader.readLine(); 85 } 86 bufferedReader.close(); 87 fileReader.close(); 88 } catch(FileNotFoundException e) { 89 throw new InfoException(LanguageTraslator.traslate("570"), e); 90 } catch(IOException e) { 91 throw new InfoException(LanguageTraslator.traslate("570"), e); 92 } 93 } 94 98 private HashMap getRols() { 99 if(rols == null) { 100 rols = new HashMap(); 101 } 102 return rols; 103 } 104 public Collection getRolData(String rolId) { 105 if(getRols().containsKey(rolId)) { 106 return (Collection)getRols().get(rolId); 107 } 108 return null ; 109 } 110 115 public List getAllRols(){ 116 Iterator it = rols.entrySet().iterator(); 117 Set listNames = new TreeSet(); 118 List list = new ArrayList(); 119 120 while (it.hasNext()){ 121 Rol rol; 122 Map.Entry map = (Map.Entry) it.next(); 123 Vector vector = new Vector(); 124 vector.addAll((Collection) map.getValue() ); 125 rol = new Rol((String )map.getKey(),(String )vector.get(0)); 126 listNames.add(rol); 127 } 128 list.addAll(listNames); 129 return list; 130 } 131 136 public void removeRol(String rol) throws InfoException { 137 rols.remove(rol); 138 this.removeRolToFileDataRol(); 139 } 140 141 146 private void removeRolToFileDataRol() throws InfoException { 147 try{ 148 Iterator it = rols.entrySet().iterator(); 149 File outputFile = new File(repositoryPath); 150 FileWriter fileWriter = null; 151 fileWriter = new FileWriter(outputFile.getPath()); 152 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 153 while (it.hasNext()) { 154 Map.Entry list = (Map.Entry) it.next(); 155 String idRol = (String )list.getKey(); 156 String rolString = idRol; 157 Vector vector = (Vector)list.getValue(); 158 String string = ""; 159 for (int i = 0; i < vector.size(); i++) { 160 string += ";"; 161 String s = (String ) vector.elementAt(i); 162 string += s ; 163 } 164 bufferedWriter.write (rolString + string + '\n'); 165 } 166 bufferedWriter.flush(); 167 bufferedWriter.close(); 168 fileWriter.close(); 169 } catch(IOException e) { 170 throw new InfoException(LanguageTraslator.traslate("476"),e); 171 } 172 } 173 } 174 175 | Popular Tags |