1 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 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 NOARGS = "xlSQL: no such argument(s)."; 41 protected File directory; 42 protected Map subfolders = new HashMap(); 43 private xlSqlSelect sqlSelect; 44 45 52 public xlFolder(File dir) throws xlException { 53 readSubFolders(dir); 54 } 55 56 66 public String [] getColumnNames(String subfolder, String docname) { 67 String [] ret = { "" }; 68 String subfolderU = subfolder.toUpperCase(); 69 String 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 (NOARGS); 81 } 82 } else { 83 throw new IllegalArgumentException (NOARGS); 84 } 85 } else { 86 throw new IllegalArgumentException (NOARGS); 87 } 88 89 return ret; 90 } 91 92 102 public String [] getColumnTypes(String subfolder, String docname) { 103 String [] ret = { "" }; 104 String subfolderU = subfolder.toUpperCase(); 105 String 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 (NOARGS); 117 } 118 } else { 119 throw new IllegalArgumentException (NOARGS); 120 } 121 } else { 122 throw new IllegalArgumentException (NOARGS); 123 } 124 125 return ret; 126 } 127 128 138 public int getRows(String subfolder, String docname) { 139 int ret = 0; 140 String subfolderU = subfolder.toUpperCase(); 141 String 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 (NOARGS); 153 } 154 } else { 155 throw new IllegalArgumentException (NOARGS); 156 } 157 } else { 158 throw new IllegalArgumentException (NOARGS); 159 } 160 161 return ret; 162 } 163 164 169 public String [] getSchemas() { 170 String [] s = (String []) subfolders.keySet().toArray(new String [0]); 171 String [] ret = new String [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 190 public String [] getTables(String subfolder) { 191 String [] ret = { "" }; 192 String subfolderU = subfolder.toUpperCase(); 193 194 if (subfolders.containsKey(subfolderU)) { 195 xlSubFolder wb = (xlSubFolder) subfolders.get(subfolderU); 196 String [] t = (String []) wb.validfiles.keySet() 197 .toArray(new String [0]); 198 String [] tables = new String [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 (NOARGS); 208 } 209 210 return ret; 211 } 212 213 224 public String [][] getValues(String subfolder, String docname) 225 throws xlException { 226 String [][] ret = 227 { 228 { "" } 229 }; 230 String subfolderU = subfolder.toUpperCase(); 231 String 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 (NOARGS); 243 } 244 } else { 245 throw new IllegalArgumentException (NOARGS); 246 } 247 } else { 248 throw new IllegalArgumentException (NOARGS); 249 } 250 251 return ret; 252 } 253 254 262 public void addRow(String subfolder, String docname) { 263 String subfolderU = subfolder.toUpperCase(); 264 String 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 (NOARGS); 276 } 277 } else { 278 throw new IllegalArgumentException (NOARGS); 279 } 280 } else { 281 throw new IllegalArgumentException (NOARGS); 282 } 283 } 284 285 290 public abstract void addSchema(String subfolder); 291 292 298 public abstract void addTable(String subfolder, String docname); 299 300 307 public void flush(xlSqlSelect query) throws Exception { 308 sqlSelect = query; 309 310 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 326 public void removeTable(String subfolder, String docname) { 327 String subfolderU = subfolder.toUpperCase(); 329 String 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 367 public boolean schemaExists(String subfolder) { 368 return false; 369 } 370 371 379 public boolean tableExists(String subfolder, String 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 404 public void touchSchema(String subfolder) { 405 String 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 (NOARGS); 412 } 413 } 414 415 423 public void touchTable(String subfolder, String docname) { 424 String subfolderU = subfolder.toUpperCase(); 425 String 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 (NOARGS); 437 } 438 } else { 439 throw new IllegalArgumentException (NOARGS); 440 } 441 } else { 442 throw new IllegalArgumentException (NOARGS); 443 } 444 } 445 446 protected abstract void readSubFolders(File dir) throws xlException; 447 } | Popular Tags |