1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.service.impl;
17
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.kuali.rice.kns.bo.State;
25 import org.kuali.rice.kns.service.CountryService;
26 import org.kuali.rice.kns.service.KualiModuleService;
27 import org.kuali.rice.kns.service.StateService;
28 import org.kuali.rice.kns.util.KNSPropertyConstants;
29
30 public class StateServiceImpl implements StateService {
31 private static Logger LOG = Logger.getLogger(StateServiceImpl.class);
32
33 private KualiModuleService kualiModuleService;
34 private CountryService countryService;
35
36
37
38
39 public State getByPrimaryId(String postalStateCode) {
40 String postalCountryCode = countryService.getDefaultCountry().getPostalCountryCode();
41 return this.getByPrimaryId(postalCountryCode, postalStateCode);
42 }
43
44
45
46
47 public State getByPrimaryId(String postalCountryCode, String postalStateCode) {
48 if (StringUtils.isBlank(postalCountryCode) || StringUtils.isBlank(postalStateCode)) {
49 LOG.debug("neither postalCountryCode nor postalStateCode can be empty String.");
50 return null;
51 }
52
53 Map<String, Object> postalStateMap = new HashMap<String, Object>();
54 postalStateMap.put(KNSPropertyConstants.POSTAL_COUNTRY_CODE, postalCountryCode);
55 postalStateMap.put(KNSPropertyConstants.POSTAL_STATE_CODE, postalStateCode);
56
57 return kualiModuleService.getResponsibleModuleService(State.class).getExternalizableBusinessObject(State.class, postalStateMap);
58 }
59
60 public State getByPrimaryIdIfNecessary(String postalStateCode, State existingState) {
61 String postalCountryCode = countryService.getDefaultCountry().getPostalCountryCode();
62
63 return this.getByPrimaryIdIfNecessary(postalCountryCode, postalStateCode, existingState);
64 }
65
66 public State getByPrimaryIdIfNecessary(String postalCountryCode, String postalStateCode, State existingState) {
67 if (existingState != null) {
68 String existingCountryCode = existingState.getPostalCountryCode();
69 String existingPostalStateCode = existingState.getPostalStateCode();
70 if (StringUtils.equals(postalCountryCode, existingCountryCode) && StringUtils.equals(postalStateCode, existingPostalStateCode)) {
71 return existingState;
72 }
73 }
74
75 return this.getByPrimaryId(postalCountryCode, postalStateCode);
76 }
77
78
79
80
81 public List<State> findAllStates() {
82 String postalCountryCode = countryService.getDefaultCountry().getPostalCountryCode();
83 return this.findAllStates(postalCountryCode);
84 }
85
86
87
88
89 public List<State> findAllStates(String postalCountryCode) {
90 if (StringUtils.isBlank(postalCountryCode)) {
91 throw new IllegalArgumentException("The postalCountryCode cannot be empty String.");
92 }
93
94 Map<String, Object> postalStateMap = new HashMap<String, Object>();
95 postalStateMap.put(KNSPropertyConstants.POSTAL_COUNTRY_CODE, postalCountryCode);
96
97 return kualiModuleService.getResponsibleModuleService(State.class).getExternalizableBusinessObjectsList(State.class, postalStateMap);
98 }
99
100
101
102
103
104
105 public void setKualiModuleService(KualiModuleService kualiModuleService) {
106 this.kualiModuleService = kualiModuleService;
107 }
108
109
110
111
112
113
114 public void setCountryService(CountryService countryService) {
115 this.countryService = countryService;
116 }
117 }