001: /*
002: * $Id: ModelFieldType.java,v 1.1 2003/08/16 22:05:48 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.entity.model;
025:
026: import java.util.*;
027: import org.w3c.dom.*;
028:
029: import org.ofbiz.base.util.*;
030:
031: /**
032: * Generic Entity - FieldType model class
033: *
034: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
035: * @version $Revision: 1.1 $
036: * @since 2.0
037: */
038: public class ModelFieldType {
039:
040: /** The type of the Field */
041: protected String type = null;
042:
043: /** The java-type of the Field */
044: protected String javaType = null;
045:
046: /** The sql-type of the Field */
047: protected String sqlType = null;
048:
049: /** The sql-type-alias of the Field, this is optional */
050: protected String sqlTypeAlias = null;
051:
052: /** validators to be called when an update is done */
053: protected List validators = new ArrayList();
054:
055: /** Default Constructor */
056: public ModelFieldType() {
057: }
058:
059: /** XML Constructor */
060: public ModelFieldType(Element fieldTypeElement) {
061: this .type = UtilXml.checkEmpty(fieldTypeElement
062: .getAttribute("type"));
063: this .javaType = UtilXml.checkEmpty(fieldTypeElement
064: .getAttribute("java-type"));
065: this .sqlType = UtilXml.checkEmpty(fieldTypeElement
066: .getAttribute("sql-type"));
067: this .sqlTypeAlias = UtilXml.checkEmpty(fieldTypeElement
068: .getAttribute("sql-type-alias"));
069:
070: NodeList validateList = fieldTypeElement
071: .getElementsByTagName("validate");
072:
073: for (int i = 0; i < validateList.getLength(); i++) {
074: Element element = (Element) validateList.item(i);
075:
076: this .validators.add(UtilXml.checkEmpty(element
077: .getAttribute("name")));
078: }
079: }
080:
081: /** The type of the Field */
082: public String getType() {
083: return this .type;
084: }
085:
086: /** The java-type of the Field */
087: public String getJavaType() {
088: return this .javaType;
089: }
090:
091: /** The sql-type of the Field */
092: public String getSqlType() {
093: return this .sqlType;
094: }
095:
096: /** The sql-type-alias of the Field */
097: public String getSqlTypeAlias() {
098: return this .sqlTypeAlias;
099: }
100:
101: /** validators to be called when an update is done */
102: public List getValidators() {
103: return this .validators;
104: }
105:
106: /** A simple function to derive the max length of a String created from the field value, based on the sql-type
107: * @return max length of a String representing the Field value
108: */
109: public int stringLength() {
110: if (sqlType.indexOf("VARCHAR") >= 0) {
111: if (sqlType.indexOf("(") > 0 && sqlType.indexOf(")") > 0) {
112: String length = sqlType.substring(
113: sqlType.indexOf("(") + 1, sqlType.indexOf(")"));
114:
115: return Integer.parseInt(length);
116: } else {
117: return 255;
118: }
119: } else if (sqlType.indexOf("CHAR") >= 0) {
120: if (sqlType.indexOf("(") > 0 && sqlType.indexOf(")") > 0) {
121: String length = sqlType.substring(
122: sqlType.indexOf("(") + 1, sqlType.indexOf(")"));
123:
124: return Integer.parseInt(length);
125: } else {
126: return 255;
127: }
128: } else if (sqlType.indexOf("TEXT") >= 0
129: || sqlType.indexOf("LONG") >= 0) {
130: return 5000;
131: }
132: return 20;
133: }
134: }
|