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;
20 import org.kuali.ole.module.purap.PurapKeyConstants;
21 import org.kuali.ole.module.purap.PurapPropertyConstants;
22 import org.kuali.ole.module.purap.businessobject.PurApItem;
23 import org.kuali.ole.module.purap.businessobject.PurchasingItemBase;
24 import org.kuali.ole.sys.OLEKeyConstants;
25 import org.kuali.ole.sys.OLEPropertyConstants;
26 import org.kuali.ole.sys.businessobject.UnitOfMeasure;
27 import org.kuali.ole.sys.document.validation.GenericValidation;
28 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29 import org.kuali.rice.kns.service.DataDictionaryService;
30 import org.kuali.rice.krad.service.BusinessObjectService;
31 import org.kuali.rice.krad.util.GlobalVariables;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
36 public class PurchasingUnitOfMeasureValidation extends GenericValidation {
37
38 private PurApItem itemForValidation;
39 private DataDictionaryService dataDictionaryService;
40 private BusinessObjectService businessObjectService;
41
42
43
44
45 @Override
46 public boolean validate(AttributedDocumentEvent event) {
47 boolean valid = true;
48 PurchasingItemBase purItem = (PurchasingItemBase) itemForValidation;
49 GlobalVariables.getMessageMap().addToErrorPath(PurapConstants.ITEM_TAB_ERRORS);
50
51
52 if (purItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
53 String uomCode = purItem.getItemUnitOfMeasureCode();
54 if (StringUtils.isEmpty(uomCode)) {
55 valid = false;
56 String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(purItem.getClass().getName()).
57 getAttributeDefinition(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).
58 getLabel();
59 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + purItem.getItemIdentifierString());
60 } else {
61
62 Map<String, String> fieldValues = new HashMap<String, String>();
63 fieldValues.put(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, purItem.getItemUnitOfMeasureCode());
64 if (businessObjectService.countMatching(UnitOfMeasure.class, fieldValues) != 1) {
65
66 valid = false;
67 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, PurapKeyConstants.PUR_ITEM_UNIT_OF_MEASURE_CODE_INVALID, " in " + purItem.getItemIdentifierString());
68 }
69 }
70 }
71
72 GlobalVariables.getMessageMap().clearErrorPath();
73
74
75 if (purItem.getItemType().isAmountBasedGeneralLedgerIndicator()
76 && StringUtils.isNotBlank(purItem.getItemUnitOfMeasureCode())) {
77 valid = false;
78 String errorPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "["
79 + (itemForValidation.getItemLineNumber() - 1) + "]."
80 + PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE;
81 String attributeLabel = dataDictionaryService.getDataDictionary()
82 .getBusinessObjectEntry(purItem.getClass().getName())
83 .getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).getLabel();
84 GlobalVariables.getMessageMap().putError(errorPrefix, PurapKeyConstants.ERROR_ITEM_UOM_NOT_ALLOWED,
85 attributeLabel + " in " + purItem.getItemIdentifierString());
86 }
87
88 return valid;
89 }
90
91 public PurApItem getItemForValidation() {
92 return itemForValidation;
93 }
94
95 public void setItemForValidation(PurApItem itemForValidation) {
96 this.itemForValidation = itemForValidation;
97 }
98
99 public DataDictionaryService getDataDictionaryService() {
100 return dataDictionaryService;
101 }
102
103 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
104 this.dataDictionaryService = dataDictionaryService;
105 }
106
107 public BusinessObjectService getBusinessObjectService() {
108 return businessObjectService;
109 }
110
111 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
112 this.businessObjectService = businessObjectService;
113 }
114
115 }