001 /*
002 * Copyright 2011 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.kfs.module.cg.dataaccess.impl;
017
018 import java.sql.Date;
019 import java.util.ArrayList;
020 import java.util.Iterator;
021 import java.util.List;
022
023 import org.apache.ojb.broker.query.Criteria;
024 import org.apache.ojb.broker.query.QueryByCriteria;
025 import org.apache.ojb.broker.query.QueryFactory;
026 import org.kuali.kfs.module.cg.dataaccess.CloseDao;
027 import org.kuali.kfs.module.cg.document.ProposalAwardCloseDocument;
028 import org.kuali.kfs.sys.KFSConstants;
029 import org.kuali.kfs.sys.context.SpringContext;
030 import org.kuali.rice.kew.exception.WorkflowException;
031 import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
032 import org.kuali.rice.kns.service.DateTimeService;
033 import org.kuali.rice.kns.service.DocumentService;
034
035 /**
036 * @see CloseDao
037 */
038 public class CloseDaoOjb extends PlatformAwareDaoBaseOjb implements CloseDao {
039
040 /**
041 * @see org.kuali.kfs.module.cg.dataaccess.CloseDao#getMaxApprovedClose()
042 */
043 public ProposalAwardCloseDocument getMaxApprovedClose() {
044
045 DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
046 Date today = dateTimeService.getCurrentSqlDateMidnight();
047
048 Criteria criteria = new Criteria();
049 criteria.addEqualTo("userInitiatedCloseDate", today);
050 criteria.addEqualTo("documentHeader.financialDocumentStatusCode", KFSConstants.DocumentStatusCodes.ENROUTE);
051 QueryByCriteria query = QueryFactory.newQuery(ProposalAwardCloseDocument.class, criteria);
052 query.addOrderByDescending("documentNumber");
053
054 Iterator<ProposalAwardCloseDocument> documents = (Iterator<ProposalAwardCloseDocument>) getPersistenceBrokerTemplate().getIteratorByQuery(query);
055 ArrayList<String> documentHeaderIds = new ArrayList<String>();
056 while (documents.hasNext()) {
057 ProposalAwardCloseDocument document = (ProposalAwardCloseDocument) documents.next();
058 documentHeaderIds.add(document.getDocumentNumber());
059 }
060
061 List<ProposalAwardCloseDocument> docs = null;
062
063 if (documentHeaderIds.size() > 0) {
064
065 try {
066 docs = SpringContext.getBean(DocumentService.class).getDocumentsByListOfDocumentHeaderIds(ProposalAwardCloseDocument.class, documentHeaderIds);
067 } catch (WorkflowException we) {
068 throw new RuntimeException(we);
069 }
070
071 if (docs.size() > 1) {
072 ProposalAwardCloseDocument close = docs.remove(0);
073 Date closeDate = close.getCloseOnOrBeforeDate();
074 for (ProposalAwardCloseDocument cfdaClose: docs) {
075 if (cfdaClose.getCloseOnOrBeforeDate().equals(closeDate)) {
076 //disapprove docs with same close date??
077 }
078
079 }
080 return close;
081
082 } else if (docs.size() == 1) {
083 return docs.get(0);
084 } else
085 return null;
086
087 } else {
088 return null;
089 }
090
091
092 }
093
094 public ProposalAwardCloseDocument getMostRecentClose() {
095 DateTimeService dateTimeService = SpringContext.getBean(DateTimeService.class);
096 Date today = dateTimeService.getCurrentSqlDateMidnight();
097
098 Criteria criteria = new Criteria();
099 criteria.addEqualTo("userInitiatedCloseDate", today);
100 criteria.addEqualTo("documentHeader.financialDocumentStatusCode", KFSConstants.DocumentStatusCodes.APPROVED);
101 QueryByCriteria query = QueryFactory.newQuery(ProposalAwardCloseDocument.class, criteria);
102 query.addOrderByDescending("documentNumber");
103
104 Iterator<ProposalAwardCloseDocument> documents = (Iterator<ProposalAwardCloseDocument>) getPersistenceBrokerTemplate().getIteratorByQuery(query);
105 ArrayList<String> documentHeaderIds = new ArrayList<String>();
106 while (documents.hasNext()) {
107 ProposalAwardCloseDocument document = (ProposalAwardCloseDocument) documents.next();
108 documentHeaderIds.add(document.getDocumentNumber());
109 }
110
111 List<ProposalAwardCloseDocument> docs = null;
112
113 if (documentHeaderIds.size() > 0) {
114
115 try {
116 docs = SpringContext.getBean(DocumentService.class).getDocumentsByListOfDocumentHeaderIds(ProposalAwardCloseDocument.class, documentHeaderIds);
117 } catch (WorkflowException we) {
118 throw new RuntimeException(we);
119 }
120
121 if (docs.size() > 1) {
122 ProposalAwardCloseDocument close = docs.remove(0);
123 return close;
124
125 } else if (docs.size() == 1) {
126 return docs.get(0);
127 } else
128 return null;
129
130 } else {
131 return null;
132 }
133 }
134
135
136 /**
137 * @see org.kuali.kfs.module.cg.dataaccess.CloseDao#save(org.kuali.kfs.module.cg.businessobject.Close)
138 */
139 public void save(ProposalAwardCloseDocument close) {
140 getPersistenceBrokerTemplate().store(close);
141 }
142
143 }