001/* 002 * Copyright (c) 2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2013-02-21 23:16:13 +0000 (Thu, 21 Feb 2013) $' 007 * '$Revision: 31483 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.objectmanager.library; 031 032import java.sql.ResultSet; 033import java.sql.SQLException; 034import java.sql.Statement; 035import java.util.Hashtable; 036import java.util.Vector; 037 038import org.apache.commons.logging.Log; 039import org.apache.commons.logging.LogFactory; 040import org.kepler.objectmanager.lsid.KeplerLSID; 041 042/** 043 * The LibItem class represents one node of the Component Library Tree. 044 * The data in this class corresponds directly to the columns of the 045 * Library_Index table as well as the data stored in auxiliary tables 046 * through foreign key references. 047 * 048 * @author Aaron Schultz 049 * 050 */ 051public class LibItem { 052 053 private static final Log log = LogFactory.getLog(LibItem.class.getName()); 054 private static final boolean isDebugging = log.isDebugEnabled(); 055 056 /** 057 * the name of the table in the database used to store attributes 058 */ 059 public static final String LIBRARY_ATTRIBUTES_TABLE_NAME = "LIBRARY_ATTRIBUTES"; 060 061 /** 062 * Library Index ID 063 */ 064 private int _liid; 065 066 /** 067 * The parent liid 068 */ 069 private Integer _parent; 070 071 /** 072 * preorder tree traversal left integer 073 */ 074 private int _left; 075 076 /** 077 * preorder tree traversal right integer 078 */ 079 private int _right; 080 081 /** 082 * preorder tree traversal level 083 */ 084 private int _level; 085 086 /** 087 * The default LSID associated with this item. 088 */ 089 private KeplerLSID _lsid; 090 091 /** 092 * The type of item 093 */ 094 private int _type; 095 096 /** 097 * The name of the item 098 */ 099 private String _name; 100 101 /** 102 * Attributes that are associated with this item. 103 */ 104 private Hashtable<String, String> _attributes; 105 106 /** 107 * KeplerLSIDs that are associated with this item. 108 */ 109 private Vector<KeplerLSID> _lsids; 110 111 /** 112 * public constructor 113 */ 114 public LibItem() { 115 _attributes = new Hashtable<String, String>(); 116 _lsids = new Vector<KeplerLSID>(); 117 } 118 119 /** 120 * Update the database row that matches this LibItem liid. This method only 121 * updates LSID,TYPE,NAME columns. 122 * 123 * @throws SQLException 124 */ 125 /* 126 public void update(Statement stmt) throws SQLException { 127 if (getLiid() <= 0) { 128 log.warn("LIID in LibItem was not set before update(stmt) call"); 129 return; 130 } 131 if (getType() <= 0) { 132 log.warn("LIID in LibItem was not set before update(stmt) call"); 133 return; 134 } 135 if (getName() == null || getName().trim() == "") { 136 log.warn("NAME in LibItem was not set before update(stmt) call"); 137 return; 138 } 139 String update = "UPDATE " + LibIndex.LIBRARY_INDEX_TABLE_NAME + " set "; 140 if (getLsid() != null) { 141 update += " LSID = '" + getLsid() + "',"; 142 } 143 update += " TYPE = " + getType(); 144 update += ", NAME = '" + getName() + "'"; 145 update += " WHERE LIID = " + getLiid(); 146 147 if (isDebugging) 148 log.debug(update); 149 stmt.executeUpdate(update); 150 stmt.getConnection().commit(); 151 } 152 */ 153 154 /** 155 * Delete this populated LibItem from the Library_Index table. This will 156 * also delete all the children and update the LFT and RGT indexes of all 157 * the other items in the table. 158 * 159 * @param stmt 160 * @throws SQLException 161 */ 162 public void delete(Statement stmt) throws SQLException { 163 164 // double check the LFT and RGT values 165 String query = "SELECT LFT,RGT FROM " 166 + LibIndex.LIBRARY_INDEX_TABLE_NAME + " WHERE LIID = " 167 + getLiid(); 168 if (isDebugging) 169 log.debug(query); 170 ResultSet rs = null; 171 try { 172 rs = stmt.executeQuery(query); 173 if (rs == null) 174 throw new SQLException("Query Failed: " + query); 175 if (rs.next()) { 176 int l = rs.getInt(1); 177 int r = rs.getInt(2); 178 if (l != getLeft()) 179 log.warn("Left value wasn't set before delete " + getLiid()); 180 if (r != getRight()) 181 log.warn("Right value wasn't set before delete " + getLiid()); 182 setLeft(l); 183 setRight(r); 184 } 185 } finally { 186 if(rs != null) { 187 rs.close(); 188 } 189 } 190 191 // The number of rows to be deleted 192 int delCount = getRight() - getLeft(); 193 194 String delete = "delete from " + LibIndex.LIBRARY_INDEX_TABLE_NAME 195 + " WHERE LFT >= " + getLeft() + " AND RGT <= " + getRight(); 196 197 String updateLeft = "UPDATE " + LibIndex.LIBRARY_INDEX_TABLE_NAME 198 + " SET LFT = LFT - " + (delCount + 1) + " WHERE LFT >= " 199 + getLeft(); 200 String updateRight = "UPDATE " + LibIndex.LIBRARY_INDEX_TABLE_NAME 201 + " SET RGT = RGT - " + (delCount + 1) + " WHERE RGT >= " 202 + getLeft(); 203 204 if (isDebugging) { 205 log.debug("\n" + delete + "\n" + updateLeft + "\n" + updateRight); 206 } 207 208 stmt.executeUpdate(updateLeft); 209 stmt.executeUpdate(updateRight); 210 stmt.executeUpdate(delete); 211 stmt.getConnection().commit(); 212 213 } 214 215 /** 216 * @return the _liid 217 */ 218 public int getLiid() { 219 return _liid; 220 } 221 222 /** 223 * @param liid 224 * the _liid to set 225 */ 226 public void setLiid(int liid) { 227 _liid = liid; 228 } 229 230 /** 231 * @return the _parent 232 */ 233 public Integer getParent() { 234 return _parent; 235 } 236 237 /** 238 * @param parent 239 * the _parent to set 240 */ 241 public void setParent(Integer parent) { 242 _parent = parent; 243 } 244 245 /** 246 * @return the _left 247 */ 248 public int getLeft() { 249 return _left; 250 } 251 252 /** 253 * @param left 254 * the _left to set 255 */ 256 public void setLeft(int left) { 257 _left = left; 258 } 259 260 /** 261 * @return the _right 262 */ 263 public int getRight() { 264 return _right; 265 } 266 267 /** 268 * @param right 269 * the _right to set 270 */ 271 public void setRight(int right) { 272 _right = right; 273 } 274 275 /** 276 * @return the _level 277 */ 278 public int getLevel() { 279 return _level; 280 } 281 282 /** 283 * @param level 284 * the _level to set 285 */ 286 public void setLevel(int level) { 287 _level = level; 288 } 289 290 /** 291 * @return the _lsid 292 */ 293 public KeplerLSID getLsid() { 294 return _lsid; 295 } 296 297 /** 298 * @param lsid 299 * the _lsid to set 300 */ 301 public void setLsid(KeplerLSID lsid) { 302 _lsid = lsid; 303 } 304 305 /** 306 * @return the _type 307 */ 308 public int getType() { 309 return _type; 310 } 311 312 /** 313 * @param type 314 * the _type to set 315 */ 316 public void setType(int type) { 317 _type = type; 318 } 319 320 /** 321 * @return the _name 322 */ 323 public String getName() { 324 return _name; 325 } 326 327 /** 328 * @param name 329 * the _name to set 330 */ 331 public void setName(String name) { 332 _name = name; 333 } 334 335 /** 336 * @param name 337 * @param value 338 */ 339 public void addAttribute(String name, String value) { 340 if (value == null || name == null) return; 341 if (name.trim().equals("")) return; 342 if (_attributes.containsKey(name)) { 343 _attributes.remove(name); 344 } 345 _attributes.put(name, value); 346 } 347 348 /** 349 * @param attributeName 350 * @return 351 */ 352 public String getAttributeValue(String attributeName) { 353 return _attributes.get(attributeName); 354 } 355 356 public Hashtable<String, String> getAttributes() { 357 return _attributes; 358 } 359 360 public Vector<KeplerLSID> getLsids() { 361 return _lsids; 362 } 363 364 public void addLsid(KeplerLSID lsid) { 365 _lsids.add(lsid); 366 } 367 368 public void removeLsid(KeplerLSID lsid) { 369 _lsids.remove(lsid); 370 } 371 372 public String toString() { 373 return getName(); 374 } 375 376 public String debugString() { 377 String s = new String(); 378 s += "LibItem: \n"; 379 s += getLiid() + ", "; 380 s += getParent() + ", "; 381 s += getLeft() + ", "; 382 s += getRight() + ", "; 383 s += getLevel() + ", "; 384 s += getLsid() + ", "; 385 s += getType() + ", "; 386 s += getName() + "\n"; 387 s += " Attributes -> \n"; 388 for (String attName : getAttributes().keySet()) { 389 s += " " + attName + ": " + getAttributes().get(attName) 390 + "\n"; 391 } 392 s += " Lsids -> \n"; 393 for (KeplerLSID lsid : getLsids()) { 394 s += " " + lsid.toString() + "\n"; 395 } 396 return s; 397 } 398}