001 /**
002 * Copyright 2005-2012 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 */
016 package org.kuali.rice.krad.dao.impl;
017
018 import org.apache.log4j.Logger;
019 import org.apache.ojb.broker.query.Criteria;
020 import org.apache.ojb.broker.query.QueryByCriteria;
021 import org.apache.ojb.broker.query.QueryFactory;
022 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
023 import org.kuali.rice.krad.dao.BusinessObjectDao;
024 import org.kuali.rice.krad.dao.DocumentDao;
025 import org.kuali.rice.krad.document.Document;
026 import org.kuali.rice.krad.service.DocumentAdHocService;
027 import org.kuali.rice.krad.service.KRADServiceLocatorInternal;
028 import org.kuali.rice.krad.util.KRADPropertyConstants;
029 import org.kuali.rice.krad.util.OjbCollectionAware;
030 import org.springframework.dao.DataAccessException;
031
032 import java.util.ArrayList;
033 import java.util.List;
034
035 /**
036 * This class is the OJB implementation of the DocumentDao interface.
037 */
038 public class DocumentDaoOjb extends PlatformAwareDaoBaseOjb implements DocumentDao, OjbCollectionAware{
039 private static final Logger LOG = Logger.getLogger(DocumentDaoOjb.class);
040 protected BusinessObjectDao businessObjectDao;
041 protected DocumentAdHocService documentAdHocService;
042
043
044 public DocumentDaoOjb(BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) {
045 super();
046 this.businessObjectDao = businessObjectDao;
047 this.documentAdHocService = documentAdHocService;
048 }
049
050 /**
051 * @see org.kuali.dao.DocumentDao#save(null)
052 */
053 @Override
054 public <T extends Document> T save(T document) throws DataAccessException {
055 if ( LOG.isDebugEnabled() ) {
056 LOG.debug( "About to store document: " + document, new Throwable() );
057 }
058 Document retrievedDocument = findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
059 KRADServiceLocatorInternal.getOjbCollectionHelper().processCollections(this, document, retrievedDocument);
060 this.getPersistenceBrokerTemplate().store(document);
061 return document;
062 }
063
064 /**
065 * Retrieve a Document of a specific type with a given document header ID.
066 *
067 * @param clazz
068 * @param id
069 * @return Document with given id
070 */
071 @Override
072 public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
073 List<String> idList = new ArrayList<String>();
074 idList.add(id);
075
076 List<T> documentList = findByDocumentHeaderIds(clazz, idList);
077
078 T document = null;
079 if ((null != documentList) && (documentList.size() > 0)) {
080 document = documentList.get(0);
081 }
082
083 return document;
084 }
085
086 /**
087 * Retrieve a List of Document instances with the given ids
088 *
089 * @param clazz
090 * @param idList
091 * @return List
092 */
093 @Override
094 public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
095 Criteria criteria = new Criteria();
096 criteria.addIn(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
097
098 QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
099
100 // this cast is correct because OJB produces a collection which contains elements of the class defined on the query
101 @SuppressWarnings("unchecked")
102 List<T> tempList = new ArrayList<T>(this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
103
104 for (T doc : tempList) {
105 documentAdHocService.addAdHocs(doc);
106 }
107 return tempList;
108 }
109
110 /**
111 * Returns the {@link BusinessObjectDao}
112 * @see org.kuali.rice.krad.dao.DocumentDao#getBusinessObjectDao()
113 * @return the {@link BusinessObjectDao}
114 */
115 @Override
116 public BusinessObjectDao getBusinessObjectDao() {
117 return businessObjectDao;
118 }
119
120 /**
121 * Sets the {@link BusinessObjectDao}
122 * @param businessObjectDao ths {@link BusinessObjectDao}
123 */
124 public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
125 this.businessObjectDao = businessObjectDao;
126 }
127
128 /**
129 * @return the documentAdHocService
130 */
131 @Override
132 public DocumentAdHocService getDocumentAdHocService() {
133 return this.documentAdHocService;
134 }
135
136 /**
137 * Setter for injecting the DocumentAdHocService
138 * @param dahs
139 */
140 public void setDocumentAdHocService(DocumentAdHocService dahs) {
141 this.documentAdHocService = dahs;
142 }
143 }