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.integration.cab.CapitalAssetBuilderModuleService;
20 import org.kuali.ole.module.purap.PurapConstants;
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.module.purap.document.PurchasingDocument;
26 import org.kuali.ole.sys.OLEKeyConstants;
27 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
28 import org.kuali.ole.vnd.businessobject.CommodityCode;
29 import org.kuali.rice.krad.service.BusinessObjectService;
30 import org.kuali.rice.krad.util.GlobalVariables;
31
32 import java.util.HashMap;
33 import java.util.Map;
34
35 public class PurchasingNewIndividualItemValidation extends PurchasingAccountsPayableNewIndividualItemValidation {
36
37 private BusinessObjectService businessObjectService;
38 private CapitalAssetBuilderModuleService capitalAssetBuilderModuleService;
39 private PurchasingUnitOfMeasureValidation unitOfMeasureValidation;
40 private PurchasingItemUnitPriceValidation itemUnitPriceValidation;
41 private PurchasingItemDescriptionValidation itemDescriptionValidation;
42 private PurchasingItemQuantityValidation itemQuantityValidation;
43 private PurchasingBelowTheLineItemNoUnitCostValidation belowTheLineItemNoUnitCostValidation;
44
45 public boolean validate(AttributedDocumentEvent event) {
46 boolean valid = true;
47 valid &= super.validate(event);
48 String recurringPaymentTypeCode = ((PurchasingDocument) event.getDocument()).getRecurringPaymentTypeCode();
49
50 if (!getItemForValidation().getItemType().isAdditionalChargeIndicator()) {
51 valid &= capitalAssetBuilderModuleService.validateItemCapitalAssetWithErrors(recurringPaymentTypeCode, getItemForValidation(), false);
52 }
53 unitOfMeasureValidation.setItemForValidation(getItemForValidation());
54 valid &= unitOfMeasureValidation.validate(event);
55
56 if (getItemForValidation().getItemType().isLineItemIndicator()) {
57 itemUnitPriceValidation.setItemForValidation(getItemForValidation());
58 valid &= itemUnitPriceValidation.validate(event);
59
60 itemDescriptionValidation.setItemForValidation(getItemForValidation());
61 valid &= itemDescriptionValidation.validate(event);
62
63 itemQuantityValidation.setItemForValidation(getItemForValidation());
64 valid &= itemQuantityValidation.validate(event);
65
66 valid &= validateCommodityCodes(getItemForValidation(), commodityCodeIsRequired());
67 } else {
68
69 belowTheLineItemNoUnitCostValidation.setItemForValidation(getItemForValidation());
70 valid &= belowTheLineItemNoUnitCostValidation.validate(event);
71 }
72 return valid;
73 }
74
75 protected boolean commodityCodeIsRequired() {
76 return false;
77 }
78
79
80
81
82
83
84
85 public boolean validateContainsAtLeastOneItem(PurchasingDocument purDocument) {
86 boolean valid = false;
87 for (PurApItem item : purDocument.getItems()) {
88 if (!((PurchasingItemBase) item).isEmpty() && item.getItemType().isLineItemIndicator()) {
89
90 return true;
91 }
92 }
93 String documentType = getDocumentTypeLabel(purDocument.getDocumentHeader().getWorkflowDocument().getDocumentTypeName());
94
95 if (!valid) {
96 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_REQUIRED, documentType);
97 }
98
99 return valid;
100 }
101
102
103
104
105
106
107
108
109
110 protected boolean validateCommodityCodes(PurApItem item, boolean commodityCodeRequired) {
111 boolean valid = true;
112 String identifierString = item.getItemIdentifierString();
113 PurchasingItemBase purItem = (PurchasingItemBase) item;
114
115
116 if (commodityCodeRequired && StringUtils.isBlank(purItem.getPurchasingCommodityCode())) {
117
118 valid = false;
119 String attributeLabel = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(CommodityCode.class.getName()).
120 getAttributeDefinition(PurapPropertyConstants.ITEM_COMMODITY_CODE).getLabel();
121 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + identifierString);
122 } else if (StringUtils.isNotBlank(purItem.getPurchasingCommodityCode())) {
123
124 Map<String, String> fieldValues = new HashMap<String, String>();
125 fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purItem.getPurchasingCommodityCode());
126 if (businessObjectService.countMatching(CommodityCode.class, fieldValues) != 1) {
127
128 valid = false;
129 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID, " in " + identifierString);
130 } else {
131 valid &= validateThatCommodityCodeIsActive(item);
132 }
133 }
134
135 return valid;
136 }
137
138 protected boolean validateThatCommodityCodeIsActive(PurApItem item) {
139 item.refreshReferenceObject(PurapPropertyConstants.COMMODITY_CODE);
140 if (!((PurchasingItemBase) item).getCommodityCode().isActive()) {
141
142 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in " + item.getItemIdentifierString());
143 return false;
144 }
145 return true;
146 }
147
148 public BusinessObjectService getBusinessObjectService() {
149 return businessObjectService;
150 }
151
152 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
153 this.businessObjectService = businessObjectService;
154 }
155
156 public CapitalAssetBuilderModuleService getCapitalAssetBuilderModuleService() {
157 return capitalAssetBuilderModuleService;
158 }
159
160 public void setCapitalAssetBuilderModuleService(CapitalAssetBuilderModuleService capitalAssetBuilderModuleService) {
161 this.capitalAssetBuilderModuleService = capitalAssetBuilderModuleService;
162 }
163
164 public PurchasingUnitOfMeasureValidation getUnitOfMeasureValidation() {
165 return unitOfMeasureValidation;
166 }
167
168 public void setUnitOfMeasureValidation(PurchasingUnitOfMeasureValidation unitOfMeasureValidation) {
169 this.unitOfMeasureValidation = unitOfMeasureValidation;
170 }
171
172 public PurchasingItemUnitPriceValidation getItemUnitPriceValidation() {
173 return itemUnitPriceValidation;
174 }
175
176 public void setItemUnitPriceValidation(PurchasingItemUnitPriceValidation itemUnitPriceValidation) {
177 this.itemUnitPriceValidation = itemUnitPriceValidation;
178 }
179
180 public PurchasingItemDescriptionValidation getItemDescriptionValidation() {
181 return itemDescriptionValidation;
182 }
183
184 public void setItemDescriptionValidation(PurchasingItemDescriptionValidation itemDescriptionValidation) {
185 this.itemDescriptionValidation = itemDescriptionValidation;
186 }
187
188 public PurchasingItemQuantityValidation getItemQuantityValidation() {
189 return itemQuantityValidation;
190 }
191
192 public void setItemQuantityValidation(PurchasingItemQuantityValidation itemQuantityValidation) {
193 this.itemQuantityValidation = itemQuantityValidation;
194 }
195
196 public PurchasingBelowTheLineItemNoUnitCostValidation getBelowTheLineItemNoUnitCostValidation() {
197 return belowTheLineItemNoUnitCostValidation;
198 }
199
200 public void setBelowTheLineItemNoUnitCostValidation(PurchasingBelowTheLineItemNoUnitCostValidation belowTheLineItemNoUnitCostValidation) {
201 this.belowTheLineItemNoUnitCostValidation = belowTheLineItemNoUnitCostValidation;
202 }
203
204
205 }