001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.dao.impl; 017 018import org.kuali.rice.core.framework.persistence.jpa.OrmUtils; 019import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria; 020import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria; 021import org.kuali.rice.krad.dao.BusinessObjectDao; 022import org.kuali.rice.krad.dao.DocumentDao; 023import org.kuali.rice.krad.document.Document; 024import org.kuali.rice.krad.service.DocumentAdHocService; 025import org.kuali.rice.krad.util.KRADPropertyConstants; 026import org.springframework.dao.DataAccessException; 027 028import javax.persistence.EntityManager; 029import javax.persistence.PersistenceContext; 030import javax.persistence.PersistenceException; 031import java.util.ArrayList; 032import java.util.List; 033 034/** 035 * This class is the OJB implementation of the DocumentDao interface. 036 */ 037public class DocumentDaoJpa implements DocumentDao { 038 039 private BusinessObjectDao businessObjectDao; 040 private DocumentAdHocService documentAdHocService; 041 042 @PersistenceContext 043 private EntityManager entityManager; 044 045 public DocumentDaoJpa(EntityManager entityManager, BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) { 046 super(); 047 this.entityManager = entityManager; 048 this.businessObjectDao = businessObjectDao; 049 this.documentAdHocService = documentAdHocService; 050 } 051 052 /** 053 * @see org.kuali.dao.DocumentDao#save(null) 054 */ 055 @Override 056 public <T extends Document> T save(T document) throws DataAccessException { 057 T attachedDoc = (T) findByDocumentHeaderId(document.getClass(),document.getDocumentNumber()); 058 if (attachedDoc == null) { 059 entityManager.persist(document.getDocumentHeader()); 060 entityManager.persist(document); 061 return document; 062 } 063 OrmUtils.reattach(document, attachedDoc); 064 return entityManager.merge(attachedDoc); 065 } 066 067 /** 068 * Retrieve a Document of a specific type with a given document header ID. 069 * 070 * @param clazz 071 * @param id 072 * @return Document with given id 073 */ 074 @Override 075 public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) { 076 return entityManager.find(clazz, id); 077 } 078 079 /** 080 * Retrieve a List of Document instances with the given ids 081 * 082 * @param clazz 083 * @param idList 084 * @return List 085 */ 086 @Override 087 public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) { 088 Criteria criteria = new Criteria(clazz.getName()); 089 criteria.in(KRADPropertyConstants.DOCUMENT_NUMBER, idList); 090 List<T> list = new ArrayList<T>(); 091 try { 092 list = new QueryByCriteria(entityManager, criteria).toQuery().getResultList(); 093 } catch (PersistenceException e) { 094 e.printStackTrace(); 095 } 096 for (T doc : list) { 097 documentAdHocService.addAdHocs(doc); 098 entityManager.refresh(doc); 099 } 100 return list; 101 } 102 103 @Override 104 public BusinessObjectDao getBusinessObjectDao() { 105 return businessObjectDao; 106 } 107 108 public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) { 109 this.businessObjectDao = businessObjectDao; 110 } 111 112 /** 113 * @return the entityManager 114 */ 115 public EntityManager getEntityManager() { 116 return this.entityManager; 117 } 118 119 /** 120 * @param entityManager the entityManager to set 121 */ 122 public void setEntityManager(EntityManager entityManager) { 123 this.entityManager = entityManager; 124 } 125 126 /** 127 * @return the documentAdHocService 128 */ 129 @Override 130 public DocumentAdHocService getDocumentAdHocService() { 131 return this.documentAdHocService; 132 } 133 134 /** 135 * Setter for injecting the DocumentAdHocService 136 * @param dahs 137 */ 138 public void setDocumentAdHocService(DocumentAdHocService dahs) { 139 this.documentAdHocService = dahs; 140 } 141 142}