001/**
002 * Copyright 2005-2015 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 java.util.ArrayList;
019import java.util.List;
020import java.util.UUID;
021
022import org.apache.commons.lang.StringUtils;
023import org.apache.log4j.Logger;
024import org.apache.ojb.broker.query.Criteria;
025import org.apache.ojb.broker.query.QueryByCriteria;
026import org.apache.ojb.broker.query.QueryFactory;
027import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
028import org.kuali.rice.kns.service.KNSServiceLocator;
029import org.kuali.rice.krad.bo.DataObjectBase;
030import org.kuali.rice.krad.bo.PersistableBusinessObject;
031import org.kuali.rice.krad.dao.BusinessObjectDao;
032import org.kuali.rice.krad.dao.DocumentDao;
033import org.kuali.rice.krad.document.Document;
034import org.kuali.rice.krad.service.DocumentAdHocService;
035import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
036import org.kuali.rice.krad.service.util.OjbCollectionAware;
037import org.kuali.rice.krad.service.util.OjbCollectionHelper;
038import org.kuali.rice.krad.util.KRADPropertyConstants;
039import org.springframework.dao.DataAccessException;
040
041/**
042 * OJB implementation of the DocumentDao interface
043 *
044 * @author Kuali Rice Team (rice.collab@kuali.org)
045 */
046@Deprecated
047public class DocumentDaoOjb extends PlatformAwareDaoBaseOjb implements DocumentDao, OjbCollectionAware {
048    private static final Logger LOG = Logger.getLogger(DocumentDaoOjb.class);
049
050    protected BusinessObjectDao businessObjectDao;
051    protected DocumentAdHocService documentAdHocService;
052    private OjbCollectionHelper ojbCollectionHelper;
053
054
055    public DocumentDaoOjb(BusinessObjectDao businessObjectDao, DocumentAdHocService documentAdHocService) {
056        super();
057        this.businessObjectDao = businessObjectDao;
058        this.documentAdHocService = documentAdHocService;
059    }
060
061    @Override
062    public <T extends Document> T save(T document) throws DataAccessException {
063        if ( LOG.isDebugEnabled() ) {
064                LOG.debug( "About to store document: " + document, new Throwable() );
065        }
066        Document retrievedDocument = findByDocumentHeaderId(document.getClass(),document.getDocumentNumber());
067        if ( document instanceof PersistableBusinessObject ) {
068                if ( retrievedDocument != null && retrievedDocument instanceof PersistableBusinessObject ) {
069                        getOjbCollectionHelper().processCollections(this, (PersistableBusinessObject)document, (PersistableBusinessObject)retrievedDocument);
070                }
071        }
072        if ( document instanceof DataObjectBase ) {
073                if (StringUtils.isEmpty(document.getObjectId())) {
074                        ((DataObjectBase)document).setObjectId(UUID.randomUUID().toString());
075                }
076                // KRAD/JPA - have to change the handle to object to that just saved
077                document.setDocumentHeader( KRADServiceLocatorWeb.getDocumentHeaderService().saveDocumentHeader(document.getDocumentHeader()) );
078        }
079        this.getPersistenceBrokerTemplate().store(document);
080        return document;
081    }
082
083    /**
084     * Retrieve a Document of a specific type with a given document header ID.
085     *
086     * @param clazz
087     * @param id
088     * @return Document with given id
089     */
090    @Override
091    public <T extends Document> T findByDocumentHeaderId(Class<T> clazz, String id) {
092        List<String> idList = new ArrayList<String>();
093        idList.add(id);
094
095        List<T> documentList = findByDocumentHeaderIds(clazz, idList);
096
097        T document = null;
098        if ((null != documentList) && (documentList.size() > 0)) {
099            document = documentList.get(0);
100        }
101
102        return document;
103    }
104
105    /**
106     * Retrieve a List of Document instances with the given ids
107     *
108     * @param clazz
109     * @param idList
110     * @return List
111     */
112    @Override
113    public <T extends Document> List<T> findByDocumentHeaderIds(Class<T> clazz, List<String> idList) {
114        Criteria criteria = new Criteria();
115        criteria.addIn(KRADPropertyConstants.DOCUMENT_NUMBER, idList);
116
117        QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
118        
119        // this cast is correct because OJB produces a collection which contains elements of the class defined on the query
120        @SuppressWarnings("unchecked")
121        List<T> tempList = new ArrayList<T>(this.getPersistenceBrokerTemplate().getCollectionByQuery(query));
122        
123        for (T doc : tempList) {
124                documentAdHocService.addAdHocs(doc);
125        }
126        return tempList;
127    }
128
129    /**
130     * Returns the {@link BusinessObjectDao}
131     * @see org.kuali.rice.krad.dao.DocumentDao#getBusinessObjectDao()
132     * @return the {@link BusinessObjectDao}
133     */
134    @Override
135    public BusinessObjectDao getBusinessObjectDao() {
136        return businessObjectDao;
137    }
138
139    /**
140     * Sets the {@link BusinessObjectDao}
141     * @param businessObjectDao ths {@link BusinessObjectDao}
142     */
143    public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
144        this.businessObjectDao = businessObjectDao;
145    }
146
147    /**
148         * @return the documentAdHocService
149         */
150    @Override
151        public DocumentAdHocService getDocumentAdHocService() {
152                return this.documentAdHocService;
153        }
154
155    /**
156     * Setter for injecting the DocumentAdHocService
157     * @param dahs
158     */
159    public void setDocumentAdHocService(DocumentAdHocService dahs) {
160        this.documentAdHocService = dahs;
161    }
162
163    protected OjbCollectionHelper getOjbCollectionHelper() {
164        if (ojbCollectionHelper == null) {
165            ojbCollectionHelper = KNSServiceLocator.getOjbCollectionHelper();
166        }
167
168        return ojbCollectionHelper;
169    }
170
171    public void setOjbCollectionHelper(OjbCollectionHelper ojbCollectionHelper) {
172        this.ojbCollectionHelper = ojbCollectionHelper;
173    }
174}