001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.gl.util;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.kuali.kfs.KFSPropertyConstants;
025:
026: /**
027: * This class converts field values from G/L Business Objects to G?L transactions
028: */
029: public class BusinessObjectFieldConverter {
030:
031: /**
032: * This method converts the field values from normal GL business objects to GL transaction
033: *
034: * @param fields list of fields in GL business object
035: * @return the list of fields for GL transaction
036: */
037: public static List convertToTransactionFields(List fields) {
038: List transactionFields = new ArrayList();
039:
040: Iterator propsIter = fields.iterator();
041: while (propsIter.hasNext()) {
042: String propertyName = (String) propsIter.next();
043:
044: // convert property name from normal BO to GL transaction
045: String transactionPropertyName = propertyName;
046:
047: Map propertyMappingTable = getPropertyMappingTable();
048: transactionPropertyName = convertPropertyName(
049: propertyMappingTable, propertyName);
050:
051: // create a new entry for current property
052: transactionFields.add(transactionPropertyName);
053: }
054: return transactionFields;
055: }
056:
057: /**
058: * This method converts the field values from normal GL business objects to GL transaction
059: *
060: * @param fieldValues the map of field values for normal GL business objects
061: * @return the map of field values for GL transaction
062: */
063: public static Map convertToTransactionFieldValues(Map fieldValues) {
064: Map transactionFieldValues = new HashMap();
065:
066: Iterator propsIter = fieldValues.keySet().iterator();
067: while (propsIter.hasNext()) {
068: String propertyName = (String) propsIter.next();
069: String propertyValue = (String) fieldValues
070: .get(propertyName);
071:
072: // convert property name from normal BO to GL transaction
073: String transactionPropertyName = propertyName;
074:
075: Map propertyMappingTable = getPropertyMappingTable();
076: transactionPropertyName = convertPropertyName(
077: propertyMappingTable, propertyName);
078:
079: // create a new entry for current property
080: transactionFieldValues.put(transactionPropertyName,
081: propertyValue);
082: }
083: return transactionFieldValues;
084: }
085:
086: /**
087: * This method converts the property name of a normal business object to GL transaction
088: *
089: * @param propertyName the property name of a normal business object
090: * @return the property name of GL transaction
091: */
092: public static String convertToTransactionPropertyName(
093: String propertyName) {
094: return convertPropertyName(getPropertyMappingTable(),
095: propertyName);
096: }
097:
098: /**
099: * This method converts the property name of a normal business object from GL transaction
100: *
101: * @param propertyName the property name of GL transaction
102: * @return the property name of a normal business object
103: */
104: public static String convertFromTransactionPropertyName(
105: String propertyName) {
106: return convertPropertyName(getSwappedPropertyMappingTable(),
107: propertyName);
108: }
109:
110: /**
111: * This method converts the field values from GL transaction to normal GL business objects
112: *
113: * @param fieldValues the map of field values for GL transaction
114: * @return the map of field values for normal GL business objects
115: */
116: public static Map convertFromTransactionFieldValues(Map fieldValues) {
117: Map boFieldValues = new HashMap();
118:
119: Iterator propsIter = fieldValues.keySet().iterator();
120: while (propsIter.hasNext()) {
121: String propertyName = (String) propsIter.next();
122: String propertyValue = (String) fieldValues
123: .get(propertyName);
124:
125: // convert property name from normal BO to GL transaction
126: String transactionPropertyName = propertyName;
127: Map propertyMappingTable = getSwappedPropertyMappingTable();
128: transactionPropertyName = convertPropertyName(
129: propertyMappingTable, propertyName);
130:
131: // create a new entry for current property
132: boFieldValues.put(transactionPropertyName, propertyValue);
133: }
134: return boFieldValues;
135: }
136:
137: /**
138: * This method defines a table that maps normal properties into transaction properties
139: *
140: * @return a property mapping table
141: */
142: private static Map getPropertyMappingTable() {
143: Map propertyMappingTable = new HashMap();
144:
145: propertyMappingTable.put(KFSPropertyConstants.OBJECT_CODE,
146: KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
147: propertyMappingTable.put(KFSPropertyConstants.SUB_OBJECT_CODE,
148: KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
149: propertyMappingTable.put(KFSPropertyConstants.OBJECT_TYPE_CODE,
150: KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
151:
152: propertyMappingTable.put(
153: KFSPropertyConstants.BALANCE_TYPE_CODE,
154: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
155: propertyMappingTable.put(
156: KFSPropertyConstants.DOCUMENT_TYPE_CODE,
157: KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE);
158: propertyMappingTable.put(KFSPropertyConstants.ORIGIN_CODE,
159: KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE);
160: propertyMappingTable.put(KFSPropertyConstants.DOCUMENT_NUMBER,
161: KFSPropertyConstants.DOCUMENT_NUMBER);
162:
163: return propertyMappingTable;
164: }
165:
166: /**
167: * This method defines a table that maps transaction properties into normal properties
168: *
169: * @return a property mapping table
170: */
171: private static Map getSwappedPropertyMappingTable() {
172: Map propertyMappingTable = getPropertyMappingTable();
173: Map swappedPropertyMappingTable = new HashMap();
174:
175: Iterator iterator = propertyMappingTable.keySet().iterator();
176: while (iterator.hasNext()) {
177: String propertyKey = (String) iterator.next();
178: String propertyValue = (String) propertyMappingTable
179: .get(propertyKey);
180:
181: if (propertyValue != null
182: && !swappedPropertyMappingTable
183: .containsKey(propertyValue)) {
184: swappedPropertyMappingTable.put(propertyValue,
185: propertyKey);
186: }
187: }
188: return swappedPropertyMappingTable;
189: }
190:
191: /**
192: * This method retrieves a name of the given property name from the given mapping table
193: *
194: * @param propertyMappingTable the property mapping table
195: * @param propertyName the property name of a normal business object
196: * @return the property name of GL transaction
197: */
198: private static String convertPropertyName(Map propertyMappingTable,
199: String propertyName) {
200:
201: String transactionPropertyName = propertyName;
202: if (propertyMappingTable.containsKey(propertyName)) {
203: transactionPropertyName = (String) propertyMappingTable
204: .get(propertyName);
205: }
206: return transactionPropertyName;
207: }
208:
209: /**
210: * Escapes any special characters in map name/property values
211: *
212: * @param fieldValues map of field keys and their values
213: * @param specialCharacter special characters to replace
214: * @param replacement value to replace special characters with
215: */
216: public static void escapeSpecialCharacter(Map fieldValues,
217: String specialCharacter, String replacement) {
218: Iterator propsIter = fieldValues.keySet().iterator();
219: while (propsIter.hasNext()) {
220: String propertyName = (String) propsIter.next();
221: String propertyValue = (String) fieldValues
222: .get(propertyName);
223:
224: String propertyValueAfterEscaped = propertyValue
225: .replaceAll(specialCharacter, replacement);
226: fieldValues.put(propertyName, propertyValueAfterEscaped);
227: }
228: }
229:
230: /**
231: * Escapes any single quotes in map name/property values
232: * @param fieldValues
233: */
234: public static void escapeSingleQuote(Map fieldValues) {
235: String specialCharacter = "'";
236: String replacement = " ";
237: escapeSpecialCharacter(fieldValues, specialCharacter,
238: replacement);
239: }
240: }
|