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.options;
17  
18  import java.text.ParseException;
19  import java.text.SimpleDateFormat;
20  import java.util.ArrayList;
21  import java.util.Date;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.gl.businessobject.OriginEntryFull;
27  import org.kuali.ole.sys.OLEPropertyConstants;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.rice.core.api.util.ConcreteKeyValue;
30  import org.kuali.rice.core.api.util.KeyValue;
31  import org.kuali.rice.core.api.util.type.KualiDecimal;
32  import org.kuali.rice.kns.service.DataDictionaryService;
33  import org.kuali.rice.krad.keyvalues.KeyValuesBase;
34  
35  /**
36   * An extension of KeyValuesBase that 
37   */
38  public class OriginEntryFieldFinder extends KeyValuesBase {
39  
40      /**
41       * Returns a list of all field names and display field names for the Origin Entry class
42       * @return a List of key/value pair options
43       * @see org.kuali.rice.kns.lookup.keyvalues.KeyValuesFinder#getKeyValues()
44       */
45      public List getKeyValues() {
46          List activeLabels = new ArrayList();
47          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, "Fiscal Year"));
48          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, "Chart Code"));
49          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.ACCOUNT_NUMBER, "Account Number"));
50          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, "Sub-Account Number"));
51          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FINANCIAL_OBJECT_CODE, "Object Code"));
52          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FINANCIAL_SUB_OBJECT_CODE, "Sub-Object Code"));
53          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, "Balance Type"));
54          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, "Object Type"));
55          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, "Fiscal Period"));
56          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE, "Document Type"));
57          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE, "Origin code"));
58          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.DOCUMENT_NUMBER, "Document Number"));
59          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER, "Sequence Number"));
60          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.TRANSACTION_LEDGER_ENTRY_DESC, "Description"));
61          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT, "Amount"));
62          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE, "Debit Credit Indicator"));
63          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.TRANSACTION_DATE, "Transaction Date"));
64          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.ORGANIZATION_DOCUMENT_NUMBER, "Org Doc Number"));
65          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.PROJECT_CODE, "Project Code"));
66          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.ORGANIZATION_REFERENCE_ID, "Org Ref ID"));
67          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.REFERENCE_FIN_DOCUMENT_TYPE_CODE, "Ref Doc Type"));
68          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FIN_SYSTEM_REF_ORIGINATION_CODE, "Ref Origin code"));
69          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FINANCIAL_DOCUMENT_REFERENCE_NBR, "Ref Doc Number"));
70          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE, "Reversal Date"));
71          activeLabels.add(new ConcreteKeyValue(OLEPropertyConstants.TRANSACTION_ENCUMBRANCE_UPDT_CD, "Enc Update Code"));
72          return activeLabels;
73      }
74  
75      /**
76       * Given the property field name for a field, returns the display name
77       * 
78       * @param fieldName the property field name for a field
79       * @return the display field name of that field
80       */
81      public String getFieldDisplayName(String fieldName) {
82          for (Iterator iter = getKeyValues().iterator(); iter.hasNext();) {
83              KeyValue klp = (KeyValue) iter.next();
84              if (klp.getKey().equals(fieldName)) {
85                  return klp.getValue();
86              }
87          }
88          return "Error";
89      }
90  
91      /**
92       * Given the display name of a field, returns the property field name
93       * 
94       * @param fieldDisplayName the display name of the field
95       * @return the property field name for that field
96       */
97      public String getFieldName(String fieldDisplayName) {
98          for (Iterator iter = getKeyValues().iterator(); iter.hasNext();) {
99              KeyValue klp = (KeyValue) iter.next();
100             if (klp.getValue().equals(fieldDisplayName)) {
101                 return (String) klp.getKey();
102             }
103         }
104         return "Error";
105     }
106 
107     /**
108      * Given a field name and a value, determines if that value is valid for the field
109      * 
110      * @param fieldName the name of a field to inquire on
111      * @param value the value that the field will potentially be set to
112      * @return true if the value is valid, false if otherwise
113      */
114     public boolean isValidValue(String fieldName, String value) {
115         if (StringUtils.isBlank(fieldName)) {
116             return false;
117         }
118         String fieldType = getFieldType(fieldName);
119         int fieldLength = getFieldLength(fieldName);
120 
121         if (allowNull(fieldName) && (value == null || value.length() == 0)) {
122             return true;
123         }
124         if (!allowNull(fieldName) && (value == null || value.length() == 0)) {
125             return false;
126         }
127         if (value.length() > fieldLength) {
128             return false;
129         }
130         if ("KualiDecimal".equals(fieldType)) {
131             try {
132                 KualiDecimal d = new KualiDecimal(value);
133                 return true;
134             }
135             catch (NumberFormatException nfe) {
136                 return false;
137             }
138         }
139         else if ("Integer".equals(fieldType)) {
140             try {
141                 Integer d = new Integer(value);
142                 return true;
143             }
144             catch (NumberFormatException nfe) {
145                 return false;
146             }
147         }
148         else if ("Date".equals(fieldType)) {
149             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
150             try {
151                 Date d = df.parse(value);
152                 return true;
153             }
154             catch (ParseException e) {
155                 return false;
156             }
157         }
158         return true;
159     }
160 
161     /**
162      * Returns a String with the name of the type of the given field
163      * 
164      * @param fieldName the name of the field to inquire on
165      * @return a String with the name of the class that field returns
166      */
167     public String getFieldType(String fieldName) {
168         if (fieldName.equals(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
169             return "Integer";
170         }
171         if (fieldName.equals(OLEPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER)) {
172             return "Integer";
173         }
174         if (fieldName.equals(OLEPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT)) {
175             return "KualiDecimal";
176         }
177         if (fieldName.equals(OLEPropertyConstants.TRANSACTION_DATE)) {
178             return "Date";
179         }
180         if (fieldName.equals(OLEPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE)) {
181             return "Date";
182         }
183         return "String";
184     }
185 
186     /**
187      * Returns whether the given field can be set to null or not
188      * 
189      * @param fieldName the name of the field to inquire about
190      * @return true if it can be set to null, false otherwise
191      */
192     public boolean allowNull(String fieldName) {
193         if (fieldName.equals(OLEPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT)) {
194             return false;
195         }
196         return true;
197     }
198 
199     /**
200      * Returns the length of a given field in Origin Entry
201      * 
202      * @param fieldName the name of the Origin Entry field to get a length for
203      * @return the length of the field
204      */
205     public int getFieldLength(String fieldName) {
206         DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
207         int fieldLength = 0;
208         fieldLength = dataDictionaryService.getAttributeMaxLength(OriginEntryFull.class, fieldName);
209         return fieldLength;
210     }
211     
212 }