1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.vnd.document.validation.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase;
20 import org.kuali.ole.sys.OLEConstants;
21 import org.kuali.ole.sys.context.SpringContext;
22 import org.kuali.ole.vnd.VendorConstants;
23 import org.kuali.ole.vnd.VendorKeyConstants;
24 import org.kuali.ole.vnd.VendorUtils;
25 import org.kuali.ole.vnd.businessobject.VendorDetail;
26 import org.kuali.ole.vnd.businessobject.VendorType;
27 import org.kuali.ole.vnd.document.service.VendorService;
28 import org.kuali.rice.core.api.datetime.DateTimeService;
29 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
30 import org.kuali.rice.krad.service.BusinessObjectService;
31 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32 import org.kuali.rice.krad.util.GlobalVariables;
33 import org.kuali.rice.krad.util.ObjectUtils;
34
35
36
37
38
39
40 public class VendorPreRules extends MaintenancePreRulesBase {
41
42 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VendorPreRules.class);
43
44 protected VendorDetail newVendorDetail;
45 protected String personId;
46
47 public VendorPreRules() {
48 }
49
50
51
52
53
54
55
56 public String getPersonId() {
57 if (ObjectUtils.isNull(personId)) {
58 this.personId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
59 }
60 return this.personId;
61 }
62
63
64
65
66
67
68 @Override
69 protected boolean doCustomPreRules(MaintenanceDocument document) {
70 setupConvenienceObjects(document);
71 setVendorNamesAndIndicator(document);
72 setVendorRestriction(document);
73 if (StringUtils.isBlank(question) || (question.equals(VendorConstants.CHANGE_TO_PARENT_QUESTION_ID))) {
74 detectAndConfirmChangeToParent(document);
75 }
76
77
78 if (newVendorDetail.getVendorHeaderGeneratedIdentifier() == null &&
79 newVendorDetail.getVendorDetailAssignedIdentifier() == null) {
80 displayReview(document);
81 }
82 return true;
83 }
84
85
86
87
88
89
90
91
92 protected void setupConvenienceObjects(MaintenanceDocument document) {
93
94 newVendorDetail = (VendorDetail) document.getNewMaintainableObject().getDataObject();
95 }
96
97
98
99
100
101
102
103
104 protected void setVendorNamesAndIndicator(MaintenanceDocument document) {
105 if (StringUtils.isBlank(newVendorDetail.getVendorName()) && !StringUtils.isBlank(newVendorDetail.getVendorFirstName()) && !StringUtils.isBlank(newVendorDetail.getVendorLastName())) {
106
107 newVendorDetail.setVendorFirstLastNameIndicator(true);
108 newVendorDetail.setVendorFirstName(removeDelimiter(newVendorDetail.getVendorFirstName()));
109 newVendorDetail.setVendorLastName(removeDelimiter(newVendorDetail.getVendorLastName()));
110
111 }
112 else if (!StringUtils.isBlank(newVendorDetail.getVendorName()) && StringUtils.isBlank(newVendorDetail.getVendorFirstName()) && StringUtils.isBlank(newVendorDetail.getVendorLastName())) {
113 newVendorDetail.setVendorFirstLastNameIndicator(false);
114 }
115 }
116
117
118
119
120
121
122 protected void setVendorRestriction(MaintenanceDocument document) {
123 VendorDetail oldVendorDetail = (VendorDetail) document.getOldMaintainableObject().getDataObject();
124 Boolean oldVendorRestrictedIndicator = null;
125 if (ObjectUtils.isNotNull(oldVendorDetail)) {
126 oldVendorRestrictedIndicator = oldVendorDetail.getVendorRestrictedIndicator();
127 }
128
129 if ((ObjectUtils.isNull(oldVendorRestrictedIndicator) || (!oldVendorRestrictedIndicator)) && ObjectUtils.isNotNull(newVendorDetail.getVendorRestrictedIndicator()) && newVendorDetail.getVendorRestrictedIndicator()) {
130
131 newVendorDetail.setVendorRestrictedDate(SpringContext.getBean(DateTimeService.class).getCurrentSqlDate());
132 newVendorDetail.setVendorRestrictedPersonIdentifier(getPersonId());
133 }
134 else if (ObjectUtils.isNotNull(oldVendorRestrictedIndicator) && oldVendorRestrictedIndicator && ObjectUtils.isNotNull(newVendorDetail.getVendorRestrictedIndicator()) && (!newVendorDetail.getVendorRestrictedIndicator())) {
135
136 newVendorDetail.setVendorRestrictedDate(null);
137 newVendorDetail.setVendorRestrictedPersonIdentifier(null);
138 }
139
140 }
141
142
143
144
145
146
147
148 protected String removeDelimiter(String str) {
149 String result = str.replaceAll(VendorConstants.NAME_DELIM, OLEConstants.BLANK_SPACE);
150 return result;
151 }
152
153
154
155
156
157
158
159 public void displayReview(MaintenanceDocument document) {
160 VendorDetail vendorDetail = (VendorDetail) document.getDocumentDataObject();
161
162 VendorType vendorType = vendorDetail.getVendorHeader().getVendorType();
163
164 if (vendorType == null) {
165 vendorType = new VendorType();
166 vendorType.setVendorTypeCode(vendorDetail.getVendorHeader().getVendorTypeCode());
167 vendorType = (VendorType) KRADServiceLocatorWeb.getLegacyDataAdapter().retrieve(vendorType);
168 }
169 if (vendorType != null && vendorType.isVendorShowReviewIndicator()) {
170 String questionText = vendorType.getVendorReviewText();
171
172
173
174
175
176 if (questionText != null) {
177
178 if (vendorDetail.getVendorName() != null) {
179 questionText = questionText.replace("{0}", vendorDetail.getVendorName());
180 }
181 else {
182 questionText = questionText.replace("{0}", "(not entered)");
183 }
184 questionText = questionText.replace("{1}", document.getDocumentNumber());
185 Boolean proceed = super.askOrAnalyzeYesNoQuestion(VendorConstants.ACKNOWLEDGE_NEW_VENDOR_INFO, questionText);
186
187 if (!proceed) {
188 abortRulesCheck();
189 }
190 }
191 }
192 }
193
194
195
196
197
198
199
200
201
202 protected void detectAndConfirmChangeToParent(MaintenanceDocument document) {
203 boolean proceed = true;
204 VendorDetail oldVendorDetail = (VendorDetail) document.getOldMaintainableObject().getDataObject();
205 boolean oldVendorIsParent = oldVendorDetail.isVendorParentIndicator();
206 boolean newVendorIsParent = newVendorDetail.isVendorParentIndicator();
207 if (!oldVendorIsParent && newVendorIsParent) {
208
209 VendorDetail oldParentVendor = SpringContext.getBean(VendorService.class).getParentVendor(oldVendorDetail.getVendorHeaderGeneratedIdentifier());
210 String oldParentVendorName = oldParentVendor.getVendorName();
211 String oldParentVendorNumber = oldParentVendor.getVendorNumber();
212 proceed = askOrAnalyzeYesNoQuestion(VendorConstants.CHANGE_TO_PARENT_QUESTION_ID, VendorUtils.buildMessageText(VendorKeyConstants.CONFIRM_VENDOR_CHANGE_TO_PARENT, oldVendorDetail.getVendorName() + " (" + oldVendorDetail.getVendorNumber() + ")", oldParentVendorName + " (" + oldParentVendorNumber + ")"));
213 if (proceed) {
214 newVendorDetail.setVendorParentIndicator(true);
215 }
216 else {
217 newVendorDetail.setVendorParentIndicator(false);
218 }
219 }
220 if (!proceed) {
221 abortRulesCheck();
222 }
223 }
224
225 }
226