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.options;
017    
018    import java.text.ParseException;
019    import java.text.SimpleDateFormat;
020    import java.util.ArrayList;
021    import java.util.Date;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import org.apache.commons.lang.StringUtils;
026    import org.kuali.kfs.gl.businessobject.OriginEntryFull;
027    import org.kuali.kfs.sys.KFSPropertyConstants;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.rice.core.util.KeyLabelPair;
030    import org.kuali.rice.kns.lookup.keyvalues.KeyValuesBase;
031    import org.kuali.rice.kns.service.DataDictionaryService;
032    import org.kuali.rice.kns.util.KualiDecimal;
033    
034    /**
035     * An extension of KeyValuesBase that 
036     */
037    public class OriginEntryFieldFinder extends KeyValuesBase {
038    
039        /**
040         * Returns a list of all field names and display field names for the Origin Entry class
041         * @return a List of key/value pair options
042         * @see org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder#getKeyValues()
043         */
044        public List getKeyValues() {
045            List activeLabels = new ArrayList();
046            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, "Fiscal Year"));
047            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, "Chart Code"));
048            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.ACCOUNT_NUMBER, "Account Number"));
049            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, "Sub-Account Number"));
050            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FINANCIAL_OBJECT_CODE, "Object Code"));
051            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE, "Sub-Object Code"));
052            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, "Balance Type"));
053            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, "Object Type"));
054            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, "Fiscal Period"));
055            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, "Document Type"));
056            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE, "Origin code"));
057            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.DOCUMENT_NUMBER, "Document Number"));
058            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER, "Sequence Number"));
059            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_DESC, "Description"));
060            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT, "Amount"));
061            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE, "Debit Credit Indicator"));
062            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.TRANSACTION_DATE, "Transaction Date"));
063            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.ORGANIZATION_DOCUMENT_NUMBER, "Org Doc Number"));
064            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.PROJECT_CODE, "Project Code"));
065            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.ORGANIZATION_REFERENCE_ID, "Org Ref ID"));
066            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.REFERENCE_FIN_DOCUMENT_TYPE_CODE, "Ref Doc Type"));
067            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FIN_SYSTEM_REF_ORIGINATION_CODE, "Ref Origin code"));
068            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FINANCIAL_DOCUMENT_REFERENCE_NBR, "Ref Doc Number"));
069            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE, "Reversal Date"));
070            activeLabels.add(new KeyLabelPair(KFSPropertyConstants.TRANSACTION_ENCUMBRANCE_UPDT_CD, "Enc Update Code"));
071            return activeLabels;
072        }
073    
074        /**
075         * Given the property field name for a field, returns the display name
076         * 
077         * @param fieldName the property field name for a field
078         * @return the display field name of that field
079         */
080        public String getFieldDisplayName(String fieldName) {
081            for (Iterator iter = getKeyValues().iterator(); iter.hasNext();) {
082                KeyLabelPair klp = (KeyLabelPair) iter.next();
083                if (klp.getKey().equals(fieldName)) {
084                    return klp.getLabel();
085                }
086            }
087            return "Error";
088        }
089    
090        /**
091         * Given the display name of a field, returns the property field name
092         * 
093         * @param fieldDisplayName the display name of the field
094         * @return the property field name for that field
095         */
096        public String getFieldName(String fieldDisplayName) {
097            for (Iterator iter = getKeyValues().iterator(); iter.hasNext();) {
098                KeyLabelPair klp = (KeyLabelPair) iter.next();
099                if (klp.getLabel().equals(fieldDisplayName)) {
100                    return (String) klp.getKey();
101                }
102            }
103            return "Error";
104        }
105    
106        /**
107         * Given a field name and a value, determines if that value is valid for the field
108         * 
109         * @param fieldName the name of a field to inquire on
110         * @param value the value that the field will potentially be set to
111         * @return true if the value is valid, false if otherwise
112         */
113        public boolean isValidValue(String fieldName, String value) {
114            if (StringUtils.isBlank(fieldName)) {
115                return false;
116            }
117            String fieldType = getFieldType(fieldName);
118            int fieldLength = getFieldLength(fieldName);
119    
120            if (allowNull(fieldName) && (value == null || value.length() == 0)) {
121                return true;
122            }
123            if (!allowNull(fieldName) && (value == null || value.length() == 0)) {
124                return false;
125            }
126            if (value.length() > fieldLength) {
127                return false;
128            }
129            if ("KualiDecimal".equals(fieldType)) {
130                try {
131                    KualiDecimal d = new KualiDecimal(value);
132                    return true;
133                }
134                catch (NumberFormatException nfe) {
135                    return false;
136                }
137            }
138            else if ("Integer".equals(fieldType)) {
139                try {
140                    Integer d = new Integer(value);
141                    return true;
142                }
143                catch (NumberFormatException nfe) {
144                    return false;
145                }
146            }
147            else if ("Date".equals(fieldType)) {
148                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
149                try {
150                    Date d = df.parse(value);
151                    return true;
152                }
153                catch (ParseException e) {
154                    return false;
155                }
156            }
157            return true;
158        }
159    
160        /**
161         * Returns a String with the name of the type of the given field
162         * 
163         * @param fieldName the name of the field to inquire on
164         * @return a String with the name of the class that field returns
165         */
166        public String getFieldType(String fieldName) {
167            if (fieldName.equals(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
168                return "Integer";
169            }
170            if (fieldName.equals(KFSPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER)) {
171                return "Integer";
172            }
173            if (fieldName.equals(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT)) {
174                return "KualiDecimal";
175            }
176            if (fieldName.equals(KFSPropertyConstants.TRANSACTION_DATE)) {
177                return "Date";
178            }
179            if (fieldName.equals(KFSPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE)) {
180                return "Date";
181            }
182            return "String";
183        }
184    
185        /**
186         * Returns whether the given field can be set to null or not
187         * 
188         * @param fieldName the name of the field to inquire about
189         * @return true if it can be set to null, false otherwise
190         */
191        public boolean allowNull(String fieldName) {
192            if (fieldName.equals(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT)) {
193                return false;
194            }
195            return true;
196        }
197    
198        /**
199         * Returns the length of a given field in Origin Entry
200         * 
201         * @param fieldName the name of the Origin Entry field to get a length for
202         * @return the length of the field
203         */
204        public int getFieldLength(String fieldName) {
205            DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
206            int fieldLength = 0;
207            fieldLength = dataDictionaryService.getAttributeMaxLength(OriginEntryFull.class, fieldName);
208            return fieldLength;
209        }
210        
211    }