001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.module.purap.document.validation.impl;
017
018 import org.apache.commons.lang.StringUtils;
019 import org.kuali.kfs.integration.cab.CapitalAssetBuilderModuleService;
020 import org.kuali.kfs.integration.purap.CapitalAssetLocation;
021 import org.kuali.kfs.integration.purap.CapitalAssetSystem;
022 import org.kuali.kfs.module.purap.PurapConstants;
023 import org.kuali.kfs.module.purap.PurapKeyConstants;
024 import org.kuali.kfs.module.purap.PurapPropertyConstants;
025 import org.kuali.kfs.module.purap.businessobject.PurchasingCapitalAssetItem;
026 import org.kuali.kfs.module.purap.businessobject.RequisitionCapitalAssetSystem;
027 import org.kuali.kfs.module.purap.document.PurchasingDocument;
028 import org.kuali.kfs.module.purap.document.RequisitionDocument;
029 import org.kuali.kfs.module.purap.document.service.PurchasingService;
030 import org.kuali.kfs.sys.context.SpringContext;
031 import org.kuali.kfs.sys.document.validation.GenericValidation;
032 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
033 import org.kuali.rice.kns.service.DataDictionaryService;
034 import org.kuali.rice.kns.util.GlobalVariables;
035 import org.kuali.rice.kns.util.ObjectUtils;
036 import org.kuali.rice.kns.util.RiceKeyConstants;
037
038 public class PurchasingCapitalAssetValidation extends GenericValidation {
039 CapitalAssetBuilderModuleService capitalAssetBuilderModuleService;
040 PurchasingService purchasingService;
041 protected static String ERROR_PATH_PREFIX_FOR_IND_SYSTEM = "document.purchasingCapitalAssetItems[";
042 protected static String ERROR_PATH_SUFFIX_FOR_IND_SYSTEM = "].purchasingCapitalAssetSystem";
043 protected static String ERROR_PATH_PREFIX_FOR_ONE_SYSTEM = "document.purchasingCapitalAssetSystems[0]";
044
045 public boolean validate(AttributedDocumentEvent event) {
046 GlobalVariables.getMessageMap().clearErrorPath();
047 boolean valid = true;
048 PurchasingDocument purchasingDocument = (PurchasingDocument) event.getDocument();
049
050 boolean requiredByObjectSubType = !capitalAssetBuilderModuleService.validatePurchasingObjectSubType(purchasingDocument);
051 boolean requiredByChart = !capitalAssetBuilderModuleService.validateAllFieldRequirementsByChart(purchasingDocument);
052 boolean capitalAssetRequired = requiredByObjectSubType && requiredByChart;
053
054 if (capitalAssetRequired) {
055 // if capital asset required, check to see if the capital asset data are setup
056 String typeCode = purchasingDocument.getCapitalAssetSystemTypeCode();
057 if (StringUtils.isBlank(typeCode) || StringUtils.isBlank(purchasingDocument.getCapitalAssetSystemStateCode()) ||
058 purchasingDocument.getPurchasingCapitalAssetSystems() == null || purchasingDocument.getPurchasingCapitalAssetItems() == null){
059 valid = false;
060 }
061 else if ((typeCode.equals(PurapConstants.CapitalAssetTabStrings.ONE_SYSTEM) || typeCode.equals(PurapConstants.CapitalAssetTabStrings.MULTIPLE_SYSTEMS)) &&
062 purchasingDocument.getPurchasingCapitalAssetSystems().size() == 0) {
063 valid = false;
064 }
065 /* TODO
066 * either complete the following with checking that capital asset items are correctly setup, or replace this whole part (and above)
067 * with checking on a flag that indicates whether select/update capital asset has been done since last item changes
068 */
069 else if (purchasingDocument.getPurchasingCapitalAssetItems().isEmpty()) {
070 valid = false;
071 }
072
073 if (!valid) {
074 GlobalVariables.getMessageMap().putError("newPurchasingItemCapitalAssetLine", PurapKeyConstants.ERROR_CAPITAL_ASSET_REQD_FOR_PUR_OBJ_SUB_TYPE);
075 return valid;
076 }
077 }
078 else {
079 // if capital asset not required, reset system type and state code in case they are filled in
080 // if capital asset items are empty, then set sytem type code and system state code to null
081 // fix to jira KFSMI-5146
082 if (purchasingDocument.getPurchasingCapitalAssetItems().isEmpty()) {
083 purchasingDocument.setCapitalAssetSystemTypeCode(null);
084 purchasingDocument.setCapitalAssetSystemStateCode(null);
085 }
086 }
087
088 // We only need to do capital asset validations if the capital asset system type is not blank.
089 if (StringUtils.isNotBlank(purchasingDocument.getCapitalAssetSystemTypeCode())) {
090 valid &= capitalAssetBuilderModuleService.validatePurchasingData(purchasingDocument);
091
092 // FIXME hjs move this to cab module service
093 // validate complete location addresses
094 if (purchasingDocument.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.INDIVIDUAL)) {
095 for (CapitalAssetSystem system : purchasingDocument.getPurchasingCapitalAssetSystems()) {
096 for (CapitalAssetLocation location : system.getCapitalAssetLocations()) {
097 valid &= purchasingService.checkCapitalAssetLocation(location);
098 }
099 }
100 }
101 else if (purchasingDocument.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.ONE_SYSTEM)) {
102 CapitalAssetSystem system = purchasingDocument.getPurchasingCapitalAssetSystems().get(0);
103 for (CapitalAssetLocation location : system.getCapitalAssetLocations()) {
104 valid &= purchasingService.checkCapitalAssetLocation(location);
105 }
106 }
107
108 // Validate asset type code if entered by user.
109 valid &= validateAssetTypeExistence(purchasingDocument);
110 }
111
112
113 return valid;
114 }
115
116 /**
117 * Validate user input asset type code.
118 *
119 * @param purchasingDocument
120 * @return
121 */
122 protected boolean validateAssetTypeExistence(PurchasingDocument purchasingDocument) {
123 boolean valid = true;
124 // validate for Individual system
125 if (purchasingDocument.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.INDIVIDUAL)) {
126 int i = 0;
127 for (PurchasingCapitalAssetItem capitalAssetItem : purchasingDocument.getPurchasingCapitalAssetItems()) {
128 if (ObjectUtils.isNotNull(capitalAssetItem) && ObjectUtils.isNotNull(capitalAssetItem.getPurchasingCapitalAssetSystem())) {
129 String assetTypeCode = capitalAssetItem.getPurchasingCapitalAssetSystem().getCapitalAssetTypeCode();
130 if (StringUtils.isNotBlank(assetTypeCode) && !capitalAssetBuilderModuleService.isAssetTypeExisting(assetTypeCode)) {
131 valid = false;
132 String errorPath = ERROR_PATH_PREFIX_FOR_IND_SYSTEM + new Integer(i).toString() + ERROR_PATH_SUFFIX_FOR_IND_SYSTEM;
133 addAssetTypeErrorWithFullErrorPath(errorPath);
134 }
135 }
136 i++;
137 }
138 }
139 else if (purchasingDocument.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.ONE_SYSTEM)) {
140 // validate for One system
141 if (ObjectUtils.isNotNull(purchasingDocument.getPurchasingCapitalAssetSystems())) {
142 CapitalAssetSystem system = purchasingDocument.getPurchasingCapitalAssetSystems().get(0);
143 if (ObjectUtils.isNotNull(system) && StringUtils.isNotBlank(system.getCapitalAssetTypeCode()) && !capitalAssetBuilderModuleService.isAssetTypeExisting(system.getCapitalAssetTypeCode())) {
144 valid = false;
145 String errorPath = ERROR_PATH_PREFIX_FOR_ONE_SYSTEM;
146 addAssetTypeErrorWithFullErrorPath(errorPath);
147 }
148 }
149 }
150 // Validate for Multiple system is ignored since currently it's not supported to enter.
151 return valid;
152 }
153
154 /**
155 * Add asset type error to the global message map.
156 *
157 * @param errorPath
158 */
159 protected void addAssetTypeErrorWithFullErrorPath(String errorPath) {
160 GlobalVariables.getMessageMap().addToErrorPath(errorPath);
161 String label = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(RequisitionCapitalAssetSystem.class.getName()).getAttributeDefinition(PurapPropertyConstants.CAPITAL_ASSET_TYPE_CODE).getLabel();
162 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CAPITAL_ASSET_TYPE_CODE, RiceKeyConstants.ERROR_EXISTENCE, label);
163 GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
164 }
165
166 public CapitalAssetBuilderModuleService getCapitalAssetBuilderModuleService() {
167 return capitalAssetBuilderModuleService;
168 }
169
170 public void setCapitalAssetBuilderModuleService(CapitalAssetBuilderModuleService capitalAssetBuilderModuleService) {
171 this.capitalAssetBuilderModuleService = capitalAssetBuilderModuleService;
172 }
173
174 public PurchasingService getPurchasingService() {
175 return purchasingService;
176 }
177
178 public void setPurchasingService(PurchasingService purchasingService) {
179 this.purchasingService = purchasingService;
180 }
181
182 }