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 if(itemForValidation.getItemLineNumber() != null){
79 String errorPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "["
80 + (itemForValidation.getItemLineNumber() - 1) + "]."
81 + PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE;
82 String attributeLabel = dataDictionaryService.getDataDictionary()
83 .getBusinessObjectEntry(purItem.getClass().getName())
84 .getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).getLabel();
85 GlobalVariables.getMessageMap().putError(errorPrefix, PurapKeyConstants.ERROR_ITEM_UOM_NOT_ALLOWED,
86 attributeLabel + " in " + purItem.getItemIdentifierString());
87 }
88 }
89
90 return valid;
91 }
92
93 public PurApItem getItemForValidation() {
94 return itemForValidation;
95 }
96
97 public void setItemForValidation(PurApItem itemForValidation) {
98 this.itemForValidation = itemForValidation;
99 }
100
101 public DataDictionaryService getDataDictionaryService() {
102 return dataDictionaryService;
103 }
104
105 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
106 this.dataDictionaryService = dataDictionaryService;
107 }
108
109 public BusinessObjectService getBusinessObjectService() {
110 return businessObjectService;
111 }
112
113 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
114 this.businessObjectService = businessObjectService;
115 }
116
117 }