KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > model > ModelFieldType


1 /*
2  * $Id: ModelFieldType.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.entity.model;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.*;
28 import org.w3c.dom.*;
29
30 import org.ofbiz.base.util.*;
31
32 /**
33  * Generic Entity - FieldType model class
34  *
35  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
36  * @version $Rev: 5720 $
37  * @since 2.0
38  */

39 public class ModelFieldType implements Serializable JavaDoc {
40
41     /** The type of the Field */
42     protected String JavaDoc type = null;
43
44     /** The java-type of the Field */
45     protected String JavaDoc javaType = null;
46
47     /** The sql-type of the Field */
48     protected String JavaDoc sqlType = null;
49
50     /** The sql-type-alias of the Field, this is optional */
51     protected String JavaDoc sqlTypeAlias = null;
52
53     /** validators to be called when an update is done */
54     protected List validators = new ArrayList();
55
56     /** Default Constructor */
57     public ModelFieldType() {}
58
59     /** XML Constructor */
60     public ModelFieldType(Element fieldTypeElement) {
61         this.type = UtilXml.checkEmpty(fieldTypeElement.getAttribute("type"));
62         this.javaType = UtilXml.checkEmpty(fieldTypeElement.getAttribute("java-type"));
63         this.sqlType = UtilXml.checkEmpty(fieldTypeElement.getAttribute("sql-type"));
64         this.sqlTypeAlias = UtilXml.checkEmpty(fieldTypeElement.getAttribute("sql-type-alias"));
65
66         NodeList validateList = fieldTypeElement.getElementsByTagName("validate");
67         for (int i = 0; i < validateList.getLength(); i++) {
68             Element element = (Element) validateList.item(i);
69             String JavaDoc methodName = element.getAttribute("method");
70             String JavaDoc className = element.getAttribute("class");
71             if (methodName != null) {
72                 this.validators.add(new ModelFieldValidator(className, methodName));
73             }
74         }
75     }
76
77     /** The type of the Field */
78     public String JavaDoc getType() {
79         return this.type;
80     }
81
82     /** The java-type of the Field */
83     public String JavaDoc getJavaType() {
84         return this.javaType;
85     }
86
87     /** The sql-type of the Field */
88     public String JavaDoc getSqlType() {
89         return this.sqlType;
90     }
91
92     /** The sql-type-alias of the Field */
93     public String JavaDoc getSqlTypeAlias() {
94         return this.sqlTypeAlias;
95     }
96
97     /** validators to be called when an update is done */
98     public List getValidators() {
99         return this.validators;
100     }
101
102     /** A simple function to derive the max length of a String created from the field value, based on the sql-type
103      * @return max length of a String representing the Field value
104      */

105     public int stringLength() {
106         if (sqlType.indexOf("VARCHAR") >= 0) {
107             if (sqlType.indexOf("(") > 0 && sqlType.indexOf(")") > 0) {
108                 String JavaDoc length = sqlType.substring(sqlType.indexOf("(") + 1, sqlType.indexOf(")"));
109
110                 return Integer.parseInt(length);
111             } else {
112                 return 255;
113             }
114         } else if (sqlType.indexOf("CHAR") >= 0) {
115             if (sqlType.indexOf("(") > 0 && sqlType.indexOf(")") > 0) {
116                 String JavaDoc length = sqlType.substring(sqlType.indexOf("(") + 1, sqlType.indexOf(")"));
117
118                 return Integer.parseInt(length);
119             } else {
120                 return 255;
121             }
122         } else if (sqlType.indexOf("TEXT") >= 0 || sqlType.indexOf("LONG") >= 0) {
123             return 5000;
124         }
125         return 20;
126     }
127
128     class ModelFieldValidator implements Serializable JavaDoc {
129
130         protected String JavaDoc validatorClass = null;
131         protected String JavaDoc validatorMethod = null;
132
133         public ModelFieldValidator(String JavaDoc className, String JavaDoc methodName) {
134             this.validatorClass = className;
135             this.validatorMethod = methodName;
136         }
137
138         public String JavaDoc getClassName() {
139             if (UtilValidate.isNotEmpty(validatorClass) && UtilValidate.isNotEmpty(validatorMethod)) {
140                 return validatorClass;
141             }
142             return null;
143         }
144
145         public String JavaDoc getMethodName() {
146             if (UtilValidate.isNotEmpty(validatorClass) && UtilValidate.isNotEmpty(validatorMethod)) {
147                 return validatorMethod;
148             }
149             return null;
150         }
151     }
152 }
153
Popular Tags