Coverage Report - org.kuali.rice.kew.doctype.dao.impl.DocumentTypeDAOOjbImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeDAOOjbImpl
0%
0/139
0%
0/52
2.619
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.doctype.dao.impl;
 18  
 
 19  
 import org.apache.log4j.Logger;
 20  
 import org.apache.ojb.broker.PersistenceBroker;
 21  
 import org.apache.ojb.broker.query.Criteria;
 22  
 import org.apache.ojb.broker.query.QueryByCriteria;
 23  
 import org.apache.ojb.broker.query.QueryFactory;
 24  
 import org.apache.ojb.broker.query.ReportQueryByCriteria;
 25  
 import org.kuali.rice.kew.doctype.DocumentTypeAttribute;
 26  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 27  
 import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO;
 28  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 29  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 30  
 import org.springmodules.orm.ojb.OjbFactoryUtils;
 31  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 32  
 
 33  
 import java.math.BigDecimal;
 34  
 import java.sql.Connection;
 35  
 import java.sql.ResultSet;
 36  
 import java.sql.Statement;
 37  
 import java.util.ArrayList;
 38  
 import java.util.Collection;
 39  
 import java.util.Iterator;
 40  
 import java.util.List;
 41  
 
 42  
 
 43  0
 public class DocumentTypeDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentTypeDAO {
 44  
 
 45  0
         public static final Logger LOG = Logger.getLogger(DocumentTypeDAOOjbImpl.class);
 46  
 
 47  
         public void delete(DocumentType documentType) {
 48  0
                 this.getPersistenceBrokerTemplate().delete(documentType);
 49  0
         }
 50  
 
 51  
         public DocumentType findById(Long docTypeId) {
 52  0
                 Criteria crit = new Criteria();
 53  0
                 crit.addEqualTo("documentTypeId", docTypeId);
 54  0
                 return (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit));
 55  
         }
 56  
 
 57  
         public DocumentType findByName(String name) {
 58  0
                 return findByName(name, true);
 59  
         }
 60  
 
 61  
         public DocumentType findByName(String name, boolean caseSensitive) {
 62  0
                 Criteria crit = new Criteria();
 63  0
                 if(!caseSensitive){
 64  0
                         crit.addEqualTo("UPPER(name)", name.trim().toUpperCase());
 65  
                 }else{
 66  0
                         crit.addEqualTo("name", name);
 67  
                 }
 68  0
                 crit.addEqualTo("currentInd", Boolean.TRUE);
 69  0
                 DocumentType docType = (DocumentType) this.getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(DocumentType.class, crit));
 70  0
                 return docType;
 71  
         }
 72  
 
 73  
         public Integer getMaxVersionNumber(String docTypeName) {
 74  0
                 return getMostRecentDocType(docTypeName).getVersion();
 75  
         }
 76  
 
 77  
         public List getChildDocumentTypeIds(Long parentDocumentTypeId) {
 78  0
                 List<Long> childrenIds = new ArrayList<Long>();
 79  0
                 PersistenceBroker broker = getPersistenceBroker(false);
 80  0
                 Connection conn = null;
 81  0
                 Statement st = null;
 82  0
                 ResultSet rs = null;
 83  
                 try {
 84  0
                         conn = broker.serviceConnectionManager().getConnection();
 85  0
                         st = conn.createStatement();
 86  0
                         rs = st.executeQuery("select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = " + parentDocumentTypeId);
 87  0
                         while (rs.next()) {
 88  0
                                 childrenIds.add(new Long(rs.getLong("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  
         public 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(Long 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 DocumentType getMostRecentDocType(Long documentTypeId) {
 209  0
                 Criteria crit = new Criteria();
 210  0
                 crit.addEqualTo("documentTypeId", documentTypeId);
 211  0
                 QueryByCriteria query = new QueryByCriteria(DocumentType.class, crit);
 212  0
                 query.addOrderByDescending("version");
 213  
 
 214  0
                 Iterator docTypes = this.getPersistenceBrokerTemplate().getCollectionByQuery(query).iterator();
 215  0
                 while (docTypes.hasNext()) {
 216  0
                         return (DocumentType) docTypes.next();
 217  
                 }
 218  0
                 return null;
 219  
         }
 220  
 
 221  
         public List findAllCurrentRootDocuments() {
 222  0
                 Criteria crit = new Criteria();
 223  0
                 crit.addIsNull("docTypeParentId");
 224  0
                 return findAllCurrent(crit);
 225  
         }
 226  
 
 227  
     public List findAllCurrent() {
 228  0
         return findAllCurrent(new Criteria());
 229  
     }
 230  
 
 231  
     public List findAllCurrentByName(String name) {
 232  0
         Criteria crit = new Criteria();
 233  0
         crit.addEqualTo("name", name);
 234  0
         return findAllCurrent(crit);
 235  
     }
 236  
 
 237  
     public List<DocumentType> findPreviousInstances(String documentTypeName) {
 238  0
         Criteria crit = new Criteria();
 239  0
         crit.addEqualTo("name", documentTypeName);
 240  0
         crit.addEqualTo("currentInd", Boolean.FALSE);
 241  0
         return findAll(crit);
 242  
     }
 243  
 
 244  
     private List<DocumentType> findAllCurrent(Criteria crit) {
 245  0
         crit.addEqualTo("currentInd", Boolean.TRUE);
 246  0
         return findAll(crit);
 247  
     }
 248  
 
 249  
     private List<DocumentType> findAll(Criteria crit) {
 250  0
         return (List<DocumentType>) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentType.class, crit));
 251  
     }
 252  
 
 253  
     public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) {
 254  0
             Criteria crit = new Criteria();
 255  0
             crit.addEqualTo("ruleAttributeId", ruleAttribute.getRuleAttributeId());
 256  0
             return (List) this.getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(DocumentTypeAttribute.class, crit));
 257  
     }
 258  
 
 259  
     public Long findDocumentTypeIdByDocumentId(String documentId) {
 260  0
             Criteria crit = new Criteria();
 261  0
             crit.addEqualTo("documentId", documentId);
 262  0
             ReportQueryByCriteria query = QueryFactory.newReportQuery(DocumentRouteHeaderValue.class, crit);
 263  0
             query.setAttributes(new String[] { "documentTypeId" });
 264  
 
 265  0
             Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
 266  0
             while (iter.hasNext()) {
 267  0
                 Object[] row = (Object[]) iter.next();
 268  0
                 BigDecimal id = (BigDecimal)row[0];
 269  0
                 return new Long(id.longValue());
 270  
             }
 271  0
             return null;
 272  
     }
 273  
 
 274  
 }