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.module.ld.service.impl;
017    
018    import java.util.HashMap;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.gl.businessobject.Entry;
023    import org.kuali.kfs.gl.businessobject.Transaction;
024    import org.kuali.kfs.module.ld.LaborConstants;
025    import org.kuali.kfs.module.ld.batch.service.impl.LaborPosterServiceImpl;
026    import org.kuali.kfs.module.ld.service.LaborTransactionDescriptionService;
027    import org.kuali.kfs.sys.KFSConstants;
028    import org.kuali.kfs.sys.KFSPropertyConstants;
029    import org.kuali.kfs.sys.context.SpringContext;
030    import org.kuali.rice.kns.service.DataDictionaryService;
031    import org.kuali.rice.kns.util.ObjectUtils;
032    
033    public class LaborTransactionDescriptionServiceImpl implements LaborTransactionDescriptionService {
034        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborPosterServiceImpl.class);
035    
036        private Map<String, String> transactionDescriptionMap;
037        
038        /**
039         * @see org.kuali.kfs.module.ld.service.LaborTransactionDescriptionService#getTransactionDescription(org.kuali.kfs.gl.businessobject.Transaction)
040         */
041        public String getTransactionDescription(Transaction transaction) {
042            String documentTypeCode = transaction.getFinancialDocumentTypeCode();
043            String description = this.getTransactionDescription(documentTypeCode);
044            description = StringUtils.isNotEmpty(description) ? description : transaction.getTransactionLedgerEntryDescription();
045    
046            // make sure the length of the description cannot excess the specified maximum
047            int transactionDescriptionMaxLength = SpringContext.getBean(DataDictionaryService.class).getAttributeMaxLength(transaction.getClass(), KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_DESC);
048            if (StringUtils.isNotEmpty(description) && description.length() > transactionDescriptionMaxLength) {
049                description = StringUtils.left(description, transactionDescriptionMaxLength);
050            }
051    
052            return description;
053        }
054    
055        /**
056         * @see org.kuali.kfs.module.ld.service.LaborTransactionDescriptionService#getTransactionDescriptionByDocumentType(java.lang.String)
057         */
058        public String getTransactionDescription(String descriptionKey) {       
059            if(transactionDescriptionMap.containsKey(descriptionKey)) {
060                return transactionDescriptionMap.get(descriptionKey);
061            }
062            else {        
063                LOG.warn("Cannot find a description for the given key: " + descriptionKey);
064            }
065            
066            return KFSConstants.EMPTY_STRING;
067        }
068    
069        /**
070         * Sets the transactionDescriptionMap attribute value.
071         * @param transactionDescriptionMap The transactionDescriptionMap to set.
072         */
073        public void setTransactionDescriptionMap(Map<String, String> transactionDescriptionMap) {
074            this.transactionDescriptionMap = transactionDescriptionMap;
075        }
076    }