KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > datatypes > NodeDataType


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.datatypes;
11
12 import java.util.Collection JavaDoc;
13 import org.mmbase.util.Casting;
14 import org.mmbase.bridge.*;
15 import org.mmbase.util.logging.*;
16
17 /**
18  * The Node data type describes a data type which is based on an MMBase 'node' field. So the value
19  * is an MMBase node, which can normally be described by a foreign key.
20  *
21  * @author Pierre van Rooden
22  * @author Michiel Meeuwissen
23  * @version $Id: NodeDataType.java,v 1.30 2006/07/17 07:32:29 pierre Exp $
24  * @since MMBase-1.8
25  */

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; // increase this if object serialization changes (which we shouldn't do!)
31

32     protected MustExistRestriction mustExistRestriction = new MustExistRestriction();
33
34     /**
35      * Constructor for node field.
36      */

37     public NodeDataType(String JavaDoc 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 JavaDoc castToValidate(Object JavaDoc value, Node node, Field field) throws CastException {
55         if (value == null) return null;
56         Object JavaDoc preCast = preCast(value, node, field); // resolves enumerations
57
if (preCast instanceof Node) {
58             return preCast;
59         } else {
60             Object JavaDoc 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     /**
73      * Whether the Node of the value must exist
74      *
75      * XXX MM: How can you have a non-existing node? I don't really get it. AFAIK all nodes exist.
76      * especially since a node field is essentially a foreign key.
77      */

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 JavaDoc validateCastValue(Collection JavaDoc errors, Object JavaDoc castValue, Object JavaDoc 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 JavaDoc v, Node node, Field field) {
112             if (getValue().equals(Boolean.TRUE)) {
113                 if (v != null) {
114                     if (v instanceof String JavaDoc) {
115                         Cloud cloud = getCloud(node, field);
116                         return cloud != null && cloud.hasNode((String JavaDoc)v);
117                     } else if (v instanceof Number JavaDoc) {
118                         int num = ((Number JavaDoc)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                         //log.info("Not valid because node value is a " + v.getClass());
126
return false;
127                     }
128                 }
129             }
130             return true;
131         }
132     }
133
134 }
135
Popular Tags