View Javadoc

1   /*   Copyright (C) 2003 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.jaggenerator.validation;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.StringTokenizer;
24  
25  /***
26   * Represents Struts 1.1 validations for a particular database column, ultimately destined to be
27   * included in the 'validation.xml' declarative validations configuration file.
28   * <p>
29   * This class represents a Struts validation as two parts: a 'depends' list and an XML fragment:
30   * the 'depends list' is a comma-seperated list of validation methods (e.g. 'integer', 'date', 'mask'),
31   * and the XML fragment contains any parameters needed by those validation methods.
32   *
33   * @author Michael O'Connor - Finalist IT Group
34   */
35  public class StrutsValidation {
36  
37     private String dependsList = "";
38     private String xml = "";
39  
40  
41     /***
42      * Creates a StrutsValidation without generating the validations.
43      */
44     public StrutsValidation() {
45     }
46  
47     /***
48      *
49      * @param sqlType the column type (SQL data type, e.g. NUMBER(12)).
50      * @param jdbcType the JDBC data type.
51      * @param required whether or not the field is mandatory.
52      */
53     public StrutsValidation(String sqlType, String jdbcType, boolean required) {
54        ArrayList validationsList = new ArrayList(3);
55        StringBuffer validations = new StringBuffer();
56        List parameters = getParams(sqlType);
57  
58        if (required) {
59           validationsList.add("required");
60        }
61        if (sqlType.startsWith("NUMBER") || sqlType.equals("MEDIUMINT")) {
62           if ("INTEGER".equals(jdbcType)) {
63              validationsList.add("integer");
64           } else {
65              validationsList.add("float");
66  
67              if (parameters.size() == 2) {
68                 validationsList.add("decimal");
69                 generateDecimalMaskXML(Integer.parseInt((String) parameters.get(0)),
70                       Integer.parseInt((String) parameters.get(1)));
71              }
72              else if (!parameters.isEmpty()) {
73                 validationsList.add("maxlength");
74                 generateMaxlengthXML(Integer.parseInt((String) parameters.get(0)));
75              }
76           }
77        }
78        if (sqlType.startsWith("INT")) {
79           validationsList.add("integer");
80           if (!parameters.isEmpty()) {
81                 validationsList.add("maxlength");
82                 generateMaxlengthXML(Integer.parseInt((String) parameters.get(0)));
83           }
84        }
85        if (sqlType.indexOf("CHAR") != -1 && !parameters.isEmpty()) {
86           validationsList.add("maxlength");
87           generateMaxlengthXML(Integer.parseInt((String) parameters.get(0)));
88        }
89        if (sqlType.indexOf("DATE") != -1 || sqlType.indexOf("TIMESTAMP") != -1) {
90           validationsList.add("date");
91           if ("TIMESTAMP".equals(jdbcType)) {
92              generateTimestampXML();
93           } else {
94              generateDateXML();
95           }
96        }
97        if (sqlType.indexOf("BIGINT") != -1) {
98           validationsList.add("long");
99           if (!parameters.isEmpty()) {
100                validationsList.add("maxlength");
101                generateMaxlengthXML(Integer.parseInt((String) parameters.get(0)));
102          }
103       }
104       if (sqlType.indexOf("SMALLINT") != -1) {
105          validationsList.add("short");
106          if (!parameters.isEmpty()) {
107                validationsList.add("maxlength");
108                generateMaxlengthXML(Integer.parseInt((String) parameters.get(0)));
109          }
110       }
111       if (sqlType.indexOf("TINYINT") != -1) {
112          validationsList.add("byte");
113          if (!parameters.isEmpty()) {
114                validationsList.add("maxlength");
115                generateMaxlengthXML(Integer.parseInt((String) parameters.get(0)));
116          }
117       }
118       if (sqlType.indexOf("FLOAT") != -1) {
119          validationsList.add("float");
120       }
121 
122       Iterator v = validationsList.iterator();
123       while (v.hasNext()) {
124          String item = (String) v.next();
125          validations.append(item);
126          if (v.hasNext()) {
127             validations.append(", ");
128          }
129       }
130       dependsList = validations.toString();
131 
132    }
133 
134 
135    /*** @see {@link #getDependsList()}. */
136    public void setDependsList(String dependsList) {
137       this.dependsList = dependsList;
138    }
139 
140    /*** @see {@link #getXml()}. */
141    public void setXml(String xml) {
142       this.xml = xml;
143    }
144 
145    /***
146     * Gets the comma-seperated list of Struts validators to be applied to this field.
147     * @return the dependsList.
148     */
149    public String getDependsList() {
150       return dependsList;
151    }
152 
153    /***
154     * Gets the corresponding XML fragment that complements the dependsList.
155     * @return the xml fragment.
156     */
157    public String getXml() {
158       return xml;
159    }
160 
161 
162    private void generateDateXML() {
163       StringBuffer temp = new StringBuffer(xml);
164       temp.append("<var>\n");
165       temp.append("   <var-name>datePattern</var-name>\n");
166       temp.append("   <var-value>${date_format}</var-value>\n");
167       temp.append("</var>");
168       setXml(temp.toString());
169    }
170 
171    private void generateTimestampXML() {
172       StringBuffer temp = new StringBuffer(xml);
173       temp.append("<var>\n");
174       temp.append("   <var-name>datePattern</var-name>\n");
175       temp.append("   <var-value>${timestamp_format}</var-value>\n");
176       temp.append("</var>");
177       setXml(temp.toString());   }
178 
179    private void generateMaxlengthXML(int maxLength) {
180       StringBuffer temp = new StringBuffer(xml);
181       temp.append("<arg1 key=\"${var:maxlength}\" name=\"maxlength\" resource=\"false\"/>\n");
182       temp.append("<var>\n");
183       temp.append("   <var-name>maxlength</var-name>\n");
184       temp.append("   <var-value>").append(maxLength).append("</var-value>\n");
185       temp.append("</var>");
186       setXml(temp.toString());
187    }
188 
189    private void generateDecimalMaskXML(int length, int precision) {
190       StringBuffer temp = new StringBuffer(xml);
191       temp.append("<arg1 key=\"${var:decimalPrecision}\" name=\"decimal\" resource=\"false\"/>");
192       temp.append("<arg2 key=\"${var:decimalLength}\" name=\"decimal\" resource=\"false\"/>");
193       temp.append("<var>\n");
194       temp.append("   <var-name>decimalLength</var-name>\n");
195       temp.append("   <var-value>").append(length).append("</var-value>\n");
196       temp.append("</var>");
197       temp.append("<var>\n");
198       temp.append("   <var-name>decimalPrecision</var-name>\n");
199       temp.append("   <var-value>").append(precision).append("</var-value>\n");
200       temp.append("</var>");
201       setXml(temp.toString());
202    }
203 
204    public static List getParams(String sqlType) {
205       String trimmed = sqlType.trim();
206       ArrayList params = new ArrayList();
207       int openBracketPos = trimmed.indexOf('(');
208       int closeBracketPos = trimmed.indexOf(')');
209       if (openBracketPos != -1 && closeBracketPos == (trimmed.length() - 1)) {
210          StringTokenizer tokie = new StringTokenizer(trimmed.substring(openBracketPos + 1, closeBracketPos), ",");
211          while (tokie.hasMoreTokens()) {
212             params.add(tokie.nextToken().trim());
213          }
214       }
215 
216       return params;
217    }
218 
219 }