Coverage Report - org.kuali.rice.krad.dao.impl.DocumentDaoJpa
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentDaoJpa
0%
0/33
0%
0/4
1.4
 
 1  
 /*
 2  
  * Copyright 2006-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.krad.dao.impl;
 17  
 
 18  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 19  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
 20  
 import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
 21  
 import org.kuali.rice.krad.dao.BusinessObjectDao;
 22  
 import org.kuali.rice.krad.dao.DocumentDao;
 23  
 import org.kuali.rice.krad.document.Document;
 24  
 import org.kuali.rice.krad.service.DocumentAdHocService;
 25  
 import org.kuali.rice.krad.util.KRADPropertyConstants;
 26  
 import org.springframework.dao.DataAccessException;
 27  
 
 28  
 import javax.persistence.EntityManager;
 29  
 import javax.persistence.PersistenceContext;
 30  
 import javax.persistence.PersistenceException;
 31  
 import java.util.ArrayList;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * This class is the OJB implementation of the DocumentDao interface.
 36  
  */
 37  
 public class DocumentDaoJpa implements DocumentDao {
 38  
     
 39  
         private BusinessObjectDao businessObjectDao;
 40  
     private DocumentAdHocService documentAdHocService;
 41  
 
 42  
         @PersistenceContext
 43  
         private EntityManager entityManager;
 44  
 
 45  
     public DocumentDaoJpa(EntityManager entityManager, BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) {
 46  0
         super();
 47  0
         this.entityManager = entityManager;
 48  0
         this.businessObjectDao = businessObjectDao;
 49  0
         this.documentAdHocService = documentAdHocService;
 50  0
     }
 51  
 
 52  
     /**
 53  
      * @see org.kuali.dao.DocumentDao#save(null)
 54  
      */
 55  
     @Override
 56  
     public <T extends Document> T save(T document) throws DataAccessException {
 57  0
                 T attachedDoc = (T) findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
 58  0
                 if (attachedDoc == null) {
 59  0
                         entityManager.persist(document.getDocumentHeader());
 60  0
                         entityManager.persist(document);
 61  0
                         return document;
 62  
                 }
 63  0
                 OrmUtils.reattach(document, attachedDoc);
 64  0
                 return entityManager.merge(attachedDoc);
 65  
     }
 66  
 
 67  
         /**
 68  
      * Retrieve a Document of a specific type with a given document header ID.
 69  
      *
 70  
      * @param clazz
 71  
      * @param id
 72  
      * @return Document with given id
 73  
      */
 74  
     @Override
 75  
     public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
 76  0
         return entityManager.find(clazz, id);
 77  
     }
 78  
 
 79  
     /**
 80  
      * Retrieve a List of Document instances with the given ids
 81  
      *
 82  
      * @param clazz
 83  
      * @param idList
 84  
      * @return List
 85  
      */
 86  
     @Override
 87  
     public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
 88  0
                 Criteria criteria = new Criteria(clazz.getName());
 89  0
                 criteria.in(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
 90  0
                 List<T> list = new ArrayList<T>();
 91  
                 try {
 92  0
                         list = new QueryByCriteria(entityManager, criteria).toQuery().getResultList();
 93  0
                 } catch (PersistenceException e) {
 94  0
                         e.printStackTrace();
 95  0
                 }
 96  0
         for (T doc : list) {
 97  0
                 documentAdHocService.addAdHocs(doc);
 98  0
                         entityManager.refresh(doc);
 99  
         }
 100  0
                 return list;
 101  
     }
 102  
 
 103  
     @Override
 104  
     public BusinessObjectDao getBusinessObjectDao() {
 105  0
         return businessObjectDao;
 106  
     }
 107  
 
 108  
     public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
 109  0
         this.businessObjectDao = businessObjectDao;
 110  0
     }
 111  
 
 112  
     /**
 113  
      * @return the entityManager
 114  
      */
 115  
     public EntityManager getEntityManager() {
 116  0
         return this.entityManager;
 117  
     }
 118  
 
 119  
     /**
 120  
      * @param entityManager the entityManager to set
 121  
      */
 122  
     public void setEntityManager(EntityManager entityManager) {
 123  0
         this.entityManager = entityManager;
 124  0
     }
 125  
 
 126  
     /**
 127  
          * @return the documentAdHocService
 128  
          */
 129  
     @Override
 130  
         public DocumentAdHocService getDocumentAdHocService() {
 131  0
                 return this.documentAdHocService;
 132  
         }
 133  
 
 134  
     /**
 135  
      * Setter for injecting the DocumentAdHocService
 136  
      * @param dahs
 137  
      */
 138  
     public void setDocumentAdHocService(DocumentAdHocService dahs) {
 139  0
             this.documentAdHocService = dahs;
 140  0
     }
 141  
 
 142  
 }