1 10 package org.mmbase.datatypes; 11 12 import java.util.Collection ; 13 import org.mmbase.util.Casting; 14 import org.mmbase.bridge.*; 15 import org.mmbase.util.logging.*; 16 17 26 public class NodeDataType extends BasicDataType { 27 28 private static final Logger log = Logging.getLoggerInstance(NodeDataType.class); 29 30 private static final long serialVersionUID = 1L; 32 protected MustExistRestriction mustExistRestriction = new MustExistRestriction(); 33 34 37 public NodeDataType(String name) { 38 super(name, Node.class); 39 } 40 41 42 protected void inheritRestrictions(BasicDataType origin) { 43 super.inheritRestrictions(origin); 44 if (origin instanceof NodeDataType) { 45 mustExistRestriction.inherit(((NodeDataType)origin).mustExistRestriction); 46 } 47 } 48 protected void cloneRestrictions(BasicDataType origin) { 49 super.cloneRestrictions(origin); 50 if (origin instanceof NodeDataType) { 51 mustExistRestriction = new MustExistRestriction(((NodeDataType)origin).mustExistRestriction); 52 } 53 } 54 protected Object castToValidate(Object value, Node node, Field field) throws CastException { 55 if (value == null) return null; 56 Object preCast = preCast(value, node, field); if (preCast instanceof Node) { 58 return preCast; 59 } else { 60 Object res = Casting.toType(Node.class, getCloud(node, field), preCast); 61 if (res == null) { 62 if (Casting.toString(value).equals("-1")) { 63 return null; 64 } 65 throw new CastException("No such node " + preCast); 66 } else { 67 return res; 68 } 69 } 70 } 71 72 78 public boolean mustExist() { 79 return mustExistRestriction.getValue().equals(Boolean.TRUE); 80 } 81 82 public MustExistRestriction getMustExistRestriction() { 83 mustExistRestriction.setFixed(true); 84 return mustExistRestriction; 85 } 86 87 protected Collection validateCastValue(Collection errors, Object castValue, Object value, Node node, Field field) { 88 errors = super.validateCastValue(errors, castValue, value, node, field); 89 errors = mustExistRestriction.validate(errors, value, node, field); 90 return errors; 91 } 92 93 private class MustExistRestriction extends AbstractRestriction { 94 MustExistRestriction(MustExistRestriction me) { 95 super(me); 96 enforceStrength = DataType.ENFORCE_ONCHANGE; 97 } 98 MustExistRestriction() { 99 super("mustExist", Boolean.TRUE); 100 enforceStrength = DataType.ENFORCE_ONCHANGE; 101 } 102 protected Cloud getCloud(Node node, Field field) { 103 Cloud cloud = node != null ? node.getCloud() : null; 104 if (cloud == null) cloud = field != null ? field.getNodeManager().getCloud() : null; 105 if (cloud == null) { 106 cloud = ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null); 107 } 108 return cloud; 109 } 110 111 protected boolean simpleValid(Object v, Node node, Field field) { 112 if (getValue().equals(Boolean.TRUE)) { 113 if (v != null) { 114 if (v instanceof String ) { 115 Cloud cloud = getCloud(node, field); 116 return cloud != null && cloud.hasNode((String )v); 117 } else if (v instanceof Number ) { 118 int num = ((Number )v).intValue(); 119 if (num < 0) return false; 120 Cloud cloud = getCloud(node, field); 121 return cloud != null && cloud.hasNode(num); 122 } else if (v instanceof Node) { 123 return true; 124 } else { 125 return false; 127 } 128 } 129 } 130 return true; 131 } 132 } 133 134 } 135 | Popular Tags |