KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nilostep > xlsql > database > xlFolder


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;
21
22 import com.nilostep.xlsql.sql.xlSqlSelect;
23
24 import java.io.*;
25
26 import java.util.*;
27 import java.util.logging.*;
28
29
30 /**
31  * Abstract Folder. Extend for particular use.
32  *
33  * @version $Revision: 1.18 $
34  * @author $author$
35  */

36 public abstract class xlFolder implements xlDatabase {
37     protected static final Logger logger = Logger.getAnonymousLogger();
38     protected static final int UPDATE = 1;
39     static final int DELETE = 2;
40     protected static final String JavaDoc NOARGS = "xlSQL: no such argument(s).";
41     protected File directory;
42     protected Map subfolders = new HashMap();
43     private xlSqlSelect sqlSelect;
44
45     /**
46      * Creates a new xlFolder object.
47      *
48      * @param dir ( root ) directory where datasource is stored
49      *
50      * @throws xlException DOCUMENT ME!
51      */

52     public xlFolder(File dir) throws xlException {
53         readSubFolders(dir);
54     }
55
56     /**
57      * DOCUMENT ME!
58      *
59      * @param subfolder schema type of identifier for document
60      * @param docname document name
61      *
62      * @return String array with columnnames (assume never null or empty)
63      *
64      * @throws IllegalArgumentException DOCUMENT ME!
65      */

66     public String JavaDoc[] getColumnNames(String JavaDoc subfolder, String JavaDoc docname) {
67         String JavaDoc[] ret = { "" };
68         String JavaDoc subfolderU = subfolder.toUpperCase();
69         String JavaDoc docnameU = docname.toUpperCase();
70
71         if (subfolders.containsKey(subfolderU)) {
72             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU);
73
74             if (wb.files.containsKey(docnameU)) {
75                 xlFile doc = (xlFile) wb.files.get(docnameU);
76
77                 if (doc.isValid()) {
78                     ret = doc.getColumnNames();
79                 } else {
80                     throw new IllegalArgumentException JavaDoc(NOARGS);
81                 }
82             } else {
83                 throw new IllegalArgumentException JavaDoc(NOARGS);
84             }
85         } else {
86             throw new IllegalArgumentException JavaDoc(NOARGS);
87         }
88
89         return ret;
90     }
91
92     /**
93      * DOCUMENT ME!
94      *
95      * @param subfolder schema type of identifier for document
96      * @param docname DOCUMENT ME!
97      *
98      * @return DOCUMENT ME!
99      *
100      * @throws IllegalArgumentException DOCUMENT ME!
101      */

102     public String JavaDoc[] getColumnTypes(String JavaDoc subfolder, String JavaDoc docname) {
103         String JavaDoc[] ret = { "" };
104         String JavaDoc subfolderU = subfolder.toUpperCase();
105         String JavaDoc docnameU = docname.toUpperCase();
106
107         if (subfolders.containsKey(subfolderU)) {
108             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU);
109
110             if (wb.files.containsKey(docnameU)) {
111                 xlFile doc = (xlFile) wb.files.get(docnameU);
112
113                 if (doc.isValid()) {
114                     ret = doc.getColumnTypes();
115                 } else {
116                     throw new IllegalArgumentException JavaDoc(NOARGS);
117                 }
118             } else {
119                 throw new IllegalArgumentException JavaDoc(NOARGS);
120             }
121         } else {
122             throw new IllegalArgumentException JavaDoc(NOARGS);
123         }
124
125         return ret;
126     }
127
128     /**
129      * DOCUMENT ME!
130      *
131      * @param subfolder schema type of identifier for document
132      * @param docname DOCUMENT ME!
133      *
134      * @return DOCUMENT ME!
135      *
136      * @throws IllegalArgumentException DOCUMENT ME!
137      */

138     public int getRows(String JavaDoc subfolder, String JavaDoc docname) {
139         int ret = 0;
140         String JavaDoc subfolderU = subfolder.toUpperCase();
141         String JavaDoc docnameU = docname.toUpperCase();
142
143         if (subfolders.containsKey(subfolderU)) {
144             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU);
145
146             if (wb.files.containsKey(docnameU)) {
147                 xlFile doc = (xlFile) wb.files.get(docnameU);
148
149                 if (doc.isValid()) {
150                     ret = doc.getRows() - 1;
151                 } else {
152                     throw new IllegalArgumentException JavaDoc(NOARGS);
153                 }
154             } else {
155                 throw new IllegalArgumentException JavaDoc(NOARGS);
156             }
157         } else {
158             throw new IllegalArgumentException JavaDoc(NOARGS);
159         }
160
161         return ret;
162     }
163
164     /**
165      * DOCUMENT ME!
166      *
167      * @return DOCUMENT ME!
168      */

169     public String JavaDoc[] getSchemas() {
170         String JavaDoc[] s = (String JavaDoc[]) subfolders.keySet().toArray(new String JavaDoc[0]);
171         String JavaDoc[] ret = new String JavaDoc[s.length];
172
173         for (int i = 0; i < s.length; i++) {
174             xlSubFolder wb = (xlSubFolder) subfolders.get(s[i]);
175             ret[i] = wb.subFolderName;
176         }
177
178         return ret;
179     }
180
181     /**
182      * DOCUMENT ME!
183      *
184      * @param subfolder schema type of identifier for document
185      *
186      * @return DOCUMENT ME!
187      *
188      * @throws IllegalArgumentException DOCUMENT ME!
189      */

190     public String JavaDoc[] getTables(String JavaDoc subfolder) {
191         String JavaDoc[] ret = { "" };
192         String JavaDoc subfolderU = subfolder.toUpperCase();
193
194         if (subfolders.containsKey(subfolderU)) {
195             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU);
196             String JavaDoc[] t = (String JavaDoc[]) wb.validfiles.keySet()
197                                                  .toArray(new String JavaDoc[0]);
198             String JavaDoc[] tables = new String JavaDoc[t.length];
199
200             for (int i = 0; i < t.length; i++) {
201                 xlFile doc = (xlFile) wb.validfiles.get(t[i]);
202                 tables[i] = doc.getSName();
203             }
204
205             ret = tables;
206         } else {
207             throw new IllegalArgumentException JavaDoc(NOARGS);
208         }
209
210         return ret;
211     }
212
213     /**
214      * DOCUMENT ME!
215      *
216      * @param subfolder schema type of identifier for document
217      * @param docname DOCUMENT ME!
218      *
219      * @return DOCUMENT ME!
220      *
221      * @throws xlException DOCUMENT ME!
222      * @throws IllegalArgumentException DOCUMENT ME!
223      */

224     public String JavaDoc[][] getValues(String JavaDoc subfolder, String JavaDoc docname)
225                          throws xlException {
226         String JavaDoc[][] ret =
227         {
228             { "" }
229         };
230         String JavaDoc subfolderU = subfolder.toUpperCase();
231         String JavaDoc docnameU = docname.toUpperCase();
232
233         if (subfolders.containsKey(subfolderU)) {
234             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU);
235
236             if (wb.files.containsKey(docnameU)) {
237                 xlFile doc = (xlFile) wb.files.get(docnameU);
238
239                 if (doc.isValid()) {
240                     ret = doc.getValues();
241                 } else {
242                     throw new IllegalArgumentException JavaDoc(NOARGS);
243                 }
244             } else {
245                 throw new IllegalArgumentException JavaDoc(NOARGS);
246             }
247         } else {
248             throw new IllegalArgumentException JavaDoc(NOARGS);
249         }
250
251         return ret;
252     }
253
254     /**
255      * DOCUMENT ME!
256      *
257      * @param subfolder schema type of identifier for document
258      * @param docname DOCUMENT ME!
259      *
260      * @throws IllegalArgumentException DOCUMENT ME!
261      */

262     public void addRow(String JavaDoc subfolder, String JavaDoc docname) {
263         String JavaDoc subfolderU = subfolder.toUpperCase();
264         String JavaDoc docnameU = docname.toUpperCase();
265
266         if (subfolders.containsKey(subfolderU)) {
267             xlSubFolder sF = (xlSubFolder) subfolders.get(subfolderU);
268
269             if (sF.files.containsKey(docnameU)) {
270                 xlFile doc = (xlFile) sF.files.get(docnameU);
271
272                 if (doc.isValid()) {
273                     doc.addRow();
274                 } else {
275                     throw new IllegalArgumentException JavaDoc(NOARGS);
276                 }
277             } else {
278                 throw new IllegalArgumentException JavaDoc(NOARGS);
279             }
280         } else {
281             throw new IllegalArgumentException JavaDoc(NOARGS);
282         }
283     }
284
285     /**
286      * DOCUMENT ME!
287      *
288      * @param subfolder schema type of identifier for document
289      */

290     public abstract void addSchema(String JavaDoc subfolder);
291
292     /**
293      * DOCUMENT ME!
294      *
295      * @param subfolder DOCUMENT ME!
296      * @param docname DOCUMENT ME!
297      */

298     public abstract void addTable(String JavaDoc subfolder, String JavaDoc docname);
299
300     /**
301      * DOCUMENT ME!
302      *
303      * @param query DOCUMENT ME!
304      *
305      * @throws Exception DOCUMENT ME!
306      */

307     public void flush(xlSqlSelect query) throws Exception JavaDoc {
308         sqlSelect = query;
309
310         // execute xlSubFolfer.close() for all subfolder objects
311
Iterator i = subfolders.values().iterator();
312
313         while (i.hasNext()) {
314             xlSubFolder wb;
315             wb = (xlSubFolder) i.next();
316             wb.close(query);
317         }
318     }
319
320     /**
321      * DOCUMENT ME!
322      *
323      * @param subfolder schema type of identifier for document
324      * @param docname DOCUMENT ME!
325      */

326     public void removeTable(String JavaDoc subfolder, String JavaDoc docname) {
327         // If subfolder and/or document do not exist: ok (IF EXISTS option)
328
String JavaDoc subfolderU = subfolder.toUpperCase();
329         String JavaDoc docnameU = docname.toUpperCase();
330
331         if (subfolders.containsKey(subfolderU)) {
332             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU);
333             wb.bDirty[DELETE] = true;
334
335             if (wb.files.containsKey(docnameU)) {
336                 xlFile doc = (xlFile) wb.files.get(docnameU);
337
338                 if (doc.isValid()) {
339                     doc.setIsChanged(DELETE, true);
340
341                     Collection cl = wb.files.values();
342                     xlFile s;
343                     Iterator l = cl.iterator();
344
345                     while (l.hasNext()) {
346                         s = (xlFile) l.next();
347
348                         if (!s.getIsChanged(DELETE)) {
349                             wb.bDirty[DELETE] = false;
350                             wb.bDirty[xlConstants.UPDATE] = true;
351
352                             break;
353                         }
354                     }
355                 }
356             }
357         }
358     }
359
360     /**
361      * DOCUMENT ME!
362      *
363      * @param subfolder schema type of identifier for document
364      *
365      * @return DOCUMENT ME!
366      */

367     public boolean schemaExists(String JavaDoc subfolder) {
368         return false;
369     }
370
371     /**
372      * DOCUMENT ME!
373      *
374      * @param subfolder schema type of identifier for document
375      * @param docname DOCUMENT ME!
376      *
377      * @return DOCUMENT ME!
378      */

379     public boolean tableExists(String JavaDoc subfolder, String JavaDoc docname) {
380         boolean ret = false;
381
382         if (subfolders.containsKey(subfolder)) {
383             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolder);
384
385             if (wb.files.containsKey(docname)) {
386                 xlFile doc = (xlFile) wb.files.get(docname);
387
388                 if (!doc.getIsChanged(DELETE)) {
389                     ret = true;
390                 }
391             }
392         }
393
394         return ret;
395     }
396
397     /**
398      * DOCUMENT ME!
399      *
400      * @param subfolder schema type of identifier for document
401      *
402      * @throws IllegalArgumentException DOCUMENT ME!
403      */

404     public void touchSchema(String JavaDoc subfolder) {
405         String JavaDoc subfolderU = subfolder.toUpperCase();
406
407         if (subfolders.containsKey(subfolderU)) {
408             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU);
409             wb.bDirty[xlConstants.UPDATE] = true;
410         } else {
411             throw new IllegalArgumentException JavaDoc(NOARGS);
412         }
413     }
414
415     /**
416      * DOCUMENT ME!
417      *
418      * @param subfolder schema type of identifier for document
419      * @param docname DOCUMENT ME!
420      *
421      * @throws IllegalArgumentException DOCUMENT ME!
422      */

423     public void touchTable(String JavaDoc subfolder, String JavaDoc docname) {
424         String JavaDoc subfolderU = subfolder.toUpperCase();
425         String JavaDoc docnameU = docname.toUpperCase();
426
427         if (subfolders.containsKey(subfolderU)) {
428             xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU);
429
430             if (wb.files.containsKey(docnameU)) {
431                 xlFile doc = (xlFile) wb.files.get(docnameU);
432
433                 if (doc.isValid()) {
434                     doc.setIsChanged(xlConstants.UPDATE, true);
435                 } else {
436                     throw new IllegalArgumentException JavaDoc(NOARGS);
437                 }
438             } else {
439                 throw new IllegalArgumentException JavaDoc(NOARGS);
440             }
441         } else {
442             throw new IllegalArgumentException JavaDoc(NOARGS);
443         }
444     }
445
446     protected abstract void readSubFolders(File dir) throws xlException;
447 }
Popular Tags