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.PurapConstants.ItemFields;
20 import org.kuali.ole.module.purap.PurapConstants.ItemTypeCodes;
21 import org.kuali.ole.module.purap.PurapKeyConstants;
22 import org.kuali.ole.module.purap.PurapPropertyConstants;
23 import org.kuali.ole.module.purap.businessobject.PurApItem;
24 import org.kuali.ole.module.purap.businessobject.PurchasingItemBase;
25 import org.kuali.ole.sys.OLEKeyConstants;
26 import org.kuali.ole.sys.OLEPropertyConstants;
27 import org.kuali.ole.sys.businessobject.UnitOfMeasure;
28 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29 import org.kuali.ole.vnd.businessobject.CommodityCode;
30 import org.kuali.rice.kns.service.DataDictionaryService;
31 import org.kuali.rice.krad.service.BusinessObjectService;
32 import org.kuali.rice.krad.util.GlobalVariables;
33 import org.kuali.rice.krad.util.ObjectUtils;
34
35 import java.math.BigDecimal;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 public class PurchasingAddItemValidation extends PurchasingAccountsPayableAddItemValidation {
40
41 private BusinessObjectService businessObjectService;
42 private DataDictionaryService dataDictionaryService;
43
44 @Override
45 public boolean validate(AttributedDocumentEvent event) {
46 boolean valid = true;
47 GlobalVariables.getMessageMap().addToErrorPath(PurapPropertyConstants.NEW_PURCHASING_ITEM_LINE);
48
49 PurApItem refreshedItem = getItemForValidation();
50 refreshedItem.refreshReferenceObject("itemType");
51 super.setItemForValidation(refreshedItem);
52
53 valid &= super.validate(event);
54 valid &= validateItemUnitPrice(getItemForValidation());
55 valid &= validateUnitOfMeasure(getItemForValidation());
56 if (getItemForValidation().getItemType().isLineItemIndicator()) {
57
58 valid &= validateItemQuantity(getItemForValidation());
59 valid &= validateCommodityCodes(getItemForValidation(), commodityCodeIsRequired());
60 }
61 GlobalVariables.getMessageMap().removeFromErrorPath(PurapPropertyConstants.NEW_PURCHASING_ITEM_LINE);
62
63 return valid;
64 }
65
66
67
68
69
70
71
72
73
74 protected boolean validateCommodityCodes(PurApItem item, boolean commodityCodeRequired) {
75 boolean valid = true;
76 String identifierString = item.getItemIdentifierString();
77 PurchasingItemBase purItem = (PurchasingItemBase) item;
78
79
80 if (commodityCodeRequired && StringUtils.isBlank(purItem.getPurchasingCommodityCode())) {
81
82 valid = false;
83 String attributeLabel = dataDictionaryService.
84 getDataDictionary().getBusinessObjectEntry(CommodityCode.class.getName()).
85 getAttributeDefinition(PurapPropertyConstants.ITEM_COMMODITY_CODE).getLabel();
86 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + identifierString);
87 } else if (StringUtils.isNotBlank(purItem.getPurchasingCommodityCode())) {
88
89 Map<String, String> fieldValues = new HashMap<String, String>();
90 fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purItem.getPurchasingCommodityCode());
91 if (businessObjectService.countMatching(CommodityCode.class, fieldValues) != 1) {
92
93 valid = false;
94 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID, " in " + identifierString);
95 } else {
96 valid &= validateThatCommodityCodeIsActive(item);
97 }
98 }
99
100 return valid;
101 }
102
103
104
105
106
107
108
109
110 public boolean validateItemUnitPrice(PurApItem item) {
111 boolean valid = true;
112 if (item.getItemType().isLineItemIndicator()) {
113 if (ObjectUtils.isNull(item.getItemUnitPrice())) {
114 valid = false;
115 String attributeLabel = dataDictionaryService.
116 getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
117 getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_PRICE).getLabel();
118 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + item.getItemIdentifierString());
119 }
120 }
121
122 if (ObjectUtils.isNotNull(item.getItemUnitPrice())) {
123 if ((BigDecimal.ZERO.compareTo(item.getItemUnitPrice()) > 0) && ((!item.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE)) && (!item.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE)))) {
124
125 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_AMOUNT_BELOW_ZERO, ItemFields.UNIT_COST, item.getItemIdentifierString());
126 valid = false;
127 } else if ((BigDecimal.ZERO.compareTo(item.getItemUnitPrice()) < 0) && ((item.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE)) || (item.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE)))) {
128
129 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_AMOUNT_NOT_BELOW_ZERO, ItemFields.UNIT_COST, item.getItemIdentifierString());
130 valid = false;
131 }
132 }
133
134 return valid;
135 }
136
137
138
139
140
141
142
143 public boolean validateUnitOfMeasure(PurApItem item) {
144 boolean valid = true;
145 PurchasingItemBase purItem = (PurchasingItemBase) item;
146
147 if (purItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
148 String uomCode = purItem.getItemUnitOfMeasureCode();
149 if (StringUtils.isEmpty(uomCode)) {
150 valid = false;
151 String attributeLabel = dataDictionaryService.
152 getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
153 getAttributeDefinition(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).
154 getLabel();
155 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + item.getItemIdentifierString());
156 } else {
157
158 Map<String, String> fieldValues = new HashMap<String, String>();
159 fieldValues.put(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, purItem.getItemUnitOfMeasureCode());
160 if (businessObjectService.countMatching(UnitOfMeasure.class, fieldValues) != 1) {
161
162 valid = false;
163 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, PurapKeyConstants.PUR_ITEM_UNIT_OF_MEASURE_CODE_INVALID, " in " + item.getItemIdentifierString());
164 }
165 }
166 }
167
168
169
170 if (purItem.getItemType().isAmountBasedGeneralLedgerIndicator()
171 && StringUtils.isNotBlank(purItem.getItemUnitOfMeasureCode())) {
172 valid = false;
173 String attributeLabel = dataDictionaryService.getDataDictionary()
174 .getBusinessObjectEntry(item.getClass().getName())
175 .getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).getLabel();
176 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE,
177 PurapKeyConstants.ERROR_ITEM_UOM_NOT_ALLOWED,
178 attributeLabel + " in " + item.getItemIdentifierString());
179 }
180
181 return valid;
182 }
183
184
185
186
187
188
189
190 public boolean validateItemDescription(PurApItem item) {
191 boolean valid = true;
192 if (StringUtils.isEmpty(item.getItemDescription())) {
193 valid = false;
194 String attributeLabel = dataDictionaryService.
195 getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
196 getAttributeDefinition(PurapPropertyConstants.ITEM_DESCRIPTION).getLabel();
197 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_DESCRIPTION, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + item.getItemIdentifierString());
198 }
199 return valid;
200 }
201
202
203
204
205
206
207
208
209 public boolean validateItemQuantity(PurApItem item) {
210 boolean valid = true;
211 PurchasingItemBase purItem = (PurchasingItemBase) item;
212 if (purItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && (ObjectUtils.isNull(purItem.getItemQuantity()))) {
213 valid = false;
214 String attributeLabel = dataDictionaryService.
215 getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
216 getAttributeDefinition(PurapPropertyConstants.ITEM_QUANTITY).getLabel();
217 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.QUANTITY, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + item.getItemIdentifierString());
218 } else if (purItem.getItemType().isAmountBasedGeneralLedgerIndicator() && ObjectUtils.isNotNull(purItem.getItemQuantity())) {
219 valid = false;
220 String attributeLabel = dataDictionaryService.
221 getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
222 getAttributeDefinition(PurapPropertyConstants.ITEM_QUANTITY).getLabel();
223 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.QUANTITY, PurapKeyConstants.ERROR_ITEM_QUANTITY_NOT_ALLOWED, attributeLabel + " in " + item.getItemIdentifierString());
224 }
225
226 return valid;
227 }
228
229
230
231
232
233
234
235 protected boolean commodityCodeIsRequired() {
236 return false;
237 }
238
239 protected boolean validateThatCommodityCodeIsActive(PurApItem item) {
240 if (!((PurchasingItemBase) item).getCommodityCode().isActive()) {
241
242 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in " + item.getItemIdentifierString());
243 return false;
244 }
245 return true;
246 }
247
248 public BusinessObjectService getBusinessObjectService() {
249 return businessObjectService;
250 }
251
252 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
253 this.businessObjectService = businessObjectService;
254 }
255
256 public DataDictionaryService getDataDictionaryService() {
257 return dataDictionaryService;
258 }
259
260 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
261 this.dataDictionaryService = dataDictionaryService;
262 }
263
264 }