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.gl.businessobject.lookup;
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.sys.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(propertyMappingTable, propertyName);
049    
050                // create a new entry for current property
051                transactionFields.add(transactionPropertyName);
052            }
053            return transactionFields;
054        }
055    
056        /**
057         * This method converts the field values from normal GL business objects to GL transaction
058         * 
059         * @param fieldValues the map of field values for normal GL business objects
060         * @return the map of field values for GL transaction
061         */
062        public static Map convertToTransactionFieldValues(Map fieldValues) {
063            Map transactionFieldValues = new HashMap();
064    
065            Iterator propsIter = fieldValues.keySet().iterator();
066            while (propsIter.hasNext()) {
067                String propertyName = (String) propsIter.next();
068                String propertyValue = (String) fieldValues.get(propertyName);
069    
070                // convert property name from normal BO to GL transaction
071                String transactionPropertyName = propertyName;
072    
073                Map propertyMappingTable = getPropertyMappingTable();
074                transactionPropertyName = convertPropertyName(propertyMappingTable, propertyName);
075    
076                // create a new entry for current property
077                transactionFieldValues.put(transactionPropertyName, propertyValue);
078            }
079            return transactionFieldValues;
080        }
081    
082        /**
083         * This method converts the property name of a normal business object to GL transaction
084         * 
085         * @param propertyName the property name of a normal business object
086         * @return the property name of GL transaction
087         */
088        public static String convertToTransactionPropertyName(String propertyName) {
089            return convertPropertyName(getPropertyMappingTable(), propertyName);
090        }
091    
092        /**
093         * This method converts the property name of a normal business object from GL transaction
094         * 
095         * @param propertyName the property name of GL transaction
096         * @return the property name of a normal business object
097         */
098        public static String convertFromTransactionPropertyName(String propertyName) {
099            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(KFSPropertyConstants.OBJECT_CODE, KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
136            propertyMappingTable.put(KFSPropertyConstants.SUB_OBJECT_CODE, KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
137            propertyMappingTable.put(KFSPropertyConstants.OBJECT_TYPE_CODE, KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
138    
139            propertyMappingTable.put(KFSPropertyConstants.BALANCE_TYPE_CODE, KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
140            propertyMappingTable.put(KFSPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE);
141            propertyMappingTable.put(KFSPropertyConstants.ORIGIN_CODE, KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE);
142            propertyMappingTable.put(KFSPropertyConstants.DOCUMENT_NUMBER, KFSPropertyConstants.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    }