1 package org.jahia.webapps.addressbook; 2 3 import java.sql.*; 4 import java.io.*; 5 import java.util.*; 6 7 import org.apache.ojb.broker.PersistenceBroker; 9 import org.apache.ojb.broker.*; 10 import org.apache.ojb.broker.query.*; 11 import org.apache.ojb.broker.accesslayer.*; 12 13 import org.jahia.tools.*; 14 import org.jahia.tools.db.*; 15 16 17 23 public class DbAddBook 24 { 25 26 private AddressBook addbookRef; 27 28 private static String SEPARATOR = ","; 29 30 31 36 public DbAddBook (AddressBook addbook) { 37 38 this.addbookRef = addbook; 40 } 41 42 43 44 45 46 47 48 54 public int insert (Contact newContact) { 55 56 int max = 0; 57 58 Tools.toConsole("Dbaddbook: Insert", "Begin"); 59 61 max = AdbApplicationDB.getNextId("contact_id","contact"); 62 newContact.setId(max); 63 AdbApplicationDB.insertContact(newContact); 64 return max; 65 66 } 67 68 69 74 public void delete (int contactId) { 75 76 try { 79 Contact c= AdbApplicationDB.SearchContactById(contactId); 81 if (!c.equals(null)) { 82 String civilTitle = c.getCivilTitle(); 83 if (civilTitle.equals("Company")) { 84 String companyName = c.getLastName(); 85 AdbApplicationDB.updateCompany(contactId,companyName); 86 Tools.toConsole("DbAddBook : delete"); 91 } 93 } 94 96 } catch (Exception e) { 97 Tools.toConsole("DbAddBook : delete",e.toString()); 98 } 99 } 100 101 102 108 public void update (int id, Contact newContact) { 109 AdbApplicationDB.update(id,newContact); 110 } 111 112 113 114 private static String toExportFormat(String input) { 115 if (input == null) { 116 return null; 117 } 118 StringBuffer sb = new StringBuffer (input); 119 if (input.indexOf(",") != -1) { 120 String result = Tools.replace(input, "\"", "\"\""); 121 return "\"" + result + "\""; 122 } else { 123 return input; 124 } 125 } 126 127 132 public void save(String filename) { 133 134 String sql = "select * from contact where addbook_id=" + this.addbookRef.getId(); 136 try { 137 ResultSet rs = AdbApplicationDB.executeQuery(sql); 138 139 PrintWriter out = new PrintWriter(new FileWriter(filename)); 140 ResultSetMetaData rsmd = rs.getMetaData(); 141 int numberOfColumns = rsmd.getColumnCount(); 142 for(int i = 1; i <= numberOfColumns; i++) { 143 out.print(toExportFormat(rsmd.getColumnName(i)) + SEPARATOR); 144 } 145 out.print("\n"); 146 int nb = 0; 147 while (rs.next()) { 148 149 for(int i = 1; i <= numberOfColumns; i++) { 150 151 String column = rsmd.getColumnName(i).toLowerCase(); 152 if (column.startsWith("comm_id_")) { 153 String table = column.substring(0,column.length()-5); 154 String provisoire= table.substring(1); 156 out.print(toExportFormat(readData(table,rs.getInt(column))) + SEPARATOR); 158 159 } else if (column.equals("company")) { 160 String company = rs.getString(column); 161 try { 163 int comp_id = Integer.parseInt(company); 164 Contact tmp = AdbApplicationDB.SearchContactById(comp_id); 165 company = tmp.getLastName(); 166 } catch(NumberFormatException nfe) { 167 } 169 out.print(toExportFormat(company) + SEPARATOR); 170 171 } else { 172 out.print(toExportFormat(rs.getString(column)) + SEPARATOR); 173 } 174 } 175 out.print("\n"); 176 nb++; 177 } 178 out.close(); 179 Tools.toConsole("DbAddBook: save","File saved : " + nb + " contacts has been written"); 180 181 } catch (SQLException sqle) { 182 Tools.toConsole("DbAddBook: save","Can't read address book content: "+sqle.toString()); 183 } catch (IOException ioe) { 184 Tools.toConsole("DbAddBook: save","Can't write to file: "+ioe.toString()); 185 } 186 } 187 188 189 194 public Vector readCompanies () { 195 Vector companies = new Vector(); 196 companies= AdbApplicationDB.readCompanies(this.addbookRef.getId()); 197 198 200 201 return companies; 202 203 204 } 205 206 207 215 public String readData(String table, int id) { 216 217 String ids = table + "_id"; 218 String resultat=null; 219 return resultat; 228 } 229 230 231 232 233 234 240 public static void renameCategory(int cat_id, String cat_name) { 241 242 AdbApplicationDB.renameCategory(cat_id, cat_name ); 243 245 } 246 247 248 253 public static void deleteCategory(int cat_id) { 254 try { 255 AdbApplicationDB.executeQuery("delete from category where category_id=" + cat_id); 256 AdbApplicationDB.executeQuery("update contact set category_id=-1 where category_id=" + cat_id); 257 }catch (SQLException e) {} 258 } 259 260 261 262 263 } 264 | Popular Tags |