Source code: com/obinary/cms/core/Content.java
1 /**
2 *
3 * Magnolia and its source-code is licensed under the LGPL.
4 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6 * you are required to provide proper attribution to obinary.
7 * If you reproduce or distribute the document without making any substantive modifications to its content,
8 * please use the following attribution line:
9 *
10 * Copyright 1993-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17 package com.obinary.cms.core;
18
19
20
21
22
23 import com.obinary.cms.admin.Authenticator;
24
25 import javax.jcr.*;
26 import javax.jcr.objectclass.NoSuchObjectClassException;
27 import javax.servlet.http.HttpServletRequest;
28 import java.io.OutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32
33 /**
34 * User: sameercharles
35 * Date: Apr 20, 2003
36 * Time: 10:14:21 AM
37 * @author Sameer Charles
38 * @version 1.0
39 */
40
41
42 public class Content extends ContentHandler {
43
44
45 private String path;
46 private Node rootNode;
47
48
49
50 /**
51 * constructor
52 *
53 */
54 public Content () {
55 }
56
57
58
59 /**
60 * constructor
61 *
62 * @param path absolute (primary) path to this <code>Node</code>
63 */
64 public Content (String path) {
65 this.path = path;
66 }
67
68
69
70 /**
71 * constructor to get existing node
72 *
73 * @param rootNode node to start with
74 * @param path absolute (primary) path to this <code>Node</code>
75 * @throws ElementNotFoundException
76 * @throws RepositoryException
77 */
78 public Content (Node rootNode, String path) throws ElementNotFoundException, RepositoryException {
79 this.path = path;
80 this.rootNode = rootNode;
81 this.init();
82 }
83
84
85
86 /**
87 * constructor to get existing node
88 *
89 * @param elem , initialized node object
90 * @throws ElementNotFoundException
91 * @throws RepositoryException
92 */
93 public Content (Element elem) throws ElementNotFoundException, RepositoryException {
94 this.node = (Node)elem;
95 this.path = this.getHandle();
96 }
97
98
99
100 /**
101 * constructor to create a new node
102 *
103 * @param rootNode node to start with
104 * @param in InputStream , used to create content under the root node
105 * @throws ElementNotFoundException
106 * @throws RepositoryException
107 */
108 public Content (Node rootNode, InputStream in) throws ElementNotFoundException, RepositoryException {
109 this.rootNode = rootNode;
110 this.node = rootNode.addNode(in);
111 this.path = this.getHandle();
112 }
113
114
115
116 /**
117 * Package private constructor
118 *
119 * @param rootNode node to start with
120 * @param path absolute (primary) path to this <code>Node</code>
121 * @param isHierarchyNode create a new node as hierarchy node, else node
122 * @throws ElementNotFoundException
123 * @throws RepositoryException
124 */
125 public Content (Node rootNode, String path, boolean isHierarchyNode) throws ElementNotFoundException, RepositoryException {
126 this.path = path;
127 this.rootNode = rootNode;
128 if (isHierarchyNode)
129 this.node = this.rootNode.addHierarchyNode(this.path);
130 else
131 this.node = this.rootNode.addNode(this.path);
132 }
133
134
135
136 /**
137 * Package private constructor
138 *
139 * @param rootNode node to start with
140 * @param path absolute (primary) path to this <code>Node</code>
141 * @param isHierarchyNode create a new node as hierarchy node, else node
142 * @param adjustName
143 * @throws ElementNotFoundException
144 * @throws RepositoryException
145 */
146 public Content (Node rootNode, String path, boolean isHierarchyNode, boolean adjustName) throws ElementNotFoundException, RepositoryException {
147 this.path = path;
148 this.rootNode = rootNode;
149 if (isHierarchyNode) {
150 this.node = this.rootNode.addHierarchyNode(this.path,adjustName);
151 }
152 else
153 this.node = this.rootNode.addNode(this.path,adjustName);
154 }
155
156
157
158 /**
159 * <p>Initializes node content (top level properties)</p>
160 *
161 * @throws ElementNotFoundException RepositoryException
162 */
163 protected void init() throws ElementNotFoundException, RepositoryException {
164 this.node = this.rootNode.getNode(this.path);
165 }
166
167
168 public void addObjectClass(String objectClassName) throws NoSuchObjectClassException, ConstraintViolationException {
169 this.node.addObjectClass(objectClassName);
170 }
171
172
173 /**
174 * <p>get ContainerList node of the current node with the specified name</p>
175 *
176 * @param containerListName name of the node acting as <code>ContainerList</code>
177 * @return <code>ContainerList</code>
178 * @throws ElementNotFoundException
179 * @throws RepositoryException
180 */
181 public ContainerList getContainerList(String containerListName) throws ElementNotFoundException, RepositoryException {
182 return new ContainerList(this.node, containerListName);
183 }
184
185
186
187 /**
188 * <p>create ContainerList node under the current node with the specified name</p>
189 *
190 * @param containerListName name of the node to be created as <code>ContainerList</code>
191 * @return newly created <node>ContainerList</node>
192 * @throws ElementNotFoundException
193 * @throws RepositoryException
194 */
195 public ContainerList createContainerList(String containerListName) throws ElementNotFoundException, RepositoryException {
196 return new ContainerList(this.node, containerListName, true);
197 }
198
199
200
201 /**
202 * <p>get Container node of the current node with the specified name</p>
203 *
204 * @param containerName name of the node acting as <code>Container</code>
205 * @return <node>Container</node>
206 * @throws ElementNotFoundException
207 * @throws RepositoryException
208 */
209 public Container getContainer(String containerName) throws ElementNotFoundException, RepositoryException {
210 return (new Container(this.node, containerName));
211 }
212
213
214
215 /**
216 * <p>create Container node under the current node with the specified name</p>
217 *
218 * @param containerName name of the node to be created as <code>Container</code>
219 * @return newly created <node>Container</node>
220 * @throws ElementNotFoundException
221 * @throws RepositoryException
222 */
223 public Container createContainer(String containerName) throws ElementNotFoundException, RepositoryException {
224 return (new Container(this.node, containerName, true));
225 }
226
227
228
229 /**
230 * <p>get meta data of the current node</p>
231 *
232 * @return MetaData meta information of the content <code>Node</code>
233 */
234 public MetaData getMetaData() {
235 return new MetaData(this.node);
236
237 }
238
239
240
241 /**
242 * <p>get meta data of the current node</p>
243 *
244 * @throws RepositoryException
245 * @return MetaData meta information of the context under the content <code>Node</code>
246 */
247 public MetaData getMetaData(String context) throws RepositoryException {
248 return new MetaData(this.node,context);
249 }
250
251
252
253 /**
254 * <p>get top level atom</p>
255 *
256 * @return Atom requested <code>Atom</code> object
257 */
258 public Atom getAtom(String atomName) {
259 try {
260 return (new Atom(this.node, atomName));
261 } catch (RepositoryException re) {return (new Atom());}
262 }
263
264
265
266 /**
267 * <p>get node name</p>
268 *
269 * @return String name of the current <code>Node</code>
270 */
271 public String getName() {
272 return this.node.getName();
273 }
274
275
276
277 /**
278 *
279 * <p>create top level atom</p>
280 *
281 * @param atomName to be created
282 * @return Atom requested <code>Atom</code> object
283 * @throws ElementNotFoundException
284 * @throws RepositoryException
285 */
286 public Atom createAtom(String atomName) throws ElementNotFoundException, RepositoryException {
287 return (new Atom(this.node, atomName, true));
288 }
289
290
291
292 /**
293 *
294 * <p>create top level atom with the given value and type</p>
295 *
296 * @param atomName to be created
297 * @param value to be set initially
298 * @param type propertyType
299 * @return Atom requested <code>Atom</code> object
300 * @throws ElementNotFoundException
301 * @throws RepositoryException
302 */
303 public Atom createAtom(String atomName, Value value, int type) throws ElementNotFoundException, RepositoryException {
304 return (new Atom(this.node, atomName, value, type));
305 }
306
307
308
309 /**
310 *
311 * <p>create atom with the specified name</p>
312 *
313 * @throws ElementNotFoundException
314 * @throws RepositoryException
315 */
316 public void deleteAtom(String atomName) throws ElementNotFoundException, RepositoryException {
317 this.node.remove(atomName);
318 }
319
320
321
322 /**
323 *
324 * <p>delete container with the specified name from the current node</p>
325 *
326 * @param containerName to be deleted
327 * @throws javax.jcr.ElementNotFoundException
328 * @throws javax.jcr.RepositoryException
329 */
330 public void deleteContainer(String containerName) throws ElementNotFoundException, RepositoryException {
331 this.node.remove(containerName);
332 }
333
334
335
336 /**
337 *
338 * <p>delete container list with the specified name from the current node</p>
339 *
340 * @param containerListName to be deleted
341 * @throws javax.jcr.ElementNotFoundException
342 * @throws javax.jcr.RepositoryException
343 */
344 public void deleteContainerList(String containerListName) throws ElementNotFoundException, RepositoryException {
345 this.node.remove(containerListName);
346 }
347
348
349
350 /**
351 *
352 * <p>move content to the specified location</p>
353 *
354 * @param path where current node has to be moved
355 * @throws javax.jcr.ElementNotFoundException
356 * @throws javax.jcr.RepositoryException
357 */
358 public void moveTo(String path) throws ElementNotFoundException, RepositoryException {
359 this.node.moveTo(path);
360 }
361
362
363
364 /**
365 *
366 * <p>copy content to the specified location</p>
367 *
368 * @param path where current node has to be copied
369 * @throws javax.jcr.ElementNotFoundException
370 * @throws javax.jcr.RepositoryException
371 */
372 public void copyTo(String path) throws ElementNotFoundException, RepositoryException {
373 this.node.copyTo(path);
374 }
375
376
377
378 /**
379 *
380 * <p>move current node to the specified location above the named <code>beforename</code>
381 * </p>
382 *
383 * @param parentPath where current node has to be moved
384 * @param beforeName name of the node before the current node has to be placed
385 * @throws javax.jcr.ElementNotFoundException
386 * @throws javax.jcr.RepositoryException
387 */
388 public void order(String parentPath,String beforeName) throws ElementNotFoundException, RepositoryException {
389 this.node.orderElement(parentPath, beforeName);
390 }
391
392
393
394 /**
395 * <p>get xml data as bite stream<br>
396 * </p>
397 *
398 * @param out OutputStream to which xml bite stream will be written
399 * @param onlyThis boolean saying weather to stream only current node or the
400 * entire sub tree.
401 * @throws javax.jcr.ElementNotFoundException
402 * @throws javax.jcr.RepositoryException
403 */
404 public void toStream(OutputStream out, boolean onlyThis) throws IOException, RepositoryException {
405 this.node.serialize(out,false,onlyThis);
406 }
407
408
409
410 /**
411 * <p>you could call this method anytime to update working page properties
412 * - Modification date & Author ID </p>
413 */
414 public void updateMetaData(HttpServletRequest request) {
415 MetaData md = this.getMetaData();
416 md.setModificationDate();
417 md.setAuthorId(Authenticator.getUserId(request));
418 md = null;
419 }
420
421
422
423 /**
424 * <p>get Node object used to create current content object</p>
425 *
426 * @return Node
427 */
428 public Node getJCRNode() {
429 return this.node;
430 }
431
432
433
434 }