001    /*
002     * Copyright 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 org.kuali.kfs.module.external.kc.service.impl;
017    
018    import java.net.MalformedURLException;
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Map;
025    
026    import javax.xml.ws.WebServiceException;
027    
028    import org.apache.commons.lang.StringUtils;
029    import org.kuali.kfs.integration.cg.ContractsAndGrantsAccountAwardInformation;
030    import org.kuali.kfs.module.external.kc.KcConstants;
031    import org.kuali.kfs.module.external.kc.businessobject.AwardAccount;
032    import org.kuali.kfs.module.external.kc.businessobject.AwardAccountDTO;
033    import org.kuali.kfs.module.external.kc.service.ExternalizableBusinessObjectService;
034    import org.kuali.kfs.module.external.kc.webService.AwardAccountService;
035    import org.kuali.kfs.module.external.kc.webService.AwardAccountSoapService;
036    import org.kuali.rice.kns.bo.ExternalizableBusinessObject;
037    
038    public class AwardAccountServiceImpl implements ExternalizableBusinessObjectService {
039        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AwardAccountServiceImpl.class);
040        
041        protected  AwardAccountService getWebService() {
042            AwardAccountSoapService soapService = null;
043            try {
044                soapService = new AwardAccountSoapService();
045            }
046            catch (MalformedURLException ex) {
047                LOG.error("Could not intialize AwardAccountSoapService: " + ex.getMessage());
048                throw new RuntimeException("Could not intialize AwardAccountSoapService: " + ex.getMessage());
049            }
050            AwardAccountService port = soapService.getAwardAccountServicePort(); 
051            return port;
052        }
053    
054        public ExternalizableBusinessObject findByPrimaryKey(Map primaryKeys) {        
055            Collection ebos = findMatching(primaryKeys);
056            
057            if(ebos != null && ebos.iterator().hasNext()){
058                return (ExternalizableBusinessObject) ebos.iterator().next();
059            }else{
060                return null;
061            }
062        }
063    
064        public Collection findMatching(Map fieldValues) {
065            Map hashMapList = new HashMap();
066    
067            for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) {
068                Map.Entry e = (Map.Entry) i.next();
069    
070                String key = (String) e.getKey();
071                String val = (String) e.getValue();
072    
073                if ( KcConstants.AwardAccount.KC_ALLOWABLE_CRITERIA_PARAMETERS.contains(key)  && (val.length() > 0)) {
074                    hashMapList.put(key, val);
075                }
076            }
077            
078            String accountNumber = (String)hashMapList.get("accountNumber");
079            String chartOfAccountsCode = (String)hashMapList.get("chartOfAccountsCode");
080            
081            List awardAccounts = new ArrayList();             
082            List<AwardAccountDTO> awardAccountDTOs = null;
083            
084            //get award account DTO
085            try{
086                awardAccountDTOs = getWebService().getAwardAccounts(accountNumber, chartOfAccountsCode);
087            } catch (WebServiceException ex) {
088                LOG.error("Could not retrieve award accounts: "+ ex.getMessage());
089            }
090            
091            if (awardAccountDTOs != null && !awardAccountDTOs.isEmpty()) {            
092                ContractsAndGrantsAccountAwardInformation awardAccountInfo = null; 
093                
094                for(AwardAccountDTO awardAccount : (List<AwardAccountDTO>)awardAccountDTOs){
095                    //create if no error messages
096                    if(StringUtils.isEmpty(awardAccount.getErrorMessage())){                    
097                        awardAccountInfo = new AwardAccount(awardAccount, accountNumber, chartOfAccountsCode, "");
098                        awardAccounts.add(awardAccountInfo);
099                    }
100                }
101            }
102                    
103            return awardAccounts;
104        }
105    }