1 16 package org.apache.juddi.datastore.jdbc; 17 18 import java.sql.Connection ; 19 import java.sql.PreparedStatement ; 20 import java.sql.ResultSet ; 21 import java.sql.Timestamp ; 22 import java.util.Vector ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.juddi.datatype.business.BusinessEntity; 27 28 31 class BusinessEntityTable 32 { 33 private static Log log = LogFactory.getLog(BusinessEntityTable.class); 35 36 static String insertSQL = null; 37 static String deleteSQL = null; 38 static String selectSQL = null; 39 static String selectByPublisherSQL = null; 40 static String verifyOwnershipSQL = null; 41 static String selectPublisherSQL = null; 42 43 static 44 { 45 StringBuffer sql = null; 47 48 sql = new StringBuffer (150); 50 sql.append("INSERT INTO BUSINESS_ENTITY ("); 51 sql.append("BUSINESS_KEY,"); 52 sql.append("AUTHORIZED_NAME,"); 53 sql.append("PUBLISHER_ID,"); 54 sql.append("OPERATOR,"); 55 sql.append("LAST_UPDATE) "); 56 sql.append("VALUES (?,?,?,?,?)"); 57 insertSQL = sql.toString(); 58 59 sql = new StringBuffer (100); 61 sql.append("DELETE FROM BUSINESS_ENTITY "); 62 sql.append("WHERE BUSINESS_KEY=?"); 63 deleteSQL = sql.toString(); 64 65 sql = new StringBuffer (200); 67 sql.append("SELECT "); 68 sql.append("AUTHORIZED_NAME,"); 69 sql.append("OPERATOR "); 70 sql.append("FROM BUSINESS_ENTITY "); 71 sql.append("WHERE BUSINESS_KEY=?"); 72 selectSQL = sql.toString(); 73 74 sql = new StringBuffer (200); 76 sql.append("SELECT "); 77 sql.append("BUSINESS_KEY "); 78 sql.append("FROM BUSINESS_ENTITY "); 79 sql.append("WHERE PUBLISHER_ID=?"); 80 selectByPublisherSQL = sql.toString(); 81 82 sql = new StringBuffer (200); 84 sql.append("SELECT "); 85 sql.append("* "); 86 sql.append("FROM BUSINESS_ENTITY "); 87 sql.append("WHERE BUSINESS_KEY=? "); 88 sql.append("AND PUBLISHER_ID=?"); 89 verifyOwnershipSQL = sql.toString(); 90 91 sql = new StringBuffer (200); 93 sql.append("SELECT "); 94 sql.append("PUBLISHER_ID "); 95 sql.append("FROM BUSINESS_ENTITY "); 96 sql.append("WHERE BUSINESS_KEY=?"); 97 selectPublisherSQL = sql.toString(); 98 } 99 100 108 public static void insert(BusinessEntity business,String publisherID,Connection connection) 109 throws java.sql.SQLException 110 { 111 PreparedStatement statement = null; 112 Timestamp timeStamp = new Timestamp (System.currentTimeMillis()); 113 114 try 115 { 116 statement = connection.prepareStatement(insertSQL); 117 statement.setString(1,business.getBusinessKey()); 118 statement.setString(2,business.getAuthorizedName()); 119 statement.setString(3,publisherID); 120 statement.setString(4,business.getOperator()); 121 statement.setTimestamp(5,timeStamp); 122 123 log.debug("insert into BUSINESS_ENTITY table:\n\n\t" + insertSQL + 124 "\n\t BUSINESS_KEY=" + business.getBusinessKey() + 125 "\n\t AUTHORIZED_NAME=" + business.getAuthorizedName() + 126 "\n\t PUBLISHER_ID=" + publisherID + 127 "\n\t OPERATOR=" + business.getOperator() + 128 "\n\t LAST_UPDATE=" + timeStamp.getTime() + "\n"); 129 130 statement.executeUpdate(); 132 } 133 finally 134 { 135 try { 136 statement.close(); 137 } 138 catch (Exception e) 139 { 140 log.warn("An Exception was encountered while attempting to close " + 141 "the Insert BusinessEntity PreparedStatement: "+e.getMessage(),e); 142 } 143 } 144 } 145 146 153 public static void delete(String businessKey,Connection connection) 154 throws java.sql.SQLException 155 { 156 PreparedStatement statement = null; 157 158 try 159 { 160 statement = connection.prepareStatement(deleteSQL); 162 statement.setString(1,businessKey.toString()); 163 164 log.debug("delete from BUSINESS_ENTITY table:\n\n\t" + deleteSQL + 165 "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n"); 166 167 statement.executeUpdate(); 169 } 170 finally 171 { 172 try { 173 statement.close(); 174 } 175 catch (Exception e) 176 { 177 log.warn("An Exception was encountered while attempting to close " + 178 "the Delete BusinessEntity PreparedStatement: "+e.getMessage(),e); 179 } 180 } 181 } 182 183 190 public static BusinessEntity select(String businessKey,Connection connection) 191 throws java.sql.SQLException 192 { 193 BusinessEntity business = null; 194 PreparedStatement statement = null; 195 ResultSet resultSet = null; 196 197 try 198 { 199 statement = connection.prepareStatement(selectSQL); 200 statement.setString(1,businessKey.toString()); 201 202 log.debug("select from BUSINESS_ENTITY table:\n\n\t" + selectSQL + 203 "\n\t BUSINESS_KEY=" + businessKey.toString() + "\n"); 204 205 resultSet = statement.executeQuery(); 206 if (resultSet.next()) 207 { 208 business = new BusinessEntity(); 209 business.setBusinessKey(businessKey); 210 business.setAuthorizedName(resultSet.getString(1)); business.setOperator(resultSet.getString(2)); } 213 214 return business; 215 } 216 finally 217 { 218 try { 219 resultSet.close(); 220 statement.close(); 221 } 222 catch (Exception e) 223 { 224 log.warn("An Exception was encountered while attempting to close " + 225 "the Select BusinessEntity ResultSet and PreparedStatement: "+e.getMessage(),e); 226 } 227 } 228 } 229 230 239 public static Vector selectByPublisherID(String publisherID,Connection connection) 240 throws java.sql.SQLException 241 { 242 Vector keyList = new Vector (); 243 PreparedStatement statement = null; 244 ResultSet resultSet = null; 245 246 try 247 { 248 statement = connection.prepareStatement(selectByPublisherSQL); 250 statement.setString(1,publisherID); 251 252 log.debug("select from BUSINESS_ENTITY table:\n\n\t" + selectByPublisherSQL + 253 "\n\t PUBLISHER_ID=" + publisherID + "\n"); 254 255 resultSet = statement.executeQuery(); 257 while (resultSet.next()) 258 keyList.add(resultSet.getString(1)); 260 return keyList; 261 } 262 finally 263 { 264 try { 265 resultSet.close(); 266 statement.close(); 267 } 268 catch (Exception e) 269 { 270 log.warn("An Exception was encountered while attempting to close " + 271 "the Select BusinessEntity ResultSet and PreparedStatement: "+e.getMessage(),e); 272 } 273 } 274 } 275 276 285 public static boolean verifyOwnership(String businessKey,String publisherID,Connection connection) 286 throws java.sql.SQLException 287 { 288 if ((businessKey == null) || (publisherID == null)) 289 return false; 290 291 boolean authorized = false; 292 PreparedStatement statement = null; 293 ResultSet resultSet = null; 294 295 try 296 { 297 statement = connection.prepareStatement(verifyOwnershipSQL); 298 statement.setString(1,businessKey); 299 statement.setString(2,publisherID); 300 301 log.debug("checking ownership of BUSINESS_ENTITY:\n\n\t" + verifyOwnershipSQL + 302 "\n\t BUSINESS_KEY=" + businessKey + 303 "\n\t PUBLISHER_ID=" + publisherID + "\n"); 304 305 resultSet = statement.executeQuery(); 306 if (resultSet.next()) 307 authorized = true; 308 309 return authorized; 310 } 311 finally 312 { 313 try { 314 resultSet.close(); 315 statement.close(); 316 } 317 catch (Exception e) { } 318 } 319 } 320 321 332 public static String selectPublisherID(String businessKey,Connection connection) 333 throws java.sql.SQLException 334 { 335 if (businessKey == null) 336 return null; 337 338 String publisherID = null; 339 PreparedStatement statement = null; 340 ResultSet resultSet = null; 341 342 try 343 { 344 statement = connection.prepareStatement(selectPublisherSQL); 345 statement.setString(1,businessKey); 346 347 log.debug("fetching publishers ID for BUSINESS_ENTITY:\n\n\t" + selectPublisherSQL + 348 "\n\t BUSINESS_KEY=" + businessKey + "\n"); 349 350 resultSet = statement.executeQuery(); 351 if (resultSet.next()) 352 publisherID = resultSet.getString(1); 354 return publisherID; 355 } 356 finally 357 { 358 try { 359 resultSet.close(); 360 statement.close(); 361 } 362 catch (Exception e) { } 363 } 364 } 365 } 366 | Popular Tags |