KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > implementation > BasicRelation


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
11 package org.mmbase.bridge.implementation;
12
13 import org.mmbase.security.*;
14 import org.mmbase.bridge.*;
15 import org.mmbase.module.core.*;
16 import org.mmbase.util.logging.*;
17
18 /**
19  * @javadoc
20  *
21  * @author Rob Vermeulen
22  * @author Pierre van Rooden
23  * @version $Id: BasicRelation.java,v 1.40 2006/01/24 12:29:27 michiel Exp $
24  */

25 public class BasicRelation extends BasicNode implements Relation {
26     private static final Logger log = Logging.getLoggerInstance(BasicRelation.class);
27
28     private RelationManager relationManager = null;
29     protected int snum;
30     protected int dnum;
31
32     private static final int UNSET = -999;
33
34     private int sourceNodeType = UNSET;
35     private int destinationNodeType = UNSET;
36
37     protected boolean relationChanged = false; // Indicates a change in snum or dnum
38

39     /**
40      * @javadoc
41      */

42     BasicRelation(MMObjectNode node, BasicCloud cloud) {
43         super(node, cloud);
44     }
45
46     /**
47      * @javadoc
48      */

49     BasicRelation(MMObjectNode node, BasicRelationManager nodeManager) {
50         super(node, nodeManager);
51     }
52
53     /**
54      * @javadoc
55      */

56     BasicRelation(MMObjectNode node, BasicCloud cloud, int id) {
57         super(node, cloud, id);
58     }
59
60     public final boolean isRelation() {
61         return true;
62     }
63     public Relation toRelation() {
64         return this;
65     }
66
67     /**
68      * Initializes the node.
69      * Determines nodemanager and cloud (depending on information available),
70      * Sets references to MMBase modules and initializes state in case of a transaction.
71      */

72     protected void init() {
73         super.init();
74         if (nodeManager instanceof RelationManager) {
75             relationManager = (RelationManager)nodeManager;
76         }
77         snum = getIntValue("snumber");
78         dnum = getIntValue("dnumber");
79
80     }
81
82
83     public Node getSource() {
84         // Note that this will not return an accurate value when the field is
85
// edited during a transaction.
86
return nodeManager.getCloud().getNode(snum);
87     }
88
89     public Node getDestination() {
90         // Note that this will not return an accurate value when the field is
91
// edited during a transaction.
92
return nodeManager.getCloud().getNode(dnum);
93     }
94
95     public void setSource(Node node) {
96         if (node.getCloud() != cloud) {
97             throw new BridgeException("Source and relation are not in the same transaction or from different clouds.");
98         }
99         relationChanged = true;
100         int source=node.getIntValue("number");
101         if (source==-1) {
102             // set a temporary field, transactionmanager resolves this
103
getNode().setValue("_snumber", node.getValue(MMObjectBuilder.TMP_FIELD_NUMBER));
104         } else {
105           getNode().setValue("snumber",source);
106         }
107         snum = node.getNumber();
108         sourceNodeType = node.getIntValue("otype");
109     }
110
111     public void setDestination(Node node) {
112         if (node.getCloud() != cloud) {
113             throw new BridgeException("Destination and relation are not in the same transaction or from different clouds.");
114         }
115         relationChanged = true;
116         int dest=node.getIntValue("number");
117         if (dest==-1) {
118             // set a temporary field, transactionmanager resolves this
119
getNode().setValue("_dnumber", node.getValue(MMObjectBuilder.TMP_FIELD_NUMBER));
120         } else {
121           getNode().setValue("dnumber",dest);
122         }
123        dnum = node.getNumber();
124        destinationNodeType = node.getIntValue("otype");
125     }
126
127     public RelationManager getRelationManager() {
128         if (relationManager == null) {
129             int stypenum = BasicCloudContext.mmb.getTypeRel().getNodeType(snum);
130             int dtypenum = BasicCloudContext.mmb.getTypeRel().getNodeType(dnum);
131             if (log.isDebugEnabled()) {
132                 log.debug(stypenum + ", " + dtypenum + ", " + getNode().getIntValue("rnumber"));
133             }
134
135             relationManager=cloud.getRelationManager(stypenum,dtypenum, getNode().getIntValue("rnumber"));
136         }
137         return relationManager;
138     }
139
140
141     /**
142      *
143      * @javadoc
144      */

145     void checkValid() {
146         if (log.isDebugEnabled()) {
147             log.debug("s : " + snum + " d: " + dnum);
148         }
149         //int snumber = snumtype.getNumber();
150
//int dnumber = dnumtype.getNumber();
151

152         if (sourceNodeType == UNSET) {
153             sourceNodeType = -1;
154             if (snum != -1) sourceNodeType = BasicCloudContext.mmb.getTypeDef().getNodeType(snum);
155         }
156         if (destinationNodeType == UNSET) {
157             destinationNodeType = -1;
158             if (dnum!=-1) destinationNodeType = BasicCloudContext.mmb.getTypeDef().getNodeType(dnum);
159         }
160
161         int rnumber = getNode().getIntValue("rnumber");
162         if (!BasicCloudContext.mmb.getTypeRel().contains(sourceNodeType, destinationNodeType, rnumber)) {
163             if (!BasicCloudContext.mmb.getTypeRel().contains(destinationNodeType, sourceNodeType, rnumber)) {
164                 if (! cloud.hasNode(sourceNodeType)) {
165                     throw new BridgeException("Source type of relation " + this + ": " + sourceNodeType + " does not point to a valid node.");
166                 }
167                 if (! cloud.hasNode(destinationNodeType)) {
168                     throw new BridgeException("Destination type of relation " + this + ": " + destinationNodeType + " does not point to a valid node.");
169                 }
170                 if (! cloud.hasNode(rnumber)) {
171                     throw new BridgeException("Rnumber of relation " + this + ": " + rnumber + " does not point to a valid node.");
172                 }
173                 throw new BridgeException("Source and/or Destination node are not of the correct type, or relation not allowed ("
174                                           + cloud.getNode(sourceNodeType).getValue("name") + ","
175                                           + cloud.getNode(destinationNodeType).getValue("name") + ","
176                                           + cloud.getNode(rnumber).getValue("sname") + ")");
177             }
178         }
179
180     }
181
182     public void setValueWithoutProcess(String JavaDoc fieldName, Object JavaDoc value) {
183         edit(ACTION_EDIT);
184         if ("rnumber".equals(fieldName)) {
185             throw new BridgeException("Not allowed to change field '" + fieldName + "'.");
186         } else if ("snumber".equals(fieldName) || "dnumber".equals(fieldName)) {
187             relationChanged = true;
188         }
189         super.setValueWithoutProcess(fieldName, value);
190     }
191
192     public void commit() {
193         // Check types of source and destination
194
// Normally, this check would be run in the core.
195
// However, the current system mdoes not throw an exception when a wrong
196
// node is created, we want to do this so for them moment
197
// we perform this check here.
198
// Note that we do not realign the node since InsRel does it for us,
199
// but we DO update snum and dnum.
200
//
201
// XXX:This check should ultimately be removed, once we have an agreement
202
// on throwing the exception in the core
203

204         checkValid();
205         if (! (BasicCloud.isTemporaryId(snum) || BasicCloud.isTemporaryId(dnum))) {
206             if (isNew()) {
207                 cloud.verify(Operation.CREATE, BasicCloudContext.mmb.getTypeDef().getIntValue(getNodeManager().getName()), snum, dnum);
208                 relationChanged = false;
209             } else {
210                 if (relationChanged) {
211                     cloud.verify(Operation.CHANGE_RELATION, noderef.getNumber(), snum, dnum);
212                     relationChanged = false;
213                 }
214             }
215         }
216         super.commit();
217         if (!(cloud instanceof Transaction)) {
218             snum = getNode().getIntValue("snumber");
219             dnum = getNode().getIntValue("dnumber");
220         }
221     }
222
223 }
224
Popular Tags