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.util.List;
019    import java.util.Map;
020    import java.util.Properties;
021    
022    import org.apache.log4j.Logger;
023    import org.kuali.kfs.module.external.kc.service.ExternalizableBusinessObjectService;
024    import org.kuali.kfs.sys.KFSConstants;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.kfs.sys.service.impl.KfsModuleServiceImpl;
027    import org.kuali.rice.kns.bo.ExternalizableBusinessObject;
028    import org.kuali.rice.kns.datadictionary.BusinessObjectEntry;
029    import org.kuali.rice.kns.service.DataDictionaryService;
030    import org.kuali.rice.kns.service.KNSServiceLocator;
031    import org.kuali.rice.kns.util.KNSConstants;
032    import org.kuali.rice.kns.util.ObjectUtils;
033    
034    public class KcKfsModuleServiceImpl  extends KfsModuleServiceImpl  {
035        
036        protected static final Logger LOG = Logger.getLogger(KcKfsModuleServiceImpl.class);
037    
038        public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(Class<T> businessObjectClass, Map<String, Object> fieldValues) {
039            Class<? extends ExternalizableBusinessObject> implementationClass = getExternalizableBusinessObjectImplementation(businessObjectClass);
040            return (T) getExternalizableBusinessObjectService(implementationClass).findByPrimaryKey(fieldValues);
041        }
042    
043        public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsListForLookup(Class<T> businessObjectClass, Map<String, Object> fieldValues, boolean unbounded) {
044            return getExternalizableBusinessObjectsList( businessObjectClass,fieldValues);
045        }
046    
047        public <T extends ExternalizableBusinessObject> List<T> getExternalizableBusinessObjectsList(Class<T> businessObjectClass, Map<String, Object> fieldValues) {                
048            Class<? extends ExternalizableBusinessObject> implementationClass = getExternalizableBusinessObjectImplementation(businessObjectClass);
049            return (List<T>) getExternalizableBusinessObjectService(implementationClass).findMatching(fieldValues);                   
050        }
051    
052        /**
053         * Finds the business object service via the class to service mapping provided in the module configuration.
054         * 
055         * @param clazz
056         * @return
057         */
058        private ExternalizableBusinessObjectService getExternalizableBusinessObjectService(Class clazz){
059            String serviceName = null;
060            ExternalizableBusinessObjectService eboService = null;
061            
062            Map<Class, String> externalizableBusinessObjectServices = ((KcFinancialSystemModuleConfiguration)getModuleConfiguration()).getExternalizableBusinessObjectServiceImplementations();
063            
064            if(ObjectUtils.isNotNull(externalizableBusinessObjectServices) && ObjectUtils.isNotNull(clazz)){
065                serviceName = (String)externalizableBusinessObjectServices.get(clazz);
066                eboService = (ExternalizableBusinessObjectService)SpringContext.getService(serviceName);            
067            }
068            
069            return eboService;
070        }
071    
072        /**
073         * Gets primary key fields from the Datadictionary entries for the object.
074         * 
075         * @see org.kuali.rice.kns.service.impl.ModuleServiceBase#listPrimaryKeyFieldNames(java.lang.Class)
076         */
077        public List listPrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
078            Class clazz = getExternalizableBusinessObjectImplementation(businessObjectInterfaceClass);
079            final BusinessObjectEntry boEntry = SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getBusinessObjectEntry(clazz.getName());        
080            if (boEntry == null) {
081                return null;
082            }
083            return boEntry.getPrimaryKeys();
084        }
085        
086        /**
087         * Changing the base url to KC url
088         * 
089         * @see org.kuali.rice.kns.service.impl.ModuleServiceBase#getInquiryUrl(java.lang.Class)
090         */
091        protected String getInquiryUrl(Class inquiryBusinessObjectClass){
092            String baseUrl = KNSServiceLocator.getKualiConfigurationService().getPropertyString(KFSConstants.KC_APPLICATION_URL_KEY);
093            String inquiryUrl = baseUrl;
094            if (!inquiryUrl.endsWith("/")) {
095                inquiryUrl = inquiryUrl + "/";
096            }
097            return inquiryUrl + "kr/" + KNSConstants.INQUIRY_ACTION;
098        }
099    
100        /**
101         * Mapping the kfs classes and parameters over to KC equivalents
102         * 
103         * @see org.kuali.rice.kns.service.impl.ModuleServiceBase#getUrlParameters(java.lang.String, java.util.Map)
104         */
105        protected Properties getUrlParameters(String businessObjectClassAttribute, Map<String, String[]> parameters){
106            Properties urlParameters = new Properties();
107            String paramNameToConvert = null;        
108            Map<String, String> kfsToKcInquiryUrlParameterMapping = ((KcFinancialSystemModuleConfiguration)getModuleConfiguration()).getKfsToKcInquiryUrlParameterMapping();
109            Map<String, String> kfsToKcInquiryUrlClassMapping = ((KcFinancialSystemModuleConfiguration)getModuleConfiguration()).getKfsToKcInquiryUrlClassMapping();
110            
111            for (String paramName : parameters.keySet()) {
112                String parameterName = paramName;
113                String[] parameterValues = parameters.get(paramName);
114                
115                if (parameterValues.length > 0) {
116                    //attempt to convert parameter name if necessary
117                    paramNameToConvert = businessObjectClassAttribute + "." + paramName;
118                    if( kfsToKcInquiryUrlParameterMapping.containsKey(paramNameToConvert) ){
119                        parameterName = (String)kfsToKcInquiryUrlParameterMapping.get(paramNameToConvert);
120                    }
121                    urlParameters.put(parameterName, parameterValues[0]);
122                }
123            }
124            
125            urlParameters.put(KNSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, kfsToKcInquiryUrlClassMapping.get(businessObjectClassAttribute));
126            urlParameters.put(KNSConstants.DISPATCH_REQUEST_PARAMETER, 
127                    KNSConstants.CONTINUE_WITH_INQUIRY_METHOD_TO_CALL);
128            return urlParameters;
129        }
130            
131    }