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 * @(#)Partitions.java 022 */ 023 024 package edu.upenn.crimson.io; 025 026 import edu.upenn.crimson.*; 027 import java.util.ArrayList; 028 import java.util.Iterator; 029 030 /** 031 * Functions related to the PARTITIONS table. 032 * 033 * @author Stephen Fisher 034 * @version $Id: Partitions.java,v 1.12 2009/07/16 16:19:52 fisher Exp $ 035 */ 036 037 public class Partitions { 038 039 //-------------------------------------------------------------------------- 040 // Miscellaneous Methods 041 042 /** This will load all partitions from the PARTITIONS table. */ 043 public static void loadAll() { 044 // make sure a database is open. We don't need to check for 045 // the existance of the PARTITIONS database because this was done 046 // when the database was opened. 047 if (! Database.isOpen()) { 048 CrimsonUtils.printError("Unable to build partition list, no open database."); 049 return; 050 } 051 052 // clear the lists before we start 053 ObjectHandles.clearPartitionLists(); 054 055 CrimsonUtils.printMsg("Loading partition info..."); 056 057 // get the list of partitions in the PARTITIONS table 058 String sql = "SELECT id, tree_id, model_id, notes, length FROM PARTITIONS"; 059 ArrayList results = Database.sqlToArray(sql); 060 061 // if the query failed then exit 062 if (results == null) { 063 CrimsonUtils.printError("Unable to build partition lists, error accessing PARTITIONS table."); 064 return; 065 } 066 067 // convert each row in the results to a Partition 068 for (Iterator rows = results.iterator(); rows.hasNext();) { 069 ArrayList row = (ArrayList) rows.next(); 070 071 String id = String.valueOf(row.get(0)); 072 String treeID = String.valueOf(row.get(1)); 073 String modelID = String.valueOf(row.get(2)); 074 String notes = String.valueOf(row.get(3)); 075 int length = Integer.parseInt(String.valueOf(row.get(4))); 076 new Partition(id, ObjectHandles.getTree(treeID), 077 ObjectHandles.getModel(modelID), length, notes); 078 } 079 } 080 081 /** 082 * Removes a partition from the database. When partitions are 083 * deleted, the database will automatically remove the related 084 * entries in the PART_DATA table. 085 */ 086 public static boolean delete(String id) { 087 if (! Database.isOpen()) { 088 CrimsonUtils.printError("Can not delete " + id + ", no open database."); 089 return false; 090 } 091 092 if (! Database.execUpdate("DELETE FROM PART_DATA WHERE partition_id = '" + id.toUpperCase() + "'")) { 093 CrimsonUtils.printError("Error deleting " + id + " from PART_DATA"); 094 return false; 095 } 096 097 // if successfully delete all partition data, then delete the partition 098 if (! Database.delete("PARTITIONS", id)) return false; 099 100 // rebuild table lists 101 Trees.loadAll(); 102 loadAll(); 103 104 return true; 105 } 106 107 /** 108 * This will return true if a partition exists in PARTITIONS with an id 109 * equal to 'id'. 110 */ 111 public static boolean dbContains(String id) { 112 ArrayList out = Database.sqlToArray("SELECT id FROM PARTITIONS WHERE id = '" + id.toUpperCase() + "'"); 113 if ((out == null) || (out.size() == 0)) return false; 114 else return true; 115 } 116 117 /** 118 * Returns the data in the PARTITIONS table. Each table record 119 * will be separated by a '\n' in the output. 120 */ 121 public String toString() { 122 // SELECT * FROM partitions; 123 String sql = "SELECT ID, TREE_ID, MODEL_ID, NOTES, LENGTH FROM PARTITIONS"; 124 ArrayList results = Database.sqlToArray(sql); 125 126 if (results == null) { 127 CrimsonUtils.printError("Error printing PARTITION table."); 128 return ""; 129 } 130 131 StringBuffer out = new StringBuffer(""); 132 out.append("ID \t TREE_ID \t MODEL_ID \t NOTES \t LENGTH\n"); 133 for (Iterator rows = results.iterator(); rows.hasNext();) { 134 ArrayList row = (ArrayList) rows.next(); 135 for (int i = 0; i < 4; i++) out.append(String.valueOf(row.get(i)) + " \t "); 136 out.append(String.valueOf(row.get(4)) + "\n"); 137 } 138 139 return out.toString().trim(); 140 } 141 142 } // Partitions.java