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.coa.service.impl;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.kuali.ole.coa.businessobject.Account;
022import org.kuali.ole.coa.businessobject.ObjectCode;
023import org.kuali.ole.coa.businessobject.SubAccount;
024import org.kuali.ole.coa.businessobject.SubObjectCode;
025import org.kuali.ole.sys.OLEConstants;
026import org.kuali.ole.sys.OLEPropertyConstants;
027import org.kuali.rice.krad.bo.BusinessObject;
028import org.kuali.rice.krad.datadictionary.InactivationBlockingMetadata;
029import org.kuali.rice.krad.service.PersistenceService;
030import org.kuali.rice.krad.service.impl.InactivationBlockingDetectionServiceImpl;
031
032public class IndirectCostRecoveryRateDetailInactivationBlockingDetectionServiceImpl extends InactivationBlockingDetectionServiceImpl {
033    protected PersistenceService persistenceService;
034    
035    @Override
036    protected Map<String, String> buildInactivationBlockerQueryMap(BusinessObject blockedBo, InactivationBlockingMetadata inactivationBlockingMetadata) {
037        Class<? extends BusinessObject> boClass = blockedBo.getClass();
038        if (!(Account.class.isAssignableFrom(boClass) || SubAccount.class.isAssignableFrom(boClass) || ObjectCode.class.isAssignableFrom(boClass) || SubObjectCode.class.isAssignableFrom(boClass))) {
039            throw new IllegalArgumentException("BO must be either an Account, SubAccount, ObjectCode, or SubObjectCode (was: " + boClass + ")");
040        }
041        
042        // this code assumes that the PK field names in the BO are identical to the field names in the ICR Rate Detail BO
043        Map<String, Object> fieldValues = persistenceService.getPrimaryKeyFieldValues(blockedBo);
044        if (Account.class.isAssignableFrom(boClass)) {
045            fieldValues.put(OLEPropertyConstants.ACCOUNT_ACTIVE_INDICATOR, OLEConstants.ParameterValues.NO);
046        }
047        else {
048            fieldValues.put(OLEPropertyConstants.ACTIVE, OLEConstants.ParameterValues.YES);
049        }
050        return convertFieldValuesToStrings(fieldValues);
051    }
052    
053    /**
054     * Converts the map of PKs - which is a Map of <String, Object> to a Map of <String, String> by turning any objects inside into Strings...
055     * @param fieldValues field values to convert
056     * @return the Map of fieldValues converted to a Map of <String, String>
057     */
058    protected Map<String, String> convertFieldValuesToStrings(Map<String, Object> fieldValues) {
059        Map<String, String> newFieldValues = new HashMap<String, String>();
060        for (String key : fieldValues.keySet()) {
061            final Object value = fieldValues.get(key);
062            if (value != null) {
063                newFieldValues.put(key, value.toString());
064            }
065        }
066        return newFieldValues;
067    }
068
069    public void setPersistenceService(PersistenceService persistenceService) {
070        this.persistenceService = persistenceService;
071    }
072}