View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.sys.businessobject;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.rice.kns.service.DataDictionaryService;
24  import org.kuali.rice.krad.bo.BusinessObject;
25  import org.kuali.rice.krad.datadictionary.AttributeDefinition;
26  
27  /**
28   * An abstract class which provides help in determining field lengths of business objects being parsed from Strings
29   */
30  public abstract class BusinessObjectStringParserFieldUtils {
31      protected org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(getClass());
32      private Map<String, Integer> fieldLengthMap;
33      private Map<String, Integer> fieldBeginningPositionMap;
34      
35      /**
36       * @return a Map with attribute names as keys and lengths of how long those fields are specified to be in the DataDictionary as values
37       */
38      public Map<String, Integer> getFieldLengthMap() {
39          if (fieldLengthMap == null) {
40              initializeFieldLengthMap();
41          }
42          return fieldLengthMap;
43      }
44      
45      /**
46       * Calculates a map with the field length of all of the attributes of the class given by the
47       * getBusinessObjectClass method
48       */
49      protected void initializeFieldLengthMap() {
50          fieldLengthMap = new HashMap<String, Integer>();
51          DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
52          List<AttributeDefinition> attributes = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(getBusinessObjectClass().getName()).getAttributes();
53  
54          for (AttributeDefinition attributeDefinition : attributes) {
55              Integer fieldLength;
56              fieldLength = dataDictionaryService.getAttributeMaxLength(getBusinessObjectClass(), attributeDefinition.getName());
57              fieldLengthMap.put(attributeDefinition.getName(), fieldLength);
58          }
59      }
60      
61      /**
62       * @return the class of the BusinessObject that this utility class will help parse from a String
63       */
64      public abstract Class<? extends BusinessObject> getBusinessObjectClass();
65  
66      /**
67       * @return a Map with business object field names as keys and starting positions of each field in the String as values
68       */
69      public Map<String, Integer> getFieldBeginningPositionMap() {
70          if (fieldBeginningPositionMap == null) {
71              initializeFieldBeginningPositionMap();
72          }
73          return fieldBeginningPositionMap;
74      }
75      
76      /**
77       * Calculates the beginning positions of each field in the array returned by getOrderedProperties, based on
78       * the length map calculated by getFieldLengthMap().
79       */
80      protected void initializeFieldBeginningPositionMap() {
81          fieldBeginningPositionMap = new HashMap<String, Integer>();
82          Map<String, Integer> lengthMap = getFieldLengthMap();
83          
84          int lengthTracker = 0;
85     
86          for (String property : getOrderedProperties()) {
87              fieldBeginningPositionMap.put(property, new Integer(lengthTracker));
88              if (LOG.isDebugEnabled()) {
89                  LOG.debug("Finding position for property: "+property+"; length = "+lengthMap.get(property));
90              }
91              lengthTracker += lengthMap.get(property).intValue();
92          }
93      }
94      
95      /**
96       * @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
97       */
98      public abstract String[] getOrderedProperties();
99  }