1 10 package org.mmbase.core; 11 12 import org.mmbase.bridge.Field; 13 import org.mmbase.bridge.NodeManager; 14 import org.mmbase.datatypes.*; 15 import org.mmbase.core.*; 16 import org.mmbase.core.util.Fields; 17 import org.mmbase.module.core.MMObjectBuilder; 18 import org.mmbase.module.core.MMBase; 19 import org.mmbase.storage.*; 20 import org.mmbase.util.*; 21 import java.util.Collection ; 22 23 import org.mmbase.util.logging.*; 24 25 28 public class CoreField extends AbstractField implements Field, Storable, Cloneable { 29 30 private static final Logger log = Logging.getLoggerInstance(CoreField.class); 31 32 private static final int NO_POSITION = -1; 33 34 private int storageType; private int searchPosition = NO_POSITION; 36 private int listPosition = NO_POSITION; 37 private int editPosition = NO_POSITION; 38 39 private int maxLength = -1; 40 41 private MMObjectBuilder parent = null; 42 private int storagePosition = -1; 43 private Object storageIdentifier = null; 44 45 private boolean notNull = false; 46 47 private int savedHashcode = -1; 48 private boolean hashcodeChanged = true; 49 50 51 55 public CoreField(Field field) { 56 this(field.getName(), field.getType(), field.getListItemType(), field.getState(), field.getDataType()); 57 } 58 59 64 protected CoreField(String name, int type, int listItemType, int state, DataType dataType) { 65 super(name, type, listItemType, state, dataType); 66 hashcodeChanged = true; 67 if (dataType instanceof LengthDataType) { 69 long length = ((LengthDataType)dataType).getMaxLength(); 73 if (length > Integer.MAX_VALUE) { 74 maxLength = Integer.MAX_VALUE; 75 } else { 76 maxLength = (int)length; 77 } 78 } 79 } 80 81 86 protected CoreField(String name, CoreField coreField) { 87 super(name, coreField, true); 88 setSearchPosition(coreField.getSearchPosition()); 89 setEditPosition(coreField.getEditPosition()); 90 setListPosition(coreField.getListPosition()); 91 setStoragePosition(coreField.getStoragePosition()); 92 setParent(coreField.getParent()); 93 setMaxLength(coreField.getMaxLength()); 94 setUnique(coreField.isUnique()); 95 hashcodeChanged = true; 96 } 97 98 public NodeManager getNodeManager() { 99 throw new UnsupportedOperationException ("Core fields currently do not support calls to getNodeManager."); 100 } 101 102 public Object clone() { 103 return clone (null); 104 } 105 106 public Object clone(String name) { 107 hashcodeChanged = true; 108 return super.clone(name, true); 109 } 110 111 public void setReadOnly(boolean readOnly) { 112 hashcodeChanged = true; 113 this.readOnly = readOnly; 114 } 115 116 public void setNotNull(boolean nl) { 117 hashcodeChanged = true; 118 notNull = nl; 119 } 120 121 public boolean isNotNull() { 122 return notNull; 123 } 124 125 129 public int getSearchPosition() { 130 return searchPosition; 131 } 132 133 137 public void setSearchPosition(int i) { 138 hashcodeChanged = true; 139 searchPosition = i; 140 } 141 142 146 public int getListPosition() { 147 return listPosition; 148 } 149 150 154 public void setListPosition(int i) { 155 hashcodeChanged = true; 156 listPosition = i; 157 } 158 159 163 public int getEditPosition() { 164 return editPosition; 165 } 166 167 171 public void setEditPosition(int i) { 172 editPosition = i; 173 hashcodeChanged = true; 174 } 175 176 179 public int getStoragePosition() { 180 return storagePosition; 181 } 182 183 186 public void setStoragePosition(int i) { 187 storagePosition = i; 188 hashcodeChanged = true; 189 } 190 191 194 public MMObjectBuilder getParent() { 195 return parent; 196 } 197 198 202 public void setParent(MMObjectBuilder parent) { 203 this.parent = parent; 204 hashcodeChanged = true; 205 } 206 207 public void setState(int state) { 208 super.setState(state); 209 hashcodeChanged = true; 210 } 211 212 public void setType(int type) { 213 this.type = type; 214 hashcodeChanged = true; 215 } 216 217 public void setListItemType(int listItemType) { 218 this.listItemType = listItemType; 219 hashcodeChanged = true; 220 } 221 222 public Collection validate(Object value) { 223 Collection errors = getDataType().validate(value, null, this); 224 return LocalizedString.toStrings(errors, parent.getMMBase().getLocale()); 225 } 226 227 231 public boolean storageEquals(CoreField f) { 232 return 233 getName().equals(f.getName()) 234 && readOnly == f.isReadOnly() 235 && state == f.getState() 236 && getDataType().isRequired() == f.getDataType().isRequired() 237 && getDataType().isUnique() == f.getDataType().isUnique() 238 && maxLength == f.getMaxLength() 239 && (parent == null ? f.getParent() == null : parent.equals(f.getParent())) 240 && getStorageIdentifier().equals(f.getStorageIdentifier()) 241 && getStorageType() == f.getStorageType() && storagePosition == f.getStoragePosition(); 243 } 244 245 249 public boolean equals(Object o) { 250 if (o instanceof CoreField) { 251 CoreField f = (CoreField) o; 252 return 253 storageEquals(f) 254 && getLocalizedDescription().equals(f.getLocalizedDescription()) 255 && getLocalizedGUIName().equals(f.getLocalizedGUIName()) 256 && searchPosition == f.searchPosition 257 && listPosition == f.listPosition 258 && editPosition == f.editPosition 259 ; 260 } else { 261 return false; 262 } 263 } 264 265 268 public int hashCode() { 269 if (hashcodeChanged) { 270 int result = 0; 271 result = HashCodeUtil.hashCode(result, getName()); 272 result = HashCodeUtil.hashCode(result, getType()); 273 result = HashCodeUtil.hashCode(result, getState()); 274 result = HashCodeUtil.hashCode(result, getDataType().isRequired()); 275 result = HashCodeUtil.hashCode(result, getDataType().isUnique()); 276 result = HashCodeUtil.hashCode(result, parent); 277 result = HashCodeUtil.hashCode(result, storagePosition); 278 savedHashcode = result; 279 hashcodeChanged = false; 280 } 281 return savedHashcode; 282 } 283 284 290 public int compareTo(Object o) { 291 int pos1 = getStoragePosition(); 292 int pos2 = ((CoreField)o).getStoragePosition(); 293 if (pos1 < pos2) { 294 return -1; 295 } else if (pos1 > pos2) { 296 return 1; 297 } else { 298 return 0; 299 } 300 } 301 302 public void finish() { 303 dataType.finish(this); 304 } 305 306 public void rewrite() { 307 dataType.rewrite(this); 308 } 309 310 318 public int getMaxLength() { 319 return maxLength; 320 } 321 322 public void setMaxLength(int size) { 323 this.maxLength = size; 324 if (size > 0 && (dataType instanceof LengthDataType) && size < ((LengthDataType)dataType).getMaxLength()) { 325 if (dataType.isFinished()) { 326 dataType = (DataType) dataType.clone(); 327 } 328 ((LengthDataType)dataType).setMaxLength(size); 329 } 330 hashcodeChanged = true; 331 } 332 333 public void setDataType(DataType dataType) throws IllegalArgumentException { 334 super.setDataType(dataType); 335 setMaxLength(maxLength); 337 hashcodeChanged = true; 338 } 339 340 public void setUnique(boolean unique) { 341 dataType.setUnique(unique); 342 hashcodeChanged = true; 343 } 344 345 350 public Object getStorageIdentifier() throws StorageException { 351 if (storageIdentifier == null) { 353 storageIdentifier = MMBase.getMMBase().getStorageManagerFactory().getStorageIdentifier(this); 354 } 355 return storageIdentifier; 356 } 357 358 public int getStorageType() { 359 return storageType; 360 } 361 362 public void setStorageType(int type) { 363 storageType = type; 364 hashcodeChanged = true; 365 } 366 367 public boolean inStorage() { 368 return !isVirtual(); 369 } 370 371 375 public String getGUIType() { 376 return dataType.getName(); 377 } 378 379 public String toString() { 380 return super.toString() + (parent != null ? " of " + parent.getTableName() : " without parent"); 381 } 382 383 384 385 } 386 | Popular Tags |