Coverage Report - org.kuali.rice.kew.doctype.dao.impl.DocumentTypeDAOOjbImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeDAOOjbImpl
0%
0/156
0%
0/54
2.636
 
 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.doctype.dao.impl;
 17  
 
 18  
 import java.sql.Connection;
 19  
 import java.sql.PreparedStatement;
 20  
 import java.sql.ResultSet;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collection;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.log4j.Logger;
 27  
 import org.apache.ojb.broker.PersistenceBroker;
 28  
 import org.apache.ojb.broker.accesslayer.ReportQueryRsIterator;
 29  
 import org.apache.ojb.broker.query.Criteria;
 30  
 import org.apache.ojb.broker.query.QueryByCriteria;
 31  
 import org.apache.ojb.broker.query.QueryFactory;
 32  
 import org.apache.ojb.broker.query.ReportQueryByCriteria;
 33  
 import org.kuali.rice.kew.doctype.DocumentTypeAttribute;
 34  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 35  
 import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO;
 36  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 37  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 38  
 import org.springmodules.orm.ojb.OjbFactoryUtils;
 39  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 40  
 
 41  
 
 42  0
 public class DocumentTypeDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentTypeDAO {
 43  
 
 44  0
         public static final Logger LOG = Logger.getLogger(DocumentTypeDAOOjbImpl.class);
 45  
 
 46  
         public void delete(DocumentType documentType) {
 47  0
                 this.getPersistenceBrokerTemplate().delete(documentType);
 48  0
         }
 49  
 
 50  
         public DocumentType findById(String docTypeId) {
 51  0
                 Criteria crit = new Criteria();
 52  0
                 crit.addEqualTo("documentTypeId", docTypeId);
 53  0
                 return (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit));
 54  
         }
 55  
 
 56  
         public DocumentType findByName(String name) {
 57  0
                 return findByName(name, true);
 58  
         }
 59  
 
 60  
         public DocumentType findByName(String name, boolean caseSensitive) {
 61  0
                 Criteria crit = new Criteria();
 62  0
                 if(!caseSensitive){
 63  0
                         crit.addEqualTo("UPPER(name)", name.trim().toUpperCase());
 64  
                 }else{
 65  0
                         crit.addEqualTo("name", name);
 66  
                 }
 67  0
                 crit.addEqualTo("currentInd", Boolean.TRUE);
 68  0
                 DocumentType docType = (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit));
 69  0
                 return docType;
 70  
         }
 71  
 
 72  
         public Integer getMaxVersionNumber(String docTypeName) {
 73  0
                 return getMostRecentDocType(docTypeName).getVersion();
 74  
         }
 75  
 
 76  
         public List<String> getChildDocumentTypeIds(String parentDocumentTypeId) {
 77  0
                 List<String> childrenIds = new ArrayList<String>();
 78  0
                 PersistenceBroker broker = getPersistenceBroker(false);
 79  0
                 Connection conn = null;
 80  0
                 PreparedStatement st = null;
 81  0
                 ResultSet rs = null;
 82  
                 try {
 83  0
                         conn = broker.serviceConnectionManager().getConnection();
 84  0
                         st = conn.prepareStatement("select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = ?");
 85  0
                         st.setString(1, parentDocumentTypeId);
 86  0
                         rs = st.executeQuery();
 87  0
                         while (rs.next()) {
 88  0
                                 childrenIds.add(rs.getString("DOC_TYP_ID"));
 89  
                         }
 90  0
                 } catch (Exception e) {
 91  0
                         LOG.error("Error occured fetching children document type ids for document type " + parentDocumentTypeId, e);
 92  0
                         throw new RuntimeException(e);
 93  
                 } finally {
 94  0
                         try {
 95  0
                                 st.close();
 96  0
                         } catch (Exception e) {
 97  0
                                 LOG.warn("Failed to close Statement", e);
 98  0
                         }
 99  
 
 100  
                         try {
 101  0
                                 rs.close();
 102  0
                         } catch (Exception e) {
 103  0
                                 LOG.warn("Failed to close Resultset", e);
 104  0
                         }
 105  
 
 106  0
                 if (broker != null) {
 107  
                         try {
 108  0
                                 OjbFactoryUtils.releasePersistenceBroker(broker, this.getPersistenceBrokerTemplate().getPbKey());
 109  0
                         } catch (Exception e) {
 110  0
                                 LOG.error("Failed closing connection: " + e.getMessage(), e);
 111  0
                         }
 112  
                 }
 113  
                 }
 114  0
                 return childrenIds;
 115  
         }
 116  
 
 117  
         protected DocumentType getMostRecentDocType(String docTypeName) {
 118  0
                 Criteria crit = new Criteria();
 119  0
                 crit.addEqualTo("name", docTypeName);
 120  0
                 QueryByCriteria query = new QueryByCriteria(DocumentType.class, crit);
 121  0
                 query.addOrderByDescending("version");
 122  
 
 123  0
                 Iterator docTypes = this.getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator();
 124  0
                 while (docTypes.hasNext()) {
 125  0
                         return (DocumentType) docTypes.next();
 126  
                 }
 127  0
                 return null;
 128  
         }
 129  
 
 130  
         public void save(DocumentType documentType) {
 131  0
                 this.getPersistenceBrokerTemplate().store(documentType);
 132  0
         }
 133  
 
 134  
         public List findByDocumentId(String documentId) {
 135  0
                 Criteria crit = new Criteria();
 136  0
                 crit.addEqualTo("documentId", documentId);
 137  0
                 return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit));
 138  
         }
 139  
 
 140  
         public Collection<DocumentType> find(DocumentType documentType, DocumentType docTypeParent, boolean climbHierarchy) {
 141  0
                 LOG.debug("documentType: "+ documentType);
 142  0
                 LOG.debug("docTypeParent: "+ docTypeParent);
 143  0
                 LOG.debug("climbHierarchy: " + climbHierarchy);
 144  
 
 145  0
                 Criteria crit = new Criteria();
 146  0
                 if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getLabel())) {
 147  0
                         crit.addLike("UPPER(label)", documentType.getLabel().trim().toUpperCase());
 148  
                 }
 149  0
                 if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) {
 150  0
                         String docTypeName = documentType.getName();
 151  0
                         crit.addLike("UPPER(name)", ("%" + docTypeName.trim() + "%").toUpperCase());
 152  
                 }
 153  0
                 if (documentType != null && documentType.getActive() != null) {
 154  0
                         crit.addEqualTo("active", documentType.getActive());
 155  
                 }
 156  0
                 if (documentType != null && documentType.getDocumentTypeId() != null) {
 157  0
                         crit.addEqualTo("documentTypeId", documentType.getDocumentTypeId());
 158  
                 }
 159  0
                 if (documentType != null && documentType.getActualApplicationId() != null){
 160  0
                         crit.addEqualTo("actualApplicationIde", documentType.getActualApplicationId());
 161  
                 }
 162  0
                 if (docTypeParent != null) {
 163  0
                         if (!"".equals(docTypeParent.getName()) && docTypeParent.getName() != null) {
 164  0
                                 Criteria parentCrit = new Criteria();
 165  
                                 //addParentNameOrCriteria(docTypeParent.getName(), parentCrit);
 166  0
                                 addParentIdOrCriteria(docTypeParent.getDocumentTypeId(), parentCrit);
 167  0
                                 if (climbHierarchy) {
 168  0
                                         assembleChildrenCriteria(docTypeParent.getChildrenDocTypes(), parentCrit);
 169  
                                 }
 170  0
                                 parentCrit.addEqualTo("currentInd", Boolean.TRUE);
 171  0
                                 crit.addAndCriteria(parentCrit);
 172  0
                         }
 173  
                 } else {
 174  0
                         if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) {
 175  0
                                 DocumentType searchDocumentType = findByName(documentType.getName());
 176  0
                                 if ((searchDocumentType != null) && climbHierarchy) {
 177  0
                                         LOG.debug("searchDocumentType: "+ searchDocumentType);
 178  0
                                         Criteria criteria = new Criteria();
 179  
                                         //addParentNameOrCriteria(searchDocumentType.getName(), criteria);
 180  0
                     addParentIdOrCriteria(searchDocumentType.getDocumentTypeId(), criteria);
 181  0
                     assembleChildrenCriteria(searchDocumentType.getChildrenDocTypes(), criteria);
 182  0
                                         criteria.addEqualTo("currentInd", Boolean.TRUE);
 183  0
                                         crit.addOrCriteria(criteria);
 184  
                                 }
 185  
                         }
 186  
                 }
 187  0
                 crit.addEqualTo("currentInd", Boolean.TRUE);
 188  0
                 return (Collection<DocumentType>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit));
 189  
         }
 190  
 
 191  
     private void addParentIdOrCriteria(String parentId, Criteria mainCriteria) {
 192  0
         Criteria parentCriteria = new Criteria();
 193  0
         parentCriteria.addEqualTo("docTypeParentId", parentId);
 194  0
         mainCriteria.addOrCriteria(parentCriteria);
 195  0
     }
 196  
 
 197  
         private void assembleChildrenCriteria(Collection childrenDocTypes, Criteria crit) {
 198  0
                 if (childrenDocTypes != null) {
 199  0
                         Iterator childrenDocTypesIter = childrenDocTypes.iterator();
 200  0
                         while (childrenDocTypesIter.hasNext()) {
 201  0
                                 DocumentType child = (DocumentType) childrenDocTypesIter.next();
 202  0
                                 addParentIdOrCriteria(child.getDocumentTypeId(), crit);
 203  0
                                 assembleChildrenCriteria(child.getChildrenDocTypes(), crit);
 204  0
                         }
 205  
                 }
 206  0
         }
 207  
 
 208  
         public List findAllCurrentRootDocuments() {
 209  0
                 Criteria crit = new Criteria();
 210  0
                 crit.addIsNull("docTypeParentId");
 211  0
                 return findAllCurrent(crit);
 212  
         }
 213  
 
 214  
     public List findAllCurrent() {
 215  0
         return findAllCurrent(new Criteria());
 216  
     }
 217  
 
 218  
     public List findAllCurrentByName(String name) {
 219  0
         Criteria crit = new Criteria();
 220  0
         crit.addEqualTo("name", name);
 221  0
         return findAllCurrent(crit);
 222  
     }
 223  
 
 224  
     public List<DocumentType> findPreviousInstances(String documentTypeName) {
 225  0
         Criteria crit = new Criteria();
 226  0
         crit.addEqualTo("name", documentTypeName);
 227  0
         crit.addEqualTo("currentInd", Boolean.FALSE);
 228  0
         return findAll(crit);
 229  
     }
 230  
 
 231  
     private List<DocumentType> findAllCurrent(Criteria crit) {
 232  0
         crit.addEqualTo("currentInd", Boolean.TRUE);
 233  0
         return findAll(crit);
 234  
     }
 235  
 
 236  
     private List<DocumentType> findAll(Criteria crit) {
 237  0
         return (List<DocumentType>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit));
 238  
     }
 239  
 
 240  
     public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) {
 241  0
             Criteria crit = new Criteria();
 242  0
             crit.addEqualTo("ruleAttributeId", ruleAttribute.getId());
 243  0
             return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentTypeAttribute.class, crit));
 244  
     }
 245  
 
 246  
     public String findDocumentTypeIdByDocumentId(String documentId) {
 247  0
             Criteria crit = new Criteria();
 248  0
             crit.addEqualTo("documentId", documentId);
 249  0
             ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, crit);
 250  0
             query.setAttributes(new String[] { "documentTypeId" });
 251  
 
 252  0
             ReportQueryRsIterator iter = (ReportQueryRsIterator)getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
 253  
         try {
 254  0
             while (iter.hasNext()) {
 255  0
                 Object[] row = (Object[]) iter.next();
 256  0
                 return (String)row[0];
 257  
             }
 258  
             } finally {
 259  0
                 iter.releaseDbResources();
 260  0
             }
 261  0
             return null;
 262  
     }
 263  
     
 264  
     public String findDocumentTypeIdByName(String documentTypeName) {
 265  0
             Criteria crit = new Criteria();
 266  0
             crit.addEqualTo("name", documentTypeName);
 267  0
         crit.addEqualTo("currentInd", Boolean.TRUE);
 268  0
             ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentType.class, crit);
 269  0
             query.setAttributes(new String[] { "documentTypeId" });
 270  
 
 271  0
             ReportQueryRsIterator iter = (ReportQueryRsIterator)getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
 272  
             try {
 273  0
                     while (iter.hasNext()) {
 274  0
                             Object[] row = (Object[]) iter.next();
 275  0
                             return (String)row[0];
 276  
                     }
 277  
             } finally {
 278  0
                     iter.releaseDbResources();
 279  0
             }
 280  0
             return null;
 281  
     }
 282  
     
 283  
     public String findDocumentTypeNameById(String documentTypeId) {
 284  0
         Criteria crit = new Criteria();
 285  0
         crit.addEqualTo("documentTypeId", documentTypeId);
 286  0
         ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentType.class, crit);
 287  0
         query.setAttributes(new String[] { "name" });
 288  
 
 289  0
         ReportQueryRsIterator iter = (ReportQueryRsIterator)getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
 290  
         try {
 291  0
             while (iter.hasNext()) {
 292  0
                 Object[] row = (Object[]) iter.next();
 293  0
                 return (String)row[0];
 294  
             }
 295  
         } finally {
 296  0
             iter.releaseDbResources();
 297  0
         }
 298  0
         return null;
 299  
     }
 300  
 
 301  
 }