1 24 package org.ofbiz.entity.model; 25 26 import java.io.Serializable ; 27 import java.util.*; 28 import org.w3c.dom.*; 29 30 import org.ofbiz.base.util.*; 31 32 39 public class ModelFieldType implements Serializable { 40 41 42 protected String type = null; 43 44 45 protected String javaType = null; 46 47 48 protected String sqlType = null; 49 50 51 protected String sqlTypeAlias = null; 52 53 54 protected List validators = new ArrayList(); 55 56 57 public ModelFieldType() {} 58 59 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 methodName = element.getAttribute("method"); 70 String className = element.getAttribute("class"); 71 if (methodName != null) { 72 this.validators.add(new ModelFieldValidator(className, methodName)); 73 } 74 } 75 } 76 77 78 public String getType() { 79 return this.type; 80 } 81 82 83 public String getJavaType() { 84 return this.javaType; 85 } 86 87 88 public String getSqlType() { 89 return this.sqlType; 90 } 91 92 93 public String getSqlTypeAlias() { 94 return this.sqlTypeAlias; 95 } 96 97 98 public List getValidators() { 99 return this.validators; 100 } 101 102 105 public int stringLength() { 106 if (sqlType.indexOf("VARCHAR") >= 0) { 107 if (sqlType.indexOf("(") > 0 && sqlType.indexOf(")") > 0) { 108 String 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 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 { 129 130 protected String validatorClass = null; 131 protected String validatorMethod = null; 132 133 public ModelFieldValidator(String className, String methodName) { 134 this.validatorClass = className; 135 this.validatorMethod = methodName; 136 } 137 138 public String getClassName() { 139 if (UtilValidate.isNotEmpty(validatorClass) && UtilValidate.isNotEmpty(validatorMethod)) { 140 return validatorClass; 141 } 142 return null; 143 } 144 145 public String getMethodName() { 146 if (UtilValidate.isNotEmpty(validatorClass) && UtilValidate.isNotEmpty(validatorMethod)) { 147 return validatorMethod; 148 } 149 return null; 150 } 151 } 152 } 153 | Popular Tags |