001 /* 002 * CRIMSON 003 * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 004 * University of Pennsylvania. 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License as 008 * published by the Free Software Foundation; either version 2 of the 009 * License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, but 012 * WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 * 02110-1301 USA. 020 * 021 * @(#)ObjectHandles.java 022 */ 023 024 package edu.upenn.crimson; 025 026 import edu.upenn.crimson.io.*; 027 import java.util.Iterator; 028 import java.util.HashMap; 029 import java.util.Set; 030 import javax.swing.DefaultComboBoxModel; 031 032 /** 033 * ObjectHandles. 034 * 035 * @author Stephen Fisher 036 * @version $Id: ObjectHandles.java,v 1.31 2007/05/16 18:55:58 fisher Exp $ 037 */ 038 039 public class ObjectHandles { 040 041 /** 042 * The HashMap of all existing Trees. This is set by 043 * buildLists(). 044 */ 045 private static HashMap treePool = new HashMap(); 046 047 /** 048 * This is a duplicate list of Trees in the treePool. It is 049 * used by the GUI. 050 */ 051 private static DefaultComboBoxModel treeList = new DefaultComboBoxModel(); 052 053 /** 054 * This is a duplicate list of Trees in the treePool. It is used 055 * by the GUI and is set by buildLists(). 056 */ 057 private static TreeTableModel treeTableModel = new TreeTableModel(); 058 059 /** 060 * The HashMap of all existing Partitions. This is set by 061 * buildLists(). 062 */ 063 private static HashMap partitionPool = new HashMap(); 064 065 /** 066 * The HashMap of all existing Models. This is set by 067 * buildLists(). 068 */ 069 private static HashMap modelPool = new HashMap(); 070 071 /** 072 * This is a duplicate list of Models in the modelPool. It is 073 * used by the GUI. 074 */ 075 private static DefaultComboBoxModel modelList = new DefaultComboBoxModel(); 076 077 /** 078 * This is a duplicate list of Models in the modelPool. It is used 079 * by the GUI and is set by buildLists(). 080 */ 081 private static ModelTableModel modelTableModel = new ModelTableModel(); 082 083 /** 084 * The set of all existing Queries. This is set by 085 * buildLists(). 086 */ 087 private static HashMap queryPool = new HashMap(); 088 089 /** 090 * This is a duplicate list of Queries in the queryPool. It is 091 * used by the GUI and is set by buildLists(). 092 */ 093 private static QueryTableModel queryTableModel = new QueryTableModel(); 094 095 //-------------------------------------------------------------------------- 096 // Setters and Getters 097 098 public static HashMap getTreePool() { return treePool; } 099 100 public static DefaultComboBoxModel getTreeList() { return treeList; } 101 102 public static TreeTableModel getTreeTableModel() { return treeTableModel; } 103 104 public static HashMap getPartitionPool() { return partitionPool; } 105 106 public static HashMap getModelPool() { return modelPool; } 107 108 public static DefaultComboBoxModel getModelList() { return modelList; } 109 110 public static ModelTableModel getModelTableModel() { return modelTableModel; } 111 112 public static HashMap getQueryPool() { return queryPool; } 113 114 public static QueryTableModel getQueryTableModel() { return queryTableModel; } 115 116 //-------------------------------------------------------------------------- 117 // Miscellaneous Methods 118 119 /** This will clear the lists. */ 120 public static void clearLists() { 121 clearTreeLists(); 122 clearPartitionLists(); 123 clearModelLists(); 124 clearQueryLists(); 125 } 126 127 /** 128 * This will (re)build the table lists based on the current 129 * connection. 130 */ 131 public static void buildLists() { 132 Trees.loadAll(); 133 Partitions.loadAll(); 134 Models.loadAll(); 135 Queries.load(""); // load all queries 136 } 137 138 //-------------------------------------------------------------------------- 139 // Miscellaneous TREE Methods 140 141 /** This will clear the Tree lists. */ 142 public static void clearTreeLists() { 143 treePool.clear(); 144 treeList.removeAllElements(); 145 treeTableModel.removeAllRows(); 146 } 147 148 /** This will add the tree to the relevant lists. */ 149 public static void addTree(Tree tree) { 150 if (containsTree(tree.getID())) { 151 CrimsonUtils.printError("Duplicate tree ID '" + tree.getID() + "' not added to treePool"); 152 return; 153 } else { 154 treePool.put(tree.getID(), tree); 155 treeList.addElement(tree.getID()); 156 treeTableModel.addRow(tree); 157 } 158 } 159 160 /** 161 * Returns true if the Tree object exists in the treePool. 162 */ 163 public static boolean containsTree(String id) { 164 if (CrimsonUtils.isEmpty(id)) return false; 165 else return treePool.containsKey(id.toUpperCase()); 166 } 167 168 /** 169 * Returns the Tree object for the given ID. 170 * @XXX Should throw an exception if 'id' is not found. 171 */ 172 public static Tree getTree(String id) { 173 if (CrimsonUtils.isEmpty(id)) return null; 174 else return (Tree) treePool.get(id.toUpperCase()); 175 } 176 177 /** Returns the set of all Tree IDs. */ 178 public static Set getTrees() { return treePool.keySet(); } 179 180 /** Returns a iterator over all Trees in treePool. */ 181 public static Iterator treeIterator() { return treePool.values().iterator(); } 182 183 /** 184 * Changes the Tree's ID in treePool and treeTableModel. 185 */ 186 /* 187 public static void renameTree(String oldID, String newID) { 188 Tree tree = getTree(oldID); 189 190 // make sure tree exists 191 if (tree == null) { 192 CrimsonUtils.printMsg("Tree \"" + oldID + "\" not found.", CrimsonUtils.ERROR); 193 return; 194 } 195 196 renameTree(tree, newID); 197 } 198 */ 199 200 /** 201 * Changes the Tree's ID in treePool and treeTableModel. 202 * @XXX This needs and SQL component to change the table names. 203 */ 204 /* 205 public static void renameTree(Tree tree, String newID) { 206 // make sure tree exists 207 if (tree == null) { 208 CrimsonUtils.printMsg("Null tree, can not be renamed.", CrimsonUtils.ERROR); 209 return; 210 } 211 212 // check to make sure ID doesn't already exist in treePool 213 if (treePool.containsKey(newID)) { 214 String msg = "ID \"" + newID + "\" already exists in treePool."; 215 throw new InvalidIDException(msg); 216 } 217 218 // change ID and add to treePool 219 // tree.id = newID; 220 221 // XXXXXXXX 222 // need to run SQL to change tree table names 223 224 // after changing the table names, rebuild the lists 225 buildTreeLists(); 226 } 227 */ 228 229 /** 230 * Uses 'base' to create a random Tree ID string that doesn't 231 * already exist in the treePool. 232 */ 233 public static String randomTreeID(String base) { 234 String id = base + Long.toString(Math.abs(CrimsonUtils.random.nextLong())); 235 236 // loop until we get an id that isn't in the curreent set 237 while (containsTree(id)) { 238 id = base + Long.toString(Math.abs(CrimsonUtils.random.nextLong())); 239 } 240 return id.toUpperCase(); 241 } 242 243 //-------------------------------------------------------------------------- 244 // Miscellaneous PARTITION Methods 245 246 /** This will clear the Partition lists. */ 247 public static void clearPartitionLists() { 248 partitionPool.clear(); 249 } 250 251 /** This will add the partition to the relevant lists. */ 252 public static void addPartition(Partition partition) { 253 if (containsPartition(partition.getID())) { 254 CrimsonUtils.printError("Duplicate partitition ID '" + partition.getID() + "' not added to partitionPool"); 255 return; 256 } else { 257 partitionPool.put(partition.getID(), partition); 258 } 259 } 260 261 /** 262 * Returns true if the Partition object exists in the partitionPool. 263 */ 264 public static boolean containsPartition(String id) { 265 if (CrimsonUtils.isEmpty(id)) return false; 266 else return partitionPool.containsKey(id.toUpperCase()); 267 } 268 269 /** 270 * Returns the Partition object for the given ID. 271 * @XXX Should throw an exception if 'id' is not found. 272 */ 273 public static Partition getPartition(String id) { 274 if (CrimsonUtils.isEmpty(id)) return null; 275 else return (Partition) partitionPool.get(id.toUpperCase()); 276 } 277 278 /** Returns the set of all Partition IDs. */ 279 public static Set getPartitions() { return partitionPool.keySet(); } 280 281 /** Returns a iterator over all Partitions in partitionPool. */ 282 public static Iterator partitionIterator() { return partitionPool.values().iterator(); } 283 284 //-------------------------------------------------------------------------- 285 // Miscellaneous MODEL Methods 286 287 /** This will clear the Model lists. */ 288 public static void clearModelLists() { 289 modelPool.clear(); 290 modelList.removeAllElements(); 291 modelTableModel.removeAllRows(); 292 } 293 294 /** This will add the model to the relevant lists. */ 295 public static void addModel(Model model) { 296 if (containsModel(model.getID())) { 297 CrimsonUtils.printError("Duplicate model ID '" + model.getID() + "' not added to modelPool"); 298 return; 299 } else { 300 modelPool.put(model.getID(), model); 301 modelList.addElement(model.getID()); 302 modelTableModel.addRow(model); 303 } 304 } 305 306 /** This will delete the model from the relevant lists. */ 307 public static void deleteModel(String id) { 308 if (CrimsonUtils.isEmpty(id) || (! containsModel(id))) return; 309 310 id = id.toUpperCase(); 311 modelPool.remove(id); 312 modelList.removeElement(id); 313 314 // rebuild modelTableModel. This is simpler than trying to 315 // figure out which row needs to be deleted. 316 modelTableModel.removeAllRows(); 317 for (Iterator i = modelPool.values().iterator(); i.hasNext();) { 318 modelTableModel.addRow((Model) i.next()); 319 } 320 } 321 322 /** 323 * Returns true if the Model object exists in the modelPool. 324 */ 325 public static boolean containsModel(String id) { 326 if (CrimsonUtils.isEmpty(id)) return false; 327 else return modelPool.containsKey(id.toUpperCase()); 328 } 329 330 /** 331 * Returns the Model object for the given ID. 332 * @XXX Should throw an exception if 'id' is not found. 333 */ 334 public static Model getModel(String id) { 335 if (CrimsonUtils.isEmpty(id)) return null; 336 else return (Model) modelPool.get(id.toUpperCase()); 337 } 338 339 /** Returns the set of all Model IDs. */ 340 public static Set getModels() { return modelPool.keySet(); } 341 342 /** Returns a iterator over all Models in modelPool. */ 343 public static Iterator modelIterator() { return modelPool.values().iterator(); } 344 345 /** 346 * Uses 'base' to create a random Model ID string that doesn't 347 * already exist in the modelPool. 348 */ 349 public static String randomModelID(String base) { 350 String id = base + Long.toString(Math.abs(CrimsonUtils.random.nextLong())); 351 352 // loop until we get an id that isn't in the curreent set 353 while (containsModel(id)) { 354 id = base + Long.toString(Math.abs(CrimsonUtils.random.nextLong())); 355 } 356 return id.toUpperCase(); 357 } 358 359 //-------------------------------------------------------------------------- 360 // Miscellaneous QUERY Methods 361 362 /** This will clear the Query lists. */ 363 public static void clearQueryLists() { 364 queryPool.clear(); 365 queryTableModel.removeAllRows(); 366 } 367 368 /** This will add the query to the relevant lists. */ 369 public static void addQuery(Query query) { 370 if (containsQuery(query.getID())) { 371 CrimsonUtils.printError("Duplicate query ID '" + query.getID() + "' not added to queryPool"); 372 return; 373 } else { 374 queryPool.put(query.getID(), query); 375 queryTableModel.addRow(query); 376 } 377 } 378 379 /** This will delete the query from the relevant lists. */ 380 public static void deleteQuery(String id) { 381 if (CrimsonUtils.isEmpty(id) || (! containsQuery(id))) return; 382 383 queryPool.remove(id.toUpperCase()); 384 385 // rebuild queryTableModel. This is simpler than trying to 386 // figure out which row needs to be deleted. 387 queryTableModel.removeAllRows(); 388 for (Iterator i = queryPool.values().iterator(); i.hasNext();) { 389 queryTableModel.addRow((Query) i.next()); 390 } 391 } 392 393 /** 394 * Returns true if the Query object exists in the queryPool. 395 */ 396 public static boolean containsQuery(String id) { 397 if (CrimsonUtils.isEmpty(id)) return false; 398 else return queryPool.containsKey(id.toUpperCase()); 399 } 400 401 /** 402 * Returns the Query object for the given ID. 403 * @XXX Should throw an exception if 'id' is not found. 404 */ 405 public static Query getQuery(String id) { 406 if (CrimsonUtils.isEmpty(id)) return null; 407 else return (Query) queryPool.get(id.toUpperCase()); 408 } 409 410 /** Returns the set of all Query IDs. */ 411 public static Set getQueries() { return queryPool.keySet(); } 412 413 /** Returns a iterator over all Querys in queryPool. */ 414 public static Iterator queryIterator() { return queryPool.values().iterator(); } 415 416 /** 417 * Changes the Query's ID in queryPool and queryTableModel. 418 */ 419 /* 420 public static void renameQuery(String oldID, String newID) { 421 Query query = getQuery(oldID); 422 423 // make sure query exists 424 if (query == null) { 425 CrimsonUtils.printMsg("Query \"" + oldID + "\" not found.", CrimsonUtils.ERROR); 426 return; 427 } 428 429 renameQuery(query, newID); 430 } 431 */ 432 433 /** 434 * Changes the Query's ID in queryPool and queryTableModel. 435 * @XXX This needs and SQL component to change the table names. 436 */ 437 /* 438 public static void renameQuery(Query query, String newID) { 439 // make sure query exists 440 if (query == null) { 441 CrimsonUtils.printMsg("Null query, can not be renamed.", CrimsonUtils.ERROR); 442 return; 443 } 444 445 // check to make sure ID doesn't already exist in queryPool 446 if (queryPool.containsKey(newID)) { 447 String msg = "ID \"" + newID + "\" already exists in queryPool."; 448 throw new InvalidIDException(msg); 449 } 450 451 // change ID and add to queryPool 452 // query.id = newID; 453 454 // XXXXXXXX 455 // need to run SQL to change query table names 456 457 // after changing the table names, rebuild the lists 458 buildQueryLists(); 459 } 460 */ 461 462 /** 463 * Uses 'base' to create a random Query ID string that doesn't 464 * already exist in the queryPool. 465 */ 466 public static String randomQueryID(String base) { 467 String id = base + Long.toString(Math.abs(CrimsonUtils.random.nextLong())); 468 469 // loop until we get an id that isn't in the curreent set 470 while (containsQuery(id)) { 471 id = base + Long.toString(Math.abs(CrimsonUtils.random.nextLong())); 472 } 473 return id.toUpperCase(); 474 } 475 476 } // ObjectHandles.java