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