001: /*
002: * $Id: ModelField.java,v 1.4 2003/12/25 19:03:55 jonesde 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.entity.jdbc.*;
030: import org.ofbiz.base.util.*;
031:
032: /**
033: * Generic Entity - Field model class
034: *
035: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
036: * @version $Revision: 1.4 $
037: * @since 2.0
038: */
039: public class ModelField {
040:
041: /** The name of the Field */
042: protected String name = "";
043:
044: /** The type of the Field */
045: protected String type = "";
046:
047: /** The col-name of the Field */
048: protected String colName = "";
049:
050: /** boolean which specifies whether or not the Field is a Primary Key */
051: protected boolean isPk = false;
052:
053: protected boolean isAutoCreatedInternal = false;
054:
055: /** validators to be called when an update is done */
056: protected List validators = new ArrayList();
057:
058: /** Default Constructor */
059: public ModelField() {
060: }
061:
062: /** Fields Constructor */
063: public ModelField(String name, String type, String colName,
064: boolean isPk) {
065: this .name = name;
066: this .type = type;
067: this .setColName(colName);
068: this .isPk = isPk;
069: }
070:
071: /** XML Constructor */
072: public ModelField(Element fieldElement) {
073: this .type = UtilXml.checkEmpty(fieldElement
074: .getAttribute("type"));
075: this .name = UtilXml.checkEmpty(fieldElement
076: .getAttribute("name"));
077: this .setColName(UtilXml.checkEmpty(fieldElement
078: .getAttribute("col-name")));
079: this .isPk = false; // is set elsewhere
080:
081: NodeList validateList = fieldElement
082: .getElementsByTagName("validate");
083:
084: for (int i = 0; i < validateList.getLength(); i++) {
085: Element element = (Element) validateList.item(i);
086:
087: this .validators.add(UtilXml.checkEmpty(element
088: .getAttribute("name")));
089: }
090: }
091:
092: /** DB Names Constructor */
093: public ModelField(DatabaseUtil.ColumnCheckInfo ccInfo,
094: ModelFieldTypeReader modelFieldTypeReader) {
095: this .colName = ccInfo.columnName;
096: this .name = ModelUtil.dbNameToVarName(this .colName);
097:
098: // figure out the type according to the typeName, columnSize and decimalDigits
099: this .type = ModelUtil.induceFieldType(ccInfo.typeName,
100: ccInfo.columnSize, ccInfo.decimalDigits,
101: modelFieldTypeReader);
102:
103: // how do we find out if it is a primary key? for now, if not nullable, assume it is a pk
104: // this is a bad assumption, but since this output must be edited by hand later anyway, oh well
105: if ("NO".equals(ccInfo.isNullable)) {
106: this .isPk = true;
107: } else {
108: this .isPk = false;
109: }
110: }
111:
112: /** The name of the Field */
113: public String getName() {
114: return this .name;
115: }
116:
117: public void setName(String name) {
118: this .name = name;
119: }
120:
121: /** The type of the Field */
122: public String getType() {
123: return this .type;
124: }
125:
126: public void setType(String type) {
127: this .type = type;
128: }
129:
130: /** The col-name of the Field */
131: public String getColName() {
132: return this .colName;
133: }
134:
135: public void setColName(String colName) {
136: this .colName = colName;
137: if (this .colName == null || this .colName.length() == 0) {
138: this .colName = ModelUtil.javaNameToDbName(UtilXml
139: .checkEmpty(this .name));
140: }
141: }
142:
143: /** boolean which specifies whether or not the Field is a Primary Key */
144: public boolean getIsPk() {
145: return this .isPk;
146: }
147:
148: public void setIsPk(boolean isPk) {
149: this .isPk = isPk;
150: }
151:
152: public boolean getIsAutoCreatedInternal() {
153: return this .isAutoCreatedInternal;
154: }
155:
156: public void setIsAutoCreatedInternal(boolean isAutoCreatedInternal) {
157: this .isAutoCreatedInternal = isAutoCreatedInternal;
158: }
159:
160: /** validators to be called when an update is done */
161: public String getValidator(int index) {
162: return (String) this .validators.get(index);
163: }
164:
165: public int getValidatorsSize() {
166: return this .validators.size();
167: }
168:
169: public void addValidator(String validator) {
170: this .validators.add(validator);
171: }
172:
173: public String removeValidator(int index) {
174: return (String) this.validators.remove(index);
175: }
176: }
|