KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > SnipImpl


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.snip;
26
27 import org.picocontainer.PicoContainer;
28 import org.radeox.api.engine.RenderEngine;
29 import org.radeox.api.engine.context.RenderContext;
30 import org.radeox.util.logging.Logger;
31 import org.snipsnap.app.Application;
32 import org.snipsnap.container.Components;
33 import org.snipsnap.interceptor.Aspects;
34 import org.snipsnap.render.context.SnipRenderContext;
35 import org.snipsnap.snip.attachment.Attachment;
36 import org.snipsnap.snip.attachment.Attachments;
37 import org.snipsnap.snip.label.Labels;
38 import org.snipsnap.snip.label.RenderEngineLabel;
39 import org.snipsnap.snip.name.NameFormatter;
40 import org.snipsnap.snip.name.PathRemoveFormatter;
41 import org.snipsnap.snip.name.WeblogNameFormatter;
42 import org.snipsnap.user.Permissions;
43 import org.snipsnap.user.User;
44
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import java.io.BufferedInputStream JavaDoc;
47 import java.io.BufferedOutputStream JavaDoc;
48 import java.io.File JavaDoc;
49 import java.io.FileInputStream JavaDoc;
50 import java.io.FileOutputStream JavaDoc;
51 import java.io.IOException JavaDoc;
52 import java.io.Writer JavaDoc;
53 import java.sql.Timestamp JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56
57 /**
58  * Central class for snips.
59  * <p/>
60  * TODO: cUser, mUser, cTime, ... -> modified to composite object
61  *
62  * @author Stephan J. Schmidt
63  * @version $Id: SnipImpl.java 1716 2004-07-16 14:33:35Z leo $
64  */

65 public class SnipImpl implements Snip {
66   private NameFormatter nameFormatter;
67   private String JavaDoc applicationOid;
68
69   //@TODO think about that
70
public Snip parent;
71   private List JavaDoc children;
72   private Snip comment;
73   private Comments comments;
74   private String JavaDoc name, content;
75   private String JavaDoc oUser;
76
77   // @TODO: Composite Object
78
private Permissions permissions;
79   private Access access;
80   private Labels labels;
81   private Attachments attachments;
82   private Modified modified;
83   private int version = 1;
84
85   // @TODO: Remove
86
private String JavaDoc commentedName;
87   private String JavaDoc parentName;
88
89   private void init() {
90     if (null == children) {
91       children = SnipSpaceFactory.getInstance().getChildren((Snip) Aspects.getThis());
92     }
93   }
94
95   public SnipImpl(String JavaDoc name, String JavaDoc content) {
96     this.name = name;
97     this.content = content;
98     this.modified = new Modified();
99     this.access = new Access();
100   }
101
102   public void handle(HttpServletRequest JavaDoc request) {
103     access.handle(name, request);
104     SnipSpaceFactory.getInstance().delayedStore((Snip) Aspects.getThis());
105   }
106
107   public int getVersion() {
108     return this.version;
109   }
110
111   public void setVersion(int version) {
112     this.version = version;
113   }
114
115   public void setApplication(String JavaDoc applicationOid) {
116     this.applicationOid = applicationOid;
117   }
118
119   public String JavaDoc getApplication() {
120     return applicationOid;
121   }
122
123   public Access getAccess() {
124     return access;
125   }
126
127   public Modified getModified() {
128     return modified;
129   }
130
131   /**
132    * Returns true, when the snip is a weblog.
133    * Currently only test against 'start'.
134    * Should be extendet to test a "weblog"-label
135    *
136    * @return true, if the snip is a weblog
137    */

138   public boolean isWeblog() {
139     return content.indexOf("{weblog") != -1;
140   }
141
142   /**
143    * Conveniance function for JSP
144    *
145    * @return true, if snip is not a weblog
146    */

147   public boolean isNotWeblog() {
148     return !isWeblog();
149   }
150
151   public String JavaDoc getOwner() {
152     return getCUser();
153   }
154
155   public boolean isOwner(User user) {
156     return user.getLogin().equals(getOwner());
157   }
158
159   public void addPermission(String JavaDoc permission, String JavaDoc role) {
160     permissions.add(permission, role);
161   }
162
163   public void setPermissions(Permissions permissions) {
164     this.permissions = permissions;
165   }
166
167   public Permissions getPermissions() {
168     return permissions;
169   }
170
171   public String JavaDoc getOUser() {
172     return oUser;
173   }
174
175   public void setOUser(User oUser) {
176     this.oUser = oUser.getLogin();
177   }
178
179   public void setOUser(String JavaDoc oUser) {
180     this.oUser = oUser;
181   }
182
183   public Attachments getAttachments() {
184     return attachments;
185   }
186
187   public void setAttachments(Attachments attachments) {
188     this.attachments = attachments;
189   }
190
191   public Labels getLabels() {
192     return labels;
193   }
194
195   public void setLabels(Labels labels) {
196     this.labels = labels;
197   }
198
199   public Links getBackLinks() {
200     return access.getBackLinks();
201   }
202
203   public Links getSnipLinks() {
204     return access.getSnipLinks();
205   }
206
207   public void setBackLinks(Links backLinks) {
208     access.setBackLinks(backLinks);
209   }
210
211   public void setSnipLinks(Links snipLinks) {
212     access.setSnipLinks(snipLinks);
213   }
214
215   public int getViewCount() {
216     return access.getViewCount();
217   }
218
219   public void setViewCount(int count) {
220     access.setViewCount(count);
221   }
222
223   public int incViewCount() {
224     return access.incViewCount();
225   }
226
227   public Timestamp JavaDoc getCTime() {
228     return modified.getcTime();
229   }
230
231   public void setCTime(Timestamp JavaDoc cTime) {
232     this.modified.setcTime(cTime);
233   }
234
235   public Timestamp JavaDoc getMTime() {
236     return modified.getmTime();
237   }
238
239   public void setMTime(Timestamp JavaDoc mTime) {
240     this.modified.setmTime(mTime);
241   }
242
243   public String JavaDoc getCUser() {
244     return modified.getcUser();
245   }
246
247   public void setCUser(User cUser) {
248     this.modified.setcUser(cUser.getLogin());
249   }
250
251   public void setCUser(String JavaDoc cUser) {
252     this.modified.setcUser(cUser);
253   }
254
255   public String JavaDoc getMUser() {
256     return modified.getmUser();
257   }
258
259   public void setMUser(User mUser) {
260     this.modified.setmUser(mUser.getLogin());
261   }
262
263   public void setMUser(String JavaDoc mUser) {
264     this.modified.setmUser(mUser);
265   }
266
267   public List JavaDoc getChildren() {
268     init();
269     return children;
270   }
271
272   public void setCommentedSnip(Snip comment) {
273     this.comment = comment;
274   }
275
276   public Snip getCommentedSnip() {
277     if (null != commentedName && !"".equals(commentedName) && null == comment) {
278       comment = SnipSpaceFactory.getInstance().load(commentedName);
279     }
280     return comment;
281   }
282
283   public boolean isComment() {
284     return !(null == getCommentedSnip());
285   }
286
287   public Comments getComments() {
288     if (null == comments) {
289       comments = new Comments((Snip) Aspects.getThis());
290     }
291     return comments;
292   }
293
294   /**
295    * Get a list of child snips, ordered by date with
296    * the newest one first
297    *
298    * @return List of child snips
299    */

300   public List JavaDoc getChildrenDateOrder() {
301     return SnipSpaceFactory.getInstance().getChildrenDateOrder((Snip) Aspects.getThis(), 10);
302   }
303
304   public List JavaDoc getChildrenModifiedOrder() {
305     return SnipSpaceFactory.getInstance().getChildrenModifiedOrder((Snip) Aspects.getThis(), 10);
306   }
307
308   /**
309    * Add a child snip. Sets the parent of
310    * the child to this snip and <b>stores</b> the
311    * child because of the new parent.
312    *
313    * @param snip Snip to add as child
314    */

315   public void addSnip(Snip snip) {
316     init();
317     if (!children.contains(snip)) {
318       snip.setParent((Snip) Aspects.getThis());
319       children.add(snip);
320       SnipSpaceFactory.getInstance().systemStore(snip);
321     }
322   }
323
324   /**
325    * Removes child snip from this nsip
326    *
327    * @param snip Child to remove
328    */

329   public void removeSnip(Snip snip) {
330     init();
331     if (children.contains(snip)) {
332       children.remove(snip);
333       // snip.setParent(null);
334
}
335   }
336
337   public Snip getParent() {
338     if (null != parentName && !"".equals(parentName) && null == parent) {
339       parent = SnipSpaceFactory.getInstance().load(parentName);
340     }
341     return parent;
342   }
343
344   /**
345    * Directly sets the parent snip, does
346    * not add the snip to the parent.
347    * This is needed for restoring from storage.
348    * Better solution wanted
349    *
350    * @param parentSnip new parent snip of this snip
351    */

352   public void setDirectParent(Snip parentSnip) {
353     this.parent = parentSnip;
354   }
355
356   public void setParentName(String JavaDoc name) {
357     this.parentName = name;
358   }
359
360   public String JavaDoc getParentName() {
361     return this.parent == null ? parentName : this.parent.getName();
362   }
363
364   public void setCommentedName(String JavaDoc name) {
365     this.commentedName = name;
366   }
367
368   public String JavaDoc getCommentedName() {
369     return this.parent == null ? commentedName : this.comment.getName();
370   }
371
372   public void setParent(Snip parentSnip) {
373     if (parentSnip == this.parent) {
374       return;
375     }
376
377     if (null != this.parent) {
378       this.parent.removeSnip((Snip) Aspects.getThis());
379     }
380     this.parent = parentSnip;
381     parentSnip.addSnip((Snip) Aspects.getThis());
382   }
383
384   public String JavaDoc getName() {
385     return name;
386   }
387
388   /**
389    * Return a short version of the name.
390    * Useful for vertical snip listings, where
391    * the snips should not be to long.
392    * End of snip name will be replaced with "..."
393    *
394    * @return Short name of snip
395    */

396   public String JavaDoc getShortName() {
397     return SnipLink.cutLength(getName(), 20);
398   }
399
400   /**
401    * Return an encoded version of the name,
402    * especially spaces replaced with "+"
403    *
404    * @return encoded name of snip
405    */

406   public String JavaDoc getNameEncoded() {
407     try {
408       return SnipLink.encode(getName());
409     } catch (Exception JavaDoc e) {
410       return getName();
411     }
412   }
413
414   public void setName(String JavaDoc name) {
415     this.name = name;
416   }
417
418   public String JavaDoc getContent() {
419     return content;
420   }
421
422   public void setContent(String JavaDoc content) {
423     this.content = content;
424   }
425
426   public String JavaDoc getLink() {
427     return SnipLink.createLink(this.name);
428   }
429
430   public String JavaDoc getAttachmentString() {
431     StringBuffer JavaDoc tmp = new StringBuffer JavaDoc();
432     Iterator JavaDoc it = attachments.iterator();
433     File JavaDoc fileStorePath = new File JavaDoc(Application.get().getConfiguration().getFileStore(), "snips");
434     while (it.hasNext()) {
435       Attachment att = (Attachment) it.next();
436       File JavaDoc file = new File JavaDoc(fileStorePath, att.getLocation());
437       if (file.exists()) {
438         tmp.append(SnipLink.createLink(SnipLink.getSpaceRoot() + "/" + SnipLink.encode(name), att.getName(), att.getName()));
439         tmp.append(" (").append(att.getSize()).append(")");
440         if (it.hasNext()) {
441           tmp.append("<br/> ");
442         }
443       } else {
444         Logger.log(Logger.WARN, file.getAbsolutePath() + " is missing");
445       }
446     }
447     return tmp.toString();
448   }
449
450   public String JavaDoc toXML() {
451     //long start = Application.get().start();
452
PicoContainer container = Components.getContainer();
453
454 // Label typeLabel = getLabels().getLabel("Type");
455
// if(typeLabel instanceof TypeLabel) {
456
// String viewHandler = ((TypeLabel)typeLabel).getViewHandler();
457
//
458
// }
459

460     RenderEngineLabel reLabel =
461             (RenderEngineLabel) getLabels().getLabel("RenderEngine");
462     RenderEngine engine = null;
463     if (reLabel != null) {
464       try {
465         engine = (RenderEngine) container.getComponentInstance(Class.forName(reLabel.getValue()));
466       } catch (ClassNotFoundException JavaDoc e) {
467         e.printStackTrace();
468       }
469     }
470
471     // make sure we get a render engine
472
if (null == engine) {
473       engine = (RenderEngine) container.getComponentInstance(Components.DEFAULT_ENGINE);
474     }
475
476
477     RenderContext context = new SnipRenderContext((Snip) Aspects.getThis(),
478                                                   (SnipSpace) container.getComponentInstance(SnipSpace.class));
479     context.setParameters(Application.get().getParameters());
480
481     String JavaDoc xml = "";
482     // should the engine be set by the engine to the context?
483
// System.out.println("RENDERING: " + getName());
484
// System.out.flush();
485

486     xml = engine.render(content, context);
487     //Logger.debug(getName() + " is cacheable: " + context.isCacheable());
488
//String xml = SnipFormatter.toXML(this, getContent());
489
//Application.get().stop(start, "Formatting " + name);
490
return xml;
491   }
492
493   public String JavaDoc getXMLContent() {
494     String JavaDoc tmp = null;
495     try {
496       tmp = toXML();
497     } catch (Exception JavaDoc e) {
498       tmp = "<span class=\"error\">" + e + "</span>";
499       e.printStackTrace();
500       Logger.warn("SnipImpl: unable to get XMLContent", e);
501     } catch (Error JavaDoc err) {
502       err.printStackTrace();
503       tmp = "<span class=\"error\">" + err + "</span>";
504     }
505
506     return tmp;
507   }
508
509   public Writer JavaDoc appendTo(Writer JavaDoc s) throws IOException JavaDoc {
510     s.write(getXMLContent());
511     return s;
512   }
513
514   public SnipPath getPath() {
515     return new SnipPath(this);
516   }
517
518   public String JavaDoc getTitle() {
519     if (null == nameFormatter) {
520       nameFormatter = new PathRemoveFormatter();
521       nameFormatter.setParent(new WeblogNameFormatter());
522     }
523     return nameFormatter.format(name);
524   }
525
526   public int hashCode() {
527     return name.hashCode();
528   }
529
530 // public String toString() {
531
// return "{name="+getName()+", parent="+parent+", @"+hashCode()+"}";
532
// }
533

534   public boolean equals(Object JavaDoc obj) {
535     if (!(obj instanceof Snip)) {
536       return false;
537     }
538
539     return ((Snip) obj).getName().equals((this.name));
540   }
541
542   public String JavaDoc toString() {
543     return getName();
544   }
545
546   public Snip copy(String JavaDoc newName) {
547     SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
548     Snip newSnip = space.create(newName, getContent());
549     newSnip.setLabels(new Labels(newSnip, getLabels().toString()));
550     newSnip.setPermissions(getPermissions());
551
552     List JavaDoc atts = getAttachments().getAll();
553     Iterator JavaDoc attsIt = atts.iterator();
554     File JavaDoc fileStorePath = new File JavaDoc(Application.get().getConfiguration().getFileStore(), "snips");
555     while (attsIt.hasNext()) {
556       Attachment oldAtt = (Attachment) attsIt.next();
557       Attachment att = newSnip.getAttachments().addAttachment(oldAtt.getName(), oldAtt.getContentType(), oldAtt.getSize(), oldAtt.getLocation());
558       att.setDate(oldAtt.getDate());
559       String JavaDoc location = att.getLocation();
560       File JavaDoc attFile = new File JavaDoc(fileStorePath, location);
561       if (attFile.exists()) {
562         try {
563           File JavaDoc newLocation = new File JavaDoc(newSnip.getName(), attFile.getName());
564           File JavaDoc newAttFile = new File JavaDoc(fileStorePath, newLocation.getPath());
565           newAttFile.getParentFile().mkdirs();
566           BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(newAttFile));
567           BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(attFile));
568           byte buf[] = new byte[4096];
569           int length = -1;
570           while ((length = in.read(buf)) != -1) {
571             out.write(buf, 0, length);
572           }
573           out.flush();
574           out.close();
575           in.close();
576           att.setLocation(newLocation.getPath());
577         } catch (IOException JavaDoc e) {
578           Logger.warn("SnipImpl: unable to copy attachment: " + attFile, e);
579         }
580       } else {
581         newSnip.getAttachments().removeAttachment(att);
582       }
583     }
584     space.store(newSnip);
585     return newSnip;
586   }
587 }
588
Popular Tags