001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.sampleu.travel.service;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.HashMap;
021    import java.util.Iterator;
022    import java.util.List;
023    import java.util.Map;
024    
025    import org.kuali.rice.krad.service.BusinessObjectService;
026    import org.kuali.rice.krad.service.KRADServiceLocator;
027    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
028    import org.kuali.rice.krad.service.LookupService;
029    import org.springframework.beans.BeanUtils;
030    
031    import edu.sampleu.travel.bo.FiscalOfficer;
032    import edu.sampleu.travel.bo.TravelAccount;
033    import edu.sampleu.travel.dto.FiscalOfficerInfo;
034    import edu.sampleu.travel.dto.TravelAccountInfo;
035    
036    /**
037     * 
038     * @author Kuali Rice Team (rice.collab@kuali.org)
039     */
040    public class FiscalOfficerServiceImpl implements FiscalOfficerService {
041    
042        protected static String[] FOI_SKIP = { "accounts" };
043        
044        protected BusinessObjectService businessObjectService;
045        protected LookupService lookupService;
046        
047        @Override
048        public FiscalOfficerInfo createFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
049            getBusinessObjectService().save(toFiscalOfficer(fiscalOfficerInfo));
050            
051            Map<String, Object> criteria = new HashMap<String, Object>();
052            criteria.put("id", fiscalOfficerInfo.getId());
053            FiscalOfficer fiscalOfficer = getBusinessObjectService().findByPrimaryKey(FiscalOfficer.class, criteria);
054            return toFiscalOfficerInfo(fiscalOfficer);
055        }
056    
057        @Override
058        public List<FiscalOfficerInfo> lookupFiscalOfficer(Map<String, String> criteria) {
059            @SuppressWarnings("unchecked")
060            Collection<FiscalOfficer> temp = getLookupService().findCollectionBySearch(FiscalOfficer.class, criteria);
061            
062            List<FiscalOfficerInfo> results = new ArrayList<FiscalOfficerInfo>();
063            if(temp != null) {
064                for(FiscalOfficer fiscalOfficer : temp) {
065                    results.add(toFiscalOfficerInfo(fiscalOfficer));
066                }
067            }
068            
069            return results;
070        }
071    
072        @Override
073        public FiscalOfficerInfo retrieveFiscalOfficer(Long id) {
074            FiscalOfficer temp = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, id);
075            FiscalOfficerInfo result = toFiscalOfficerInfo(temp);
076            return result;
077        }
078    
079        @Override
080        public FiscalOfficerInfo updateFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
081            FiscalOfficer fiscalOfficer = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, fiscalOfficerInfo.getId());
082            
083            if(fiscalOfficer != null) {
084                getBusinessObjectService().save(toFiscalOfficerUpdate(fiscalOfficerInfo, fiscalOfficer));
085                fiscalOfficer = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, fiscalOfficerInfo.getId());
086            }
087            
088            return toFiscalOfficerInfo(fiscalOfficer);
089        }
090        
091        /**
092         * This method only copies fields from the dto to the ojb bean that should be copied
093         * on an update call.
094         * 
095         * @param fiscalOfficerInfo dto to convert to ojb bean
096         * @param fiscalOfficer target ojb bean to update from info
097         * @return
098         */
099        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            // create a map of accounts that are being edited and list of accounts to be added
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            // iterator the accounts from the db, updating or removing if necessary
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                    // this really isnt the best example data set, so just orphan
125                    // instead of actually deleting
126                    //iterator.remove();
127                    
128                    account.setFoId(null);
129                    account.setFiscalOfficer(null);
130                }
131                else {
132                    BeanUtils.copyProperties(accountInfo, account);
133                }
134            }
135            
136            // add the new accounts to the list
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    }