1 package org.sapia.regis.forms; 2 3 import java.util.Map ; 4 5 import org.sapia.regis.Node; 6 import org.sapia.regis.Property; 7 import org.sapia.regis.RWNode; 8 import org.sapia.regis.type.BuiltinType; 9 10 public class Field implements Comparable { 11 12 private BuiltinType type; 13 private String name; 14 private boolean mandatory; 15 private int displayOrder; 16 17 Field(BuiltinType type, String name){ 18 this.type = type; 19 this.name = name; 20 } 21 22 public String getName(){ 23 return name; 24 } 25 26 public BuiltinType getType(){ 27 return type; 28 } 29 30 public void setMandatory(boolean mandatory){ 31 this.mandatory = mandatory; 32 } 33 34 public boolean isMandatory(){ 35 return mandatory; 36 } 37 38 public void set(RWNode instance, String value){ 39 type.parse(value); 40 instance.setProperty(name, value); 41 } 42 43 public void set(RWNode instance, Object value){ 44 if(!type.isAssignable(value)){ 45 throw new IllegalArgumentException ("Cannot assign value for field: " + name + 46 "; expected value of type: " + type.getName()); 47 } 48 instance.setProperty(name, type.toString(value)); 49 } 50 51 public void set(RWNode instance, Map properties){ 52 Object value = properties.get(name); 53 if(value == null && isMandatory()){ 54 throw new IllegalStateException ("Property: " + name + " is mandatory"); 55 } 56 if(value instanceof String ){ 57 value = type.parse((String )value); 58 } 59 else if(!type.isAssignable(value)){ 60 throw new IllegalArgumentException ("Cannot assign value for field: " + name + 61 "; expected value of type: " + type.getName()); 62 } 63 instance.setProperty(name, type.toString(value)); 64 } 65 66 public Object get(Node instance){ 67 Property prop = instance.getProperty(name); 68 if(prop.isNull()) return null; 69 return type.parse(prop.getValue()); 70 } 71 72 public int getDisplayOrder() { 73 return displayOrder; 74 } 75 76 public void setDisplayOrder(int displayOrder) { 77 this.displayOrder = displayOrder; 78 } 79 80 public int hashCode(){ 81 return name.hashCode(); 82 } 83 84 public boolean equals(Object o){ 85 if(o instanceof Field){ 86 return ((Field)o).getName().equals(name); 87 } 88 return o == this; 89 } 90 91 public int compareTo(Object f) { 92 return displayOrder - ((Field)f).getDisplayOrder(); 93 } 94 95 } 96 | Popular Tags |