001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.core.dao.ojb;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.List;
022:
023: import org.apache.log4j.Logger;
024: import org.apache.ojb.broker.query.Criteria;
025: import org.apache.ojb.broker.query.QueryByCriteria;
026: import org.apache.ojb.broker.query.QueryFactory;
027: import org.kuali.RicePropertyConstants;
028: import org.kuali.core.bo.AdHocRoutePerson;
029: import org.kuali.core.bo.AdHocRouteWorkgroup;
030: import org.kuali.core.bo.PersistableBusinessObject;
031: import org.kuali.core.dao.BusinessObjectDao;
032: import org.kuali.core.dao.DocumentDao;
033: import org.kuali.core.document.Document;
034: import org.kuali.core.util.OjbCollectionAware;
035: import org.kuali.rice.KNSServiceLocator;
036: import org.springframework.dao.DataAccessException;
037:
038: /**
039: * This class is the OJB implementation of the DocumentDao interface.
040: */
041: public class DocumentDaoOjb extends PlatformAwareDaoBaseOjb implements
042: DocumentDao, OjbCollectionAware {
043: private static Logger LOG = Logger.getLogger(DocumentDaoOjb.class);
044: private BusinessObjectDao businessObjectDao;
045:
046: public DocumentDaoOjb() {
047: super ();
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.kuali.dao.DocumentDao#save(null)
054: */
055: public void save(Document document) throws DataAccessException {
056: Document retrievedDocument = findByDocumentHeaderId(document
057: .getClass(), document.getDocumentNumber());
058: KNSServiceLocator.getOjbCollectionHelper().processCollections(
059: this , document, retrievedDocument);
060: this .getPersistenceBrokerTemplate().store(document);
061: }
062:
063: /**
064: * Retrieve a Document of a specific type with a given document header ID.
065: *
066: * @param clazz
067: * @param id
068: * @return Document with given id
069: */
070: public Document findByDocumentHeaderId(Class clazz, String id)
071: throws DataAccessException {
072: List idList = new ArrayList();
073: idList.add(id);
074:
075: List documentList = findByDocumentHeaderIds(clazz, idList);
076:
077: Document document = null;
078: if ((null != documentList) && (documentList.size() > 0)) {
079: document = (Document) documentList.get(0);
080: }
081:
082: return document;
083: }
084:
085: /**
086: * Retrieve a List of Document instances with the given ids
087: *
088: * @param clazz
089: * @param idList
090: * @return List
091: */
092: public List findByDocumentHeaderIds(Class clazz, List idList)
093: throws DataAccessException {
094: Criteria criteria = new Criteria();
095: criteria.addIn(RicePropertyConstants.DOCUMENT_NUMBER, idList);
096:
097: QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
098: ArrayList<Document> tempList = new ArrayList(this
099: .getPersistenceBrokerTemplate().getCollectionByQuery(
100: query));
101: for (Document doc : tempList)
102: addAdHocs(doc);
103: return tempList;
104: }
105:
106: /**
107: * Retrieves a collection of documents with type of given Class, and with the passed status code.
108: *
109: * @see org.kuali.core.dao.DocumentDao#findByDocumentHeaderStatusCode(java.lang.Class, java.lang.String)
110: */
111: public Collection findByDocumentHeaderStatusCode(Class clazz,
112: String statusCode) {
113: Criteria criteria = new Criteria();
114: criteria.addEqualTo(RicePropertyConstants.DOCUMENT_HEADER + "."
115: + RicePropertyConstants.FINANCIAL_DOCUMENT_STATUS_CODE,
116: statusCode);
117:
118: QueryByCriteria query = QueryFactory.newQuery(clazz, criteria);
119:
120: ArrayList<Document> tempList = new ArrayList(this
121: .getPersistenceBrokerTemplate().getCollectionByQuery(
122: query));
123: for (Document doc : tempList)
124: addAdHocs(doc);
125: return tempList;
126: }
127:
128: /**
129: *
130: * Deprecated method. Should use BusinessObjectService.linkAndSave() instead.
131: *
132: */
133: @Deprecated
134: public void saveMaintainableBusinessObject(
135: PersistableBusinessObject businessObject) {
136: /*
137: * this call is to assure all the object fk values are in sync and the fk fields is set in the main object
138: */
139: KNSServiceLocator.getPersistenceService().linkObjects(
140: businessObject);
141: this .getPersistenceBrokerTemplate().store(businessObject);
142: }
143:
144: public Document addAdHocs(Document document) {
145: /* Instead of reading the doc header to see if doc is in saved status
146: * its probably easier and faster to just do this all the time and
147: * store a null when appropriate.
148: */
149: List adHocRoutePersons;
150: List adHocRouteWorkgroups;
151: HashMap criteriaPerson = new HashMap();
152: HashMap criteriaWorkgroup = new HashMap();
153:
154: criteriaPerson.put("documentNumber", document
155: .getDocumentNumber());
156: criteriaPerson.put("type", AdHocRoutePerson.PERSON_TYPE);
157: adHocRoutePersons = (List) businessObjectDao.findMatching(
158: AdHocRoutePerson.class, criteriaPerson);
159: criteriaWorkgroup.put("documentNumber", document
160: .getDocumentNumber());
161: criteriaWorkgroup.put("type",
162: AdHocRouteWorkgroup.WORKGROUP_TYPE);
163: adHocRouteWorkgroups = (List) businessObjectDao.findMatching(
164: AdHocRouteWorkgroup.class, criteriaWorkgroup);
165: document.setAdHocRoutePersons(adHocRoutePersons);
166: document.setAdHocRouteWorkgroups(adHocRouteWorkgroups);
167:
168: return document;
169: }
170:
171: public BusinessObjectDao getBusinessObjectDao() {
172: return businessObjectDao;
173: }
174:
175: public void setBusinessObjectDao(BusinessObjectDao businessObjectDao) {
176: this.businessObjectDao = businessObjectDao;
177: }
178:
179: }
|