1 15 package org.apache.tapestry.vlib.ejb.impl; 16 17 import java.sql.Connection ; 18 import java.sql.ResultSet ; 19 import java.sql.SQLException ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import org.apache.tapestry.contrib.ejb.XEJBException; 24 import org.apache.tapestry.contrib.jdbc.IStatement; 25 import org.apache.tapestry.contrib.jdbc.StatementAssembly; 26 import org.apache.tapestry.vlib.ejb.Book; 27 import org.apache.tapestry.vlib.ejb.MasterQueryParameters; 28 import org.apache.tapestry.vlib.ejb.SortOrdering; 29 30 48 49 public class BookQueryBean extends OperationsBean 50 { 51 52 56 57 private Book[] _results; 58 59 63 64 public void ejbRemove() 65 { 66 _results = null; 67 } 68 69 71 75 76 public int getResultCount() 77 { 78 if (_results == null) 79 return 0; 80 81 return _results.length; 82 } 83 84 88 89 public Book[] get(int offset, int length) 90 { 91 Book[] result; 92 93 if (offset < 0) 94 return null; 95 96 int finalLength = Math.min(length, _results.length - offset); 97 98 if (finalLength < 0) 99 return null; 100 101 104 result = new Book[finalLength]; 105 System.arraycopy(_results, offset, result, 0, finalLength); 106 107 return result; 108 } 109 110 115 116 public int masterQuery(MasterQueryParameters parameters, SortOrdering sortOrdering) 117 { 118 IStatement statement = null; 119 Connection connection = null; 120 121 123 _results = null; 124 125 try 126 { 127 connection = getConnection(); 128 129 try 130 { 131 statement = buildMasterQuery(connection, parameters, sortOrdering); 132 } 133 catch (SQLException ex) 134 { 135 throw new XEJBException("Unable to create query statement.", ex); 136 } 137 138 processQuery(statement); 139 140 } 141 finally 142 { 143 close(connection, statement, null); 144 } 145 146 return getResultCount(); 147 } 148 149 153 154 public int ownerQuery(Integer ownerId, SortOrdering sortOrdering) 155 { 156 IStatement statement = null; 157 Connection connection = null; 158 159 161 _results = null; 162 163 try 164 { 165 connection = getConnection(); 166 167 try 168 { 169 statement = buildPersonQuery(connection, "owner.PERSON_ID", ownerId, sortOrdering); 170 } 171 catch (SQLException ex) 172 { 173 throw new XEJBException("Unable to create query statement.", ex); 174 } 175 176 processQuery(statement); 177 178 } 179 finally 180 { 181 close(connection, statement, null); 182 } 183 184 return getResultCount(); 185 } 186 187 191 192 public int holderQuery(Integer holderId, SortOrdering sortOrdering) 193 { 194 IStatement statement = null; 195 Connection connection = null; 196 197 199 _results = null; 200 201 try 202 { 203 connection = getConnection(); 204 205 try 206 { 207 statement = 208 buildPersonQuery(connection, "holder.PERSON_ID", holderId, sortOrdering); 209 } 210 catch (SQLException ex) 211 { 212 throw new XEJBException("Unable to create query statement.", ex); 213 } 214 215 processQuery(statement); 216 217 } 218 finally 219 { 220 close(connection, statement, null); 221 } 222 223 return getResultCount(); 224 } 225 226 public int borrowerQuery(Integer borrowerId, SortOrdering sortOrdering) 227 { 228 IStatement statement = null; 229 Connection connection = null; 230 231 233 _results = null; 234 235 try 236 { 237 connection = getConnection(); 238 239 try 240 { 241 statement = buildBorrowerQuery(connection, borrowerId, sortOrdering); 242 } 243 catch (SQLException ex) 244 { 245 throw new XEJBException("Unable to create query statement.", ex); 246 } 247 248 processQuery(statement); 249 250 } 251 finally 252 { 253 close(connection, statement, null); 254 } 255 256 return getResultCount(); 257 } 258 259 private void processQuery(IStatement statement) 260 { 261 ResultSet set = null; 262 263 try 264 { 265 set = statement.executeQuery(); 266 } 267 catch (SQLException ex) 268 { 269 throw new XEJBException("Unable to execute query.", ex); 270 } 271 272 try 273 { 274 processQueryResults(set); 275 } 276 catch (SQLException ex) 277 { 278 throw new XEJBException("Unable to process query results.", ex); 279 } 280 finally 281 { 282 close(null, null, set); 283 } 284 } 285 286 private void processQueryResults(ResultSet set) throws SQLException 287 { 288 List list = new ArrayList (); 289 Object [] columns = new Object [Book.N_COLUMNS]; 290 291 while (set.next()) 292 { 293 Book book = convertRowToBook(set, columns); 294 295 list.add(book); 296 } 297 298 _results = new Book[list.size()]; 299 _results = (Book[]) list.toArray(_results); 300 } 301 302 private IStatement buildMasterQuery( 303 Connection connection, 304 MasterQueryParameters parameters, 305 SortOrdering ordering) 306 throws SQLException 307 { 308 String title = parameters.getTitle(); 309 String author = parameters.getAuthor(); 310 Integer publisherId = parameters.getPublisherId(); 311 Integer ownerId = parameters.getOwnerId(); 312 313 StatementAssembly assembly = buildBaseBookQuery(); 314 315 addSubstringSearch(assembly, "book.TITLE", title); 316 addSubstringSearch(assembly, "book.AUTHOR", author); 317 318 320 assembly.addSep(" AND "); 321 assembly.add("book.HIDDEN = 0"); 322 323 if (publisherId != null) 324 { 325 assembly.addSep(" AND "); 326 assembly.add("book.PUBLISHER_ID = "); 327 assembly.addParameter(publisherId); 328 } 329 330 if (ownerId != null) 331 { 332 assembly.addSep(" AND "); 333 assembly.add("book.OWNER_ID = "); 334 assembly.addParameter(ownerId); 335 } 336 337 addSortOrdering(assembly, ordering); 338 339 return assembly.createStatement(connection); 340 } 341 342 private IStatement buildPersonQuery( 343 Connection connection, 344 String personColumn, 345 Integer personId, 346 SortOrdering sortOrdering) 347 throws SQLException 348 { 349 StatementAssembly assembly = buildBaseBookQuery(); 350 351 assembly.addSep(" AND "); 352 assembly.add(personColumn); 353 assembly.add(" = "); 354 assembly.addParameter(personId); 355 356 addSortOrdering(assembly, sortOrdering); 357 358 return assembly.createStatement(connection); 359 } 360 361 private IStatement buildBorrowerQuery( 362 Connection connection, 363 Integer borrowerId, 364 SortOrdering sortOrdering) 365 throws SQLException 366 { 367 StatementAssembly assembly = buildBaseBookQuery(); 368 369 371 assembly.addSep(" AND "); 372 assembly.add("book.HOLDER_ID = "); 373 assembly.addParameter(borrowerId); 374 assembly.addSep(" AND "); 375 assembly.add("book.HOLDER_ID <> book.OWNER_ID"); 376 377 addSortOrdering(assembly, sortOrdering); 378 379 return assembly.createStatement(connection); 380 } 381 382 } | Popular Tags |