KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.mmbase.bridge.*;
17 import org.mmbase.security.*;
18 import org.mmbase.storage.search.SearchQueryException;
19 import org.mmbase.module.core.*;
20 import org.mmbase.module.corebuilders.*;
21 import org.mmbase.util.logging.*;
22
23 /**
24  * @javadoc
25  *
26  * @author Rob Vermeulen
27  * @author Pierre van Rooden
28  * @version $Id: BasicRelationManager.java,v 1.34 2005/12/27 22:14:14 michiel Exp $
29  */

30 public class BasicRelationManager extends BasicNodeManager implements RelationManager {
31     private static final Logger log = Logging.getLoggerInstance(BasicRelationManager.class);
32
33     public MMObjectNode relDefNode;
34     private MMObjectNode typeRelNode;
35
36     /**
37      * Creates a new Relation manager (for insert).
38      * The type of manager (a strictly constrained manager or a role manager)
39      * is dependend on the type of the passed node (from either the reldef of typerel
40      * builder).
41      * @param node the node on which to base the relation manager
42      * @param cloud the cloud for which to create the manager
43      * @param id the id of the node in the temporary cloud
44      */

45     BasicRelationManager(MMObjectNode node, BasicCloud cloud, int nodeId) {
46         super(node, cloud, nodeId);
47     }
48
49     /**
50      * Creates a new instance of Relation manager.
51      * The type of manager (a strictly constrained manager or a role manager)
52      * is dependend on the type of the passed node (from either the reldef of typerel
53      * builder).
54      * @param node the node on which to base the relation manager
55      * @param cloud the cloud for which to create the manager
56      */

57     BasicRelationManager(MMObjectNode node, BasicCloud cloud) {
58         super(node, cloud);
59     }
60
61     public final boolean isRelationManager() {
62         return true;
63     }
64     public final RelationManager toRelationManager() {
65         return this;
66     }
67
68     /**
69      * Initializes the NodeManager: determines the MMObjectBuilder from the
70      * passed node (reldef or typerel), and fills temporary variables to maintain status.
71      */

72     protected void initManager() {
73         MMObjectBuilder bul = noderef.getBuilder();
74         if (bul instanceof RelDef) {
75             relDefNode = noderef;
76         } else if (bul instanceof TypeRel) {
77             typeRelNode = noderef;
78             relDefNode = typeRelNode.getBuilder().getNode(typeRelNode.getIntValue("rnumber"));
79             if (relDefNode == null) {
80                 log.warn("No node found for 'rnumber'" + typeRelNode.getIntValue("rnumber"));
81             }
82         } else {
83             throw new RuntimeException JavaDoc("The builder of node " + noderef.getNumber() + " is not reldef or typerel, but " + bul.getTableName() + " cannot instantiate a relation manager with this");
84         }
85         
86         RelDef relDef = (RelDef) relDefNode.getBuilder();
87         if (relDef != null) {
88             builder = relDef.getBuilder(relDefNode.getNumber());
89         } else {
90             log.warn("builder of " + relDefNode + " was null");
91         }
92         super.initManager();
93     }
94
95
96     protected void setNodeManager(MMObjectNode node) {
97         int nodeNumber = node.getNumber();
98         if (nodeNumber >= 0 && nodeNumber == getNode().getBuilder().getNumber()) { // this is the typedef itself
99
nodeManager = this;
100         } else {
101             super.setNodeManager(node);
102         }
103     }
104
105     public String JavaDoc getForwardRole() {
106         return relDefNode.getStringValue("sname");
107     }
108
109     public String JavaDoc getReciprocalRole() {
110         return relDefNode.getStringValue("dname");
111     }
112
113     public String JavaDoc getForwardGUIName() {
114         return relDefNode.getStringValue("sguiname");
115     }
116
117     public String JavaDoc getReciprocalGUIName() {
118         return relDefNode.getStringValue("dguiname");
119     }
120
121     public int getDirectionality() {
122         return relDefNode.getIntValue("dir");
123     }
124
125     int getBuilder() {
126         return relDefNode.getIntValue("builder");
127     }
128
129     public NodeManager getSourceManager() {
130         if (typeRelNode == null) {
131             throw new BridgeException("This relationmanager does not contain source information.");
132         }
133         int nr = typeRelNode.getIntValue("snumber");
134         return cloud.getNodeManager(nr);
135     }
136
137     public NodeManager getDestinationManager() {
138         if (typeRelNode == null) {
139             throw new BridgeException("This relationmanager does not contain destination information.");
140         }
141         int nr = typeRelNode.getIntValue("dnumber");
142         return cloud.getNodeManager(nr);
143     }
144
145
146     protected final BasicNode createBasicNode() {
147         return createBasicRelation();
148     }
149
150     /**
151      * BasicRelationManager is garantueed to create BasicRelations. Extension therefore most override this and not {@link #createBasicNode}.
152      * @since MMBase-1.8
153      */

154     protected BasicRelation createBasicRelation() {
155         if(relDefNode == null) {
156             throw new RuntimeException JavaDoc("reldef node is null");
157         }
158         NodeAndId n = createMMObjectNode();
159         BasicRelation relation = new BasicRelation(n.node, cloud, n.id);
160         relation.setValueWithoutChecks("rnumber", new Integer JavaDoc(relDefNode.getNumber()));
161         return relation;
162     }
163
164     public Relation createRelation(Node sourceNode, Node destinationNode) {
165         //
166
// checks whether all components are part of the same cloud/transaction
167
// maybe should be made more flexible?
168
//
169
if (sourceNode.getCloud() != cloud) {
170             throw new BridgeException("Relationmanager and source node are not in the same transaction or in different clouds.");
171         }
172         if (destinationNode.getCloud() != cloud) {
173             throw new BridgeException("Relationmanager and destination node are not in the same transaction or in different clouds.");
174         }
175         if (!(cloud instanceof Transaction) && (sourceNode.isNew() || destinationNode.isNew())) {
176             throw new BridgeException("Cannot add a relation to a new node that has not been committed.");
177         }
178
179        BasicRelation relation = createBasicRelation();
180        relation.setSource(sourceNode);
181        relation.setDestination(destinationNode);
182        relation.checkValid();
183        // relation.commit();
184
return relation;
185     }
186
187     public RelationList getRelations(Node node) {
188         // XXX: no caching is done here?
189
InsRel insRel = (InsRel) builder;
190         List JavaDoc result = insRel.getRelationsVector(node.getNumber());
191         return new BasicRelationList(result, this);
192     }
193
194     public boolean mayCreateRelation(Node sourceNode, Node destinationNode) {
195         return cloud.check(Operation.CREATE, builder.getNumber(),
196                            sourceNode.getNumber(), destinationNode.getNumber());
197     }
198 }
199
Popular Tags