001/*
002 * Copyright 2007 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 */
016package org.kuali.ole.gl.batch.service.impl;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.kuali.ole.gl.batch.dataaccess.OrganizationReversionUnitOfWorkDao;
023import org.kuali.ole.gl.batch.service.OrganizationReversionUnitOfWorkService;
024import org.kuali.ole.gl.businessobject.OrgReversionUnitOfWork;
025import org.kuali.ole.gl.businessobject.OrgReversionUnitOfWorkCategoryAmount;
026import org.kuali.rice.krad.service.BusinessObjectService;
027import org.springframework.transaction.annotation.Transactional;
028
029/**
030 * The base implementation of OrganizationReversionUnitOfWorkService
031 */
032@Transactional
033public class OrganizationReversionUnitOfWorkServiceImpl implements OrganizationReversionUnitOfWorkService {
034    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionUnitOfWorkServiceImpl.class);
035    
036    protected BusinessObjectService businessObjectService;
037    protected OrganizationReversionUnitOfWorkDao orgReversionUnitOfWorkDao;
038
039    /**
040     * This method takes a unit of work retrieved from the persistence store and loads its categories
041     * 
042     * @param orgRevUnitOfWork org reversion unit of work to load categories for
043     * @return the org reversion unit of work with loaded categories
044     * @see org.kuali.ole.gl.batch.service.OrganizationReversionUnitOfWorkService#loadCategories(org.kuali.ole.gl.businessobject.OrgReversionUnitOfWork)
045     */
046    public OrgReversionUnitOfWork loadCategories(OrgReversionUnitOfWork orgRevUnitOfWork) {
047        Map<String,Object> criteria = new HashMap<String, Object>();
048        criteria.put("chartOfAccountsCode", orgRevUnitOfWork.chartOfAccountsCode);
049        criteria.put("accountNbr", orgRevUnitOfWork.accountNumber);
050        criteria.put("subAccountNbr", orgRevUnitOfWork.subAccountNumber);
051
052        Collection<OrgReversionUnitOfWorkCategoryAmount> categoryAmounts = businessObjectService.findMatching(OrgReversionUnitOfWorkCategoryAmount.class, criteria);
053        Map<String, OrgReversionUnitOfWorkCategoryAmount> categories = orgRevUnitOfWork.getCategoryAmounts();
054        for ( OrgReversionUnitOfWorkCategoryAmount catAmount : categoryAmounts ) {
055            categories.put(catAmount.getCategoryCode(), catAmount);
056        }
057        return orgRevUnitOfWork;
058    }
059
060    /**
061     * Immediate deletion awaits all entries of the unit of work summary tables in the persistence store once
062     * you call this method, for this method is both powerful and deadly and also gets called to clear out
063     * those tables before every single org reversion run.
064     * @see org.kuali.ole.gl.batch.service.OrganizationReversionUnitOfWorkService#removeAll()
065     */
066    public void destroyAllUnitOfWorkSummaries() {
067        orgReversionUnitOfWorkDao.destroyAllUnitOfWorkSummaries();
068    }
069
070    /**
071     * This save method is guaranteed to save the category data as well.
072     * 
073     * @param orgRevUnitOfWork organizationReversionUnitOfWork to save
074     * @see org.kuali.ole.gl.batch.service.OrganizationReversionUnitOfWorkService#save(org.kuali.ole.gl.businessobject.OrgReversionUnitOfWork)
075     */
076    public void save(OrgReversionUnitOfWork orgRevUnitOfWork) {
077        if (LOG.isDebugEnabled()) {
078            LOG.debug("Saving org reversion summary for " + orgRevUnitOfWork.toString() + "; its category keys are: " + orgRevUnitOfWork.getCategoryAmounts().keySet());
079        }
080        businessObjectService.save(orgRevUnitOfWork);
081        for (String category: orgRevUnitOfWork.getCategoryAmounts().keySet()) {
082            final OrgReversionUnitOfWorkCategoryAmount categoryAmount = orgRevUnitOfWork.getCategoryAmounts().get(category);
083            if (LOG.isDebugEnabled()) {
084                LOG.debug("Saving category amount for " + categoryAmount.toString());
085            }
086            businessObjectService.save(categoryAmount);
087    }
088    }
089
090    /**
091     * Sets the businessObjectService attribute value.
092     * 
093     * @param businessObjectService The businessObjectService to set.
094     */
095    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
096        this.businessObjectService = businessObjectService;
097    }
098
099    /**
100     * Sets the orgReversionUnitOfWorkDao attribute value.
101     * 
102     * @param orgReversionUnitOfWorkDao The orgReversionUnitOfWorkDao to set.
103     */
104    public void setOrgReversionUnitOfWorkDao(OrganizationReversionUnitOfWorkDao orgReversionUnitOfWorkDao) {
105        this.orgReversionUnitOfWorkDao = orgReversionUnitOfWorkDao;
106    }
107
108}