1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
37  
38  public class OriginEntryFieldFinder extends KeyValuesBase {
39  
40      
41  
42  
43  
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  
77  
78  
79  
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  
93  
94  
95  
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 
109 
110 
111 
112 
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 
163 
164 
165 
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 
188 
189 
190 
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 
201 
202 
203 
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 }