001/*
002 * Copyright 2009 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 java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.kuali.ole.sys.context.SpringContext;
023import org.kuali.rice.kns.service.DataDictionaryService;
024import org.kuali.rice.krad.bo.BusinessObject;
025import org.kuali.rice.krad.datadictionary.AttributeDefinition;
026
027/**
028 * An abstract class which provides help in determining field lengths of business objects being parsed from Strings
029 */
030public abstract class BusinessObjectStringParserFieldUtils {
031    protected org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
032    private Map<String, Integer> fieldLengthMap;
033    private Map<String, Integer> fieldBeginningPositionMap;
034    
035    /**
036     * @return a Map with attribute names as keys and lengths of how long those fields are specified to be in the DataDictionary as values
037     */
038    public Map<String, Integer> getFieldLengthMap() {
039        if (fieldLengthMap == null) {
040            initializeFieldLengthMap();
041        }
042        return fieldLengthMap;
043    }
044    
045    /**
046     * Calculates a map with the field length of all of the attributes of the class given by the
047     * getBusinessObjectClass method
048     */
049    protected void initializeFieldLengthMap() {
050        fieldLengthMap = new HashMap<String, Integer>();
051        DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
052        List<AttributeDefinition> attributes = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(getBusinessObjectClass().getName()).getAttributes();
053
054        for (AttributeDefinition attributeDefinition : attributes) {
055            Integer fieldLength;
056            fieldLength = dataDictionaryService.getAttributeMaxLength(getBusinessObjectClass(), attributeDefinition.getName());
057            fieldLengthMap.put(attributeDefinition.getName(), fieldLength);
058        }
059    }
060    
061    /**
062     * @return the class of the BusinessObject that this utility class will help parse from a String
063     */
064    public abstract Class<? extends BusinessObject> getBusinessObjectClass();
065
066    /**
067     * @return a Map with business object field names as keys and starting positions of each field in the String as values
068     */
069    public Map<String, Integer> getFieldBeginningPositionMap() {
070        if (fieldBeginningPositionMap == null) {
071            initializeFieldBeginningPositionMap();
072        }
073        return fieldBeginningPositionMap;
074    }
075    
076    /**
077     * Calculates the beginning positions of each field in the array returned by getOrderedProperties, based on
078     * the length map calculated by getFieldLengthMap().
079     */
080    protected void initializeFieldBeginningPositionMap() {
081        fieldBeginningPositionMap = new HashMap<String, Integer>();
082        Map<String, Integer> lengthMap = getFieldLengthMap();
083        
084        int lengthTracker = 0;
085   
086        for (String property : getOrderedProperties()) {
087            fieldBeginningPositionMap.put(property, new Integer(lengthTracker));
088            if (LOG.isDebugEnabled()) {
089                LOG.debug("Finding position for property: "+property+"; length = "+lengthMap.get(property));
090            }
091            lengthTracker += lengthMap.get(property).intValue();
092        }
093    }
094    
095    /**
096     * @return an array of String names of fields in a business object in the order they will show up in the String to be parsed
097     */
098    public abstract String[] getOrderedProperties();
099}