KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nilostep > xlsql > database > excel > xlWorkbook


1 /*(Header: NiLOSTEP / xlSQL)
2
3  Copyright (C) 2004 NiLOSTEP
4    NiLOSTEP Information Sciences
5    http://nilostep.com
6    nilo.de.roock@nilostep.com
7
8  This program is free software; you can redistribute it and/or modify it under
9  the terms of the GNU General Public License as published by the Free Software
10  Foundation; either version 2 of the License, or (at your option) any later
11  version.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16  more details. You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software Foundation,
18  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */

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 /**
35  * Represents a schema in the Excel context: the workbook
36  *
37  * @version $Revision: 1.7 $
38  * @author $author$
39  */

40 public class xlWorkbook extends xlSubFolder {
41     protected static final String JavaDoc XLS = ".xls";
42
43     /**
44      * Creates a new xlWorkbook object.
45      *
46      * @param dir directory where workbooks are stored
47      * @param name name of workbook excluding extension
48      * @throws xlException DOCUMENT ME!
49      */

50     public xlWorkbook(File dir, String JavaDoc name) throws xlException {
51         super(dir, name);
52     }
53
54     /**
55      * Creates a new xlWorkbook object.
56      *
57      * @param dir directory where workbooks are stored
58      * @param name name of workbook excluding extension
59      * @param dirty DOCUMENT ME!
60      */

61     public xlWorkbook(File dir, String JavaDoc 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 JavaDoc[] 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             //A schema has been added so we'll create a new workbook
102
try {
103                 wbOut = Workbook.createWorkbook(getWorkbookFile());
104
105                 // execute xlSheet.close() for all sheet objects
106
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                 //
116
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             //modifications have been logged for this workbook
126
//Create copy workbook with extension *.xls_
127
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                 // execute xlSheet.close() for all sheet objects
136
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                 //Write a copy first, then remove original, finally rename
146
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             //all sheets have been dropped for this workbook: delete file
158
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