Coverage Report - org.kuali.rice.kew.routeheader.dao.impl.DocumentRouteHeaderDAOOjbImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentRouteHeaderDAOOjbImpl
0%
0/208
0%
0/60
4.25
DocumentRouteHeaderDAOOjbImpl$1
0%
0/18
0%
0/2
4.25
DocumentRouteHeaderDAOOjbImpl$2
0%
0/2
N/A
4.25
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.routeheader.dao.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.commons.lang.exception.ExceptionUtils;
 20  
 import org.apache.ojb.broker.OptimisticLockException;
 21  
 import org.apache.ojb.broker.PersistenceBroker;
 22  
 import org.apache.ojb.broker.accesslayer.LookupException;
 23  
 import org.apache.ojb.broker.query.Criteria;
 24  
 import org.apache.ojb.broker.query.QueryByCriteria;
 25  
 import org.apache.ojb.broker.query.QueryFactory;
 26  
 import org.apache.ojb.broker.query.ReportQueryByCriteria;
 27  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 28  
 import org.kuali.rice.core.api.util.RiceConstants;
 29  
 import org.kuali.rice.core.framework.persistence.platform.DatabasePlatform;
 30  
 import org.kuali.rice.kew.actionitem.ActionItem;
 31  
 import org.kuali.rice.kew.actionlist.service.ActionListService;
 32  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 33  
 import org.kuali.rice.kew.api.action.ActionRequestStatus;
 34  
 import org.kuali.rice.kew.api.exception.LockingException;
 35  
 import org.kuali.rice.kew.docsearch.SearchableAttributeValue;
 36  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 37  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent;
 38  
 import org.kuali.rice.kew.routeheader.dao.DocumentRouteHeaderDAO;
 39  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 40  
 import org.springframework.dao.CannotAcquireLockException;
 41  
 import org.springmodules.orm.ojb.OjbFactoryUtils;
 42  
 import org.springmodules.orm.ojb.PersistenceBrokerCallback;
 43  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 44  
 
 45  
 import java.sql.Connection;
 46  
 import java.sql.PreparedStatement;
 47  
 import java.sql.ResultSet;
 48  
 import java.sql.SQLException;
 49  
 import java.sql.Statement;
 50  
 import java.util.ArrayList;
 51  
 import java.util.Collection;
 52  
 import java.util.Iterator;
 53  
 import java.util.Set;
 54  
 
 55  0
 public class DocumentRouteHeaderDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentRouteHeaderDAO {
 56  
 
 57  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentRouteHeaderDAOOjbImpl.class);
 58  
 
 59  
     public void saveRouteHeader(DocumentRouteHeaderValue routeHeader) {
 60  0
         if ( LOG.isDebugEnabled() ) {
 61  0
             LOG.debug( "About to Save the route Header: " + routeHeader.getDocumentId() + " / version=" + routeHeader.getVersionNumber() );
 62  0
             DocumentRouteHeaderValue currHeader = findRouteHeader(routeHeader.getDocumentId());
 63  0
             if ( currHeader != null ) {
 64  0
                 LOG.debug( "Current Header Version: " + currHeader.getVersionNumber() );
 65  
 //                for ( SearchableAttributeValue s : currHeader.get() ) {
 66  
 //                    LOG.debug( "SA: " + s.getSearchableAttributeValueId() + " / version=" + s.get )
 67  
 //                }
 68  
             } else {
 69  0
                 LOG.debug( "Current Header: null" );
 70  
             }
 71  0
             LOG.debug( ExceptionUtils.getStackTrace(new Throwable()) );
 72  
         }
 73  
         try {
 74  0
             getPersistenceBrokerTemplate().store(routeHeader);
 75  0
             routeHeader.getDocumentContent().setDocumentId(routeHeader.getDocumentId());
 76  0
             getPersistenceBrokerTemplate().store(routeHeader.getDocumentContent());
 77  0
         } catch ( RuntimeException ex ) {
 78  0
             if ( ex.getCause() instanceof OptimisticLockException ) {
 79  0
                  LOG.error( "Optimistic Locking Exception saving document header or content. Offending object: " + ((OptimisticLockException)ex.getCause()).getSourceObject() 
 80  
                  + "; DocumentId = " + routeHeader.getDocumentId() + " ;  Version Number = " + routeHeader.getVersionNumber());
 81  
             }
 82  0
             LOG.error( "Unable to save document header or content. Route Header: " + routeHeader, ex );
 83  0
             throw ex;
 84  0
         }
 85  0
     }
 86  
 
 87  
     public DocumentRouteHeaderValueContent getContent(String documentId) {
 88  0
             Criteria crit = new Criteria();
 89  0
         crit.addEqualTo("documentId", documentId);
 90  0
         return (DocumentRouteHeaderValueContent)this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentRouteHeaderValueContent.class, crit));
 91  
     }
 92  
 
 93  
     public void clearRouteHeaderSearchValues(String documentId) {
 94  0
         Criteria crit = new Criteria();
 95  0
         crit.addEqualTo("documentId", documentId);
 96  0
         QueryByCriteria query = new QueryByCriteria(SearchableAttributeValue.class, crit);
 97  0
         query.addOrderByAscending("searchableAttributeValueId");
 98  0
         Collection<SearchableAttributeValue> results = this.getPersistenceBrokerTemplate().getCollectionByQuery(query);
 99  0
         if (!results.isEmpty()) {
 100  0
             for (SearchableAttributeValue srchAttrVal: results) {
 101  0
                 this.getPersistenceBrokerTemplate().delete(srchAttrVal);
 102  
             }
 103  
         }
 104  0
     }
 105  
 
 106  
     public void lockRouteHeader(final String documentId, final boolean wait) {
 107  
 
 108  
         /*
 109  
          * String sql = (wait ? LOCK_SQL_WAIT : LOCK_SQL_NOWAIT); try { getJdbcTemplate().update(sql, new Object[] { documentId }); } catch (CannotAcquireLockException e) { throw new LockingException("Could not aquire lock on document, documentId=" + documentId, e); }
 110  
          */
 111  
 
 112  0
             this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
 113  
             public Object doInPersistenceBroker(PersistenceBroker broker) {
 114  0
                 PreparedStatement statement = null;
 115  
                 try {
 116  0
                     Connection connection = broker.serviceConnectionManager().getConnection();
 117  0
                     String sql = getPlatform().getLockRouteHeaderQuerySQL(documentId, wait);
 118  0
                     statement = connection.prepareStatement(sql);
 119  0
                     statement.setString(1, documentId);
 120  0
                     statement.execute();
 121  0
                     return null;
 122  0
                 } catch (SQLException e) {
 123  0
                     throw new LockingException("Could not aquire lock on document, documentId=" + documentId, e);
 124  0
                 } catch (LookupException e) {
 125  0
                     throw new LockingException("Could not aquire lock on document, documentId=" + documentId, e);
 126  0
                 } catch (CannotAcquireLockException e) {
 127  0
                     throw new LockingException("Could not aquire lock on document, documentId=" + documentId, e);
 128  
                 } finally {
 129  0
                     if (statement != null) {
 130  
                         try {
 131  0
                             statement.close();
 132  0
                         } catch (SQLException e) {
 133  0
                         }
 134  
                     }
 135  
                 }
 136  
             }
 137  
         });
 138  
 
 139  0
     }
 140  
 
 141  
     public DocumentRouteHeaderValue findRouteHeader(String documentId) {
 142  0
             return findRouteHeader(documentId, false);
 143  
     }
 144  
 
 145  
     public DocumentRouteHeaderValue findRouteHeader(String documentId, boolean clearCache) {
 146  0
         Criteria crit = new Criteria();
 147  0
         crit.addEqualTo("documentId", documentId);
 148  0
         if (clearCache) {
 149  0
                 this.getPersistenceBrokerTemplate().clearCache();
 150  
         }
 151  0
         return (DocumentRouteHeaderValue) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentRouteHeaderValue.class, crit));
 152  
     }
 153  
 
 154  
     public Collection<DocumentRouteHeaderValue> findRouteHeaders(Collection<String> documentIds) {
 155  0
             return findRouteHeaders(documentIds, false);
 156  
     }
 157  
     
 158  
     public Collection<DocumentRouteHeaderValue> findRouteHeaders(Collection<String> documentIds, boolean clearCache) {
 159  0
             if (documentIds == null || documentIds.isEmpty()) {
 160  0
                     return null;
 161  
             }
 162  0
             Criteria crit = new Criteria();
 163  0
             crit.addIn("documentId", documentIds);
 164  0
             if (clearCache) {
 165  0
                 this.getPersistenceBrokerTemplate().clearCache();
 166  
         }
 167  0
             return (Collection<DocumentRouteHeaderValue>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentRouteHeaderValue.class, crit));
 168  
     }
 169  
 
 170  
     public void deleteRouteHeader(DocumentRouteHeaderValue routeHeader) {
 171  
             // need to clear action list cache for users who have this item in their action list
 172  0
             ActionListService actionListSrv = KEWServiceLocator.getActionListService();
 173  0
             Collection actionItems = actionListSrv.findByDocumentId(routeHeader.getDocumentId());
 174  0
             for (Iterator iter = actionItems.iterator(); iter.hasNext();) {
 175  0
                     ActionItem actionItem = (ActionItem) iter.next();
 176  
                     try {
 177  0
                             KEWServiceLocator.getUserOptionsService().saveRefreshUserOption(actionItem.getPrincipalId());
 178  0
                     } catch (Exception e) {
 179  0
                             LOG.error("error saving refreshUserOption", e);
 180  0
                     }
 181  0
             }
 182  0
             this.getPersistenceBrokerTemplate().delete(routeHeader);
 183  0
     }
 184  
 
 185  
     public String getNextDocumentId() {
 186  0
         return (String)this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
 187  
             public Object doInPersistenceBroker(PersistenceBroker broker) {
 188  0
                     return getPlatform().getNextValSQL("KREW_DOC_HDR_S", broker).toString();
 189  
                     }
 190  
         });
 191  
     }
 192  
 
 193  
     protected DatabasePlatform getPlatform() {
 194  0
             return (DatabasePlatform)GlobalResourceLoader.getService(RiceConstants.DB_PLATFORM);
 195  
     }
 196  
 
 197  
     public Collection<String> findPendingByResponsibilityIds(Set<String> responsibilityIds) {
 198  0
         Collection<String> documentIds = new ArrayList();
 199  0
         if (responsibilityIds.isEmpty()) {
 200  0
             return documentIds;
 201  
         }
 202  0
         PersistenceBroker broker = null;
 203  0
         Connection conn = null;
 204  0
         Statement statement = null;
 205  0
         ResultSet rs = null;
 206  
         try {
 207  0
             broker = getPersistenceBroker(false);
 208  0
             conn = broker.serviceConnectionManager().getConnection();
 209  0
             String respIds = "(";
 210  0
             int index = 0;
 211  0
             for (String responsibilityId : responsibilityIds) {
 212  0
                 respIds += responsibilityId + (index == responsibilityIds.size()-1 ? "" : ",");
 213  0
                 index++;
 214  
             }
 215  0
             respIds += ")";
 216  0
             String query = "SELECT DISTINCT(doc_hdr_id) FROM KREW_ACTN_RQST_T "+
 217  
                     "WHERE (STAT_CD='" +
 218  
                     ActionRequestStatus.INITIALIZED.getCode()+
 219  
                     "' OR STAT_CD='"+
 220  
                     ActionRequestStatus.ACTIVATED.getCode()+
 221  
                     "') AND RSP_ID IN "+respIds;
 222  0
             LOG.debug("Query to find pending documents for requeue: " + query);
 223  0
             statement = conn.createStatement();
 224  0
             rs = statement.executeQuery(query);
 225  0
             while (rs.next()) {
 226  0
                     documentIds.add(rs.getString(1));
 227  
             }
 228  0
         } catch (SQLException sqle) {
 229  0
             LOG.error("SQLException: " + sqle.getMessage(), sqle);
 230  0
             throw new WorkflowRuntimeException(sqle);
 231  0
         } catch (LookupException le) {
 232  0
             LOG.error("LookupException: " + le.getMessage(), le);
 233  0
             throw new WorkflowRuntimeException(le);
 234  
         } finally {
 235  0
                 if (rs != null) {
 236  
                 try {
 237  0
                     rs.close();
 238  0
                 } catch (SQLException e) {
 239  0
                     LOG.warn("Could not close result set.");
 240  0
                 }
 241  
             }
 242  0
             if (statement != null) {
 243  
                 try {
 244  0
                     statement.close();
 245  0
                 } catch (SQLException e) {
 246  0
                     LOG.warn("Could not close statement.");
 247  0
                 }
 248  
             }
 249  
             try {
 250  0
                 if (broker != null) {
 251  0
                     OjbFactoryUtils.releasePersistenceBroker(broker, this.getPersistenceBrokerTemplate().getPbKey());
 252  
                 }
 253  0
             } catch (Exception e) {
 254  0
                 LOG.error("Failed closing connection: " + e.getMessage(), e);
 255  0
             }
 256  0
         }
 257  0
         return documentIds;
 258  
     }
 259  
 
 260  
     public boolean hasSearchableAttributeValue(String documentId, String searchableAttributeKey, String searchableAttributeValue) {
 261  0
             Criteria crit = new Criteria();
 262  0
         crit.addEqualTo("documentId", documentId);
 263  0
         crit.addEqualTo("searchableAttributeKey", searchableAttributeKey);
 264  0
         Collection results = getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(SearchableAttributeValue.class, crit));
 265  0
         if (!results.isEmpty()) {
 266  0
             for (Iterator iterator = results.iterator(); iterator.hasNext();) {
 267  0
                 SearchableAttributeValue attribute = (SearchableAttributeValue) iterator.next();
 268  0
                 if (StringUtils.equals(attribute.getSearchableAttributeDisplayValue(), searchableAttributeValue)) {
 269  0
                     return true;
 270  
                 }
 271  0
             }
 272  
         }
 273  0
         return false;
 274  
     }
 275  
 
 276  
     public String getApplicationIdByDocumentId(String documentId) {
 277  0
             if (documentId == null) {
 278  0
                     throw new IllegalArgumentException("Encountered a null document ID.");
 279  
             }
 280  0
             String applicationId = null;
 281  0
         PersistenceBroker broker = null;
 282  0
         Connection conn = null;
 283  0
         PreparedStatement statement = null;
 284  0
         ResultSet rs = null;
 285  
         try {
 286  0
             broker = this.getPersistenceBroker(false);
 287  0
             conn = broker.serviceConnectionManager().getConnection();
 288  0
             String query = "SELECT DT.APPL_ID FROM KREW_DOC_TYP_T DT, KREW_DOC_HDR_T DH "+
 289  
                     "WHERE DH.DOC_TYP_ID=DT.DOC_TYP_ID AND "+
 290  
                     "DH.DOC_HDR_ID=?";
 291  0
             statement = conn.prepareStatement(query);
 292  0
             statement.setString(1, documentId);
 293  0
             rs = statement.executeQuery();
 294  0
             if (rs.next()) {
 295  0
                     applicationId = rs.getString(1);
 296  0
                 if (rs.wasNull()) {
 297  0
                         applicationId = null;
 298  
                 }
 299  
             }
 300  0
         } catch (SQLException sqle) {
 301  0
             LOG.error("SQLException: " + sqle.getMessage(), sqle);
 302  0
             throw new WorkflowRuntimeException(sqle);
 303  0
         } catch (LookupException le) {
 304  0
             LOG.error("LookupException: " + le.getMessage(), le);
 305  0
             throw new WorkflowRuntimeException(le);
 306  
         } finally {
 307  0
                 if (rs != null) {
 308  
                 try {
 309  0
                     rs.close();
 310  0
                 } catch (SQLException e) {
 311  0
                     LOG.warn("Could not close result set.");
 312  0
                 }
 313  
             }
 314  0
             if (statement != null) {
 315  
                 try {
 316  0
                     statement.close();
 317  0
                 } catch (SQLException e) {
 318  0
                     LOG.warn("Could not close statement.");
 319  0
                 }
 320  
             }
 321  
             try {
 322  0
                 if (broker != null) {
 323  0
                     OjbFactoryUtils.releasePersistenceBroker(broker, this.getPersistenceBrokerTemplate().getPbKey());
 324  
                 }
 325  0
             } catch (Exception e) {
 326  0
                 LOG.error("Failed closing connection: " + e.getMessage(), e);
 327  0
             }
 328  0
         }
 329  0
         return applicationId;
 330  
     }
 331  
 
 332  
     public String getDocumentStatus(String documentId) {
 333  0
         Criteria crit = new Criteria();
 334  0
             crit.addEqualTo("documentId", documentId);
 335  0
             ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, crit);
 336  0
             query.setAttributes(new String[] { "docRouteStatus" });
 337  0
             String status = null;
 338  0
             Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
 339  0
             while (iter.hasNext()) {
 340  0
                 Object[] row = (Object[]) iter.next();
 341  0
                 status = (String)row[0];
 342  0
             }
 343  0
             return status;
 344  
     }
 345  
     
 346  
     public String getAppDocId(String documentId) {
 347  0
                   Criteria crit = new Criteria();
 348  0
                   crit.addEqualTo("documentId", documentId);
 349  0
                   ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, crit);
 350  0
                   query.setAttributes(new String[] { "appDocId" });
 351  0
                   String appDocId = null;
 352  0
                   Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
 353  0
                   while (iter.hasNext()) {
 354  0
                           Object[] row = (Object[]) iter.next();
 355  0
                           appDocId = (String)row[0];
 356  0
                   }
 357  0
                   return appDocId;
 358  
           }
 359  
     
 360  
     public void save(SearchableAttributeValue searchableAttributeValue) {
 361  0
             getPersistenceBrokerTemplate().store(searchableAttributeValue);
 362  0
     }
 363  
 
 364  
         public Collection findByDocTypeAndAppId(String documentTypeName,
 365  
                         String appId) {
 366  0
         Collection documentIds = new ArrayList();
 367  
 
 368  0
         PersistenceBroker broker = null;
 369  0
         Connection conn = null;
 370  0
         ResultSet rs = null;
 371  
         try {
 372  0
             broker = getPersistenceBroker(false);
 373  0
             conn = broker.serviceConnectionManager().getConnection();
 374  
 
 375  0
             String query = 
 376  
                              "SELECT DISTINCT " +
 377  
                             "    (docHdr.doc_hdr_id) " +
 378  
                             "FROM " +
 379  
                             "    KREW_DOC_HDR_T docHdr, " +
 380  
                             "    KREW_DOC_TYP_T docTyp " +
 381  
                             "WHERE " +
 382  
                             "    docHdr.APP_DOC_ID     = ? " +
 383  
                             "    AND docHdr.DOC_TYP_ID = docTyp.DOC_TYP_ID " +
 384  
                             "    AND docTyp.DOC_TYP_NM = ?";
 385  
             
 386  0
             LOG.debug("Query to find documents by app id: " + query);
 387  
             
 388  0
             PreparedStatement stmt = conn.prepareStatement(query);
 389  0
             stmt.setString(1, appId);
 390  0
             stmt.setString(2, documentTypeName);
 391  0
             rs = stmt.executeQuery();
 392  
             
 393  0
             while (rs.next()) {
 394  0
                     documentIds.add(new String(rs.getString(1)));
 395  
             }
 396  0
             rs.close();
 397  0
         } catch (SQLException sqle) {
 398  0
             LOG.error("SQLException: " + sqle.getMessage(), sqle);
 399  0
             throw new WorkflowRuntimeException(sqle);
 400  0
         } catch (LookupException le) {
 401  0
             LOG.error("LookupException: " + le.getMessage(), le);
 402  0
             throw new WorkflowRuntimeException(le);
 403  
         } finally {
 404  0
             try {
 405  0
                 if (broker != null) {
 406  0
                     OjbFactoryUtils.releasePersistenceBroker(broker, this.getPersistenceBrokerTemplate().getPbKey());
 407  
                 }
 408  0
             } catch (Exception e) {
 409  0
                 LOG.error("Failed closing connection: " + e.getMessage(), e);
 410  0
             }
 411  0
         }
 412  0
         return documentIds;
 413  
         }
 414  
 
 415  
 }