1 20 package com.nilostep.xlsql.database.excel; 21 22 import com.nilostep.xlsql.database.*; 23 import com.nilostep.xlsql.sql.xlSqlSelect; 24 25 import java.io.*; 26 27 import java.util.*; 28 29 import jxl.Workbook; 30 31 import jxl.write.WritableWorkbook; 32 33 34 40 public class xlWorkbook extends xlSubFolder { 41 protected static final String XLS = ".xls"; 42 43 50 public xlWorkbook(File dir, String name) throws xlException { 51 super(dir, name); 52 } 53 54 61 public xlWorkbook(File dir, String name, boolean dirty) { 62 super(dir, name, dirty); 63 } 64 65 protected File getWorkbookFile() { 66 return new File(directory.getPath() + File.separator + subFolderName + 67 XLS); 68 } 69 70 protected void readFiles() throws xlException { 71 Workbook wb = null; 72 73 try { 74 wb = Workbook.getWorkbook(getWorkbookFile()); 75 76 String [] names = wb.getSheetNames(); 77 78 for (int i = 0; i < names.length; i++) { 79 xlSheet obj = new xlSheet(directory, subFolderName, names[i]); 80 files.put(names[i].toUpperCase(), obj); 81 82 if (obj.isValid()) { 83 validfiles.put(names[i].toUpperCase(), obj); 84 } 85 } 86 } catch (IOException ioe) { 87 logger.warning("xlSQL: -xls> io ERR on:" + 88 getWorkbookFile().getPath() + " , NOT mounted."); 89 } catch (jxl.read.biff.BiffException jxlr) { 90 logger.warning("xlSQL: -xls> jxl ERR on:" + 91 getWorkbookFile().getPath() + " , NOT mounted."); 92 } 93 } 94 95 protected void close(xlSqlSelect select) throws xlException { 96 sqlSelect = select; 97 98 WritableWorkbook wbOut; 99 100 if (bDirty[ADD]) { 101 try { 103 wbOut = Workbook.createWorkbook(getWorkbookFile()); 104 105 Iterator i = validfiles.values().iterator(); 107 108 while (i.hasNext()) { 109 xlSheet ws; 110 ws = (xlSheet) i.next(); 111 ws.close(wbOut, select); 112 } 113 114 115 wbOut.write(); 117 wbOut.close(); 118 bDirty[ADD] = false; 119 logger.info(getWorkbookFile().getPath() + " created. "); 120 } catch (IOException ioe) { 121 logger.severe(getWorkbookFile().getPath() + " NOT created. " + 122 "VERIFY datasource integrity."); 123 } 124 } else if (bDirty[UPDATE]) { 125 Workbook wb = null; 128 129 try { 130 wb = Workbook.getWorkbook(getWorkbookFile()); 131 132 File tmp = new File(getWorkbookFile().getName() + "_"); 133 wbOut = Workbook.createWorkbook(tmp, wb); 134 135 Iterator i = validfiles.values().iterator(); 137 138 while (i.hasNext()) { 139 xlSheet ws; 140 ws = (xlSheet) i.next(); 141 ws.close(wbOut, select); 142 } 143 144 145 wbOut.write(); 147 wbOut.close(); 148 getWorkbookFile().delete(); 149 tmp.renameTo(getWorkbookFile()); 150 logger.info(getWorkbookFile().getPath() + " re-created. "); 151 } catch (IOException ioe) { 152 throw new xlException("xlSQL: -xls> ERR: io"); 153 } catch (jxl.read.biff.BiffException jxlr) { 154 throw new xlException("xlSQL: -xls> ERR: jxl"); 155 } 156 } else if (bDirty[DELETE]) { 157 if (getWorkbookFile().delete()) { 159 logger.info(getWorkbookFile().getPath() + " deleted. "); 160 } else { 161 logger.severe(getWorkbookFile().getPath() + " NOT deleted. " + 162 "VERIFY datasource integrity."); 163 } 164 } 165 } 166 } | Popular Tags |