1 10 11 package org.mmbase.core; 12 13 import org.mmbase.bridge.*; 14 import org.mmbase.core.util.Fields; 15 import org.mmbase.datatypes.DataType; 16 import org.mmbase.util.logging.*; 17 18 26 27 abstract public class AbstractField extends AbstractDescriptor implements Field, Comparable { 28 29 private static final Logger log = Logging.getLoggerInstance(AbstractField.class); 30 31 protected DataType dataType = null; 32 protected int type = TYPE_UNKNOWN; 33 protected int state = STATE_UNKNOWN; 34 protected int listItemType = TYPE_UNKNOWN; 35 protected boolean readOnly = true; 36 37 44 protected AbstractField(String name, Field field) { 45 this(name, field, false); 46 } 47 48 55 protected AbstractField(String name, Field field, boolean cloneDataForRewrite) { 56 super(name, field, cloneDataForRewrite); 57 type = field.getType(); 58 setState(field.getState()); 59 readOnly = field.isReadOnly(); 60 listItemType = field.getListItemType(); 61 if (cloneDataForRewrite) { 62 setDataType((DataType)field.getDataType().clone()); 63 } else { 64 setDataType(dataType = field.getDataType()); 65 } 66 } 67 68 73 protected AbstractField(String name, int type, int listItemType, int state, DataType dataType) { 74 super(name); 75 this.type = type; 76 this.listItemType = listItemType; 77 setState(state); 78 setDataType(dataType); 79 } 80 81 abstract public NodeManager getNodeManager(); 82 83 public int compareTo(Object o) { 84 if (o instanceof Field) { 85 Field f = (Field) o; 86 int compared = getName().compareTo(f.getName()); 87 if (compared == 0) compared = dataType.compareTo(f.getDataType()); 88 return compared; 89 } else { 90 throw new ClassCastException ("Object is not of type Field"); 91 } 92 } 93 94 99 public boolean equals(Object o) { 100 if (o instanceof Field) { 101 Field f = (Field) o; 102 return getName().equals(f.getName()) && dataType.equals(f.getDataType()); 103 } 104 return false; 105 } 106 107 public int hashCode() { 108 return getName().hashCode() * 13 + dataType.hashCode(); 109 } 110 111 public int getState() { 112 return state; 113 } 114 115 protected void setState(int state) { 116 if (this.state == STATE_UNKNOWN) { 117 readOnly = state == STATE_SYSTEM || state == STATE_SYSTEM_VIRTUAL; 118 } 119 this.state = state; 120 } 121 122 public int getType() { 123 return type; 124 } 125 126 public int getListItemType() { 127 return listItemType; 128 } 129 130 public DataType getDataType() { 131 return dataType; 132 } 133 134 139 public void setDataType(DataType dataType) throws IllegalArgumentException { 140 int dataTypeType = dataType.getBaseType(); 141 if (dataTypeType != type) { 142 log.debug("DataType (" + dataType.getBaseTypeIdentifier() + ") is different from db type (" + Fields.getTypeDescription(type) + ")."); 143 } 144 this.dataType = dataType; 145 } 146 147 public boolean hasIndex() { 148 return (getType() == Field.TYPE_NODE) || getName().equals("number"); 149 } 150 151 abstract public int getSearchPosition(); 152 153 abstract public int getListPosition(); 154 155 abstract public int getEditPosition(); 156 157 abstract public int getStoragePosition(); 158 159 162 public boolean isUnique() { 163 return dataType.isUnique(); 164 } 165 166 abstract public int getMaxLength(); 167 168 171 public boolean isRequired() { 172 return dataType.isRequired(); 173 } 174 175 178 public boolean isVirtual() { 179 return getState() == STATE_VIRTUAL || getState() == STATE_SYSTEM_VIRTUAL; 180 } 181 182 189 public boolean isTemporary() { 190 String fieldName = getName(); 194 return fieldName != null ? fieldName.startsWith("_") : false; 195 } 196 197 200 public boolean isReadOnly() { 201 return readOnly; 202 } 203 204 abstract public String getGUIType(); 205 206 209 public String toString() { 210 return getName() + ":" + 211 Fields.getTypeDescription(getType()) + " / " + 212 Fields.getStateDescription(getState())+ "/" + 213 getDataType(); 214 } 215 216 public Object clone() { 217 return clone (null, false); 218 } 219 220 public Object clone(String name, boolean copyDataTypeForRewrite) { 221 try { 222 AbstractField clone = (AbstractField)super.clone(name); 223 if (copyDataTypeForRewrite) { 224 clone.dataType = (DataType) dataType.clone(); 225 } 226 return clone; 227 } catch (CloneNotSupportedException cnse) { 228 throw new RuntimeException ("Cannot clone this Field", cnse); 230 } 231 } 232 233 } 234 | Popular Tags |