View Javadoc
1   /*
2    * Copyright 2006 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.gl.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.kuali.ole.sys.OLEPropertyConstants;
25  
26  /**
27   * This class converts field values from G/L Business Objects to G?L transactions
28   */
29  public class BusinessObjectFieldConverter {
30  
31      /**
32       * This method converts the field values from normal GL business objects to GL transaction
33       * 
34       * @param fields list of fields in GL business object
35       * @return the list of fields for GL transaction
36       */
37      public static List convertToTransactionFields(List fields) {
38          List transactionFields = new ArrayList();
39  
40          Iterator propsIter = fields.iterator();
41          while (propsIter.hasNext()) {
42              String propertyName = (String) propsIter.next();
43  
44              // convert property name from normal BO to GL transaction
45              String transactionPropertyName = propertyName;
46  
47              Map propertyMappingTable = getPropertyMappingTable();
48              transactionPropertyName = convertPropertyName(propertyMappingTable, propertyName);
49  
50              // create a new entry for current property
51              transactionFields.add(transactionPropertyName);
52          }
53          return transactionFields;
54      }
55  
56      /**
57       * This method converts the field values from normal GL business objects to GL transaction
58       * 
59       * @param fieldValues the map of field values for normal GL business objects
60       * @return the map of field values for GL transaction
61       */
62      public static Map convertToTransactionFieldValues(Map fieldValues) {
63          Map transactionFieldValues = new HashMap();
64  
65          Iterator propsIter = fieldValues.keySet().iterator();
66          while (propsIter.hasNext()) {
67              String propertyName = (String) propsIter.next();
68              String propertyValue = (String) fieldValues.get(propertyName);
69  
70              // convert property name from normal BO to GL transaction
71              String transactionPropertyName = propertyName;
72  
73              Map propertyMappingTable = getPropertyMappingTable();
74              transactionPropertyName = convertPropertyName(propertyMappingTable, propertyName);
75  
76              // create a new entry for current property
77              transactionFieldValues.put(transactionPropertyName, propertyValue);
78          }
79          return transactionFieldValues;
80      }
81  
82      /**
83       * This method converts the property name of a normal business object to GL transaction
84       * 
85       * @param propertyName the property name of a normal business object
86       * @return the property name of GL transaction
87       */
88      public static String convertToTransactionPropertyName(String propertyName) {
89          return convertPropertyName(getPropertyMappingTable(), propertyName);
90      }
91  
92      /**
93       * This method converts the property name of a normal business object from GL transaction
94       * 
95       * @param propertyName the property name of GL transaction
96       * @return the property name of a normal business object
97       */
98      public static String convertFromTransactionPropertyName(String propertyName) {
99          return convertPropertyName(getSwappedPropertyMappingTable(), propertyName);
100     }
101 
102     /**
103      * This method converts the field values from GL transaction to normal GL business objects
104      * 
105      * @param fieldValues the map of field values for GL transaction
106      * @return the map of field values for normal GL business objects
107      */
108     public static Map convertFromTransactionFieldValues(Map fieldValues) {
109         Map boFieldValues = new HashMap();
110 
111         Iterator propsIter = fieldValues.keySet().iterator();
112         while (propsIter.hasNext()) {
113             String propertyName = (String) propsIter.next();
114             String propertyValue = (String) fieldValues.get(propertyName);
115 
116             // convert property name from normal BO to GL transaction
117             String transactionPropertyName = propertyName;
118             Map propertyMappingTable = getSwappedPropertyMappingTable();
119             transactionPropertyName = convertPropertyName(propertyMappingTable, propertyName);
120 
121             // create a new entry for current property
122             boFieldValues.put(transactionPropertyName, propertyValue);
123         }
124         return boFieldValues;
125     }
126 
127     /**
128      * This method defines a table that maps normal properties into transaction properties
129      * 
130      * @return a property mapping table
131      */
132     private static Map getPropertyMappingTable() {
133         Map propertyMappingTable = new HashMap();
134 
135         propertyMappingTable.put(OLEPropertyConstants.OBJECT_CODE, OLEPropertyConstants.FINANCIAL_OBJECT_CODE);
136         propertyMappingTable.put(OLEPropertyConstants.SUB_OBJECT_CODE, OLEPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
137         propertyMappingTable.put(OLEPropertyConstants.OBJECT_TYPE_CODE, OLEPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
138 
139         propertyMappingTable.put(OLEPropertyConstants.BALANCE_TYPE_CODE, OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
140         propertyMappingTable.put(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, OLEPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE);
141         propertyMappingTable.put(OLEPropertyConstants.ORIGIN_CODE, OLEPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE);
142         propertyMappingTable.put(OLEPropertyConstants.DOCUMENT_NUMBER, OLEPropertyConstants.DOCUMENT_NUMBER);
143 
144         return propertyMappingTable;
145     }
146 
147     /**
148      * This method defines a table that maps transaction properties into normal properties
149      * 
150      * @return a property mapping table
151      */
152     private static Map getSwappedPropertyMappingTable() {
153         Map propertyMappingTable = getPropertyMappingTable();
154         Map swappedPropertyMappingTable = new HashMap();
155 
156         Iterator iterator = propertyMappingTable.keySet().iterator();
157         while (iterator.hasNext()) {
158             String propertyKey = (String) iterator.next();
159             String propertyValue = (String) propertyMappingTable.get(propertyKey);
160 
161             if (propertyValue != null && !swappedPropertyMappingTable.containsKey(propertyValue)) {
162                 swappedPropertyMappingTable.put(propertyValue, propertyKey);
163             }
164         }
165         return swappedPropertyMappingTable;
166     }
167 
168     /**
169      * This method retrieves a name of the given property name from the given mapping table
170      * 
171      * @param propertyMappingTable the property mapping table
172      * @param propertyName the property name of a normal business object
173      * @return the property name of GL transaction
174      */
175     private static String convertPropertyName(Map propertyMappingTable, String propertyName) {
176 
177         String transactionPropertyName = propertyName;
178         if (propertyMappingTable.containsKey(propertyName)) {
179             transactionPropertyName = (String) propertyMappingTable.get(propertyName);
180         }
181         return transactionPropertyName;
182     }
183 
184     /**
185      * Escapes any special characters in map name/property values
186      * 
187      * @param fieldValues map of field keys and their values
188      * @param specialCharacter special characters to replace
189      * @param replacement value to replace special characters with
190      */
191     public static void escapeSpecialCharacter(Map fieldValues, String specialCharacter, String replacement) {
192         Iterator propsIter = fieldValues.keySet().iterator();
193         while (propsIter.hasNext()) {
194             String propertyName = (String) propsIter.next();
195             String propertyValue = (String) fieldValues.get(propertyName);
196 
197             String propertyValueAfterEscaped = propertyValue.replaceAll(specialCharacter, replacement);
198             fieldValues.put(propertyName, propertyValueAfterEscaped);
199         }
200     }
201 
202     /**
203      * Escapes any single quotes in map name/property values
204      * @param fieldValues
205      */
206     public static void escapeSingleQuote(Map fieldValues) {
207         String specialCharacter = "'";
208         String replacement = " ";
209         escapeSpecialCharacter(fieldValues, specialCharacter, replacement);
210     }
211 }