View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.integration.purap.CapitalAssetLocation;
21  import org.kuali.ole.integration.purap.CapitalAssetSystem;
22  import org.kuali.ole.module.purap.PurapConstants;
23  import org.kuali.ole.module.purap.PurapKeyConstants;
24  import org.kuali.ole.module.purap.PurapPropertyConstants;
25  import org.kuali.ole.module.purap.businessobject.PurchasingCapitalAssetItem;
26  import org.kuali.ole.module.purap.businessobject.RequisitionCapitalAssetSystem;
27  import org.kuali.ole.module.purap.document.PurchasingDocument;
28  import org.kuali.ole.module.purap.document.service.PurchasingService;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.document.validation.GenericValidation;
31  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
32  import org.kuali.rice.core.api.util.RiceKeyConstants;
33  import org.kuali.rice.kns.service.DataDictionaryService;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  
37  public class PurchasingCapitalAssetValidation extends GenericValidation {
38      CapitalAssetBuilderModuleService capitalAssetBuilderModuleService;
39      PurchasingService purchasingService;
40      protected static String ERROR_PATH_PREFIX_FOR_IND_SYSTEM = "document.purchasingCapitalAssetItems[";
41      protected static String ERROR_PATH_SUFFIX_FOR_IND_SYSTEM = "].purchasingCapitalAssetSystem";
42      protected static String ERROR_PATH_PREFIX_FOR_ONE_SYSTEM = "document.purchasingCapitalAssetSystems[0]";
43  
44      public boolean validate(AttributedDocumentEvent event) {
45          GlobalVariables.getMessageMap().clearErrorPath();
46          boolean valid = true;
47          PurchasingDocument purchasingDocument = (PurchasingDocument) event.getDocument();
48  
49          boolean requiredByObjectSubType = !capitalAssetBuilderModuleService.validatePurchasingObjectSubType(purchasingDocument);
50          boolean requiredByChart = !capitalAssetBuilderModuleService.validateAllFieldRequirementsByChart(purchasingDocument);
51          boolean capitalAssetRequired = requiredByObjectSubType && requiredByChart;
52  
53          if (capitalAssetRequired) {
54              // if capital asset required, check to see if the capital asset data are setup 
55              String typeCode = purchasingDocument.getCapitalAssetSystemTypeCode();
56              if (StringUtils.isBlank(typeCode) || StringUtils.isBlank(purchasingDocument.getCapitalAssetSystemStateCode()) ||
57                      purchasingDocument.getPurchasingCapitalAssetSystems() == null || purchasingDocument.getPurchasingCapitalAssetItems() == null) {
58                  valid = false;
59              } else if ((typeCode.equals(PurapConstants.CapitalAssetTabStrings.ONE_SYSTEM) || typeCode.equals(PurapConstants.CapitalAssetTabStrings.MULTIPLE_SYSTEMS)) &&
60                      purchasingDocument.getPurchasingCapitalAssetSystems().size() == 0) {
61                  valid = false;
62              }
63              /* TODO
64               * either complete the following with checking that capital asset items are correctly setup, or replace this whole part (and above)
65               * with checking on a flag that indicates whether select/update capital asset has been done since last item changes 
66               */
67              else if (purchasingDocument.getPurchasingCapitalAssetItems().isEmpty()) {
68                  valid = false;
69              }
70  
71              if (!valid) {
72                  GlobalVariables.getMessageMap().putError("newPurchasingItemCapitalAssetLine", PurapKeyConstants.ERROR_CAPITAL_ASSET_REQD_FOR_PUR_OBJ_SUB_TYPE);
73                  return valid;
74              }
75          } else {
76              // if capital asset not required, reset system type and state code in case they are filled in
77              // if capital asset items are empty, then set sytem type code and system state code to null
78              // fix to jira KFSMI-5146
79              if (purchasingDocument.getPurchasingCapitalAssetItems().isEmpty()) {
80                  purchasingDocument.setCapitalAssetSystemTypeCode(null);
81                  purchasingDocument.setCapitalAssetSystemStateCode(null);
82              }
83          }
84  
85          // We only need to do capital asset validations if the capital asset system type is not blank.
86          if (StringUtils.isNotBlank(purchasingDocument.getCapitalAssetSystemTypeCode())) {
87              valid &= capitalAssetBuilderModuleService.validatePurchasingData(purchasingDocument);
88  
89              // FIXME hjs move this to cab module service
90              // validate complete location addresses
91              if (purchasingDocument.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.INDIVIDUAL)) {
92                  for (CapitalAssetSystem system : purchasingDocument.getPurchasingCapitalAssetSystems()) {
93                      for (CapitalAssetLocation location : system.getCapitalAssetLocations()) {
94                          valid &= purchasingService.checkCapitalAssetLocation(location);
95                      }
96                  }
97              } else if (purchasingDocument.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.ONE_SYSTEM)) {
98                  CapitalAssetSystem system = purchasingDocument.getPurchasingCapitalAssetSystems().get(0);
99                  for (CapitalAssetLocation location : system.getCapitalAssetLocations()) {
100                     valid &= purchasingService.checkCapitalAssetLocation(location);
101                 }
102             }
103 
104             // Validate asset type code if entered by user.
105             valid &= validateAssetTypeExistence(purchasingDocument);
106         }
107 
108 
109         return valid;
110     }
111 
112     /**
113      * Validate user input asset type code.
114      *
115      * @param purchasingDocument
116      * @return
117      */
118     protected boolean validateAssetTypeExistence(PurchasingDocument purchasingDocument) {
119         boolean valid = true;
120         // validate for Individual system
121         if (purchasingDocument.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.INDIVIDUAL)) {
122             int i = 0;
123             for (PurchasingCapitalAssetItem capitalAssetItem : purchasingDocument.getPurchasingCapitalAssetItems()) {
124                 if (ObjectUtils.isNotNull(capitalAssetItem) && ObjectUtils.isNotNull(capitalAssetItem.getPurchasingCapitalAssetSystem())) {
125                     String assetTypeCode = capitalAssetItem.getPurchasingCapitalAssetSystem().getCapitalAssetTypeCode();
126                     if (StringUtils.isNotBlank(assetTypeCode) && !capitalAssetBuilderModuleService.isAssetTypeExisting(assetTypeCode)) {
127                         valid = false;
128                         String errorPath = ERROR_PATH_PREFIX_FOR_IND_SYSTEM + new Integer(i).toString() + ERROR_PATH_SUFFIX_FOR_IND_SYSTEM;
129                         addAssetTypeErrorWithFullErrorPath(errorPath);
130                     }
131                 }
132                 i++;
133             }
134         } else if (purchasingDocument.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.ONE_SYSTEM)) {
135             // validate for One system
136             if (ObjectUtils.isNotNull(purchasingDocument.getPurchasingCapitalAssetSystems())) {
137                 CapitalAssetSystem system = purchasingDocument.getPurchasingCapitalAssetSystems().get(0);
138                 if (ObjectUtils.isNotNull(system) && StringUtils.isNotBlank(system.getCapitalAssetTypeCode()) && !capitalAssetBuilderModuleService.isAssetTypeExisting(system.getCapitalAssetTypeCode())) {
139                     valid = false;
140                     String errorPath = ERROR_PATH_PREFIX_FOR_ONE_SYSTEM;
141                     addAssetTypeErrorWithFullErrorPath(errorPath);
142                 }
143             }
144         }
145         // Validate for Multiple system is ignored since currently it's not supported to enter. 
146         return valid;
147     }
148 
149     /**
150      * Add asset type error to the global message map.
151      *
152      * @param errorPath
153      */
154     protected void addAssetTypeErrorWithFullErrorPath(String errorPath) {
155         GlobalVariables.getMessageMap().addToErrorPath(errorPath);
156         String label = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(RequisitionCapitalAssetSystem.class.getName()).getAttributeDefinition(PurapPropertyConstants.CAPITAL_ASSET_TYPE_CODE).getLabel();
157         GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CAPITAL_ASSET_TYPE_CODE, RiceKeyConstants.ERROR_EXISTENCE, label);
158         GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
159     }
160 
161     public CapitalAssetBuilderModuleService getCapitalAssetBuilderModuleService() {
162         return capitalAssetBuilderModuleService;
163     }
164 
165     public void setCapitalAssetBuilderModuleService(CapitalAssetBuilderModuleService capitalAssetBuilderModuleService) {
166         this.capitalAssetBuilderModuleService = capitalAssetBuilderModuleService;
167     }
168 
169     public PurchasingService getPurchasingService() {
170         return purchasingService;
171     }
172 
173     public void setPurchasingService(PurchasingService purchasingService) {
174         this.purchasingService = purchasingService;
175     }
176 
177 }