Coverage Report - org.kuali.rice.kew.doctype.dao.impl.DocumentTypeDAOJpaImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentTypeDAOJpaImpl
0%
0/137
0%
0/60
2.609
 
 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 org.apache.log4j.Logger;
 20  
 import org.kuali.rice.core.jpa.criteria.Criteria;
 21  
 import org.kuali.rice.core.jpa.criteria.QueryByCriteria;
 22  
 import org.kuali.rice.core.util.OrmUtils;
 23  
 import org.kuali.rice.kew.doctype.DocumentTypeAttribute;
 24  
 import org.kuali.rice.kew.doctype.DocumentTypePolicy;
 25  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 26  
 import org.kuali.rice.kew.doctype.dao.DocumentTypeDAO;
 27  
 import org.kuali.rice.kew.rule.bo.RuleAttribute;
 28  
 import org.kuali.rice.kew.util.Utilities;
 29  
 
 30  
 import javax.persistence.EntityManager;
 31  
 import javax.persistence.PersistenceContext;
 32  
 import javax.persistence.Query;
 33  
 import java.util.ArrayList;
 34  
 import java.util.Collection;
 35  
 import java.util.Iterator;
 36  
 import java.util.List;
 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 = findByDocId(documentType.getDocumentTypeId());
 63  0
                 entityManager.remove(documentType);
 64  0
         }
 65  
 
 66  
         public DocumentType findByDocId(Long docId) {
 67  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 68  0
                 crit.eq("documentTypeId", docId);
 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(name)", ("%" + name.trim() + "%").toUpperCase());
 80  
 
 81  
                 }else{
 82  0
                         crit.eq("name", name);
 83  
                 }
 84  0
                 crit.eq("currentInd", Boolean.TRUE);
 85  
                 try{
 86  0
                         DocumentType docType = (DocumentType) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
 87  0
                         return docType;
 88  0
                 }catch (javax.persistence.NoResultException e){
 89  0
                    return null;
 90  
                 }
 91  
         }
 92  
 
 93  
         public Integer getMaxVersionNumber(String docTypeName) {
 94  0
                 return getMostRecentDocType(docTypeName).getVersion();
 95  
         }
 96  
 
 97  
         public List getChildDocumentTypeIds(Long parentDocumentTypeId) {
 98  0
                 List childrenIds = new ArrayList();
 99  
                 try {
 100  0
                         String sql = "select DOC_TYP_ID from KREW_DOC_TYP_T where CUR_IND = 1 and PARNT_ID = " + parentDocumentTypeId;
 101  0
                         Query query = entityManager.createNativeQuery(sql);
 102  0
                         List resultIds = query.getResultList();
 103  0
                         for (Object id:resultIds){
 104  0
                                 childrenIds.add(new Long(id.toString()));
 105  
                         }
 106  
 
 107  0
                 } catch (Exception e) {
 108  0
                         LOG.error("Error occured fetching children document type ids for document type " + parentDocumentTypeId, e);
 109  0
                         throw new RuntimeException(e);
 110  0
                 }
 111  
 
 112  0
                 return childrenIds;
 113  
         }
 114  
 
 115  
         public DocumentType getMostRecentDocType(String docTypeName) {
 116  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 117  0
                 crit.eq("name", docTypeName);
 118  0
                 crit.orderBy("version", false);
 119  
 
 120  0
                 Iterator docTypes = new QueryByCriteria(entityManager, crit).toQuery().getResultList().iterator();
 121  0
                 while (docTypes.hasNext()) {
 122  0
                         return (DocumentType) docTypes.next();
 123  
                 }
 124  0
                 return null;
 125  
         }
 126  
 
 127  
         public void save(DocumentType documentType) {
 128  0
                 Collection<DocumentTypePolicy> docPolicies = documentType.getPolicies();
 129  0
                 documentType.setPolicies(new ArrayList<DocumentTypePolicy>());
 130  
 
 131  0
                 if (documentType.getDocumentTypeId() == null){
 132  0
                         entityManager.persist(documentType);
 133  
                 } else {
 134  0
                         for(org.kuali.rice.kew.engine.node.Process process:(List<org.kuali.rice.kew.engine.node.Process>)documentType.getProcesses()){
 135  0
                                 if(process.getInitialRouteNode().getRouteNodeId()==null){
 136  0
                                         process.getInitialRouteNode().setDocumentTypeId(documentType.getDocumentTypeId());
 137  0
                                         entityManager.persist(process.getInitialRouteNode());
 138  
                                 } else {
 139  0
                                         OrmUtils.merge(entityManager, process.getInitialRouteNode());
 140  
                                 }
 141  
                         }
 142  0
                         OrmUtils.merge(entityManager, documentType);
 143  
                 }
 144  
 
 145  0
                 for (DocumentTypePolicy docTypePolicy:docPolicies){
 146  0
                         if (docTypePolicy.getDocumentTypeId() == null){
 147  0
                                 docTypePolicy.setDocumentTypeId(documentType.getDocumentTypeId());
 148  0
                                 entityManager.persist(docTypePolicy);
 149  
                         } else {
 150  0
                                 OrmUtils.merge(entityManager, docTypePolicy);
 151  
                         }
 152  
                 }
 153  
 
 154  0
                 documentType.setPolicies(docPolicies);
 155  0
         }
 156  
 
 157  
         public List findByRouteHeaderId(Long routeHeaderId) {
 158  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 159  0
                 crit.eq("routeHeaderId", routeHeaderId);
 160  0
                 return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 161  
         }
 162  
 
 163  
         public Collection<DocumentType> find(DocumentType documentType, DocumentType docTypeParent, boolean climbHierarchy) {
 164  0
                 LOG.debug("documentType: "+ documentType);
 165  0
                 LOG.debug("docTypeParent: "+ docTypeParent);
 166  0
                 LOG.debug("climbHierarchy: " + climbHierarchy);
 167  
 
 168  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 169  0
                 if (documentType != null && !Utilities.isEmpty(documentType.getLabel())) {
 170  0
                         crit.like("UPPER(label)", documentType.getLabel().trim().toUpperCase());
 171  
                 }
 172  0
                 if (documentType != null && !Utilities.isEmpty(documentType.getName())) {
 173  0
                         String docTypeName = documentType.getName();
 174  0
                         crit.like("UPPER(name)", ("%" + docTypeName.trim() + "%").toUpperCase());
 175  
                 }
 176  0
                 if (documentType != null && documentType.getActive() != null) {
 177  0
                         crit.eq("active", documentType.getActive());
 178  
                 }
 179  0
                 if (documentType != null && documentType.getDocumentTypeId() != null) {
 180  0
                         crit.eq("documentTypeId", documentType.getDocumentTypeId());
 181  
                 }
 182  0
                 if (documentType != null && documentType.getActualServiceNamespace() != null){
 183  0
                         crit.eq("serviceNamespace", documentType.getActualServiceNamespace());
 184  
                 }
 185  0
                 if (docTypeParent != null) {
 186  0
                         if (!"".equals(docTypeParent.getName()) && docTypeParent.getName() != null) {
 187  0
                                 Criteria parentCrit = new Criteria(DocumentType.class.getName());
 188  
                                 //addParentNameOrCriteria(docTypeParent.getName(), parentCrit);
 189  0
                                 addParentIdOrCriteria(docTypeParent.getDocumentTypeId(), parentCrit);
 190  0
                                 if (climbHierarchy) {
 191  0
                                         assembleChildrenCriteria(docTypeParent.getChildrenDocTypes(), parentCrit);
 192  
                                 }
 193  0
                                 parentCrit.eq("currentInd", Boolean.TRUE);
 194  0
                                 crit.and(parentCrit);
 195  0
                         }
 196  
                 } else {
 197  0
                         if (documentType != null && !Utilities.isEmpty(documentType.getName())) {
 198  0
                                 DocumentType searchDocumentType = findByName(documentType.getName());
 199  0
                                 if ((searchDocumentType != null) && climbHierarchy) {
 200  0
                                         LOG.debug("searchDocumentType: "+ searchDocumentType);
 201  0
                                         Criteria criteria = new Criteria(DocumentType.class.getName());
 202  
                                         //addParentNameOrCriteria(searchDocumentType.getName(), criteria);
 203  0
                     addParentIdOrCriteria(searchDocumentType.getDocumentTypeId(), criteria);
 204  0
                     assembleChildrenCriteria(searchDocumentType.getChildrenDocTypes(), criteria);
 205  0
                                         criteria.eq("currentInd", Boolean.TRUE);
 206  0
                                         crit.or(criteria);
 207  
                                 }
 208  
                         }
 209  
                 }
 210  0
                 crit.eq("currentInd", Boolean.TRUE);
 211  0
                 return new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 212  
         }
 213  
 
 214  
     private void addParentIdOrCriteria(Long parentId, Criteria mainCriteria) {
 215  0
         Criteria parentCriteria = new Criteria(DocumentType.class.getName());
 216  0
         parentCriteria.eq("docTypeParentId", parentId);
 217  0
         mainCriteria.or(parentCriteria);
 218  0
     }
 219  
 
 220  
         private void assembleChildrenCriteria(Collection childrenDocTypes, Criteria crit) {
 221  0
                 if (childrenDocTypes != null) {
 222  0
                         Iterator childrenDocTypesIter = childrenDocTypes.iterator();
 223  0
                         while (childrenDocTypesIter.hasNext()) {
 224  0
                                 DocumentType child = (DocumentType) childrenDocTypesIter.next();
 225  0
                                 addParentIdOrCriteria(child.getDocumentTypeId(), crit);
 226  0
                                 assembleChildrenCriteria(child.getChildrenDocTypes(), crit);
 227  0
                         }
 228  
                 }
 229  0
         }
 230  
 
 231  
         public DocumentType getMostRecentDocType(Long documentTypeId) {
 232  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 233  0
                 crit.eq("documentTypeId", documentTypeId);
 234  0
                 crit.orderBy("version", false);
 235  
 
 236  0
                 Iterator docTypes = new QueryByCriteria(entityManager, crit).toQuery().getResultList().iterator();
 237  0
                 while (docTypes.hasNext()) {
 238  0
                         return (DocumentType) docTypes.next();
 239  
                 }
 240  0
                 return null;
 241  
         }
 242  
 
 243  
         public List findAllCurrentRootDocuments() {
 244  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 245  0
                 crit.isNull("docTypeParentId");
 246  0
                 return findAllCurrent(crit);
 247  
         }
 248  
 
 249  
     public List findAllCurrent() {
 250  0
             return findAllCurrent(new Criteria(DocumentType.class.getName()));
 251  
     }
 252  
 
 253  
     public List findAllCurrentByName(String name) {
 254  0
                 Criteria crit = new Criteria(DocumentType.class.getName());
 255  0
                 crit.eq("name", name);
 256  0
                 return findAllCurrent(crit);
 257  
     }
 258  
 
 259  
     public List<DocumentType> findPreviousInstances(String name) {
 260  0
         Criteria crit = new Criteria(DocumentType.class.getName());
 261  0
         crit.eq("name", name);
 262  0
         crit.eq("currentInd", Boolean.FALSE);
 263  0
         return findAll(crit);
 264  
     }
 265  
 
 266  
     private List findAllCurrent(Criteria crit) {
 267  0
         crit.eq("currentInd", Boolean.TRUE);
 268  0
         return findAll(crit);
 269  
     }
 270  
 
 271  
     private List findAll(Criteria crit) {
 272  0
         return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 273  
     }
 274  
 
 275  
     public List findDocumentTypeAttributes(RuleAttribute ruleAttribute) {
 276  0
             Criteria crit = new Criteria(DocumentTypeAttribute.class.getName());
 277  0
             crit.eq("ruleAttributeId", ruleAttribute.getRuleAttributeId());
 278  0
             return (List) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 279  
     }
 280  
 
 281  
     public Long findDocumentTypeIdByDocumentId(Long documentId) {
 282  
             //FIXME: I don't think this does what it's supposed to
 283  0
             Criteria crit = new Criteria(DocumentType.class.getName());
 284  0
             crit.eq("routeHeaderId", documentId);
 285  
 
 286  0
             List<DocumentType> docTypes = new QueryByCriteria(entityManager, crit).toQuery().getResultList();
 287  0
             for (DocumentType docType:docTypes){
 288  0
                 return docType.getDocumentTypeId();
 289  
             }
 290  0
             return null;
 291  
     }
 292  
 
 293  
 }