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.web.optionfinder;
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.core.lookup.keyvalues.KeyValuesBase;
027: import org.kuali.core.util.KualiDecimal;
028: import org.kuali.core.web.ui.KeyLabelPair;
029: import org.kuali.kfs.KFSPropertyConstants;
030:
031: /**
032: * An extension of KeyValuesBase that
033: */
034: public class OriginEntryFieldFinder extends KeyValuesBase {
035:
036: /**
037: * Returns a list of all field names and display field names for the Origin Entry class
038: * @return a List of key/value pair options
039: * @see org.kuali.core.lookup.keyvalues.KeyValuesFinder#getKeyValues()
040: */
041: public List getKeyValues() {
042: List activeLabels = new ArrayList();
043: activeLabels.add(new KeyLabelPair("universityFiscalYear",
044: "Fiscal Year"));
045: activeLabels.add(new KeyLabelPair("chartOfAccountsCode",
046: "Chart Code"));
047: activeLabels.add(new KeyLabelPair("accountNumber",
048: "Account Number"));
049: activeLabels.add(new KeyLabelPair("subAccountNumber",
050: "Sub-Account Number"));
051: activeLabels.add(new KeyLabelPair("financialObjectCode",
052: "Object Code"));
053: activeLabels.add(new KeyLabelPair("financialSubObjectCode",
054: "Sub-Object Code"));
055: activeLabels.add(new KeyLabelPair("financialBalanceTypeCode",
056: "Balance Type"));
057: activeLabels.add(new KeyLabelPair("financialObjectTypeCode",
058: "Object Type"));
059: activeLabels.add(new KeyLabelPair("universityFiscalPeriodCode",
060: "Fiscal Period"));
061: activeLabels.add(new KeyLabelPair("financialDocumentTypeCode",
062: "Document Type"));
063: activeLabels.add(new KeyLabelPair(
064: "financialSystemOriginationCode", "Origin code"));
065: activeLabels
066: .add(new KeyLabelPair(
067: KFSPropertyConstants.DOCUMENT_NUMBER,
068: "Document Number"));
069: activeLabels.add(new KeyLabelPair(
070: "transactionLedgerEntrySequenceNumber",
071: "Sequence Number"));
072: activeLabels.add(new KeyLabelPair(
073: "transactionLedgerEntryDescription", "Description"));
074: activeLabels.add(new KeyLabelPair(
075: "transactionLedgerEntryAmount", "Amount"));
076: activeLabels.add(new KeyLabelPair("transactionDebitCreditCode",
077: "Debit Credit Indicator"));
078: activeLabels.add(new KeyLabelPair("transactionDate",
079: "Transaction Date"));
080: activeLabels.add(new KeyLabelPair("organizationDocumentNumber",
081: "Org Doc Number"));
082: activeLabels
083: .add(new KeyLabelPair("projectCode", "Project Code"));
084: activeLabels.add(new KeyLabelPair("organizationReferenceId",
085: "Org Ref ID"));
086: activeLabels.add(new KeyLabelPair(
087: "referenceFinancialDocumentTypeCode", "Ref Doc Type"));
088: activeLabels.add(new KeyLabelPair(
089: "referenceFinancialSystemOriginationCode",
090: "Ref Origin code"));
091: activeLabels.add(new KeyLabelPair(
092: "referenceFinancialDocumentNumber", "Ref Doc Number"));
093: activeLabels.add(new KeyLabelPair(
094: "financialDocumentReversalDate", "Reversal Date"));
095: activeLabels.add(new KeyLabelPair(
096: "transactionEncumbranceUpdateCode", "Enc Update Code"));
097: return activeLabels;
098: }
099:
100: /**
101: * Given the property field name for a field, returns the display name
102: *
103: * @param fieldName the property field name for a field
104: * @return the display field name of that field
105: */
106: public String getFieldDisplayName(String fieldName) {
107: for (Iterator iter = getKeyValues().iterator(); iter.hasNext();) {
108: KeyLabelPair klp = (KeyLabelPair) iter.next();
109: if (klp.getKey().equals(fieldName)) {
110: return klp.getLabel();
111: }
112: }
113: return "Error";
114: }
115:
116: /**
117: * Given the display name of a field, returns the property field name
118: *
119: * @param fieldDisplayName the display name of the field
120: * @return the property field name for that field
121: */
122: public String getFieldName(String fieldDisplayName) {
123: for (Iterator iter = getKeyValues().iterator(); iter.hasNext();) {
124: KeyLabelPair klp = (KeyLabelPair) iter.next();
125: if (klp.getLabel().equals(fieldDisplayName)) {
126: return (String) klp.getKey();
127: }
128: }
129: return "Error";
130: }
131:
132: /**
133: * Given a field name and a value, determines if that value is valid for the field
134: *
135: * @param fieldName the name of a field to inquire on
136: * @param value the value that the field will potentially be set to
137: * @return true if the value is valid, false if otherwise
138: */
139: public boolean isValidValue(String fieldName, String value) {
140: if (StringUtils.isBlank(fieldName)) {
141: return false;
142: }
143: String fieldType = getFieldType(fieldName);
144: int fieldLength = getFieldLength(fieldName);
145:
146: if (allowNull(fieldName)
147: && (value == null || value.length() == 0)) {
148: return true;
149: }
150: if (!allowNull(fieldName)
151: && (value == null || value.length() == 0)) {
152: return false;
153: }
154: if (value.length() > fieldLength) {
155: return false;
156: }
157: if ("KualiDecimal".equals(fieldType)) {
158: try {
159: KualiDecimal d = new KualiDecimal(value);
160: return true;
161: } catch (NumberFormatException nfe) {
162: return false;
163: }
164: } else if ("Integer".equals(fieldType)) {
165: try {
166: Integer d = new Integer(value);
167: return true;
168: } catch (NumberFormatException nfe) {
169: return false;
170: }
171: } else if ("Date".equals(fieldType)) {
172: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
173: try {
174: Date d = df.parse(value);
175: return true;
176: } catch (ParseException e) {
177: return false;
178: }
179: }
180: return true;
181: }
182:
183: /**
184: * Returns a String with the name of the type of the given field
185: *
186: * @param fieldName the name of the field to inquire on
187: * @return a String with the name of the class that field returns
188: */
189: public String getFieldType(String fieldName) {
190: if (fieldName.equals("universityFiscalYear")) {
191: return "Integer";
192: }
193: if (fieldName.equals("transactionLedgerEntrySequenceNumber")) {
194: return "Integer";
195: }
196: if (fieldName.equals("transactionLedgerEntryAmount")) {
197: return "KualiDecimal";
198: }
199: if (fieldName.equals("transactionDate")) {
200: return "Date";
201: }
202: if (fieldName.equals("financialDocumentReversalDate")) {
203: return "Date";
204: }
205: return "String";
206: }
207:
208: /**
209: * Returns whether the given field can be set to null or not
210: *
211: * @param fieldName the name of the field to inquire about
212: * @return true if it can be set to null, false otherwise
213: */
214: public boolean allowNull(String fieldName) {
215: if (fieldName.equals("transactionLedgerEntryAmount")) {
216: return false;
217: }
218: return true;
219: }
220:
221: /**
222: * Returns the length of a given field in Origin Entry
223: *
224: * @param fieldName the name of the Origin Entry field to get a length for
225: * @return the length of the field
226: */
227: public int getFieldLength(String fieldName) {
228: // TODO AUGH!!!!! BASE THIS ON THE DATA DICTIONARY!!!!!
229: if (fieldName.equals("universityFiscalYear")) {
230: return 4;
231: } else if (fieldName
232: .equals("transactionLedgerEntrySequenceNumber")) {
233: return 5;
234: } else if (fieldName.equals("transactionLedgerEntryAmount")) {
235: return 19;
236: } else if (fieldName.equals("transactionDate")) {
237: return 10;
238: } else if (fieldName.equals("financialDocumentReversalDate")) {
239: return 10;
240: } else if (fieldName.equals("chartOfAccountsCode")) {
241: return 2;
242: } else if (fieldName.equals("accountNumber")) {
243: return 7;
244: } else if (fieldName.equals("subAccountNumber")) {
245: return 5;
246: } else if (fieldName.equals("financialObjectCode")) {
247: return 4;
248: } else if (fieldName.equals("financialSubObjectCode")) {
249: return 3;
250: } else if (fieldName.equals("financialBalanceTypeCode")) {
251: return 2;
252: } else if (fieldName.equals("financialObjectTypeCode")) {
253: return 2;
254: } else if (fieldName.equals("universityFiscalPeriodCode")) {
255: return 2;
256: } else if (fieldName.equals("financialDocumentTypeCode")) {
257: return 4;
258: } else if (fieldName.equals("financialSystemOriginationCode")) {
259: return 2;
260: } else if (fieldName
261: .equals(KFSPropertyConstants.DOCUMENT_NUMBER)) {
262: return 14;
263: } else if (fieldName
264: .equals("transactionLedgerEntryDescription")) {
265: return 40;
266: } else if (fieldName.equals("transactionDebitCreditCode")) {
267: return 1;
268: } else if (fieldName.equals("organizationDocumentNumber")) {
269: return 10;
270: } else if (fieldName.equals("projectCode")) {
271: return 10;
272: } else if (fieldName.equals("organizationReferenceId")) {
273: return 8;
274: } else if (fieldName
275: .equals("referenceFinancialDocumentTypeCode")) {
276: return 4;
277: } else if (fieldName
278: .equals("referenceFinancialSystemOriginationCode")) {
279: return 2;
280: } else if (fieldName.equals("referenceFinancialDocumentNumber")) {
281: return 14;
282: } else if (fieldName.equals("transactionEncumbranceUpdateCode")) {
283: return 1;
284: }
285: return 0;
286: }
287: }
|