Coverage Report - org.kuali.rice.kew.doctype.dao.impl.DocumentTypeDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeDAOJpaImpl
0%
0/122
0%
0/56
2.292
 
 1  
 /*
 2  
  * Copyright 2005-2008 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 java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import javax.persistence.EntityManager;
 25  
 import javax.persistence.PersistenceContext;
 26  
 import javax.persistence.Query;
 27  
 
 28  
 import org.apache.log4j.Logger;
 29  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 30  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
 31  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
 32  
 import org.kuali.rice.kew.doctype.DocumentTypeAttribute;
 33  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 34  
 import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO;
 35  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 36  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 37  
 
 38  
 
 39  0
 public class DocumentTypeDAOJpaImpl implements DocumentTypeDAO {
 40  
 
 41  0
         public static final Logger LOG = Logger.getLogger(DocumentTypeDAOJpaImpl.class);
 42  
 
 43  
         @PersistenceContext(unitName="kew-unit")
 44  
         private EntityManager entityManager;
 45  
 
 46  
 
 47  
         /**
 48  
          * @return the entityManager
 49  
          */
 50  
         public EntityManager getEntityManager() {
 51  0
                 return this.entityManager;
 52  
         }
 53  
 
 54  
         /**
 55  
          * @param entityManager the entityManager to set
 56  
          */
 57  
         public void setEntityManager(EntityManager entityManager) {
 58  0
                 this.entityManager = entityManager;
 59  0
         }
 60  
 
 61  
         public void delete(DocumentType documentType) {
 62  0
                 DocumentType docType = findById(documentType.getDocumentTypeId());
 63  0
                 entityManager.remove(documentType);
 64  0
         }
 65  
 
 66  
         public DocumentType findById(String docTypeId) {
 67  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 68  0
                 crit.eq("documentTypeId", docTypeId);
 69  0
                         return (DocumentType) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 70  
         }
 71  
 
 72  
         public DocumentType findByName(String name){
 73  0
                 return findByName(name, true); // by default find by name is case sensitive
 74  
         }
 75  
 
 76  
         public DocumentType findByName(String name, boolean caseSensitive) {
 77  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 78  0
                 if(!caseSensitive){
 79  0
                         crit.like("UPPER(__JPA_ALIAS[[0]]__.name)", ("%" + name.trim() + "%").toUpperCase());
 80  
 
 81  
                 }else{
 82  0
                         crit.eq("name", name);
 83  
                 }
 84  0
                 crit.eq("currentInd", Boolean.TRUE);
 85  0
                 DocumentType docType = (DocumentType) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 86  0
                 return docType;
 87  
         }
 88  
 
 89  
         public Integer getMaxVersionNumber(String docTypeName) {
 90  0
                 return getMostRecentDocType(docTypeName).getVersion();
 91  
         }
 92  
 
 93  
         public List<String> getChildDocumentTypeIds(String parentDocumentTypeId) {
 94  0
                 List<String> childrenIds = new ArrayList<String>();
 95  
                 try {
 96  0
                         String sql = "select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = " + parentDocumentTypeId;
 97  0
                         Query query = entityManager.createNativeQuery(sql);
 98  0
                         List resultIds = query.getResultList();
 99  0
                         for (Object id : resultIds){
 100  0
                                 childrenIds.add(id.toString());
 101  
                         }
 102  
 
 103  0
                 } catch (Exception e) {
 104  0
                         LOG.error("Error occured fetching children document type ids for document type " + parentDocumentTypeId, e);
 105  0
                         throw new RuntimeException(e);
 106  0
                 }
 107  
 
 108  0
                 return childrenIds;
 109  
         }
 110  
 
 111  
         protected DocumentType getMostRecentDocType(String docTypeName) {
 112  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 113  0
                 crit.eq("name", docTypeName);
 114  0
                 crit.orderBy("version", false);
 115  
 
 116  0
                 Iterator docTypes = new QueryByCriteria(entityManager, crit).toQuery().getResultList().iterator();
 117  0
                 while (docTypes.hasNext()) {
 118  0
                         return (DocumentType) docTypes.next();
 119  
                 }
 120  0
                 return null;
 121  
         }
 122  
 
 123  
         public void save(DocumentType documentType) {
 124  0
                 if (documentType.getDocumentTypeId() == null){
 125  0
                         entityManager.persist(documentType);
 126  
                 } else {
 127  0
                         OrmUtils.merge(entityManager, documentType);
 128  
                 }
 129  0
         }
 130  
 
 131  
         public List findByDocumentId(String documentId) {
 132  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 133  0
                 crit.eq("documentId", documentId);
 134  0
                 return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 135  
         }
 136  
 
 137  
         public Collection<DocumentType> find(DocumentType documentType, DocumentType docTypeParent, boolean climbHierarchy) {
 138  0
                 LOG.debug("documentType: "+ documentType);
 139  0
                 LOG.debug("docTypeParent: "+ docTypeParent);
 140  0
                 LOG.debug("climbHierarchy: " + climbHierarchy);
 141  
 
 142  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 143  0
                 if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getLabel())) {
 144  0
                         crit.like("UPPER(__JPA_ALIAS[[0]]__.label)", documentType.getLabel().trim().toUpperCase());
 145  
                 }
 146  0
                 if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) {
 147  0
                         String docTypeName = documentType.getName();
 148  0
                         crit.like("UPPER(__JPA_ALIAS[[0]]__.name)", ("%" + docTypeName.trim() + "%").toUpperCase());
 149  
                 }
 150  0
                 if (documentType != null && documentType.getActive() != null) {
 151  0
                         crit.eq("active", documentType.getActive());
 152  
                 }
 153  0
                 if (documentType != null && documentType.getDocumentTypeId() != null) {
 154  0
                         crit.eq("documentTypeId", documentType.getDocumentTypeId());
 155  
                 }
 156  0
                 if (documentType != null && documentType.getActualApplicationId() != null){
 157  0
                         crit.eq("applicationId", documentType.getActualApplicationId());
 158  
                 }
 159  0
                 if (docTypeParent != null) {
 160  0
                         if (!"".equals(docTypeParent.getName()) && docTypeParent.getName() != null) {
 161  0
                                 Criteria parentCrit = new Criteria(DocumentType.class.getName());
 162  
                                 //addParentNameOrCriteria(docTypeParent.getName(), parentCrit);
 163  0
                                 addParentIdOrCriteria(docTypeParent.getDocumentTypeId(), parentCrit);
 164  0
                                 if (climbHierarchy) {
 165  0
                                         assembleChildrenCriteria(docTypeParent.getChildrenDocTypes(), parentCrit);
 166  
                                 }
 167  0
                                 parentCrit.eq("currentInd", Boolean.TRUE);
 168  0
                                 crit.and(parentCrit);
 169  0
                         }
 170  
                 } else {
 171  0
                         if (documentType != null && !org.apache.commons.lang.StringUtils.isEmpty(documentType.getName())) {
 172  0
                                 DocumentType searchDocumentType = findByName(documentType.getName());
 173  0
                                 if ((searchDocumentType != null) && climbHierarchy) {
 174  0
                                         LOG.debug("searchDocumentType: "+ searchDocumentType);
 175  0
                                         Criteria criteria = new Criteria(DocumentType.class.getName());
 176  
                                         //addParentNameOrCriteria(searchDocumentType.getName(), criteria);
 177  0
                     addParentIdOrCriteria(searchDocumentType.getDocumentTypeId(), criteria);
 178  0
                     assembleChildrenCriteria(searchDocumentType.getChildrenDocTypes(), criteria);
 179  0
                                         criteria.eq("currentInd", Boolean.TRUE);
 180  0
                                         crit.or(criteria);
 181  
                                 }
 182  
                         }
 183  
                 }
 184  0
                 crit.eq("currentInd", Boolean.TRUE);
 185  0
                 return new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 186  
         }
 187  
 
 188  
     private void addParentIdOrCriteria(String parentId, Criteria mainCriteria) {
 189  0
         Criteria parentCriteria = new Criteria(DocumentType.class.getName());
 190  0
         parentCriteria.eq("docTypeParentId", parentId);
 191  0
         mainCriteria.or(parentCriteria);
 192  0
     }
 193  
 
 194  
         private void assembleChildrenCriteria(Collection childrenDocTypes, Criteria crit) {
 195  0
                 if (childrenDocTypes != null) {
 196  0
                         Iterator childrenDocTypesIter = childrenDocTypes.iterator();
 197  0
                         while (childrenDocTypesIter.hasNext()) {
 198  0
                                 DocumentType child = (DocumentType) childrenDocTypesIter.next();
 199  0
                                 addParentIdOrCriteria(child.getDocumentTypeId(), crit);
 200  0
                                 assembleChildrenCriteria(child.getChildrenDocTypes(), crit);
 201  0
                         }
 202  
                 }
 203  0
         }
 204  
 
 205  
         
 206  
 
 207  
         public List findAllCurrentRootDocuments() {
 208  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 209  0
                 crit.isNull("docTypeParentId");
 210  0
                 return findAllCurrent(crit);
 211  
         }
 212  
 
 213  
     public List findAllCurrent() {
 214  0
             return findAllCurrent(new Criteria(DocumentType.class.getName()));
 215  
     }
 216  
 
 217  
     public List findAllCurrentByName(String name) {
 218  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 219  0
                 crit.eq("name", name);
 220  0
                 return findAllCurrent(crit);
 221  
     }
 222  
 
 223  
     public List<DocumentType> findPreviousInstances(String name) {
 224  0
         Criteria crit = new Criteria(DocumentType.class.getName());
 225  0
         crit.eq("name", name);
 226  0
         crit.eq("currentInd", Boolean.FALSE);
 227  0
         return findAll(crit);
 228  
     }
 229  
 
 230  
     private List findAllCurrent(Criteria crit) {
 231  0
         crit.eq("currentInd", Boolean.TRUE);
 232  0
         return findAll(crit);
 233  
     }
 234  
 
 235  
     private List findAll(Criteria crit) {
 236  0
         return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 237  
     }
 238  
 
 239  
     public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) {
 240  0
             Criteria crit = new Criteria(DocumentTypeAttribute.class.getName());
 241  0
             if (ruleAttribute.getRuleAttributeId() != null) {
 242  0
                     crit.eq("ruleAttributeId", ruleAttribute.getRuleAttributeId());
 243  
             }
 244  0
             return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 245  
     }
 246  
 
 247  
     public String findDocumentTypeIdByDocumentId(String documentId) {
 248  0
             Criteria crit = new Criteria(DocumentRouteHeaderValue.class.getName());
 249  0
             crit.eq("documentId", documentId);
 250  
 
 251  0
             final DocumentRouteHeaderValue docHeader = (DocumentRouteHeaderValue)new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 252  0
             return (docHeader != null) ? docHeader.getDocumentTypeId() : null;
 253  
     }
 254  
     
 255  
     public String findDocumentTypeIdByName(String documentTypeName) {
 256  0
             Criteria crit = new Criteria(DocumentType.class.getName());
 257  0
             crit.eq("name", documentTypeName);
 258  
 
 259  0
             final DocumentType documentType = (DocumentType)new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 260  0
             return (documentType != null) ? documentType.getDocumentTypeId() : null;
 261  
     }
 262  
     
 263  
     public String findDocumentTypeNameById(String documentTypeId) {
 264  0
         Criteria crit = new Criteria(DocumentType.class.getName());
 265  0
         crit.eq("documentTypeId", documentTypeId);
 266  
 
 267  0
         final DocumentType documentType = (DocumentType)new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 268  0
         return (documentType != null) ? documentType.getName() : null;
 269  
     }
 270  
 
 271  
 }