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