1 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 39 public class ModelField extends ModelChild { 40 41 42 protected String name = ""; 43 44 45 protected String type = ""; 46 47 48 protected String colName = ""; 49 50 51 protected boolean isPk = false; 52 protected boolean encrypt = false; 53 protected boolean isAutoCreatedInternal = false; 54 55 56 protected List validators = new ArrayList(); 57 58 59 public ModelField() {} 60 61 62 public ModelField(String name, String type, String colName, boolean isPk) { 63 this(name, type, colName, isPk, false); 64 } 65 66 public ModelField(String name, String type, String 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 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; 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 92 public ModelField(DatabaseUtil.ColumnCheckInfo ccInfo, ModelFieldTypeReader modelFieldTypeReader) { 93 this.colName = ccInfo.columnName; 94 this.name = ModelUtil.dbNameToVarName(this.colName); 95 96 this.type = ModelUtil.induceFieldType(ccInfo.typeName, ccInfo.columnSize, ccInfo.decimalDigits, modelFieldTypeReader); 98 99 this.isPk = ccInfo.isPk; 100 } 101 102 103 public String getName() { 104 return this.name; 105 } 106 107 public void setName(String name) { 108 this.name = name; 109 } 110 111 112 public String getType() { 113 return this.type; 114 } 115 116 public void setType(String type) { 117 this.type = type; 118 } 119 120 121 public String getColName() { 122 return this.colName; 123 } 124 125 public void setColName(String 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 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 158 public String getValidator(int index) { 159 return (String ) this.validators.get(index); 160 } 161 162 public int getValidatorsSize() { 163 return this.validators.size(); 164 } 165 166 public void addValidator(String validator) { 167 this.validators.add(validator); 168 } 169 170 public String removeValidator(int index) { 171 return (String ) this.validators.remove(index); 172 } 173 174 public boolean equals(Object 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 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 validator = (String ) 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 |