1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.validation.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.module.purap.PurapKeyConstants;
20 import org.kuali.ole.module.purap.PurapPropertyConstants;
21 import org.kuali.ole.module.purap.businessobject.PurApItem;
22 import org.kuali.ole.module.purap.businessobject.PurchasingItemBase;
23 import org.kuali.ole.sys.OLEKeyConstants;
24 import org.kuali.ole.sys.document.validation.GenericValidation;
25 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26 import org.kuali.ole.vnd.businessobject.CommodityCode;
27 import org.kuali.rice.kns.service.DataDictionaryService;
28 import org.kuali.rice.krad.service.BusinessObjectService;
29 import org.kuali.rice.krad.util.GlobalVariables;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34 public class PurchasingCommodityCodeValidation extends GenericValidation {
35
36 private BusinessObjectService businessObjectService;
37 private DataDictionaryService dataDictionaryService;
38 private PurApItem itemForValidation;
39
40 public boolean validate(AttributedDocumentEvent event) {
41 boolean valid = true;
42 GlobalVariables.getMessageMap().clearErrorPath();
43 GlobalVariables.getMessageMap().addToErrorPath("document.item[" + (itemForValidation.getItemLineNumber() - 1) + "]");
44
45 itemForValidation.refreshReferenceObject(PurapPropertyConstants.COMMODITY_CODE);
46 valid &= validateCommodityCodes(itemForValidation, commodityCodeIsRequired());
47
48 GlobalVariables.getMessageMap().removeFromErrorPath("document.item[" + (itemForValidation.getItemLineNumber() - 1) + "]");
49
50 return valid;
51
52 }
53
54 public PurApItem getItemForValidation() {
55 return itemForValidation;
56 }
57
58 public void setItemForValidation(PurApItem itemForValidation) {
59 this.itemForValidation = itemForValidation;
60 }
61
62
63
64
65
66
67
68
69
70 protected boolean validateCommodityCodes(PurApItem item, boolean commodityCodeRequired) {
71 boolean valid = true;
72 String identifierString = item.getItemIdentifierString();
73 PurchasingItemBase purItem = (PurchasingItemBase) item;
74
75
76 if (commodityCodeRequired && StringUtils.isBlank(purItem.getPurchasingCommodityCode())) {
77
78 valid = false;
79 String attributeLabel = dataDictionaryService.
80 getDataDictionary().getBusinessObjectEntry(CommodityCode.class.getName()).
81 getAttributeDefinition(PurapPropertyConstants.ITEM_COMMODITY_CODE).getLabel();
82 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + identifierString);
83 } else if (StringUtils.isNotBlank(purItem.getPurchasingCommodityCode())) {
84
85 Map<String, String> fieldValues = new HashMap<String, String>();
86 fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purItem.getPurchasingCommodityCode());
87 if (businessObjectService.countMatching(CommodityCode.class, fieldValues) != 1) {
88
89 valid = false;
90 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID, " in " + identifierString);
91 } else {
92 valid &= validateThatCommodityCodeIsActive(item);
93 }
94 }
95
96 return valid;
97 }
98
99 protected boolean validateThatCommodityCodeIsActive(PurApItem item) {
100 if (!((PurchasingItemBase) item).getCommodityCode().isActive()) {
101
102 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in " + item.getItemIdentifierString());
103 return false;
104 }
105 return true;
106 }
107
108
109
110
111
112
113
114 protected boolean commodityCodeIsRequired() {
115 return false;
116 }
117
118 public BusinessObjectService getBusinessObjectService() {
119 return businessObjectService;
120 }
121
122 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
123 this.businessObjectService = businessObjectService;
124 }
125
126 public DataDictionaryService getDataDictionaryService() {
127 return dataDictionaryService;
128 }
129
130 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
131 this.dataDictionaryService = dataDictionaryService;
132 }
133
134 }