1 19 20 package org.apache.cayenne.dataview; 21 22 import java.text.Format ; 23 24 import org.apache.commons.lang.ObjectUtils; 25 import org.apache.commons.lang.Validate; 26 import org.apache.cayenne.DataObject; 27 import org.apache.cayenne.map.ObjAttribute; 28 import org.apache.cayenne.map.ObjRelationship; 29 30 36 public class ObjEntityViewField { 37 38 private ObjEntityView owner; 40 private ObjAttribute objAttribute; 44 45 private ObjRelationship objRelationship; 46 47 private String name = ""; 49 50 private DataTypeEnum dataType = DataTypeEnum.UNKNOWN_TYPE; 52 53 private CalcTypeEnum calcType = CalcTypeEnum.NO_CALC_TYPE; 55 56 private String caption = ""; 59 60 private Format displayFormat = null; 65 66 private Format editFormat = null; 68 69 private int preferredIndex = -1; 72 73 private int index = -1; 75 76 private boolean editable = true; 78 79 private boolean visible = true; 81 82 private Object defaultValue = null; 84 85 private ObjEntityViewField lookupField = null; 88 89 public ObjEntityViewField() { 90 } 91 92 public ObjEntityView getOwner() { 93 return owner; 94 } 95 96 public DataView getRootOwner() { 97 return getOwner().getOwner(); 98 } 99 100 public int getIndex() { 101 return index; 102 } 103 104 public int getPreferredIndex() { 105 return preferredIndex; 106 } 107 108 public void setPreferredIndex(int preferredIndex) { 109 Validate.isTrue(preferredIndex >= -1); 110 this.preferredIndex = preferredIndex; 111 } 112 113 public boolean isVisible() { 114 return visible; 115 } 116 117 public void setVisible(boolean visible) { 118 this.visible = visible; 119 } 120 121 public String getName() { 122 return name; 123 } 124 125 public void setName(String name) { 126 Validate.notNull(name); 127 this.name = name; 128 } 129 130 public ObjAttribute getObjAttribute() { 131 return objAttribute; 132 } 133 134 public void setObjAttribute(ObjAttribute objAttribute) { 135 Validate.notNull(objAttribute); 136 this.objAttribute = objAttribute; 137 } 138 139 public boolean isEditable() { 140 return editable; 141 } 142 143 public void setEditable(boolean editable) { 144 this.editable = editable; 145 } 146 147 public String getCaption() { 148 return caption; 149 } 150 151 public void setCaption(String caption) { 152 this.caption = caption; 153 } 154 155 void setOwner(ObjEntityView owner) { 156 this.owner = owner; 157 } 158 159 void setIndex(int index) { 160 Validate.isTrue(owner == null || index >= 0); 161 this.index = index; 162 } 163 164 public Class getJavaClass() { 165 return getRootOwner().getDataTypeSpec().getJavaClass(dataType); 166 } 167 168 public Object getValue(DataObject obj) { 169 Object rawValue = getRawValue(obj); 170 return toValue(rawValue); 171 } 172 173 public Object toValue(Object rawValue) { 174 Object v = null; 175 if (isLookup()) { 176 if (rawValue instanceof DataObject) 177 v = lookupField.getValue((DataObject) rawValue); 178 return v; 179 } 180 if (rawValue == null) 181 return null; 182 183 v = getRootOwner().getDataTypeSpec().toDataType(dataType, rawValue); 184 return v; 185 } 186 187 public Object toRawValue(Object value) { 188 if (value == null) 189 return null; 190 DataView rootOwner = getOwner().getOwner(); 191 if (isLookup()) { 192 return rootOwner.getLookupCache().getDataObject(lookupField, value); 193 } 194 if (objAttribute == null) 195 return null; 196 197 String type = objAttribute.getType(); 198 Object v = null; 199 try { 200 Class untypedValueClass = Class.forName(type); 201 v = rootOwner.getDataTypeSpec().fromDataType( 202 untypedValueClass, 203 dataType, 204 value); 205 } 206 catch (ClassNotFoundException ex) { 207 ex.printStackTrace(); 208 } 209 return v; 210 } 211 212 public void setValue(DataObject obj, Object value) { 213 Object rawValue = toRawValue(value); 214 Object oldValue = getValue(obj); 215 setRawValue(obj, rawValue); 216 getRootOwner().fireFieldValueChangeEvent(this, obj, oldValue, value); 217 } 218 219 public String getFormattedValue(DataObject obj) { 220 Object value = getRawValue(obj); 221 String formattedValue = null; 222 if (!isLookup()) { 223 Format f = (displayFormat != null ? displayFormat : editFormat); 224 if (f == null) 225 formattedValue = ObjectUtils.toString(value); 226 else { 227 try { 228 formattedValue = f.format(value); 229 } 230 catch (Exception ex) { 231 formattedValue = ""; 232 } 233 } 234 } 235 else { 236 formattedValue = lookupField.getFormattedValue((DataObject) value); 237 } 238 return formattedValue; 239 } 240 241 public Object getRawValue(DataObject obj) { 242 if (obj == null) 243 return null; 244 Object value = null; 245 if (!isLookup() && objAttribute != null) { 246 value = obj.readProperty(objAttribute.getName()); 247 } 248 else if (isLookup() && objRelationship != null) { 249 value = obj.readProperty(objRelationship.getName()); 250 } 251 return value; 252 } 253 254 public void setRawValue(DataObject obj, Object value) { 255 if (obj != null) { 256 if (!isLookup() && objAttribute != null) { 257 obj.writeProperty(objAttribute.getName(), value); 258 } 259 else if (isLookup() && objRelationship != null) { 260 obj.setToOneTarget( 261 objRelationship.getName(), 262 (DataObject) value, 263 objRelationship.getReverseRelationship() != null); 264 } 265 } 266 } 267 268 public DataTypeEnum getDataType() { 269 return dataType; 270 } 271 272 public void setDataType(DataTypeEnum dataType) { 273 Validate.notNull(dataType); 274 this.dataType = dataType; 275 } 276 277 public CalcTypeEnum getCalcType() { 278 return calcType; 279 } 280 281 public void setCalcType(CalcTypeEnum calcType) { 282 Validate.notNull(calcType); 283 this.calcType = calcType; 284 } 285 286 public Object getDefaultValue() { 287 return defaultValue; 288 } 289 290 public void setDefaultValue(Object defaultValue) { 291 this.defaultValue = defaultValue; 292 } 293 294 public ObjRelationship getObjRelationship() { 295 return objRelationship; 296 } 297 298 public void setObjRelationship(ObjRelationship objRelationship) { 299 Validate.notNull(objRelationship); 300 this.objRelationship = objRelationship; 301 } 302 303 public boolean isLookup() { 304 return CalcTypeEnum.LOOKUP_TYPE.equals(calcType); 305 } 306 307 public Object [] getLookupValues() { 308 if (!isLookup()) 309 return null; 310 return getRootOwner().getLookupCache().getCachedValues(lookupField); 311 } 312 313 public ObjEntityViewField getLookupField() { 314 return lookupField; 315 } 316 317 public void setLookupField(ObjEntityViewField lookupField) { 318 this.lookupField = lookupField; 319 } 320 321 public Format getDisplayFormat() { 322 if (displayFormat == null 323 && isLookup() 324 && lookupField != null 325 && lookupField != this) 326 return lookupField.getDisplayFormat(); 327 return displayFormat; 328 } 329 330 public void setDisplayFormat(Format displayFormat) { 331 this.displayFormat = displayFormat; 332 } 333 334 public Format getEditFormat() { 335 if (editFormat == null 336 && isLookup() 337 && lookupField != null 338 && lookupField != this) 339 return lookupField.getEditFormat(); 340 return editFormat; 341 } 342 343 public void setEditFormat(Format editFormat) { 344 this.editFormat = editFormat; 345 } 346 347 public boolean isSameObjAttribute(ObjEntityViewField field) { 348 if (objAttribute != null) { 349 ObjAttribute fieldAttribute = field.getObjAttribute(); 350 if (fieldAttribute == null) 351 return false; 352 boolean same = (objAttribute.equals(fieldAttribute) || (objAttribute 353 .getEntity() 354 .equals(fieldAttribute.getEntity()) && objAttribute.getName().equals( 355 fieldAttribute.getName()))); 356 return same; 357 } 358 else if (isLookup()) { 359 if (field.isLookup()) 360 return getLookupField().isSameObjAttribute(field.getLookupField()); 361 else 362 return getLookupField().isSameObjAttribute(field); 363 } 364 return false; 365 } 366 } 367 | Popular Tags |