1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.demo.travel.fiscalofficer;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.kuali.rice.krad.demo.travel.account.TravelAccount;
26 import org.kuali.rice.krad.demo.travel.account.TravelAccountInfo;
27 import org.kuali.rice.krad.demo.travel.account.TravelAccountService;
28 import org.kuali.rice.krad.service.BusinessObjectService;
29 import org.kuali.rice.krad.service.KRADServiceLocator;
30 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
31 import org.kuali.rice.krad.service.LookupService;
32 import org.springframework.beans.BeanUtils;
33
34
35
36
37 public class FiscalOfficerTravelAccountServiceImpl implements FiscalOfficerService, TravelAccountService {
38
39 protected static String[] FOI_SKIP = { "accounts" };
40
41 protected BusinessObjectService businessObjectService;
42 protected LookupService lookupService;
43
44 public FiscalOfficerInfo createFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
45 getBusinessObjectService().save(toFiscalOfficer(fiscalOfficerInfo));
46
47 Map<String, Object> criteria = new HashMap<String, Object>();
48 criteria.put("id", fiscalOfficerInfo.getId());
49 FiscalOfficer fiscalOfficer = getBusinessObjectService().findByPrimaryKey(FiscalOfficer.class, criteria);
50 return toFiscalOfficerInfo(fiscalOfficer);
51 }
52
53 public List<FiscalOfficerInfo> lookupFiscalOfficer(Map<String, String> criteria) {
54 @SuppressWarnings("unchecked")
55 Collection<FiscalOfficer> temp = getLookupService().findCollectionBySearch(FiscalOfficer.class, criteria);
56
57 List<FiscalOfficerInfo> results = new ArrayList<FiscalOfficerInfo>();
58 if(temp != null) {
59 for(FiscalOfficer fiscalOfficer : temp) {
60 results.add(toFiscalOfficerInfo(fiscalOfficer));
61 }
62 }
63
64 return results;
65 }
66
67 public FiscalOfficerInfo retrieveFiscalOfficer(Long id) {
68 FiscalOfficer temp = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, id);
69 FiscalOfficerInfo result = toFiscalOfficerInfo(temp);
70 return result;
71 }
72
73 public FiscalOfficerInfo updateFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
74 FiscalOfficer fiscalOfficer = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, fiscalOfficerInfo.getId());
75
76 if(fiscalOfficer != null) {
77 getBusinessObjectService().save(toFiscalOfficerUpdate(fiscalOfficerInfo, fiscalOfficer));
78 fiscalOfficer = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, fiscalOfficerInfo.getId());
79 }
80
81 return toFiscalOfficerInfo(fiscalOfficer);
82 }
83
84 public TravelAccountInfo retrieveTravelAccount(String number) {
85 TravelAccount temp = getBusinessObjectService().findBySinglePrimaryKey(TravelAccount.class, number);
86 TravelAccountInfo result = toTravelAccountInfo(temp);
87
88 return result;
89 }
90
91
92
93
94
95
96
97
98
99 protected FiscalOfficer toFiscalOfficerUpdate(FiscalOfficerInfo fiscalOfficerInfo,
100 FiscalOfficer fiscalOfficer) {
101
102 BeanUtils.copyProperties(fiscalOfficerInfo, fiscalOfficer, FOI_SKIP);
103
104 List<TravelAccountInfo> newAccounts = new ArrayList<TravelAccountInfo>();
105 Map<String, TravelAccountInfo> accountInfoMap = new HashMap<String, TravelAccountInfo>();
106
107
108 for(TravelAccountInfo accountInfo : fiscalOfficerInfo.getAccounts()) {
109 if(accountInfo.getVersionNumber() == null) {
110 newAccounts.add(accountInfo);
111 }
112 else {
113 accountInfoMap.put(accountInfo.getNumber(), accountInfo);
114 }
115 }
116
117
118 Iterator<TravelAccount> iterator = fiscalOfficer.getAccounts().iterator();
119 while(iterator.hasNext()) {
120 TravelAccount account = iterator.next();
121 TravelAccountInfo accountInfo = accountInfoMap.get(account.getNumber());
122
123 if(accountInfo == null) {
124
125
126
127
128 account.setFoId(null);
129 account.setFiscalOfficer(null);
130 }
131 else {
132 BeanUtils.copyProperties(accountInfo, account);
133 }
134 }
135
136
137 for(TravelAccountInfo accountInfo : newAccounts) {
138 fiscalOfficer.getAccounts().add(toTravelAccount(accountInfo));
139 }
140
141 return fiscalOfficer;
142 }
143
144 protected FiscalOfficer toFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
145 FiscalOfficer fiscalOfficer = new FiscalOfficer();
146
147 BeanUtils.copyProperties(fiscalOfficerInfo, fiscalOfficer, FOI_SKIP);
148
149 if(fiscalOfficerInfo.getAccounts() != null) {
150 for(TravelAccountInfo accountInfo : fiscalOfficerInfo.getAccounts()) {
151 fiscalOfficer.getAccounts().add(toTravelAccount(accountInfo));
152 }
153 }
154
155 return fiscalOfficer;
156 }
157
158 protected FiscalOfficerInfo toFiscalOfficerInfo(FiscalOfficer fiscalOfficer) {
159 FiscalOfficerInfo fiscalOfficerInfo = null;
160
161 if(fiscalOfficer != null) {
162 fiscalOfficerInfo = new FiscalOfficerInfo();
163
164 BeanUtils.copyProperties(fiscalOfficer, fiscalOfficerInfo, FOI_SKIP);
165
166 if(fiscalOfficer.getAccounts() != null) {
167 List<TravelAccountInfo> accountInfoList = new ArrayList<TravelAccountInfo>();
168
169 for(TravelAccount travelAccount : fiscalOfficer.getAccounts()) {
170 accountInfoList.add(toTravelAccountInfo(travelAccount));
171 }
172
173 fiscalOfficerInfo.setAccounts(accountInfoList);
174 }
175 }
176
177 return fiscalOfficerInfo;
178 }
179
180 protected TravelAccountInfo toTravelAccountInfo(TravelAccount travelAccount) {
181 TravelAccountInfo travelAccountInfo = new TravelAccountInfo();
182
183 BeanUtils.copyProperties(travelAccount, travelAccountInfo);
184
185 return travelAccountInfo;
186 }
187
188 protected TravelAccount toTravelAccount(TravelAccountInfo travelAccountInfo) {
189 TravelAccount travelAccount = new TravelAccount();
190
191 BeanUtils.copyProperties(travelAccountInfo, travelAccount);
192
193 return travelAccount;
194 }
195
196 protected BusinessObjectService getBusinessObjectService() {
197 if(businessObjectService == null) {
198 businessObjectService = KRADServiceLocator.getBusinessObjectService();
199 }
200 return this.businessObjectService;
201 }
202
203 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
204 this.businessObjectService = businessObjectService;
205 }
206
207 protected LookupService getLookupService() {
208 if(lookupService == null) {
209 lookupService = KRADServiceLocatorWeb.getLookupService();
210 }
211 return this.lookupService;
212 }
213
214 public void setLookupService(LookupService lookupService) {
215 this.lookupService = lookupService;
216 }
217
218 }