KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ModelField.java 6327 2005-12-14 18:56:54Z jaz $
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.util.*;
27 import org.w3c.dom.*;
28
29 import org.ofbiz.entity.jdbc.*;
30 import org.ofbiz.base.util.*;
31
32 /**
33  * Generic Entity - Field model class
34  *
35  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
36  * @version $Rev: 6327 $
37  * @since 2.0
38  */

39 public class ModelField extends ModelChild {
40
41     /** The name of the Field */
42     protected String JavaDoc name = "";
43
44     /** The type of the Field */
45     protected String JavaDoc type = "";
46
47     /** The col-name of the Field */
48     protected String JavaDoc colName = "";
49
50     /** boolean which specifies whether or not the Field is a Primary Key */
51     protected boolean isPk = false;
52     protected boolean encrypt = false;
53     protected boolean isAutoCreatedInternal = false;
54     
55     /** validators to be called when an update is done */
56     protected List validators = new ArrayList();
57
58     /** Default Constructor */
59     public ModelField() {}
60
61     /** Fields Constructor */
62     public ModelField(String JavaDoc name, String JavaDoc type, String JavaDoc colName, boolean isPk) {
63         this(name, type, colName, isPk, false);
64     }
65
66     public ModelField(String JavaDoc name, String JavaDoc type, String JavaDoc colName, boolean isPk, boolean encrypt) {
67         this.name = name;
68         this.type = type;
69         this.setColName(colName);
70         this.isPk = isPk;
71         this.encrypt = encrypt;
72     }
73
74     /** XML Constructor */
75     public ModelField(Element fieldElement) {
76         this.type = UtilXml.checkEmpty(fieldElement.getAttribute("type"));
77         this.name = UtilXml.checkEmpty(fieldElement.getAttribute("name"));
78         this.setColName(UtilXml.checkEmpty(fieldElement.getAttribute("col-name")));
79         this.isPk = false; // is set elsewhere
80
this.encrypt = UtilXml.checkBoolean(fieldElement.getAttribute("encrypt"), false);
81
82         NodeList validateList = fieldElement.getElementsByTagName("validate");
83
84         for (int i = 0; i < validateList.getLength(); i++) {
85             Element element = (Element) validateList.item(i);
86
87             this.validators.add(UtilXml.checkEmpty(element.getAttribute("name")));
88         }
89     }
90
91     /** DB Names Constructor */
92     public ModelField(DatabaseUtil.ColumnCheckInfo ccInfo, ModelFieldTypeReader modelFieldTypeReader) {
93         this.colName = ccInfo.columnName;
94         this.name = ModelUtil.dbNameToVarName(this.colName);
95
96         // figure out the type according to the typeName, columnSize and decimalDigits
97
this.type = ModelUtil.induceFieldType(ccInfo.typeName, ccInfo.columnSize, ccInfo.decimalDigits, modelFieldTypeReader);
98
99         this.isPk = ccInfo.isPk;
100     }
101
102     /** The name of the Field */
103     public String JavaDoc getName() {
104         return this.name;
105     }
106
107     public void setName(String JavaDoc name) {
108         this.name = name;
109     }
110
111     /** The type of the Field */
112     public String JavaDoc getType() {
113         return this.type;
114     }
115
116     public void setType(String JavaDoc type) {
117         this.type = type;
118     }
119
120     /** The col-name of the Field */
121     public String JavaDoc getColName() {
122         return this.colName;
123     }
124
125     public void setColName(String JavaDoc colName) {
126         this.colName = colName;
127         if (this.colName == null || this.colName.length() == 0) {
128             this.colName = ModelUtil.javaNameToDbName(UtilXml.checkEmpty(this.name));
129         }
130     }
131
132     /** boolean which specifies whether or not the Field is a Primary Key */
133     public boolean getIsPk() {
134         return this.isPk;
135     }
136
137     public void setIsPk(boolean isPk) {
138         this.isPk = isPk;
139     }
140
141     public boolean getEncrypt() {
142         return this.encrypt;
143     }
144
145     public void setEncrypt(boolean encrypt) {
146         this.encrypt = encrypt;
147     }
148
149     public boolean getIsAutoCreatedInternal() {
150         return this.isAutoCreatedInternal;
151     }
152
153     public void setIsAutoCreatedInternal(boolean isAutoCreatedInternal) {
154         this.isAutoCreatedInternal = isAutoCreatedInternal;
155     }
156     
157     /** validators to be called when an update is done */
158     public String JavaDoc getValidator(int index) {
159         return (String JavaDoc) this.validators.get(index);
160     }
161
162     public int getValidatorsSize() {
163         return this.validators.size();
164     }
165
166     public void addValidator(String JavaDoc validator) {
167         this.validators.add(validator);
168     }
169
170     public String JavaDoc removeValidator(int index) {
171         return (String JavaDoc) this.validators.remove(index);
172     }
173
174     public boolean equals(Object JavaDoc obj) {
175         if (obj.getClass() != getClass()) return false;
176         ModelField other = (ModelField) obj;
177         return other.getName().equals(getName()) && other.getModelEntity() == getModelEntity();
178     }
179
180     public int hashCode() {
181         return getModelEntity().hashCode() ^ getName().hashCode();
182     }
183
184     public String JavaDoc toString() {
185         return getModelEntity() + "@" + getName();
186     }
187
188     public Element toXmlElement(Document document) {
189         Element root = document.createElement("field");
190         root.setAttribute("name", this.getName());
191         if (!this.getColName().equals(ModelUtil.javaNameToDbName(this.getName()))) {
192             root.setAttribute("col-name", this.getColName());
193         }
194         root.setAttribute("type", this.getType());
195         if (this.getEncrypt()) {
196             root.setAttribute("encrypt", "true");
197         }
198
199         Iterator valIter = this.validators.iterator();
200         if (valIter != null) {
201             while (valIter.hasNext()) {
202                 String JavaDoc validator = (String JavaDoc) valIter.next();
203                 Element val = document.createElement("validate");
204                 val.setAttribute("name", validator);
205                 root.appendChild(val);
206             }
207         }
208
209         return root;
210     }
211 }
212
Popular Tags