001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.coa.service.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.kuali.kfs.coa.businessobject.Account;
022    import org.kuali.kfs.coa.businessobject.ObjectCode;
023    import org.kuali.kfs.coa.businessobject.SubAccount;
024    import org.kuali.kfs.coa.businessobject.SubObjectCode;
025    import org.kuali.kfs.sys.KFSConstants;
026    import org.kuali.kfs.sys.KFSPropertyConstants;
027    import org.kuali.rice.kns.bo.BusinessObject;
028    import org.kuali.rice.kns.datadictionary.InactivationBlockingMetadata;
029    import org.kuali.rice.kns.service.PersistenceService;
030    import org.kuali.rice.kns.service.impl.InactivationBlockingDetectionServiceImpl;
031    
032    public 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 SubObjCd");
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(KFSPropertyConstants.ACCOUNT_ACTIVE_INDICATOR, KFSConstants.ParameterValues.NO);
046            }
047            else {
048                fieldValues.put(KFSPropertyConstants.ACTIVE, KFSConstants.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    }