001/*
002 * Copyright 2008 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.sys.businessobject;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.coa.businessobject.Chart;
020import org.kuali.ole.coa.businessobject.Organization;
021import org.kuali.ole.coa.service.ChartService;
022import org.kuali.ole.coa.service.OrganizationService;
023import org.kuali.ole.sys.context.SpringContext;
024
025public class ChartOrgHolderImpl implements ChartOrgHolder {
026
027    protected String chartOfAccountsCode;
028    protected String organizationCode;
029
030    protected Chart chartOfAccounts;
031    protected Organization organization;
032
033    protected static transient OrganizationService organizationService;
034    protected static transient ChartService chartService;
035
036    public ChartOrgHolderImpl() {}
037    
038    public ChartOrgHolderImpl( String chartOfAccountsCode, String organizationCode ) {
039        this.chartOfAccountsCode = chartOfAccountsCode;
040        this.organizationCode = organizationCode;
041    }
042
043    public ChartOrgHolderImpl( Organization org ) {
044        this.chartOfAccountsCode = org.getChartOfAccountsCode();
045        this.organizationCode = org.getOrganizationCode();
046        this.organization = org;
047    }
048
049    public String getChartOfAccountsCode() {
050        return chartOfAccountsCode;
051    }
052
053
054    public String getOrganizationCode() {
055        return organizationCode;
056    }
057
058    public Chart getChartOfAccounts() {
059        if ( chartOfAccounts == null && StringUtils.isNotBlank(chartOfAccountsCode) ) {
060            chartOfAccounts = getChartService().getByPrimaryId(chartOfAccountsCode);
061        }
062        return chartOfAccounts;
063    }
064
065    public Organization getOrganization() {
066        if ( organization == null && StringUtils.isNotBlank(chartOfAccountsCode) && StringUtils.isNotBlank(organizationCode) ) {
067            organization = getOrganizationService().getByPrimaryId(chartOfAccountsCode, organizationCode);
068        }
069        return organization;
070    }
071
072    private static OrganizationService getOrganizationService() {
073        if ( organizationService == null ) {
074            organizationService = SpringContext.getBean(OrganizationService.class);
075        }
076        return organizationService;
077    }
078
079    private static ChartService getChartService() {
080        if ( chartService == null ) {
081            chartService = SpringContext.getBean(ChartService.class);
082        }
083        return chartService;
084    }
085
086
087    public void setChartOfAccountsCode(String chartOfAccountsCode) {
088        this.chartOfAccountsCode = chartOfAccountsCode;
089    }
090
091
092    public void setOrganizationCode(String organizationCode) {
093        this.organizationCode = organizationCode;
094    }
095    
096    @Override
097    public boolean equals(Object obj) {
098        if ( obj == null || !(obj instanceof ChartOrgHolder) ) {
099            return false;
100        }
101        return StringUtils.equals( chartOfAccountsCode, ((ChartOrgHolder)obj).getChartOfAccountsCode() )
102                && StringUtils.equals( organizationCode, ((ChartOrgHolder)obj).getOrganizationCode() );
103    }
104    
105    @Override
106    public int hashCode() {
107        return String.valueOf(chartOfAccountsCode).hashCode() + String.valueOf(organizationCode).hashCode();
108    }
109    
110    @Override
111    public String toString() {        
112        return String.valueOf(chartOfAccountsCode) + "-" + String.valueOf(organizationCode);
113    }
114}