001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.jaggenerator.validation;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.StringTokenizer;
024:
025: /**
026: * Represents Struts 1.1 validations for a particular database column, ultimately destined to be
027: * included in the 'validation.xml' declarative validations configuration file.
028: * <p>
029: * This class represents a Struts validation as two parts: a 'depends' list and an XML fragment:
030: * the 'depends list' is a comma-seperated list of validation methods (e.g. 'integer', 'date', 'mask'),
031: * and the XML fragment contains any parameters needed by those validation methods.
032: *
033: * @author Michael O'Connor - Finalist IT Group
034: */
035: public class StrutsValidation {
036:
037: private String dependsList = "";
038: private String xml = "";
039:
040: /**
041: * Creates a StrutsValidation without generating the validations.
042: */
043: public StrutsValidation() {
044: }
045:
046: /**
047: *
048: * @param sqlType the column type (SQL data type, e.g. NUMBER(12)).
049: * @param jdbcType the JDBC data type.
050: * @param required whether or not the field is mandatory.
051: */
052: public StrutsValidation(String sqlType, String jdbcType,
053: boolean required) {
054: ArrayList validationsList = new ArrayList(3);
055: StringBuffer validations = new StringBuffer();
056: List parameters = getParams(sqlType);
057:
058: if (required) {
059: validationsList.add("required");
060: }
061: if (sqlType.startsWith("NUMBER") || sqlType.equals("MEDIUMINT")) {
062: if ("INTEGER".equals(jdbcType)) {
063: validationsList.add("integer");
064: } else {
065: validationsList.add("float");
066:
067: if (parameters.size() == 2) {
068: validationsList.add("decimal");
069: generateDecimalMaskXML(Integer
070: .parseInt((String) parameters.get(0)),
071: Integer
072: .parseInt((String) parameters
073: .get(1)));
074: } else if (!parameters.isEmpty()) {
075: validationsList.add("maxlength");
076: generateMaxlengthXML(Integer
077: .parseInt((String) parameters.get(0)));
078: }
079: }
080: }
081: if (sqlType.startsWith("INT")) {
082: validationsList.add("integer");
083: if (!parameters.isEmpty()) {
084: validationsList.add("maxlength");
085: generateMaxlengthXML(Integer
086: .parseInt((String) parameters.get(0)));
087: }
088: }
089: if (sqlType.indexOf("CHAR") != -1 && !parameters.isEmpty()) {
090: validationsList.add("maxlength");
091: generateMaxlengthXML(Integer.parseInt((String) parameters
092: .get(0)));
093: }
094: if (sqlType.indexOf("DATE") != -1) {
095: validationsList.add("date");
096: generateDateXML();
097: }
098: if (sqlType.indexOf("BIGINT") != -1) {
099: validationsList.add("long");
100: if (!parameters.isEmpty()) {
101: validationsList.add("maxlength");
102: generateMaxlengthXML(Integer
103: .parseInt((String) parameters.get(0)));
104: }
105: }
106: if (sqlType.indexOf("SMALLINT") != -1) {
107: validationsList.add("short");
108: if (!parameters.isEmpty()) {
109: validationsList.add("maxlength");
110: generateMaxlengthXML(Integer
111: .parseInt((String) parameters.get(0)));
112: }
113: }
114: if (sqlType.indexOf("TINYINT") != -1) {
115: validationsList.add("byte");
116: if (!parameters.isEmpty()) {
117: validationsList.add("maxlength");
118: generateMaxlengthXML(Integer
119: .parseInt((String) parameters.get(0)));
120: }
121: }
122: if (sqlType.indexOf("FLOAT") != -1) {
123: validationsList.add("float");
124: }
125:
126: Iterator v = validationsList.iterator();
127: while (v.hasNext()) {
128: String item = (String) v.next();
129: validations.append(item);
130: if (v.hasNext()) {
131: validations.append(", ");
132: }
133: }
134: dependsList = validations.toString();
135:
136: }
137:
138: /** @see {@link #getDependsList()}. */
139: public void setDependsList(String dependsList) {
140: this .dependsList = dependsList;
141: }
142:
143: /** @see {@link #getXml()}. */
144: public void setXml(String xml) {
145: this .xml = xml;
146: }
147:
148: /**
149: * Gets the comma-seperated list of Struts validators to be applied to this field.
150: * @return the dependsList.
151: */
152: public String getDependsList() {
153: return dependsList;
154: }
155:
156: /**
157: * Gets the corresponding XML fragment that complements the dependsList.
158: * @return the xml fragment.
159: */
160: public String getXml() {
161: return xml;
162: }
163:
164: private void generateDateXML() {
165: StringBuffer temp = new StringBuffer(xml);
166: temp.append("<var>\n");
167: temp.append(" <var-name>datePattern</var-name>\n");
168: temp.append(" <var-value>${date_format}</var-value>\n");
169: temp.append("</var>");
170: setXml(temp.toString());
171: }
172:
173: private void generateTimestampXML() {
174: StringBuffer temp = new StringBuffer(xml);
175: temp.append("<var>\n");
176: temp.append(" <var-name>datePattern</var-name>\n");
177: temp.append(" <var-value>${timestamp_format}</var-value>\n");
178: temp.append("</var>");
179: setXml(temp.toString());
180: }
181:
182: private void generateMaxlengthXML(int maxLength) {
183: StringBuffer temp = new StringBuffer(xml);
184: temp
185: .append("<arg1 key=\"${var:maxlength}\" name=\"maxlength\" resource=\"false\"/>\n");
186: temp.append("<var>\n");
187: temp.append(" <var-name>maxlength</var-name>\n");
188: temp.append(" <var-value>").append(maxLength).append(
189: "</var-value>\n");
190: temp.append("</var>");
191: setXml(temp.toString());
192: }
193:
194: private void generateDecimalMaskXML(int length, int precision) {
195: StringBuffer temp = new StringBuffer(xml);
196: temp
197: .append("<arg1 key=\"${var:decimalPrecision}\" name=\"decimal\" resource=\"false\"/>");
198: temp
199: .append("<arg2 key=\"${var:decimalLength}\" name=\"decimal\" resource=\"false\"/>");
200: temp.append("<var>\n");
201: temp.append(" <var-name>decimalLength</var-name>\n");
202: temp.append(" <var-value>").append(length).append(
203: "</var-value>\n");
204: temp.append("</var>");
205: temp.append("<var>\n");
206: temp.append(" <var-name>decimalPrecision</var-name>\n");
207: temp.append(" <var-value>").append(precision).append(
208: "</var-value>\n");
209: temp.append("</var>");
210: setXml(temp.toString());
211: }
212:
213: public static List getParams(String sqlType) {
214: String trimmed = sqlType.trim();
215: ArrayList params = new ArrayList();
216: int openBracketPos = trimmed.indexOf('(');
217: int closeBracketPos = trimmed.indexOf(')');
218: if (openBracketPos != -1
219: && closeBracketPos == (trimmed.length() - 1)) {
220: StringTokenizer tokie = new StringTokenizer(trimmed
221: .substring(openBracketPos + 1, closeBracketPos),
222: ",");
223: while (tokie.hasMoreTokens()) {
224: params.add(tokie.nextToken().trim());
225: }
226: }
227:
228: return params;
229: }
230:
231: }
|