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.service.impl; 017 018 import java.sql.Date; 019 import java.text.MessageFormat; 020 import java.util.Collection; 021 022 import org.apache.commons.lang.StringUtils; 023 import org.kuali.kfs.module.cg.CGConstants; 024 import org.kuali.kfs.module.cg.CGKeyConstants; 025 import org.kuali.kfs.module.cg.businessobject.Award; 026 import org.kuali.kfs.module.cg.businessobject.Proposal; 027 import org.kuali.kfs.module.cg.dataaccess.AwardDao; 028 import org.kuali.kfs.module.cg.dataaccess.CloseDao; 029 import org.kuali.kfs.module.cg.dataaccess.ProposalDao; 030 import org.kuali.kfs.module.cg.document.ProposalAwardCloseDocument; 031 import org.kuali.kfs.module.cg.service.CloseService; 032 import org.kuali.kfs.sys.context.SpringContext; 033 import org.kuali.rice.kew.exception.WorkflowException; 034 import org.kuali.rice.kns.bo.Note; 035 import org.kuali.rice.kns.service.DateTimeService; 036 import org.kuali.rice.kns.service.DocumentService; 037 import org.kuali.rice.kns.service.KualiConfigurationService; 038 import org.kuali.rice.kns.util.GlobalVariables; 039 import org.springframework.transaction.annotation.Transactional; 040 041 @Transactional 042 public class CloseServiceImpl implements CloseService { 043 044 private AwardDao awardDao; 045 private ProposalDao proposalDao; 046 private CloseDao closeDao; 047 private DateTimeService dateTimeService; 048 049 /** 050 * <ul> 051 * <li>Get the max proposal_close_number in cg_prpsl_close_t.</li> 052 * <li>Get the Close with that max_close_number.</li>got 053 * <li>If todays date is the same as the user_initiate_date on that Close, continue. Else, break.</li> 054 * <li>Get all proposals with a null closing_date and a submission_date <= the last_closed_date of the Close with the 055 * max_proposal_close number.</li> 056 * <li>Save the number of proposals that come back.</li> 057 * <li>Update each of these proposals setting the close_date to todays date.</li> 058 * <li>Get all awards with a null closing_date, an entry_date <= the last_closed_date of the Close with the max_close number 059 * and a status_code not equal to 'U'.</li> 060 * <li>Save the number of awards that come back.</li> 061 * <li>Update each of these awards setting the close_date to todays date.</li> 062 * <li>Update the Close with that max_close_number setting the proposal_closed_count to the number of proposals brought back 063 * above and the award_closed_count to the number of awards brought back above.</li> 064 * <li>Save the Close.</li> 065 * </ul> 066 * 067 * @see org.kuali.kfs.module.cg.service.CloseService#close() 068 */ 069 public boolean close() { 070 071 ProposalAwardCloseDocument max = closeDao.getMaxApprovedClose(); 072 Date today = dateTimeService.getCurrentSqlDateMidnight(); 073 074 if (null == max) { // no closes at all. Gotta wait until we get an approved one. 075 return true; 076 } 077 078 boolean result = true; 079 String noteText = null; 080 if (StringUtils.equals(max.getDocumentHeader().getWorkflowDocument().getRouteHeader().getCurrentRouteNodeNames(), 081 CGConstants.CGKimConstants.UNPROCESSED_ROUTING_NODE_NAME)){ 082 083 KualiConfigurationService kualiConfigurationService = SpringContext.getBean(KualiConfigurationService.class); 084 085 try { 086 087 Collection<Proposal> proposals = proposalDao.getProposalsToClose(max); 088 Long proposalCloseCount = new Long(proposals.size()); 089 for (Proposal p : proposals) { 090 p.setProposalClosingDate(today); 091 proposalDao.save(p); 092 } 093 094 Collection<Award> awards = awardDao.getAwardsToClose(max); 095 Long awardCloseCount = new Long(awards.size()); 096 for (Award a : awards) { 097 a.setAwardClosingDate(today); 098 awardDao.save(a); 099 } 100 101 max.setAwardClosedCount(awardCloseCount); 102 max.setProposalClosedCount(proposalCloseCount); 103 104 closeDao.save(max); 105 noteText = kualiConfigurationService.getPropertyString(CGKeyConstants.MESSAGE_CLOSE_JOB_SUCCEEDED); 106 107 } catch (Exception e) { 108 String messageProperty = kualiConfigurationService.getPropertyString(CGKeyConstants.ERROR_CLOSE_JOB_FAILED); 109 noteText = MessageFormat.format(messageProperty, e.getMessage(), e.getCause().getMessage()); 110 } finally { 111 result = this.addDocumentNoteAfterClosing(max, noteText); 112 } 113 } 114 return result; 115 } 116 117 public ProposalAwardCloseDocument getMostRecentClose() { 118 ProposalAwardCloseDocument mostRecentClose = closeDao.getMostRecentClose(); 119 return mostRecentClose; 120 } 121 122 /** 123 * @see org.kuali.kfs.module.cg.service.CloseService#addDocumentNoteAfterClosing(String) 124 */ 125 protected boolean addDocumentNoteAfterClosing(ProposalAwardCloseDocument close, String noteText) { 126 Note note = new Note(); 127 note.setNoteText(noteText); 128 note.setAuthorUniversalIdentifier(GlobalVariables.getUserSession().getPerson().getPrincipalId()); 129 130 DocumentService service = SpringContext.getBean(DocumentService.class); 131 try { 132 service.addNoteToDocument(close, note); 133 service.approveDocument(close, note.getNoteText(), null); 134 } catch (WorkflowException we) { 135 we.printStackTrace(); 136 return false; 137 } 138 return true; 139 } 140 141 142 public void setAwardDao(AwardDao awardDao) { 143 this.awardDao = awardDao; 144 } 145 146 public void setDateTimeService(DateTimeService dateTimeService) { 147 this.dateTimeService = dateTimeService; 148 } 149 150 public void setCloseDao(CloseDao closeDao) { 151 this.closeDao = closeDao; 152 } 153 154 public void setProposalDao(ProposalDao proposalDao) { 155 this.proposalDao = proposalDao; 156 } 157 }