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 * @(#)Models.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 import java.sql.SQLException; 030 031 /** 032 * Functions related to the MODELS table. 033 * 034 * @XXX Should see if can optimize db access by maintaining one 035 * Statement that is created every time a database connection is made. 036 * 037 * @author Stephen Fisher 038 * @version $Id: Models.java,v 1.24 2009/07/16 16:19:52 fisher Exp $ 039 */ 040 041 public class Models { 042 043 //-------------------------------------------------------------------------- 044 // Miscellaneous Methods 045 046 /** This will load all models from the MODELS table. */ 047 public static void loadAll() { 048 // make sure a database is open. We don't need to check for 049 // the existance of the MODELS database because this was done 050 // when the database was opened. 051 if (! Database.isOpen()) { 052 CrimsonUtils.printError("Unable to build model lists, no open database."); 053 return; 054 } 055 056 // clear the lists before we start 057 ObjectHandles.clearModelLists(); 058 059 CrimsonUtils.printMsg("Loading model info..."); 060 061 // get the list of models in the MODELS table 062 String sql = "SELECT ID, NOTES, DETAILS FROM MODELS"; 063 ArrayList results = Database.sqlToArray(sql); 064 065 // if the query failed then exit 066 if (results == null) { 067 CrimsonUtils.printError("Unable to build model lists, error accessing MODELS table."); 068 return; 069 } 070 071 try { 072 // convert each row in the results to a Model 073 for (Iterator rows = results.iterator(); rows.hasNext();) { 074 ArrayList row = (ArrayList) rows.next(); 075 // create the model and set the notes without saving the model 076 Model model = new Model(String.valueOf(row.get(0)), false); 077 model.setNotes(String.valueOf(row.get(1)), false); 078 079 // load the CLOB data and again don't save the model 080 model.setDetails(Database.readClob(row.get(2)), false); 081 } 082 083 } catch (SQLException e) { 084 CrimsonUtils.printError("SQL error loading CLOB in MODELS."); 085 CrimsonUtils.printError(e.getMessage()); 086 return; 087 } 088 } 089 090 /** 091 * This will publish the specified model to the MODELS table in 092 * the current database. This will replace any model object with 093 * the same name, already in the table. 094 */ 095 public static void save(Model model) { 096 if (model == null) { 097 CrimsonUtils.printError("Unable to save 'null' model ."); 098 return; 099 } 100 101 // make sure a database is open. We don't need to check for 102 // the existance of the MODELS database because this was done 103 // when the database was opened. 104 if (! Database.isOpen()) { 105 CrimsonUtils.printError("Can't save model, no open database."); 106 return; 107 } 108 109 String id = model.getID().toUpperCase(); 110 String sql; 111 // if model already exists then update, else insert 112 if (dbContains(id)) { 113 // update existing model 114 sql = "UPDATE models SET"; 115 sql += " notes = '" + model.getNotes() + "'"; 116 sql += "WHERE id = '" + id + "' "; 117 118 } else { // insert new model 119 sql = "INSERT INTO models "; 120 sql += "(id, notes) VALUES ('" + id + "', '"; 121 sql += model.getNotes() + "')"; 122 } 123 124 // UPDATE the VARCHAR2 fields 125 if (! Database.execUpdate(sql)) { 126 CrimsonUtils.printError("Model NOT saved."); 127 return; 128 } 129 130 // UPDATE the CLOB 131 if (! Database.writeClob("MODELS", id, "details", model.getDetails())) { 132 CrimsonUtils.printError("Can't update details. Model NOT saved."); 133 return; 134 } 135 136 // if we get this far then we didn't error anywhere 137 CrimsonUtils.printMsg("Model saved: " + id); 138 } 139 140 /** Removes a model from the database. */ 141 public static boolean delete(String id) { 142 if (! Database.delete("MODELS", id)) return false; 143 144 // rebuild table list 145 loadAll(); 146 147 return true; 148 } 149 150 /** 151 * This will return true if a model exists in MODELS with an id 152 * equal to 'id'. 153 */ 154 public static boolean dbContains(String id) { 155 ArrayList out = Database.sqlToArray("SELECT id FROM MODELS WHERE id = '" + id.toUpperCase() + "'"); 156 if ((out == null) || (out.size() == 0)) return false; 157 else return true; 158 } 159 160 /** 161 * Returns the non-CLOB columns in the MODELS table. Each table 162 * record will be separated by a '\n' in the output. 163 */ 164 public String toString() { 165 // SELECT id, notes FROM models; 166 String sql = "SELECT ID, NOTES FROM MODELS"; 167 ArrayList results = Database.sqlToArray(sql); 168 169 if (results == null) { 170 CrimsonUtils.printError("Error printing MODELS table."); 171 return ""; 172 } 173 174 StringBuffer out = new StringBuffer(""); 175 out.append("ID \t NOTES\n"); 176 for (Iterator rows = results.iterator(); rows.hasNext();) { 177 ArrayList row = (ArrayList) rows.next(); 178 out.append(String.valueOf(row.get(0)) + " \t "); 179 out.append(String.valueOf(row.get(1)) + "\n"); 180 } 181 182 return out.toString().trim(); 183 } 184 185 } // Models.java