Coverage Report - org.kuali.rice.kns.dao.impl.DocumentDaoJpa
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentDaoJpa
0%
0/40
0%
0/8
1.545
 
 1  
 /*
 2  
  * Copyright 2005-2008 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.kns.dao.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.persistence.EntityManager;
 23  
 import javax.persistence.PersistenceContext;
 24  
 import javax.persistence.PersistenceException;
 25  
 
 26  
 import org.apache.commons.beanutils.BeanUtils;
 27  
 import org.apache.log4j.Logger;
 28  
 import org.kuali.rice.core.util.OrmUtils;
 29  
 import org.kuali.rice.kim.bo.Group;
 30  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 31  
 import org.kuali.rice.kns.bo.AdHocRoutePerson;
 32  
 import org.kuali.rice.kns.bo.AdHocRouteWorkgroup;
 33  
 import org.kuali.rice.kns.bo.PersistableBusinessObject;
 34  
 import org.kuali.rice.kns.dao.BusinessObjectDao;
 35  
 import org.kuali.rice.kns.dao.DocumentDao;
 36  
 import org.kuali.rice.kns.document.Document;
 37  
 import org.kuali.rice.kns.service.DocumentAdHocService;
 38  
 import org.kuali.rice.kns.util.KNSPropertyConstants;
 39  
 import org.springframework.dao.DataAccessException;
 40  
 
 41  
 /**
 42  
  * This class is the OJB implementation of the DocumentDao interface.
 43  
  */
 44  
 public class DocumentDaoJpa implements DocumentDao {
 45  0
     private static Logger LOG = Logger.getLogger(DocumentDaoJpa.class);
 46  
     private BusinessObjectDao businessObjectDao;
 47  
     private DocumentAdHocService documentAdHocService;
 48  
 
 49  
         @PersistenceContext
 50  
         private EntityManager entityManager;
 51  
 
 52  
     public DocumentDaoJpa(EntityManager entityManager, BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) {
 53  0
         super();
 54  0
         this.entityManager = entityManager;
 55  0
         this.businessObjectDao = businessObjectDao;
 56  0
         this.documentAdHocService = documentAdHocService;
 57  0
     }
 58  
 
 59  
     /*
 60  
      * (non-Javadoc)
 61  
      *
 62  
      * @see org.kuali.dao.DocumentDao#save(null)
 63  
      */
 64  
     public void save(Document document) throws DataAccessException {
 65  0
                 Document attachedDoc = findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
 66  0
                 if (attachedDoc == null) {
 67  0
                         entityManager.persist(document);
 68  
                 } else {
 69  0
                         OrmUtils.reattach(attachedDoc, document);
 70  0
                         entityManager.merge(attachedDoc);
 71  
                 }
 72  0
     }
 73  
 
 74  
         /**
 75  
      * Retrieve a Document of a specific type with a given document header ID.
 76  
      *
 77  
      * @param clazz
 78  
      * @param id
 79  
      * @return Document with given id
 80  
      */
 81  
     public Document findByDocumentHeaderId(Class clazz, String id) throws DataAccessException {
 82  0
         List idList = new ArrayList();
 83  0
         idList.add(id);
 84  
 
 85  0
         List documentList = findByDocumentHeaderIds(clazz, idList);
 86  
 
 87  0
         Document document = null;
 88  0
         if ((null != documentList) && (documentList.size() > 0)) {
 89  0
             document = (Document) documentList.get(0);
 90  
         }
 91  
 
 92  0
         return document;
 93  
     }
 94  
 
 95  
     /**
 96  
      * Retrieve a List of Document instances with the given ids
 97  
      *
 98  
      * @param clazz
 99  
      * @param idList
 100  
      * @return List
 101  
      */
 102  
     public List findByDocumentHeaderIds(Class clazz, List idList) throws DataAccessException {
 103  0
                 org.kuali.rice.core.jpa.criteria.Criteria criteria = new org.kuali.rice.core.jpa.criteria.Criteria(clazz.getName());
 104  0
                 criteria.in(KNSPropertyConstants.DOCUMENT_NUMBER, (List) idList);
 105  0
                 List<Document> list = new ArrayList<Document>();
 106  
                 try {
 107  0
                         list = new org.kuali.rice.core.jpa.criteria.QueryByCriteria(entityManager, criteria).toQuery().getResultList();
 108  0
                 } catch (PersistenceException e) {
 109  0
                         e.printStackTrace();
 110  0
                 }
 111  0
         for (Document doc : list) {
 112  0
                 documentAdHocService.addAdHocs(doc);
 113  0
                         entityManager.refresh(doc);
 114  
         }
 115  0
                 return list;
 116  
     }
 117  
 
 118  
     /**
 119  
      *
 120  
      * Deprecated method. Should use BusinessObjectService.linkAndSave() instead.
 121  
      *
 122  
      */
 123  
     @Deprecated
 124  
     public void saveMaintainableBusinessObject(PersistableBusinessObject businessObject) {
 125  0
             throw new UnsupportedOperationException("Don't use this depricated method.");
 126  
     }
 127  
 
 128  
     public BusinessObjectDao getBusinessObjectDao() {
 129  0
         return businessObjectDao;
 130  
     }
 131  
 
 132  
     public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
 133  0
         this.businessObjectDao = businessObjectDao;
 134  0
     }
 135  
 
 136  
     /**
 137  
      * @return the entityManager
 138  
      */
 139  
     public EntityManager getEntityManager() {
 140  0
         return this.entityManager;
 141  
     }
 142  
 
 143  
     /**
 144  
      * @param entityManager the entityManager to set
 145  
      */
 146  
     public void setEntityManager(EntityManager entityManager) {
 147  0
         this.entityManager = entityManager;
 148  0
     }
 149  
 
 150  
     /**
 151  
          * @return the documentAdHocService
 152  
          */
 153  
         public DocumentAdHocService getDocumentAdHocService() {
 154  0
                 return this.documentAdHocService;
 155  
         }
 156  
 
 157  
     /**
 158  
      * Setter for injecting the DocumentAdHocService
 159  
      * @param dahs
 160  
      */
 161  
     public void setDocumentAdHocService(DocumentAdHocService dahs) {
 162  0
             this.documentAdHocService = dahs;
 163  0
     }
 164  
 
 165  
 }