View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.cg.service.impl;
17  
18  import java.sql.Date;
19  import java.text.MessageFormat;
20  import java.util.Collection;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.ole.module.cg.CGConstants;
24  import org.kuali.ole.module.cg.CGKeyConstants;
25  import org.kuali.ole.module.cg.businessobject.Award;
26  import org.kuali.ole.module.cg.businessobject.Proposal;
27  import org.kuali.ole.module.cg.dataaccess.AwardDao;
28  import org.kuali.ole.module.cg.dataaccess.CloseDao;
29  import org.kuali.ole.module.cg.dataaccess.ProposalDao;
30  import org.kuali.ole.module.cg.document.ProposalAwardCloseDocument;
31  import org.kuali.ole.module.cg.service.CloseService;
32  import org.kuali.ole.sys.context.SpringContext;
33  import org.kuali.rice.core.api.config.property.ConfigurationService;
34  import org.kuali.rice.core.api.datetime.DateTimeService;
35  import org.kuali.rice.kew.api.exception.WorkflowException;
36  import org.kuali.rice.krad.service.BusinessObjectService;
37  import org.kuali.rice.krad.service.DocumentService;
38  import org.springframework.transaction.annotation.Transactional;
39  
40  @Transactional
41  public class CloseServiceImpl implements CloseService {
42  
43      private AwardDao awardDao;
44      private ProposalDao proposalDao;
45      private CloseDao closeDao;
46      private DateTimeService dateTimeService;
47      protected BusinessObjectService businessObjectService;
48      protected DocumentService documentService;
49  
50      /**
51       * <ul>
52       * <li>Get the max proposal_close_number in cg_prpsl_close_t.</li>
53       * <li>Get the Close with that max_close_number.</li>got
54       * <li>If todays date is the same as the user_initiate_date on that Close, continue. Else, break.</li>
55       * <li>Get all proposals with a null closing_date and a submission_date <= the last_closed_date of the Close with the
56       * max_proposal_close number.</li>
57       * <li>Save the number of proposals that come back.</li>
58       * <li>Update each of these proposals setting the close_date to todays date.</li>
59       * <li>Get all awards with a null closing_date, an entry_date <= the last_closed_date of the Close with the max_close number and
60       * a status_code not equal to 'U'.</li>
61       * <li>Save the number of awards that come back.</li>
62       * <li>Update each of these awards setting the close_date to todays date.</li>
63       * <li>Update the Close with that max_close_number setting the proposal_closed_count to the number of proposals brought back
64       * above and the award_closed_count to the number of awards brought back above.</li>
65       * <li>Save the Close.</li>
66       * </ul>
67       * 
68       * @see org.kuali.ole.module.cg.service.CloseService#close()
69       */
70      @Override
71      public boolean close() {
72  
73          Date today = dateTimeService.getCurrentSqlDateMidnight();
74          ProposalAwardCloseDocument max = getMaxApprovedClose(today);
75  
76          if (null == max) { // no closes at all. Gotta wait until we get an approved one.
77              return true;
78          }
79  
80          boolean result = true;
81          String noteText = null;
82          if (max.getDocumentHeader().getWorkflowDocument().getCurrentRouteNodeInstances().contains( CGConstants.CGKimApiConstants.UNPROCESSED_ROUTING_NODE_NAME) ) {
83  
84              ConfigurationService kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
85  
86              try {
87  
88                  Collection<Proposal> proposals = proposalDao.getProposalsToClose(max);
89                  Long proposalCloseCount = new Long(proposals.size());
90                  for (Proposal p : proposals) {
91                      p.setProposalClosingDate(today);
92                      businessObjectService.save(p);
93                  }
94  
95                  Collection<Award> awards = awardDao.getAwardsToClose(max);
96                  Long awardCloseCount = new Long(awards.size());
97                  for (Award a : awards) {
98                      a.setAwardClosingDate(today);
99                      businessObjectService.save(a);
100                 }
101 
102                 max.setAwardClosedCount(awardCloseCount);
103                 max.setProposalClosedCount(proposalCloseCount);
104 
105                 businessObjectService.save(max);
106                 noteText = kualiConfigurationService.getPropertyValueAsString(CGKeyConstants.MESSAGE_CLOSE_JOB_SUCCEEDED);
107 
108             }
109             catch (Exception e) {
110                 String messageProperty = kualiConfigurationService.getPropertyValueAsString(CGKeyConstants.ERROR_CLOSE_JOB_FAILED);
111                 noteText = MessageFormat.format(messageProperty, e.getMessage(), e.getCause().getMessage());
112             }
113             finally {
114                 result = this.addDocumentNoteAfterClosing(max, noteText);
115             }
116         }
117         return result;
118     }
119 
120     /**
121      * @see org.kuali.ole.module.cg.service.CloseService#getMostRecentClose()
122      */
123     @Override
124     public ProposalAwardCloseDocument getMostRecentClose() {
125         Date today = dateTimeService.getCurrentSqlDateMidnight();
126         String documentNumber = closeDao.getMostRecentClose(today);
127         if (StringUtils.isNotBlank(documentNumber)) {
128             try {
129                 return (ProposalAwardCloseDocument) documentService.getByDocumentHeaderId(documentNumber);
130             }
131             catch (WorkflowException we) {
132                 throw new RuntimeException(we);
133             }
134         }
135         else {
136             return null;
137         }
138 
139     }
140 
141     /**
142      * @see org.kuali.ole.module.cg.service.CloseService#addDocumentNoteAfterClosing(String)
143      */
144     protected boolean addDocumentNoteAfterClosing(ProposalAwardCloseDocument close, String noteText) {
145         DocumentService service = SpringContext.getBean(DocumentService.class);
146         try {
147             service.createNoteFromDocument(close, noteText);
148             service.approveDocument(close, noteText, null);
149         }
150         catch (WorkflowException we) {
151             we.printStackTrace();
152             return false;
153         }
154         return true;
155     }
156 
157     /**
158      * @see org.kuali.ole.module.cg.service.CloseService#getMaxApprovedClose(java.sql.Date)
159      */
160     @Override
161     public ProposalAwardCloseDocument getMaxApprovedClose(Date today) {
162         String documentNumber = closeDao.getMaxApprovedClose(today);
163         if (StringUtils.isNotBlank(documentNumber)) {
164 
165             try {
166                 return (ProposalAwardCloseDocument) documentService.getByDocumentHeaderId(documentNumber);
167             }
168             catch (WorkflowException we) {
169                 throw new RuntimeException(we);
170             }
171         }
172         else {
173             return null;
174         }
175     }
176 
177     public void setAwardDao(AwardDao awardDao) {
178         this.awardDao = awardDao;
179     }
180 
181     public void setDateTimeService(DateTimeService dateTimeService) {
182         this.dateTimeService = dateTimeService;
183     }
184 
185     public void setCloseDao(CloseDao closeDao) {
186         this.closeDao = closeDao;
187     }
188 
189     public void setProposalDao(ProposalDao proposalDao) {
190         this.proposalDao = proposalDao;
191     }
192 
193     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
194         this.businessObjectService = businessObjectService;
195     }
196 
197     public void setDocumentService(DocumentService documentService) {
198         this.documentService = documentService;
199     }
200 }