1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.vnd.businessobject.lookup;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Properties;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.kuali.ole.integration.purap.PurchasingAccountsPayableModuleService;
28 import org.kuali.ole.sys.OLEConstants;
29 import org.kuali.ole.sys.context.SpringContext;
30 import org.kuali.ole.vnd.VendorConstants;
31 import org.kuali.ole.vnd.VendorKeyConstants;
32 import org.kuali.ole.vnd.VendorPropertyConstants;
33 import org.kuali.ole.vnd.businessobject.VendorAddress;
34 import org.kuali.ole.vnd.businessobject.VendorDetail;
35 import org.kuali.ole.vnd.document.service.VendorService;
36 import org.kuali.rice.core.web.format.Formatter;
37 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
38 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
39 import org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl;
40 import org.kuali.rice.kns.lookup.HtmlData;
41 import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
42 import org.kuali.rice.krad.bo.BusinessObject;
43 import org.kuali.rice.krad.exception.ValidationException;
44 import org.kuali.rice.krad.lookup.CollectionIncomplete;
45 import org.kuali.rice.krad.util.BeanPropertyComparator;
46 import org.kuali.rice.krad.util.GlobalVariables;
47 import org.kuali.rice.krad.util.KRADConstants;
48 import org.kuali.rice.krad.util.ObjectUtils;
49 import org.kuali.rice.krad.util.UrlFactory;
50
51 public class VendorLookupableHelperServiceImpl extends AbstractLookupableHelperServiceImpl {
52 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(VendorLookupableHelperServiceImpl.class);
53
54 protected VendorService vendorService;
55 protected ParameterService parameterService;
56
57
58
59
60
61
62
63
64 @Override
65 public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
66 VendorDetail vendor = (VendorDetail) businessObject;
67 List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
68
69 AnchorHtmlData anchorHtmlData = super.getUrlData(businessObject, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL, pkNames);
70 anchorHtmlDataList.add(anchorHtmlData);
71 String documentTypeName = OLEConstants.Vendor.DOCUMENT_TYPE;
72 String nameSpaceCode = OLEConstants.Vendor.VENDOR_NAMESPACE;
73
74 boolean hasPermission = KimApiServiceLocator.getPermissionService().isAuthorized(
75 GlobalVariables.getUserSession().getPerson().getPrincipalId(), nameSpaceCode,
76 OLEConstants.Vendor.CREATE_VENDOR_DIVISION, Collections.<String, String> emptyMap());
77 if (vendor.isVendorParentIndicator() && vendor.isActiveIndicator() && hasPermission ) {
78
79 anchorHtmlDataList.add(super.getUrlData(businessObject, OLEConstants.MAINTENANCE_NEWWITHEXISTING_ACTION, VendorConstants.CREATE_DIVISION, pkNames));
80 }
81
82
83 String b2bUrlString = SpringContext.getBean(PurchasingAccountsPayableModuleService.class).getB2BUrlString();
84 if (vendor.isB2BVendor() && StringUtils.isNotBlank(b2bUrlString)) {
85 Properties theProperties = new Properties();
86 theProperties.put("channelTitle", "Shop Catalogs");
87 String backLocation = this.getBackLocation();
88 int lastSlash = backLocation.lastIndexOf("/");
89 String returnUrlForShop = backLocation.substring(0, lastSlash+1) + "portal.do";
90 String href = UrlFactory.parameterizeUrl(returnUrlForShop, theProperties);
91 anchorHtmlDataList.add(new AnchorHtmlData(href + b2bUrlString, null, "shop"));
92 }
93 return anchorHtmlDataList;
94 }
95
96
97
98
99
100
101
102
103
104
105
106 @Override
107 protected String getActionUrlHref(BusinessObject businessObject, String methodToCall, List pkNames){
108 if (!methodToCall.equals(OLEConstants.COPY_METHOD)) {
109 Properties parameters = new Properties();
110 parameters.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
111 parameters.put(OLEConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, businessObject.getClass().getName());
112
113 for (Iterator<String> iter = pkNames.iterator(); iter.hasNext();) {
114 String fieldNm = iter.next();
115 if (!fieldNm.equals(VendorPropertyConstants.VENDOR_DETAIL_ASSIGNED_ID) ||
116 !((VendorDetail) businessObject).isVendorParentIndicator()
117 || (((VendorDetail) businessObject).isVendorParentIndicator())
118 && !methodToCall.equals(OLEConstants.MAINTENANCE_NEWWITHEXISTING_ACTION)) {
119 Object fieldVal = ObjectUtils.getPropertyValue(businessObject, fieldNm);
120 if (fieldVal == null) {
121 fieldVal = OLEConstants.EMPTY_STRING;
122 }
123 if (fieldVal instanceof java.sql.Date) {
124 String formattedString = OLEConstants.EMPTY_STRING;
125 if (Formatter.findFormatter(fieldVal.getClass()) != null) {
126 Formatter formatter = Formatter.getFormatter(fieldVal.getClass());
127 formattedString = (String) formatter.format(fieldVal);
128 fieldVal = formattedString;
129 }
130 }
131 parameters.put(fieldNm, fieldVal.toString());
132 }
133 }
134 return UrlFactory.parameterizeUrl(OLEConstants.MAINTENANCE_ACTION, parameters);
135 } else {
136 return OLEConstants.EMPTY_STRING;
137 }
138 }
139
140
141
142
143
144
145
146
147 @Override
148 public List<BusinessObject> getSearchResults(Map<String, String> fieldValues) {
149 boolean unbounded = false;
150 super.setBackLocation(fieldValues.get(OLEConstants.BACK_LOCATION));
151 super.setDocFormKey(fieldValues.get(OLEConstants.DOC_FORM_KEY));
152
153 String vendorName = fieldValues.get(VendorPropertyConstants.VENDOR_NAME);
154
155 List<BusinessObject> searchResults = (List) getLookupService().findCollectionBySearchHelper(getBusinessObjectClass(), fieldValues, unbounded);
156
157
158
159
160 if (StringUtils.isNotEmpty(vendorName)) {
161
162 fieldValues.put(VendorPropertyConstants.VENDOR_ALIAS_NAME_FULL_PATH, vendorName);
163
164 fieldValues.put(VendorPropertyConstants.VENDOR_ALIAS_ACTIVE, "Y");
165 fieldValues.remove(VendorPropertyConstants.VENDOR_NAME);
166 List<BusinessObject> searchResults2 = (List) getLookupService().findCollectionBySearchHelper(getBusinessObjectClass(), fieldValues, unbounded);
167
168 searchResults.addAll(searchResults2);
169 if (searchResults instanceof CollectionIncomplete && searchResults2 instanceof CollectionIncomplete) {
170 ((CollectionIncomplete) searchResults).setActualSizeIfTruncated(((CollectionIncomplete) searchResults).getActualSizeIfTruncated().longValue() + ((CollectionIncomplete) searchResults2).getActualSizeIfTruncated().longValue());
171 }
172 }
173
174 List<BusinessObject> processedSearchResults = new ArrayList();
175
176
177 for (BusinessObject businessObject : searchResults) {
178 VendorDetail vendor = (VendorDetail) businessObject;
179
180
181
182
183
184
185
186
187
188
189 if (!processedSearchResults.contains(vendor)) {
190 Map<String, String> tmpValues = new HashMap<String, String>();
191 List<VendorDetail> relatedVendors = new ArrayList();
192 tmpValues.put(VendorPropertyConstants.VENDOR_HEADER_GENERATED_ID, vendor.getVendorHeaderGeneratedIdentifier().toString());
193 relatedVendors = (List) getLookupService().findCollectionBySearchHelper(getBusinessObjectClass(), tmpValues, unbounded);
194
195 for (VendorDetail tmpVendor : relatedVendors) {
196 if (ObjectUtils.isNotNull(tmpVendor) && !processedSearchResults.contains(tmpVendor)) {
197
198 updateDefaultVendorAddress(tmpVendor);
199 processedSearchResults.add(tmpVendor);
200 }
201 }
202
203 if (!processedSearchResults.contains(vendor)) {
204 updateDefaultVendorAddress(vendor);
205 processedSearchResults.add(vendor);
206 }
207 }
208 }
209
210 for (BusinessObject businessObject : processedSearchResults) {
211 VendorDetail vendor = (VendorDetail) businessObject;
212 if (!vendor.isVendorParentIndicator()) {
213
214 for (BusinessObject tmpObject : processedSearchResults) {
215 VendorDetail tmpVendor = (VendorDetail) tmpObject;
216 if (tmpVendor.getVendorHeaderGeneratedIdentifier().equals(vendor.getVendorHeaderGeneratedIdentifier()) && tmpVendor.isVendorParentIndicator()) {
217 vendor.setVendorName(tmpVendor.getVendorName() + " > " + vendor.getVendorName());
218 break;
219 }
220 }
221 }
222 }
223
224 searchResults.clear();
225 searchResults.addAll(processedSearchResults);
226
227
228 List<String> defaultSortColumns = getDefaultSortColumns();
229 if (defaultSortColumns.size() > 0) {
230 Collections.sort(searchResults, new BeanPropertyComparator(getDefaultSortColumns(), true));
231 }
232
233 return searchResults;
234 }
235
236
237
238
239
240
241 private void updateDefaultVendorAddress(VendorDetail vendor) {
242 VendorAddress defaultAddress = null;
243 if(vendor.getVendorAddresses()!=null && vendor.getVendorHeader()!=null && vendor.getVendorHeader().getVendorType()!=null && vendor.getVendorHeader().getVendorType().getAddressType()!=null && vendor.getVendorHeader().getVendorType().getAddressType().getVendorAddressTypeCode()!=null){
244 defaultAddress = vendorService.getVendorDefaultAddress(vendor.getVendorAddresses(), vendor.getVendorHeader().getVendorType().getAddressType().getVendorAddressTypeCode(), "");
245 }
246 if (ObjectUtils.isNotNull(defaultAddress)) {
247
248 if (ObjectUtils.isNotNull(defaultAddress.getVendorState())) {
249 vendor.setVendorStateForLookup(defaultAddress.getVendorState().getName());
250 } else {
251 if ( LOG.isDebugEnabled() ) {
252 LOG.debug( "Warning - unable to retrieve state for " + defaultAddress.getVendorCountryCode() + " / " + defaultAddress.getVendorStateCode() );
253 }
254 vendor.setVendorStateForLookup("");
255 }
256 vendor.setDefaultAddressLine1(defaultAddress.getVendorLine1Address());
257 vendor.setDefaultAddressLine2(defaultAddress.getVendorLine2Address());
258 vendor.setDefaultAddressCity(defaultAddress.getVendorCityName());
259 vendor.setDefaultAddressPostalCode(defaultAddress.getVendorZipCode());
260 vendor.setDefaultAddressStateCode(defaultAddress.getVendorStateCode());
261 vendor.setDefaultAddressInternationalProvince(defaultAddress.getVendorAddressInternationalProvinceName());
262 vendor.setDefaultAddressCountryCode(defaultAddress.getVendorCountryCode());
263 vendor.setDefaultFaxNumber(defaultAddress.getVendorFaxNumber());
264 } else {
265 if ( LOG.isDebugEnabled() ) {
266 LOG.debug( "Warning - default vendor address was null for " + vendor.getVendorNumber() + " / " + vendor.getVendorHeader().getVendorType().getAddressType().getVendorAddressTypeCode() );
267 }
268 vendor.setVendorStateForLookup("");
269 }
270 }
271
272
273
274
275
276
277
278
279
280
281 @Override
282 public void validateSearchParameters(Map fieldValues) {
283 super.validateSearchParameters(fieldValues);
284
285 validateVendorNumber(fieldValues);
286 validateTaxNumber(fieldValues);
287
288 if (GlobalVariables.getMessageMap().hasErrors()) {
289 throw new ValidationException("Error(s) in search criteria");
290 }
291 }
292
293
294
295
296
297
298
299
300 private void validateVendorNumber(Map fieldValues) {
301 String vendorNumber = (String) fieldValues.get(VendorPropertyConstants.VENDOR_NUMBER);
302 if (StringUtils.isNotBlank(vendorNumber)) {
303 int dashPos1 = vendorNumber.indexOf(VendorConstants.DASH);
304 if (dashPos1 > -1) {
305 if (vendorNumber.indexOf(VendorConstants.DASH, dashPos1 + 1) > -1) {
306 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_NUMBER, VendorKeyConstants.ERROR_VENDOR_LOOKUP_VNDR_NUM_TOO_MANY_DASHES);
307 }
308 if (vendorNumber.matches("\\-*")) {
309 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_NUMBER, VendorKeyConstants.ERROR_VENDOR_LOOKUP_VNDR_NUM_DASHES_ONLY);
310 }
311 }
312 extractVendorNumberToVendorIds(fieldValues, vendorNumber);
313 }
314 }
315
316
317
318
319
320
321
322
323
324
325
326 private void extractVendorNumberToVendorIds(Map fieldValues, String vendorNumber) {
327 String vendorHeaderGeneratedIdentifier = null;
328 String vendorDetailAssignedIdentifier = null;
329 int indexOfDash = vendorNumber.indexOf(VendorConstants.DASH);
330 if (indexOfDash < 0) {
331 vendorHeaderGeneratedIdentifier = vendorNumber;
332 }
333 else {
334 vendorHeaderGeneratedIdentifier = vendorNumber.substring(0, indexOfDash);
335 vendorDetailAssignedIdentifier = vendorNumber.substring(indexOfDash + 1, vendorNumber.length());
336 }
337 try {
338 if (StringUtils.isNotEmpty(vendorHeaderGeneratedIdentifier)) {
339 Integer.parseInt(vendorHeaderGeneratedIdentifier);
340 }
341 if (StringUtils.isNotEmpty(vendorDetailAssignedIdentifier)) {
342 Integer.parseInt(vendorDetailAssignedIdentifier);
343 }
344 fieldValues.remove(VendorPropertyConstants.VENDOR_NUMBER);
345 fieldValues.put(VendorPropertyConstants.VENDOR_HEADER_GENERATED_ID, vendorHeaderGeneratedIdentifier);
346 if (StringUtils.isNotEmpty(vendorDetailAssignedIdentifier)) {
347 fieldValues.put(VendorPropertyConstants.VENDOR_DETAIL_ASSIGNED_ID, vendorDetailAssignedIdentifier);
348 }
349 }
350 catch (NumberFormatException headerExc) {
351 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_NUMBER, VendorKeyConstants.ERROR_VENDOR_LOOKUP_VNDR_NUM_NUMERIC_DASH_SEPARATED);
352 }
353 }
354
355
356
357
358
359
360 private void validateTaxNumber(Map fieldValues) {
361 String taxNumber = (String) fieldValues.get(VendorPropertyConstants.VENDOR_TAX_NUMBER);
362 if (StringUtils.isNotBlank(taxNumber) && (!StringUtils.isNumeric(taxNumber) || taxNumber.length() != 9)) {
363 GlobalVariables.getMessageMap().putError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_VENDOR_LOOKUP_TAX_NUM_INVALID);
364 }
365 }
366
367 public void setVendorService(VendorService vendorService) {
368 this.vendorService = vendorService;
369 }
370
371 @Override
372 public void setParameterService(ParameterService parameterService) {
373 this.parameterService = parameterService;
374 }
375
376 }