1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.cg.document;
17
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.kuali.ole.module.cg.businessobject.Proposal;
24 import org.kuali.ole.module.cg.businessobject.ProposalProjectDirector;
25 import org.kuali.ole.module.cg.businessobject.ProposalResearchRisk;
26 import org.kuali.ole.module.cg.businessobject.ProposalSubcontractor;
27 import org.kuali.ole.module.cg.businessobject.ResearchRiskType;
28 import org.kuali.ole.module.cg.businessobject.defaultvalue.NextProposalNumberFinder;
29 import org.kuali.ole.module.cg.document.service.RoutingFormResearchRiskService;
30 import org.kuali.ole.sys.OLEConstants;
31 import org.kuali.ole.sys.OLEPropertyConstants;
32 import org.kuali.ole.sys.context.SpringContext;
33 import org.kuali.ole.sys.document.FinancialSystemMaintainable;
34 import org.kuali.rice.kim.api.identity.Person;
35 import org.kuali.rice.kim.api.identity.PersonService;
36 import org.kuali.rice.kns.document.MaintenanceDocument;
37 import org.kuali.rice.krad.bo.PersistableBusinessObject;
38 import org.kuali.rice.krad.util.ObjectUtils;
39
40
41
42
43 public class ProposalMaintainableImpl extends FinancialSystemMaintainable {
44 public ProposalMaintainableImpl() {
45 super();
46 }
47
48
49
50
51
52
53 public ProposalMaintainableImpl(Proposal proposal) {
54 super(proposal);
55 this.setBoClass(proposal.getClass());
56 }
57
58
59
60
61 @Override
62 public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
63 getProposal().setProposalNumber(NextProposalNumberFinder.getLongValue());
64 getProposal().setProposalClosingDate(null);
65 super.processAfterCopy(document, parameters);
66 }
67
68
69
70
71
72 @Override
73 public void processAfterRetrieve() {
74 refreshProposal(false);
75 super.processAfterRetrieve();
76 }
77
78
79
80
81
82
83
84
85
86 @Override
87 public void prepareForSave() {
88 refreshProposal(false);
89 List<ProposalProjectDirector> directors = getProposal().getProposalProjectDirectors();
90 if (directors.size() == 1) {
91 directors.get(0).setProposalPrimaryProjectDirectorIndicator(true);
92 }
93 List<ProposalSubcontractor> proposalSubcontractors = getProposal().getProposalSubcontractors();
94 if (proposalSubcontractors != null && !proposalSubcontractors.isEmpty()) {
95 int i = 0;
96 for (ProposalSubcontractor proposalSubcontractor : proposalSubcontractors) {
97 i++;
98 if (StringUtils.isBlank(proposalSubcontractor.getProposalSubcontractorNumber())) {
99 proposalSubcontractor.setProposalSubcontractorNumber("" + i);
100 }
101 }
102 }
103 super.prepareForSave();
104 }
105
106
107
108
109
110
111
112
113
114 @Override
115 public void refresh(String refreshCaller, Map fieldValues, MaintenanceDocument document) {
116 refreshProposal(OLEConstants.KUALI_LOOKUPABLE_IMPL.equals(fieldValues.get(OLEConstants.REFRESH_CALLER)));
117 super.refresh(refreshCaller, fieldValues, document);
118 }
119
120
121
122
123
124
125
126 @Override
127 public void setGenerateDefaultValues(String docTypeName) {
128
129 initResearchRiskTypes();
130
131 super.setGenerateDefaultValues(docTypeName);
132 }
133
134
135
136
137 private void initResearchRiskTypes() {
138 List<ProposalResearchRisk> risks = getProposal().getProposalResearchRisks();
139
140 final String[] riskTypeCodesToExclude = new String[0];
141 List<ResearchRiskType> researchRiskTypes = SpringContext.getBean(RoutingFormResearchRiskService.class).getResearchRiskTypes(riskTypeCodesToExclude);
142 for (ResearchRiskType type : researchRiskTypes) {
143 ProposalResearchRisk ppr = new ProposalResearchRisk();
144 ppr.setResearchRiskTypeCode(type.getResearchRiskTypeCode());
145 ppr.setResearchRiskType(type);
146 risks.add(ppr);
147 }
148 }
149
150
151
152
153 private void refreshProposal(boolean refreshFromLookup) {
154 getProposal().refreshNonUpdateableReferences();
155
156 getNewCollectionLine(OLEPropertyConstants.PROPOSAL_SUBCONTRACTORS).refreshNonUpdateableReferences();
157
158 refreshNonUpdateableReferences(getProposal().getProposalOrganizations());
159 refreshNonUpdateableReferences(getProposal().getProposalSubcontractors());
160 refreshNonUpdateableReferences(getProposal().getProposalResearchRisks());
161
162 refreshProposalProjectDirectors(refreshFromLookup);
163 }
164
165
166
167
168
169
170 private void refreshProposalProjectDirectors(boolean refreshFromLookup) {
171 if (refreshFromLookup) {
172 getNewCollectionLine(OLEPropertyConstants.PROPOSAL_PROJECT_DIRECTORS).refreshNonUpdateableReferences();
173 refreshNonUpdateableReferences(getProposal().getProposalProjectDirectors());
174 }
175 else {
176 refreshWithSecondaryKey((ProposalProjectDirector) getNewCollectionLine(OLEPropertyConstants.PROPOSAL_PROJECT_DIRECTORS));
177 for (ProposalProjectDirector ppd : getProposal().getProposalProjectDirectors()) {
178 refreshWithSecondaryKey(ppd);
179 }
180 }
181 }
182
183
184
185
186 private static void refreshNonUpdateableReferences(Collection<? extends PersistableBusinessObject> collection) {
187 for (PersistableBusinessObject item : collection) {
188 item.refreshNonUpdateableReferences();
189 }
190 }
191
192
193
194
195
196
197
198
199
200
201 private static void refreshWithSecondaryKey(ProposalProjectDirector ppd) {
202 String secondaryKey = null;
203 if (ObjectUtils.isNotNull(ppd.getProjectDirector())) {
204 secondaryKey = ppd.getProjectDirector().getPrincipalName();
205 }
206 if (StringUtils.isNotBlank(secondaryKey)) {
207 Person dir = SpringContext.getBean(PersonService.class).getPersonByPrincipalName(secondaryKey);
208 ppd.setPrincipalId(dir == null ? null : dir.getPrincipalId());
209 }
210 if (StringUtils.isNotBlank(ppd.getPrincipalId())) {
211 Person person = SpringContext.getBean(PersonService.class).getPerson(ppd.getPrincipalId());
212 if (person != null) {
213 ppd.refreshNonUpdateableReferences();
214 }
215 }
216 }
217
218
219
220
221
222
223 public Proposal getProposal() {
224 return (Proposal) getBusinessObject();
225 }
226
227
228
229
230
231
232
233
234 @Override
235 public void addNewLineToCollection(String collectionName) {
236 refreshProposal(false);
237 super.addNewLineToCollection(collectionName);
238 }
239 }