View Javadoc

1   /**
2    * Copyright 2005-2012 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 edu.sampleu.travel.service;
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.service.BusinessObjectService;
26  import org.kuali.rice.krad.service.KRADServiceLocator;
27  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
28  import org.kuali.rice.krad.service.LookupService;
29  import org.springframework.beans.BeanUtils;
30  
31  import edu.sampleu.travel.bo.FiscalOfficer;
32  import edu.sampleu.travel.bo.TravelAccount;
33  import edu.sampleu.travel.dto.FiscalOfficerInfo;
34  import edu.sampleu.travel.dto.TravelAccountInfo;
35  
36  /**
37   * 
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class FiscalOfficerTravelAccountServiceImpl implements FiscalOfficerService, TravelAccountService {
41  
42      protected static String[] FOI_SKIP = { "accounts" };
43      
44      protected BusinessObjectService businessObjectService;
45      protected LookupService lookupService;
46      
47      @Override
48      public FiscalOfficerInfo createFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
49          getBusinessObjectService().save(toFiscalOfficer(fiscalOfficerInfo));
50          
51          Map<String, Object> criteria = new HashMap<String, Object>();
52          criteria.put("id", fiscalOfficerInfo.getId());
53          FiscalOfficer fiscalOfficer = getBusinessObjectService().findByPrimaryKey(FiscalOfficer.class, criteria);
54          return toFiscalOfficerInfo(fiscalOfficer);
55      }
56  
57      @Override
58      public List<FiscalOfficerInfo> lookupFiscalOfficer(Map<String, String> criteria) {
59          @SuppressWarnings("unchecked")
60          Collection<FiscalOfficer> temp = getLookupService().findCollectionBySearch(FiscalOfficer.class, criteria);
61          
62          List<FiscalOfficerInfo> results = new ArrayList<FiscalOfficerInfo>();
63          if(temp != null) {
64              for(FiscalOfficer fiscalOfficer : temp) {
65                  results.add(toFiscalOfficerInfo(fiscalOfficer));
66              }
67          }
68          
69          return results;
70      }
71  
72      @Override
73      public FiscalOfficerInfo retrieveFiscalOfficer(Long id) {
74          FiscalOfficer temp = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, id);
75          FiscalOfficerInfo result = toFiscalOfficerInfo(temp);
76          return result;
77      }
78  
79      @Override
80      public FiscalOfficerInfo updateFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
81          FiscalOfficer fiscalOfficer = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, fiscalOfficerInfo.getId());
82          
83          if(fiscalOfficer != null) {
84              getBusinessObjectService().save(toFiscalOfficerUpdate(fiscalOfficerInfo, fiscalOfficer));
85              fiscalOfficer = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, fiscalOfficerInfo.getId());
86          }
87          
88          return toFiscalOfficerInfo(fiscalOfficer);
89      }
90  
91      @Override
92      public TravelAccountInfo retrieveTravelAccount(String number) {
93          TravelAccount temp = getBusinessObjectService().findBySinglePrimaryKey(TravelAccount.class, number);
94          TravelAccountInfo result = toTravelAccountInfo(temp);
95  
96          return result;
97      }
98      
99      /**
100      * This method only copies fields from the dto to the ojb bean that should be copied
101      * on an update call.
102      * 
103      * @param fiscalOfficerInfo dto to convert to ojb bean
104      * @param fiscalOfficer target ojb bean to update from info
105      * @return
106      */
107     protected FiscalOfficer toFiscalOfficerUpdate(FiscalOfficerInfo fiscalOfficerInfo,
108             FiscalOfficer fiscalOfficer) {
109         
110         BeanUtils.copyProperties(fiscalOfficerInfo, fiscalOfficer, FOI_SKIP);
111         
112         List<TravelAccountInfo> newAccounts = new ArrayList<TravelAccountInfo>();
113         Map<String, TravelAccountInfo> accountInfoMap = new HashMap<String, TravelAccountInfo>();
114         
115         // create a map of accounts that are being edited and list of accounts to be added
116         for(TravelAccountInfo accountInfo : fiscalOfficerInfo.getAccounts()) {
117             if(accountInfo.getVersionNumber() == null) {
118                 newAccounts.add(accountInfo);
119             }
120             else {
121                 accountInfoMap.put(accountInfo.getNumber(), accountInfo);
122             }
123         }
124         
125         // iterator the accounts from the db, updating or removing if necessary
126         Iterator<TravelAccount> iterator = fiscalOfficer.getAccounts().iterator();
127         while(iterator.hasNext()) {
128             TravelAccount account = iterator.next();
129             TravelAccountInfo accountInfo = accountInfoMap.get(account.getNumber());
130             
131             if(accountInfo == null) {
132                 // this really isnt the best example data set, so just orphan
133                 // instead of actually deleting
134                 //iterator.remove();
135                 
136                 account.setFoId(null);
137                 account.setFiscalOfficer(null);
138             }
139             else {
140                 BeanUtils.copyProperties(accountInfo, account);
141             }
142         }
143         
144         // add the new accounts to the list
145         for(TravelAccountInfo accountInfo : newAccounts) {
146             fiscalOfficer.getAccounts().add(toTravelAccount(accountInfo));
147         }
148         
149         return fiscalOfficer;
150     }
151     
152     protected FiscalOfficer toFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
153         FiscalOfficer fiscalOfficer = new FiscalOfficer();
154         
155         BeanUtils.copyProperties(fiscalOfficerInfo, fiscalOfficer, FOI_SKIP);
156         
157         if(fiscalOfficerInfo.getAccounts() != null) {
158             for(TravelAccountInfo accountInfo : fiscalOfficerInfo.getAccounts()) {
159                 fiscalOfficer.getAccounts().add(toTravelAccount(accountInfo));
160             }
161         }
162        
163         return fiscalOfficer;
164     }
165     
166     protected FiscalOfficerInfo toFiscalOfficerInfo(FiscalOfficer fiscalOfficer) {
167         FiscalOfficerInfo fiscalOfficerInfo = null;
168         
169         if(fiscalOfficer != null) {
170             fiscalOfficerInfo = new FiscalOfficerInfo();
171             
172             BeanUtils.copyProperties(fiscalOfficer, fiscalOfficerInfo, FOI_SKIP);
173             
174             if(fiscalOfficer.getAccounts() != null) {
175                 List<TravelAccountInfo> accountInfoList = new ArrayList<TravelAccountInfo>();
176                 
177                 for(TravelAccount travelAccount : fiscalOfficer.getAccounts()) {
178                     accountInfoList.add(toTravelAccountInfo(travelAccount));
179                 }
180                 
181                 fiscalOfficerInfo.setAccounts(accountInfoList);
182             }
183         }
184         
185         return fiscalOfficerInfo;
186     }
187     
188     protected TravelAccountInfo toTravelAccountInfo(TravelAccount travelAccount) {
189         TravelAccountInfo travelAccountInfo = new TravelAccountInfo();
190         
191         BeanUtils.copyProperties(travelAccount, travelAccountInfo);
192         
193         return travelAccountInfo;
194     }
195     
196     protected TravelAccount toTravelAccount(TravelAccountInfo travelAccountInfo) {
197         TravelAccount travelAccount = new TravelAccount();
198         
199         BeanUtils.copyProperties(travelAccountInfo, travelAccount);
200         
201         return travelAccount;
202     }
203 
204     protected BusinessObjectService getBusinessObjectService() {
205         if(businessObjectService == null) {
206             businessObjectService = KRADServiceLocator.getBusinessObjectService();
207         }
208         return this.businessObjectService;
209     }
210 
211     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
212         this.businessObjectService = businessObjectService;
213     }
214 
215     protected LookupService getLookupService() {
216         if(lookupService == null) {
217             lookupService = KRADServiceLocatorWeb.getLookupService();
218         }
219         return this.lookupService;
220     }
221 
222     public void setLookupService(LookupService lookupService) {
223         this.lookupService = lookupService;
224     }
225 
226 }