Coverage Report - edu.sampleu.travel.service.FiscalOfficerServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
FiscalOfficerServiceImpl
0%
0/72
0%
0/30
2.154
 
 1  
 /**
 2  
  * Copyright 2005-2011 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  0
 public class FiscalOfficerServiceImpl implements FiscalOfficerService {
 41  
 
 42  0
     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  0
         getBusinessObjectService().save(toFiscalOfficer(fiscalOfficerInfo));
 50  
         
 51  0
         Map<String, Object> criteria = new HashMap<String, Object>();
 52  0
         criteria.put("id", fiscalOfficerInfo.getId());
 53  0
         FiscalOfficer fiscalOfficer = getBusinessObjectService().findByPrimaryKey(FiscalOfficer.class, criteria);
 54  0
         return toFiscalOfficerInfo(fiscalOfficer);
 55  
     }
 56  
 
 57  
     @Override
 58  
     public List<FiscalOfficerInfo> lookupFiscalOfficer(Map<String, String> criteria) {
 59  
         @SuppressWarnings("unchecked")
 60  0
         Collection<FiscalOfficer> temp = getLookupService().findCollectionBySearch(FiscalOfficer.class, criteria);
 61  
         
 62  0
         List<FiscalOfficerInfo> results = new ArrayList<FiscalOfficerInfo>();
 63  0
         if(temp != null) {
 64  0
             for(FiscalOfficer fiscalOfficer : temp) {
 65  0
                 results.add(toFiscalOfficerInfo(fiscalOfficer));
 66  
             }
 67  
         }
 68  
         
 69  0
         return results;
 70  
     }
 71  
 
 72  
     @Override
 73  
     public FiscalOfficerInfo retrieveFiscalOfficer(Long id) {
 74  0
         FiscalOfficer temp = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, id);
 75  0
         FiscalOfficerInfo result = toFiscalOfficerInfo(temp);
 76  0
         return result;
 77  
     }
 78  
 
 79  
     @Override
 80  
     public FiscalOfficerInfo updateFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
 81  0
         FiscalOfficer fiscalOfficer = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, fiscalOfficerInfo.getId());
 82  
         
 83  0
         if(fiscalOfficer != null) {
 84  0
             getBusinessObjectService().save(toFiscalOfficerUpdate(fiscalOfficerInfo, fiscalOfficer));
 85  0
             fiscalOfficer = getBusinessObjectService().findBySinglePrimaryKey(FiscalOfficer.class, fiscalOfficerInfo.getId());
 86  
         }
 87  
         
 88  0
         return toFiscalOfficerInfo(fiscalOfficer);
 89  
     }
 90  
     
 91  
     /**
 92  
      * This method only copies fields from the dto to the ojb bean that should be copied
 93  
      * on an update call.
 94  
      * 
 95  
      * @param fiscalOfficerInfo dto to convert to ojb bean
 96  
      * @param fiscalOfficer target ojb bean to update from info
 97  
      * @return
 98  
      */
 99  
     protected FiscalOfficer toFiscalOfficerUpdate(FiscalOfficerInfo fiscalOfficerInfo,
 100  
             FiscalOfficer fiscalOfficer) {
 101  
         
 102  0
         BeanUtils.copyProperties(fiscalOfficerInfo, fiscalOfficer, FOI_SKIP);
 103  
         
 104  0
         List<TravelAccountInfo> newAccounts = new ArrayList<TravelAccountInfo>();
 105  0
         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  0
         for(TravelAccountInfo accountInfo : fiscalOfficerInfo.getAccounts()) {
 109  0
             if(accountInfo.getVersionNumber() == null) {
 110  0
                 newAccounts.add(accountInfo);
 111  
             }
 112  
             else {
 113  0
                 accountInfoMap.put(accountInfo.getNumber(), accountInfo);
 114  
             }
 115  
         }
 116  
         
 117  
         // iterator the accounts from the db, updating or removing if necessary
 118  0
         Iterator<TravelAccount> iterator = fiscalOfficer.getAccounts().iterator();
 119  0
         while(iterator.hasNext()) {
 120  0
             TravelAccount account = iterator.next();
 121  0
             TravelAccountInfo accountInfo = accountInfoMap.get(account.getNumber());
 122  
             
 123  0
             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  0
                 account.setFoId(null);
 129  0
                 account.setFiscalOfficer(null);
 130  
             }
 131  
             else {
 132  0
                 BeanUtils.copyProperties(accountInfo, account);
 133  
             }
 134  0
         }
 135  
         
 136  
         // add the new accounts to the list
 137  0
         for(TravelAccountInfo accountInfo : newAccounts) {
 138  0
             fiscalOfficer.getAccounts().add(toTravelAccount(accountInfo));
 139  
         }
 140  
         
 141  0
         return fiscalOfficer;
 142  
     }
 143  
     
 144  
     protected FiscalOfficer toFiscalOfficer(FiscalOfficerInfo fiscalOfficerInfo) {
 145  0
         FiscalOfficer fiscalOfficer = new FiscalOfficer();
 146  
         
 147  0
         BeanUtils.copyProperties(fiscalOfficerInfo, fiscalOfficer, FOI_SKIP);
 148  
         
 149  0
         if(fiscalOfficerInfo.getAccounts() != null) {
 150  0
             for(TravelAccountInfo accountInfo : fiscalOfficerInfo.getAccounts()) {
 151  0
                 fiscalOfficer.getAccounts().add(toTravelAccount(accountInfo));
 152  
             }
 153  
         }
 154  
        
 155  0
         return fiscalOfficer;
 156  
     }
 157  
     
 158  
     protected FiscalOfficerInfo toFiscalOfficerInfo(FiscalOfficer fiscalOfficer) {
 159  0
         FiscalOfficerInfo fiscalOfficerInfo = null;
 160  
         
 161  0
         if(fiscalOfficer != null) {
 162  0
             fiscalOfficerInfo = new FiscalOfficerInfo();
 163  
             
 164  0
             BeanUtils.copyProperties(fiscalOfficer, fiscalOfficerInfo, FOI_SKIP);
 165  
             
 166  0
             if(fiscalOfficer.getAccounts() != null) {
 167  0
                 List<TravelAccountInfo> accountInfoList = new ArrayList<TravelAccountInfo>();
 168  
                 
 169  0
                 for(TravelAccount travelAccount : fiscalOfficer.getAccounts()) {
 170  0
                     accountInfoList.add(toTravelAccountInfo(travelAccount));
 171  
                 }
 172  
                 
 173  0
                 fiscalOfficerInfo.setAccounts(accountInfoList);
 174  
             }
 175  
         }
 176  
         
 177  0
         return fiscalOfficerInfo;
 178  
     }
 179  
     
 180  
     protected TravelAccountInfo toTravelAccountInfo(TravelAccount travelAccount) {
 181  0
         TravelAccountInfo travelAccountInfo = new TravelAccountInfo();
 182  
         
 183  0
         BeanUtils.copyProperties(travelAccount, travelAccountInfo);
 184  
         
 185  0
         return travelAccountInfo;
 186  
     }
 187  
     
 188  
     protected TravelAccount toTravelAccount(TravelAccountInfo travelAccountInfo) {
 189  0
         TravelAccount travelAccount = new TravelAccount();
 190  
         
 191  0
         BeanUtils.copyProperties(travelAccountInfo, travelAccount);
 192  
         
 193  0
         return travelAccount;
 194  
     }
 195  
 
 196  
     protected BusinessObjectService getBusinessObjectService() {
 197  0
         if(businessObjectService == null) {
 198  0
             businessObjectService = KRADServiceLocator.getBusinessObjectService();
 199  
         }
 200  0
         return this.businessObjectService;
 201  
     }
 202  
 
 203  
     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
 204  0
         this.businessObjectService = businessObjectService;
 205  0
     }
 206  
 
 207  
     protected LookupService getLookupService() {
 208  0
         if(lookupService == null) {
 209  0
             lookupService = KRADServiceLocatorWeb.getLookupService();
 210  
         }
 211  0
         return this.lookupService;
 212  
     }
 213  
 
 214  
     public void setLookupService(LookupService lookupService) {
 215  0
         this.lookupService = lookupService;
 216  0
     }
 217  
 
 218  
 }