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.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.ojb.broker.metadata.ClassDescriptor;
20  import org.kuali.ole.integration.cab.CapitalAssetBuilderModuleService;
21  import org.kuali.ole.integration.purap.CapitalAssetLocation;
22  import org.kuali.ole.integration.purap.CapitalAssetSystem;
23  import org.kuali.ole.module.purap.PurapConstants;
24  import org.kuali.ole.module.purap.PurapKeyConstants;
25  import org.kuali.ole.module.purap.PurapParameterConstants;
26  import org.kuali.ole.module.purap.PurapPropertyConstants;
27  import org.kuali.ole.module.purap.businessobject.PurApItem;
28  import org.kuali.ole.module.purap.businessobject.PurchasingCapitalAssetItem;
29  import org.kuali.ole.module.purap.document.PurchasingDocument;
30  import org.kuali.ole.module.purap.document.service.PurchasingDocumentSpecificService;
31  import org.kuali.ole.module.purap.document.service.PurchasingService;
32  import org.kuali.ole.module.purap.service.PurapAccountingService;
33  import org.kuali.ole.sys.OLEPropertyConstants;
34  import org.kuali.ole.sys.businessobject.Room;
35  import org.kuali.ole.sys.context.SpringContext;
36  import org.kuali.ole.sys.service.PostalCodeValidationService;
37  import org.kuali.ole.sys.service.impl.OleParameterConstants;
38  import org.kuali.ole.vnd.businessobject.VendorDetail;
39  import org.kuali.rice.core.api.util.RiceKeyConstants;
40  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
41  import org.kuali.rice.kns.service.DataDictionaryService;
42  import org.kuali.rice.krad.service.BusinessObjectService;
43  import org.kuali.rice.krad.service.SequenceAccessorService;
44  import org.kuali.rice.krad.service.impl.PersistenceServiceStructureImplBase;
45  import org.kuali.rice.krad.util.GlobalVariables;
46  import org.kuali.rice.krad.util.ObjectUtils;
47  import org.springframework.transaction.annotation.Transactional;
48  
49  import java.util.ArrayList;
50  import java.util.HashMap;
51  import java.util.List;
52  import java.util.Map;
53  
54  @Transactional
55  public class PurchasingServiceImpl extends PersistenceServiceStructureImplBase implements PurchasingService {
56      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchasingServiceImpl.class);
57  
58      private ParameterService parameterService;
59      private SequenceAccessorService sequenceAccessorService;
60      private PurapAccountingService purapAccountingService;
61      private CapitalAssetBuilderModuleService capitalAssetBuilderModuleService;
62      private PostalCodeValidationService postalCodeValidationService;
63  
64      public void setPurapAccountingService(PurapAccountingService purapAccountingService) {
65          this.purapAccountingService = purapAccountingService;
66      }
67  
68      public void setParameterService(ParameterService parameterService) {
69          this.parameterService = parameterService;
70      }
71  
72      public void setSequenceAccessorService(SequenceAccessorService sequenceAccessorService) {
73          this.sequenceAccessorService = sequenceAccessorService;
74      }
75  
76      public void setCapitalAssetBuilderModuleService(CapitalAssetBuilderModuleService capitalAssetBuilderModuleService) {
77          this.capitalAssetBuilderModuleService = capitalAssetBuilderModuleService;
78      }
79  
80      public void setPostalCodeValidationService(PostalCodeValidationService postalCodeValidationService) {
81          this.postalCodeValidationService = postalCodeValidationService;
82      }
83  
84      @Override
85      public void setupCapitalAssetItems(PurchasingDocument purDoc) {
86  
87          List<PurchasingCapitalAssetItem> camsItemsList = purDoc.getPurchasingCapitalAssetItems();
88          List<PurchasingCapitalAssetItem> newCamsItemsList = new ArrayList();
89  
90          for (PurApItem purapItem : purDoc.getItems()) {
91              if (purapItem.getItemType().isLineItemIndicator() || PurapConstants.ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE.equals(purapItem.getItemTypeCode())) {
92                  if (capitalAssetBuilderModuleService.doesItemNeedCapitalAsset(purapItem.getItemTypeCode(), purapItem.getSourceAccountingLines())) {
93                      PurchasingCapitalAssetItem camsItem = getItemIfAlreadyInCamsItemsList(purapItem, camsItemsList);
94                      // If either the camsItem is null or if its system is null and the document's system type is IND (this is
95                      // the case when the user tries to switch from ONE system type to IND system type), we'll have to create
96                      // the camsItem again along with its system to prevent the No collection found error.
97                      if (ObjectUtils.isNull(camsItem) || (purDoc.getCapitalAssetSystemTypeCode() != null && purDoc.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetSystemTypes.INDIVIDUAL) && ObjectUtils.isNull(camsItem.getPurchasingCapitalAssetSystem()))) {
98                          PurchasingCapitalAssetItem newCamsItem = createCamsItem(purDoc, purapItem);
99                          newCamsItemsList.add(newCamsItem);
100                     } else {
101                         camsItem.setPurchasingDocument(purDoc);
102                         newCamsItemsList.add(camsItem);
103                     }
104                 } else {
105                     PurchasingCapitalAssetItem camsItem = getItemIfAlreadyInCamsItemsList(purapItem, camsItemsList);
106                     if (camsItem != null && camsItem.isEmpty()) {
107                         camsItemsList.remove(camsItem);
108                     }
109                 }
110             }
111         }
112 
113         purDoc.setPurchasingCapitalAssetItems(newCamsItemsList);
114 
115         if (purDoc.getPurchasingCapitalAssetItems().isEmpty()) {
116             purDoc.setCapitalAssetSystemStateCode(null);
117             purDoc.setCapitalAssetSystemTypeCode(null);
118         }
119     }
120 
121     protected PurchasingCapitalAssetItem createCamsItem(PurchasingDocument purDoc, PurApItem purapItem) {
122         PurchasingDocumentSpecificService purchasingDocumentSpecificService = purDoc.getDocumentSpecificService();
123         if (purapItem.getItemIdentifier() == null) {
124             ClassDescriptor cd = this.getClassDescriptor(purapItem.getClass());
125             String sequenceName = cd.getFieldDescriptorByName(PurapPropertyConstants.ITEM_IDENTIFIER).getSequenceName();
126 
127             Integer itemIdentifier = sequenceAccessorService.getNextAvailableSequenceNumber(sequenceName, PurApItem.class).intValue();
128             purapItem.setItemIdentifier(itemIdentifier);
129         }
130         PurchasingCapitalAssetItem camsItem = purchasingDocumentSpecificService.createCamsItem(purDoc, purapItem);
131         return camsItem;
132     }
133 
134 
135     protected PurchasingCapitalAssetItem getItemIfAlreadyInCamsItemsList(PurApItem item, List<PurchasingCapitalAssetItem> camsItemsList) {
136         for (PurchasingCapitalAssetItem camsItem : camsItemsList) {
137             if (camsItem.getItemIdentifier() != null && camsItem.getItemIdentifier().equals(item.getItemIdentifier())) {
138                 return camsItem;
139             }
140         }
141 
142         return null;
143     }
144 
145 
146     @Override
147     public void deleteCapitalAssetItems(PurchasingDocument purDoc, Integer itemIdentifier) {
148         // delete the corresponding CAMS items.
149         int index = 0;
150         for (PurchasingCapitalAssetItem camsItem : purDoc.getPurchasingCapitalAssetItems()) {
151             if (camsItem.getItemIdentifier().equals(itemIdentifier)) {
152                 break;
153             }
154             index++;
155         }
156         purDoc.getPurchasingCapitalAssetItems().remove(index);
157     }
158 
159     @Override
160     public void setupCapitalAssetSystem(PurchasingDocument purDoc) {
161         CapitalAssetSystem resultSystem = purDoc.getDocumentSpecificService().createCapitalAssetSystem();
162         if (purDoc.getCapitalAssetSystemTypeCode() != null && (purDoc.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetTabStrings.ONE_SYSTEM) || purDoc.getCapitalAssetSystemTypeCode().equals(PurapConstants.CapitalAssetTabStrings.MULTIPLE_SYSTEMS))) {
163             if (purDoc.getPurchasingCapitalAssetSystems().size() == 0) {
164                 purDoc.getPurchasingCapitalAssetSystems().add(resultSystem);
165             }
166         }
167     }
168 
169     @Override
170     public boolean getDefaultUseTaxIndicatorValue(PurchasingDocument purDoc) {
171 
172         purDoc.refreshReferenceObject("vendorDetail");
173         VendorDetail vendor = purDoc.getVendorDetail();
174         if (vendor != null) {
175             String vendorStateCode = vendor.getDefaultAddressStateCode();
176             String billingStateCode = purDoc.getBillingStateCode();
177             if (StringUtils.equals(vendorStateCode, billingStateCode) || (vendor.isTaxableIndicator())) {
178                 return false;
179             }
180         } else {
181             // don't set use tax if no vendor on req
182             return true;
183         }
184 
185         return true;
186     }
187 
188     @Override
189     public String getDefaultAssetTypeCodeNotThisFiscalYear() {
190         //FIXME (hjs) is this breaking modularization??
191         return parameterService.getParameterValueAsString(OleParameterConstants.CAPITAL_ASSET_BUILDER_DOCUMENT.class, PurapParameterConstants.CapitalAsset.PURCHASING_DEFAULT_ASSET_TYPE_WHEN_NOT_THIS_FISCAL_YEAR);
192     }
193 
194     @Override
195     public boolean checkCapitalAssetLocation(CapitalAssetLocation location) {
196         // if any of the date fields have a value AND one of them does not have a value...
197         if (ObjectUtils.isNotNull(location) &&
198                 (StringUtils.isEmpty(location.getCapitalAssetLine1Address()) ||
199                         StringUtils.isEmpty(location.getCapitalAssetCityName()) ||
200                         StringUtils.isEmpty(location.getCapitalAssetCountryCode()))) {
201             String missingFields = "";
202             if (StringUtils.isEmpty(location.getCapitalAssetLine1Address())) {
203                 missingFields = "Address";
204                 addErrorToCapitalAssetLocation(PurapPropertyConstants.CAPITAL_ASSET_LOCATION_ADDRESS_LINE1, missingFields);
205             }
206             if (StringUtils.isEmpty(location.getCapitalAssetCityName())) {
207                 missingFields = "City";
208                 addErrorToCapitalAssetLocation(PurapPropertyConstants.CAPITAL_ASSET_LOCATION_CITY, missingFields);
209             }
210             if (StringUtils.isEmpty(location.getCapitalAssetCountryCode())) {
211                 missingFields = "Country";
212                 addErrorToCapitalAssetLocation(PurapPropertyConstants.CAPITAL_ASSET_LOCATION_COUNTRY, missingFields);
213             }
214             return false;
215         }
216         return true;
217     }
218 
219 
220     @Override
221     public boolean checkValidRoomNumber(CapitalAssetLocation location) {
222         boolean valid = true;
223         if (StringUtils.isNotBlank(location.getBuildingCode()) && StringUtils.isNotBlank(location.getBuildingRoomNumber())) {
224             Map objectKeys = new HashMap();
225             objectKeys.put(OLEPropertyConstants.CAMPUS_CODE, location.getCampusCode());
226             objectKeys.put(OLEPropertyConstants.BUILDING_CODE, location.getBuildingCode());
227             objectKeys.put(OLEPropertyConstants.BUILDING_ROOM_NUMBER, location.getBuildingRoomNumber());
228             Room room = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(Room.class, objectKeys);
229 
230             if (ObjectUtils.isNull(room)) {
231                 GlobalVariables.getMessageMap().addToErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
232                 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CAPITAL_ASSET_LOCATION_ROOM, PurapKeyConstants.ERROR_INVALID_ROOM_NUMBER, location.getBuildingCode(), location.getBuildingRoomNumber(), location.getCampusCode());
233                 GlobalVariables.getMessageMap().removeFromErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
234                 valid &= false;
235             } else if (!room.isActive()) {
236                 String label = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(Room.class.getName()).getAttributeDefinition(OLEPropertyConstants.BUILDING_ROOM_NUMBER).getLabel();
237                 //GlobalVariables.getMessageMap().putError(PurapConstants.CAPITAL_ASSET_TAB_ERRORS, RiceKeyConstants.ERROR_INACTIVE, label);
238                 GlobalVariables.getMessageMap().addToErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
239                 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CAPITAL_ASSET_LOCATION_ROOM, RiceKeyConstants.ERROR_INACTIVE, label);
240                 GlobalVariables.getMessageMap().removeFromErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
241                 valid &= false;
242             }
243         } else if (StringUtils.isBlank(location.getBuildingCode()) && StringUtils.isNotBlank(location.getBuildingRoomNumber())) {
244             Map objectKeys = new HashMap();
245             objectKeys.put(OLEPropertyConstants.CAMPUS_CODE, location.getCampusCode());
246             objectKeys.put(OLEPropertyConstants.BUILDING_ROOM_NUMBER, location.getBuildingRoomNumber());
247             Room room = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(Room.class, objectKeys);
248 
249             if (ObjectUtils.isNull(room)) {
250                 GlobalVariables.getMessageMap().addToErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
251                 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CAPITAL_ASSET_LOCATION_ROOM, PurapKeyConstants.ERROR_INVALID_ROOM_NUMBER_FOR_CAMPUS, location.getBuildingRoomNumber(), location.getCampusCode());
252                 GlobalVariables.getMessageMap().removeFromErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
253                 valid &= false;
254             } else if (!room.isActive()) {
255                 String label = getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(Room.class.getName()).getAttributeDefinition(OLEPropertyConstants.BUILDING_ROOM_NUMBER).getLabel();
256                 GlobalVariables.getMessageMap().addToErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
257                 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CAPITAL_ASSET_LOCATION_ROOM, RiceKeyConstants.ERROR_INACTIVE, label);
258                 GlobalVariables.getMessageMap().removeFromErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
259                 //GlobalVariables.getMessageMap().putError(PurapConstants.CAPITAL_ASSET_TAB_ERRORS, RiceKeyConstants.ERROR_INACTIVE, label);
260                 valid &= false;
261             }
262         }
263         return valid;
264     }
265 
266     /**
267      * Gets the dataDictionaryService attribute.
268      *
269      * @return an implementation of the dataDictionaryService.
270      */
271     public DataDictionaryService getDataDictionaryService() {
272         return SpringContext.getBean(DataDictionaryService.class);
273     }
274 
275     public void addErrorToCapitalAssetLocation(String path, String field) {
276 
277         GlobalVariables.getMessageMap().addToErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
278         GlobalVariables.getMessageMap().putError(path, PurapKeyConstants.ERROR_CAPITAL_ASSET_INCOMPLETE_ADDRESS, field);
279         GlobalVariables.getMessageMap().removeFromErrorPath(PurapPropertyConstants.NEW_PURCHASING_CAPITAL_ASSET_LOCATION_LINE);
280     }
281 
282 
283 }