1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @Override
71 public boolean close() {
72
73 Date today = dateTimeService.getCurrentSqlDateMidnight();
74 ProposalAwardCloseDocument max = getMaxApprovedClose(today);
75
76 if (null == max) {
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
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
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
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 }