001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.kra.budget.service.impl;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.kuali.core.util.ObjectUtils;
023: import org.kuali.module.kra.budget.bo.BudgetInstitutionCostShare;
024: import org.kuali.module.kra.budget.bo.BudgetThirdPartyCostShare;
025: import org.kuali.module.kra.budget.bo.BudgetUser;
026: import org.kuali.module.kra.budget.bo.InstitutionCostSharePersonnel;
027: import org.kuali.module.kra.budget.service.BudgetCostShareService;
028: import org.springframework.transaction.annotation.Transactional;
029:
030: @Transactional
031: public class BudgetCostShareServiceImpl implements
032: BudgetCostShareService {
033:
034: public void cleanseCostShare(
035: boolean institutionCostShareIndicator,
036: List<BudgetInstitutionCostShare> budgetInstitutionCostShare,
037: boolean budgetThirdPartyCostShareIndicator,
038: List<BudgetThirdPartyCostShare> budgetThirdPartyCostShare,
039: List<BudgetUser> personnel,
040: List<InstitutionCostSharePersonnel> institutionCostSharePersonnel) {
041: if (!institutionCostShareIndicator) {
042: // institution cost share gone, thus remove manually entered data
043: budgetInstitutionCostShare.clear();
044: }
045:
046: if (!budgetThirdPartyCostShareIndicator) {
047: // third party cost share gone, thus remove manually entered data
048: budgetThirdPartyCostShare.clear();
049: }
050:
051: // Make a copy of the list so to avoid ConcurrentModificationException.
052: List<InstitutionCostSharePersonnel> institutionCostSharePersonnelCopy = new ArrayList(
053: institutionCostSharePersonnel);
054:
055: // Check that all the institutionCostSharePersonnel chart/orgs still have personnel associated with them, if not, remove
056: // the ones that are orphans. I could use BudgetUser.contains here but for the type of comparision I need, I would need
057: // a misleading BudgetUser.equals method... I'm not so sure if that's useful.
058: for (InstitutionCostSharePersonnel institutionCostSharePerson : institutionCostSharePersonnelCopy) {
059: boolean found = false;
060:
061: for (BudgetUser person : personnel) {
062: if (institutionCostSharePerson.getChartOfAccountsCode()
063: .equals(person.getFiscalCampusCode())
064: && institutionCostSharePerson
065: .getOrganizationCode()
066: .equals(
067: person
068: .getPrimaryDepartmentCode())) {
069: found = true;
070: break;
071: }
072: }
073:
074: if (!found) {
075: institutionCostSharePersonnel
076: .remove(institutionCostSharePerson);
077: }
078: }
079: }
080:
081: public void reconcileCostShare(
082: String documentNumber,
083: List<BudgetUser> personnel,
084: List<InstitutionCostSharePersonnel> institutionCostSharePersonnel) {
085: for (BudgetUser person : personnel) {
086: InstitutionCostSharePersonnel institutionCostSharePerson = new InstitutionCostSharePersonnel();
087: institutionCostSharePerson
088: .setDocumentNumber(documentNumber);
089: institutionCostSharePerson
090: .setChartOfAccountsCode(StringUtils
091: .defaultString(person.getFiscalCampusCode()));
092: institutionCostSharePerson.setOrganizationCode(StringUtils
093: .defaultString(person.getPrimaryDepartmentCode()));
094:
095: // Check if it already is in list, if it is, don't add. It will also check that chart & org are set. That is important
096: // because TO BE NAMED people may not have a chart / org, we don't want to add those at this time.
097: if (!ObjectUtils.collectionContainsObjectWithIdentitcalKey(
098: institutionCostSharePersonnel,
099: institutionCostSharePerson)
100: && !StringUtils.isEmpty(institutionCostSharePerson
101: .getChartOfAccountsCode())
102: && !StringUtils.isEmpty(institutionCostSharePerson
103: .getOrganizationCode())) {
104: institutionCostSharePersonnel
105: .add(institutionCostSharePerson);
106: }
107: }
108: }
109: }
|