1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.common.VersionProperties; 4 import com.calipso.reportgenerator.common.LanguageTraslator; 5 import com.calipso.reportgenerator.common.User; 6 import com.calipso.reportgenerator.common.InfoException; 7 8 import java.util.*; 9 import java.io.*; 10 11 18 public class UserDataRepository { 19 private HashMap users = new HashMap(); 20 private String repositoryPath; 21 22 26 public UserDataRepository(String repositoryPath) throws InfoException { 27 try { 28 this.repositoryPath = repositoryPath; 29 initialize(new File(repositoryPath)); 30 } catch (IOException e) { 31 throw new InfoException(LanguageTraslator.traslate("572"), e); 32 } 33 } 34 35 41 private void initialize(File repositoryFile) throws IOException { 42 if(repositoryFile.exists()) { 43 fillUsersMapFrom(repositoryFile); 44 } 45 } 46 47 54 private void fillUsersMapFrom(File repositoryFile) { 55 try { 56 FileReader fileReader = new FileReader(repositoryFile); 57 BufferedReader bufferedReader = new BufferedReader(fileReader); 58 String line = bufferedReader.readLine(); 59 while(line != null) { 60 if(!line.equals("")) { 61 StringTokenizer tokenizer = new StringTokenizer(line, ";"); 62 String userId = tokenizer.nextToken(); 63 String userName = tokenizer.nextToken(); 64 String userCompany = tokenizer.nextToken(); 65 Vector data = new Vector(2); 66 data.add(userName); 67 data.add(userCompany); 68 getUsers().put(userId, data); 69 } 70 line = bufferedReader.readLine(); 71 } 72 bufferedReader.close(); 73 fileReader.close(); 74 } catch(FileNotFoundException e) { 75 76 } catch(IOException e) { 77 78 } 79 } 80 81 public Collection getUserData(String userId) { 82 if(getUsers().containsKey(userId)) { 83 return (Collection)getUsers().get(userId); 84 } 85 return null; 86 } 87 88 97 98 public void addNewUser(String userId, String name, String company) throws InfoException { 99 try { 100 if(getUsers().containsKey(userId)){ 101 throw new InfoException(LanguageTraslator.traslate("394")); 102 } 103 File outputFile = new File(repositoryPath); 104 FileWriter fileWriter = null; 105 fileWriter = new FileWriter(outputFile.getPath(), true); 106 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 107 bufferedWriter.write(userId + ";" + name + ";" + company + '\n'); 108 bufferedWriter.close(); 109 fileWriter.close(); 110 } catch(IOException e) { 111 throw new InfoException(LanguageTraslator.traslate("477"),e); 112 } 113 } 114 115 119 private HashMap getUsers() { 120 if(users == null) { 121 users = new HashMap(); 122 } 123 return users; 124 } 125 131 public Set getAllUsers(){ 132 Iterator it = users.entrySet().iterator(); 133 Set listNames = new TreeSet(); 134 while (it.hasNext()){ 135 User us; 136 Map.Entry list = (Map.Entry) it.next(); 137 Vector list1 = new Vector(); 138 list1.addAll((Collection) list.getValue() ); 139 us = new User((String )list.getKey(),(String )list1.get(0),(String )list1.get(1)); 140 listNames.add(us); 141 } 142 return listNames; 143 } 144 150 151 public void removeUser(String user) throws InfoException { 152 users.remove(user); 153 this.removeUserToFileDataUser(); 154 155 } 156 161 private void removeUserToFileDataUser() throws InfoException { 162 try{ 163 Iterator it = users.entrySet().iterator(); 164 165 File outputFile = new File(repositoryPath); 166 FileWriter fileWriter = null; 167 fileWriter = new FileWriter(outputFile.getPath()); 168 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); 169 while (it.hasNext()) { 170 Map.Entry list = (Map.Entry) it.next(); 171 String idUser = (String )list.getKey(); 172 String userString = idUser; 173 Vector vector = (Vector)list.getValue(); 174 String string = ""; 175 for (int i = 0; i < vector.size(); i++) { 176 string += ";"; 177 String s = (String ) vector.elementAt(i); 178 string += s ; 179 } 180 181 bufferedWriter.write (userString + string + "\n"); 182 } 183 bufferedWriter.flush(); 184 bufferedWriter.close(); 185 fileWriter.close(); 186 } catch(IOException e) { 187 throw new InfoException(LanguageTraslator.traslate("478"),e); 188 189 } 190 } 191 192 193 } 194 195 | Popular Tags |