/*
 * Copyright 2006-2007 The Kuali Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.module.chart.service.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.kuali.core.service.KualiConfigurationService;
import org.kuali.kfs.context.SpringContext;
import org.kuali.module.chart.bo.OrganizationReversion;
import org.kuali.module.chart.bo.OrganizationReversionCategory;
import org.kuali.module.chart.dao.OrganizationReversionDao;
import org.kuali.module.chart.service.OrganizationReversionService;
import org.kuali.module.gl.service.OrganizationReversionCategoryLogic;
import org.kuali.module.gl.service.impl.orgreversion.GenericOrganizationReversionCategory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class OrganizationReversionServiceImpl implements OrganizationReversionService {
    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionServiceImpl.class);

    private OrganizationReversionDao organizationReversionDao;
    private KualiConfigurationService kualiConfigurationService;

    /**
     * 
     * @see org.kuali.module.chart.service.OrganizationReversionService#getByPrimaryId(java.lang.Integer, java.lang.String,
     *      java.lang.String)
     */
    public OrganizationReversion getByPrimaryId(Integer fiscalYear, String chartCode, String orgCode) {
        LOG.debug("getByPrimaryId() started");
        return organizationReversionDao.getByPrimaryId(fiscalYear, chartCode, orgCode);
    }

    /**
     * 
     * @see org.kuali.module.chart.service.OrganizationReversionService#getCategories()
     */
    public Map<String, OrganizationReversionCategoryLogic> getCategories() {
        LOG.debug("getCategories() started");

        Map<String, OrganizationReversionCategoryLogic> categories = new HashMap<String, OrganizationReversionCategoryLogic>();

        Collection cats = organizationReversionDao.getCategories();

        for (Iterator iter = cats.iterator(); iter.hasNext();) {
            OrganizationReversionCategory orc = (OrganizationReversionCategory) iter.next();

            String categoryCode = orc.getOrganizationReversionCategoryCode();

            try {
                SpringContext.getBeansOfType(OrganizationReversionCategory.class).get("gl" + categoryCode + "OrganizationReversionCategory");
                // We have a custom implementation
                categories.put(categoryCode, SpringContext.getBeansOfType(OrganizationReversionCategoryLogic.class).get("gl" + categoryCode + "OrganizationReversionCategory"));
            }
            catch (NoSuchBeanDefinitionException e) {
                // We'll get the generic implementation
                GenericOrganizationReversionCategory cat = SpringContext.getBean(GenericOrganizationReversionCategory.class);
                cat.setCategoryCode(categoryCode);
                cat.setCategoryName(orc.getOrganizationReversionCategoryName());
                categories.put(categoryCode, (OrganizationReversionCategoryLogic) cat);                
            }
        }
        return categories;
    }

    public List<OrganizationReversionCategory> getCategoryList() {
        LOG.debug("getCategoryList() started");

        return organizationReversionDao.getCategories();
    }

    public void setOrganizationReversionDao(OrganizationReversionDao orDao) {
        organizationReversionDao = orDao;
    }

    public void setKualiConfigurationService(KualiConfigurationService kcs) {
        kualiConfigurationService = kcs;
    }
}

@@ -2910,29 +2910,65 @@
     
-try %7B%0A               
+Map%3CString, OrganizationReversionCategoryLogic%3E beanMap =
  Spr
@@ -3022,27 +3022,65 @@
 Category
+Logic
 .class)
-.get
+;%0A            if (beanMap.containsKey
 (%22gl%22 + 
@@ -3126,17 +3126,19 @@
 tegory%22)
-;
+) %7B
 %0A       
@@ -3150,158 +3150,169 @@
     
-// We have a custom implementation%0A                categories.put(categoryCode, SpringContext.getBeansOfType(OrganizationReversionCategoryLogic.class)
+LOG.info(%22Found Organization Reversion Category Logic for gl%22+categoryCode+%22OrganizationReversionCategory%22);%0A                categories.put(categoryCode, beanMap
 .get
@@ -3374,32 +3374,39 @@
 );%0A            %7D
+ else %7B
 %0A            cat
@@ -3406,105 +3406,136 @@
     
-catch (NoSuchBeanDefinitionException e) %7B%0A                // We'll get the generic implementation
+    LOG.info(%22No Organization Reversion Category Logic for gl%22 + categoryCode + %22OrganizationReversionCategory; using generic%22);
 %0A   
@@ -3785,24 +3785,24 @@
 oryName());%0A
+
             
@@ -3876,32 +3876,16 @@
 c) cat);
-                
 %0A       

Make certain that org rev category logic is initialized correctly.