View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.tem.businessobject.options;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.kfs.module.tem.TemConstants;
30  import org.kuali.kfs.module.tem.TemPropertyConstants;
31  import org.kuali.kfs.module.tem.businessobject.TemRegion;
32  import org.kuali.kfs.module.tem.service.TravelService;
33  import org.kuali.kfs.sys.KFSConstants;
34  import org.kuali.kfs.sys.context.SpringContext;
35  import org.kuali.rice.core.api.util.ConcreteKeyValue;
36  import org.kuali.rice.core.api.util.KeyValue;
37  import org.kuali.rice.krad.keyvalues.KeyValuesBase;
38  import org.kuali.rice.krad.service.KeyValuesService;
39  
40  public class TemRegionCodeValuesFinder extends KeyValuesBase {
41      protected static volatile KeyValuesService keyValuesService;
42  
43      /**
44       * @see org.kuali.rice.krad.keyvalues.KeyValuesFinder#getKeyValues()
45       */
46      @SuppressWarnings("rawtypes")
47      @Override
48      public List<KeyValue> getKeyValues() {
49  
50          String tripTypeCode = KFSConstants.EMPTY_STRING;
51  
52  
53  
54          List<KeyValue> keyValues = new ArrayList<KeyValue>();
55          keyValues.add(new ConcreteKeyValue(KFSConstants.EMPTY_STRING, KFSConstants.EMPTY_STRING));
56          TravelService travelService = SpringContext.getBean(TravelService.class);
57  
58          List<TemRegion> usRegions = new ArrayList<TemRegion>();
59  
60  
61          Map<String, Object> fieldValues = new HashMap<String, Object>();
62          if (StringUtils.isEmpty(tripTypeCode) || tripTypeCode.equals(TemConstants.TemTripTypes.IN_STATE)) {
63              fieldValues.put(TemPropertyConstants.TRIP_TYPE_CODE, TemConstants.TemTripTypes.IN_STATE);
64              List<TemRegion> inRegions = (List<TemRegion>) getKeyValuesService().findMatching(TemRegion.class, fieldValues);
65              usRegions.addAll(inRegions);
66          }
67  
68          if (StringUtils.isEmpty(tripTypeCode) || tripTypeCode.equals(TemConstants.TemTripTypes.OUT_OF_STATE)) {
69              fieldValues.put(TemPropertyConstants.TRIP_TYPE_CODE, TemConstants.TemTripTypes.OUT_OF_STATE);
70              List<TemRegion> outRegions = (List<TemRegion>) getKeyValuesService().findMatching(TemRegion.class, fieldValues);
71              Collections.sort(outRegions);
72              usRegions.addAll(outRegions);
73  
74          }
75  
76  
77  
78          Iterator<TemRegion> it = usRegions.iterator();
79  
80          String key = KFSConstants.EMPTY_STRING;
81          while (it.hasNext()){
82              TemRegion temRegion = it.next();
83  
84              String tempKey = temRegion.getRegionName();
85              if (!tempKey.equals(key)){
86                  keyValues.add(new ConcreteKeyValue(temRegion.getRegionCode().toUpperCase(), temRegion.getRegionCode().toUpperCase()));
87              }
88              key = tempKey;
89          }
90  
91  
92          if (StringUtils.isEmpty(tripTypeCode) || tripTypeCode.equals(TemConstants.TemTripTypes.INTERNATIONAL)) {
93  
94              fieldValues.put(TemPropertyConstants.TRIP_TYPE_CODE, TemConstants.TemTripTypes.INTERNATIONAL);
95              List<TemRegion> intRegions = (List<TemRegion>)getKeyValuesService().findMatching(TemRegion.class, fieldValues);
96              Collections.sort(intRegions);
97  
98  
99              keyValues.add(new ConcreteKeyValue("---", "---"));
100             it = intRegions.iterator();
101             while (it.hasNext()) {
102                 TemRegion temRegion = it.next();
103                 String tempKey = temRegion.getRegionName();
104                 if (!tempKey.equals(key)) {
105                     keyValues.add(new ConcreteKeyValue(temRegion.getRegionCode().toUpperCase(), temRegion.getRegionCode().toUpperCase()));
106                 }
107                 key = tempKey;
108             }
109         }
110 
111         return keyValues;
112     }
113 
114     protected KeyValuesService getKeyValuesService() {
115         if (keyValuesService == null) {
116             keyValuesService = SpringContext.getBean(KeyValuesService.class);
117         }
118         return keyValuesService;
119     }
120 }