001: /*
002: * Helma License Notice
003: *
004: * The contents of this file are subject to the Helma License
005: * Version 2.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://adele.helma.org/download/helma/license.txt
008: *
009: * Copyright 1998-2003 Helma Software. All Rights Reserved.
010: *
011: * $RCSfile$
012: * $Author: root $
013: * $Revision: 8604 $
014: * $Date: 2007-09-28 15:16:38 +0200 (Fre, 28 Sep 2007) $
015: */
016:
017: package helma.objectmodel.db;
018:
019: import helma.objectmodel.ObjectNotFoundException;
020:
021: import java.util.List;
022: import java.util.Vector;
023:
024: /**
025: * A wrapper around NodeManager that catches most Exceptions, or rethrows them as RuntimeExceptions.
026: * The idea behind this is that we don't care a lot about Exception classes, since Hop programming is done
027: * in JavaScript which doesn't know about them (except for the exception message).
028: */
029: public final class WrappedNodeManager {
030: NodeManager nmgr;
031:
032: /**
033: * Creates a new WrappedNodeManager object.
034: *
035: * @param nmgr ...
036: */
037: public WrappedNodeManager(NodeManager nmgr) {
038: this .nmgr = nmgr;
039: }
040:
041: /**
042: * Get a node given its id and DbMapping
043: *
044: * @param id
045: * @param dbmap
046: * @return
047: */
048: public Node getNode(String id, DbMapping dbmap) {
049: return getNode(new DbKey(dbmap, id));
050: }
051:
052: /**
053: * Get a node given its key
054: *
055: * @param key
056: * @return
057: */
058: public Node getNode(Key key) {
059: try {
060: return nmgr.getNode(key);
061: } catch (ObjectNotFoundException x) {
062: return null;
063: } catch (Exception x) {
064: nmgr.app.logError("Error retrieving Node for " + key, x);
065: throw new RuntimeException("Error retrieving Node", x);
066: }
067: }
068:
069: /**
070: * Get the node specified by the given id and Relation.
071: *
072: * @param home
073: * @param id
074: * @param rel
075: * @return
076: */
077: public Node getNode(Node home, String id, Relation rel) {
078: try {
079: return nmgr.getNode(home, id, rel);
080: } catch (ObjectNotFoundException x) {
081: return null;
082: } catch (Exception x) {
083: nmgr.app.logError("Error retrieving Node \"" + id
084: + "\" from " + home, x);
085: throw new RuntimeException("Error retrieving Node", x);
086: }
087: }
088:
089: /**
090: * Get the list of nodes contained in the collection of the given
091: * Node specified by the given Relation.
092: *
093: * @param home
094: * @param rel
095: * @return
096: */
097: public SubnodeList getNodes(Node home, Relation rel) {
098: try {
099: return nmgr.getNodes(home, rel);
100: } catch (Exception x) {
101: throw new RuntimeException("Error retrieving Nodes", x);
102: }
103: }
104:
105: /**
106: * Get a list of IDs of nodes contained in the given Node's
107: * collection specified by the given Relation.
108: *
109: * @param home
110: * @param rel
111: * @return
112: */
113: public SubnodeList getNodeIDs(Node home, Relation rel) {
114: try {
115: return nmgr.getNodeIDs(home, rel);
116: } catch (Exception x) {
117: throw new RuntimeException("Error retrieving NodeIDs", x);
118: }
119: }
120:
121: /**
122: * @see helma.objectmodel.db.NodeManager#updateSubnodeList(Node, Relation)
123: */
124: public int updateSubnodeList(Node home, Relation rel) {
125: try {
126: return nmgr.updateSubnodeList(home, rel);
127: } catch (Exception x) {
128: throw new RuntimeException("Error retrieving NodeIDs", x);
129: }
130: }
131:
132: /**
133: * Count the nodes contained in the given Node's collection
134: * specified by the given Relation.
135: *
136: * @param home
137: * @param rel
138: * @return
139: */
140: public int countNodes(Node home, Relation rel) {
141: try {
142: return nmgr.countNodes(home, rel);
143: } catch (Exception x) {
144: throw new RuntimeException("Error counting Nodes", x);
145: }
146: }
147:
148: /**
149: * Delete a node from the database
150: *
151: * @param node
152: */
153: public void deleteNode(Node node) {
154: try {
155: nmgr.deleteNode(node);
156: } catch (Exception x) {
157: throw new RuntimeException("Error deleting Node", x);
158: }
159: }
160:
161: /**
162: * Get a list of property names from the given node.
163: * TODO: this retrieves access names of child nodes, not property names
164: *
165: * @param home
166: * @param rel
167: * @return
168: */
169: public Vector getPropertyNames(Node home, Relation rel) {
170: try {
171: return nmgr.getPropertyNames(home, rel);
172: } catch (Exception x) {
173: throw new RuntimeException(
174: "Error retrieving property names ", x);
175: }
176: }
177:
178: /**
179: * Register a node with the object cache using its primary key.
180: *
181: * @param node
182: */
183: public void registerNode(Node node) {
184: nmgr.registerNode(node);
185: }
186:
187: /**
188: * Register a node with the object cache using the given key.
189: *
190: * @param node
191: */
192: public void registerNode(Node node, Key key) {
193: nmgr.registerNode(node, key);
194: }
195:
196: /**
197: * Evict a node from the object cache
198: *
199: * @param node
200: */
201: public void evictNode(Node node) {
202: nmgr.evictNode(node);
203: }
204:
205: /**
206: * Completely evict the object with the given key from the object cache
207: *
208: * @param key
209: */
210: public void evictNodeByKey(Key key) {
211: nmgr.evictNodeByKey(key);
212: }
213:
214: /**
215: * Evict the object with the given key from the object cache
216: *
217: * @param key
218: */
219: public void evictKey(Key key) {
220: nmgr.evictKey(key);
221: }
222:
223: /**
224: * Generate a new id for an object specified by the DbMapping
225: *
226: * @param map the DbMapping to generate an id for
227: * @return a new unique id
228: */
229: public String generateID(DbMapping map) {
230: try {
231: return nmgr.generateID(map);
232: } catch (Exception x) {
233: throw new RuntimeException(x.toString(), x);
234: }
235: }
236:
237: /**
238: * Gets the application's root node.
239: */
240: public Node getRootNode() {
241: try {
242: return nmgr.getRootNode();
243: } catch (Exception x) {
244: throw new RuntimeException(x.toString(), x);
245: }
246: }
247:
248: /**
249: * Checks if the given node is the application's root node.
250: */
251: public boolean isRootNode(Node node) {
252: return nmgr.isRootNode(node);
253: }
254:
255: /**
256: * Get an array of all objects in the object cache
257: */
258: public Object[] getCacheEntries() {
259: return nmgr.getCacheEntries();
260: }
261:
262: /**
263: * Write an entry to the application's event log
264: *
265: * @param msg event message
266: */
267: public void logEvent(String msg) {
268: nmgr.app.logEvent(msg);
269: }
270:
271: /**
272: * Get the DbMapping corresponding to a type name
273: *
274: * @param name a type name
275: * @return the corresponding DbMapping
276: */
277: public DbMapping getDbMapping(String name) {
278: return nmgr.app.getDbMapping(name);
279: }
280: }
|