1 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 ; 46 import java.io.BufferedInputStream ; 47 import java.io.BufferedOutputStream ; 48 import java.io.File ; 49 import java.io.FileInputStream ; 50 import java.io.FileOutputStream ; 51 import java.io.IOException ; 52 import java.io.Writer ; 53 import java.sql.Timestamp ; 54 import java.util.Iterator ; 55 import java.util.List ; 56 57 65 public class SnipImpl implements Snip { 66 private NameFormatter nameFormatter; 67 private String applicationOid; 68 69 public Snip parent; 71 private List children; 72 private Snip comment; 73 private Comments comments; 74 private String name, content; 75 private String oUser; 76 77 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 private String commentedName; 87 private String 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 name, String 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 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 applicationOid) { 116 this.applicationOid = applicationOid; 117 } 118 119 public String 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 138 public boolean isWeblog() { 139 return content.indexOf("{weblog") != -1; 140 } 141 142 147 public boolean isNotWeblog() { 148 return !isWeblog(); 149 } 150 151 public String 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 permission, String 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 getOUser() { 172 return oUser; 173 } 174 175 public void setOUser(User oUser) { 176 this.oUser = oUser.getLogin(); 177 } 178 179 public void setOUser(String 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 getCTime() { 228 return modified.getcTime(); 229 } 230 231 public void setCTime(Timestamp cTime) { 232 this.modified.setcTime(cTime); 233 } 234 235 public Timestamp getMTime() { 236 return modified.getmTime(); 237 } 238 239 public void setMTime(Timestamp mTime) { 240 this.modified.setmTime(mTime); 241 } 242 243 public String 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 cUser) { 252 this.modified.setcUser(cUser); 253 } 254 255 public String 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 mUser) { 264 this.modified.setmUser(mUser); 265 } 266 267 public List 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 300 public List getChildrenDateOrder() { 301 return SnipSpaceFactory.getInstance().getChildrenDateOrder((Snip) Aspects.getThis(), 10); 302 } 303 304 public List getChildrenModifiedOrder() { 305 return SnipSpaceFactory.getInstance().getChildrenModifiedOrder((Snip) Aspects.getThis(), 10); 306 } 307 308 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 329 public void removeSnip(Snip snip) { 330 init(); 331 if (children.contains(snip)) { 332 children.remove(snip); 333 } 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 352 public void setDirectParent(Snip parentSnip) { 353 this.parent = parentSnip; 354 } 355 356 public void setParentName(String name) { 357 this.parentName = name; 358 } 359 360 public String getParentName() { 361 return this.parent == null ? parentName : this.parent.getName(); 362 } 363 364 public void setCommentedName(String name) { 365 this.commentedName = name; 366 } 367 368 public String 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 getName() { 385 return name; 386 } 387 388 396 public String getShortName() { 397 return SnipLink.cutLength(getName(), 20); 398 } 399 400 406 public String getNameEncoded() { 407 try { 408 return SnipLink.encode(getName()); 409 } catch (Exception e) { 410 return getName(); 411 } 412 } 413 414 public void setName(String name) { 415 this.name = name; 416 } 417 418 public String getContent() { 419 return content; 420 } 421 422 public void setContent(String content) { 423 this.content = content; 424 } 425 426 public String getLink() { 427 return SnipLink.createLink(this.name); 428 } 429 430 public String getAttachmentString() { 431 StringBuffer tmp = new StringBuffer (); 432 Iterator it = attachments.iterator(); 433 File fileStorePath = new File (Application.get().getConfiguration().getFileStore(), "snips"); 434 while (it.hasNext()) { 435 Attachment att = (Attachment) it.next(); 436 File file = new File (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 toXML() { 451 PicoContainer container = Components.getContainer(); 453 454 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 e) { 467 e.printStackTrace(); 468 } 469 } 470 471 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 xml = ""; 482 486 xml = engine.render(content, context); 487 return xml; 491 } 492 493 public String getXMLContent() { 494 String tmp = null; 495 try { 496 tmp = toXML(); 497 } catch (Exception e) { 498 tmp = "<span class=\"error\">" + e + "</span>"; 499 e.printStackTrace(); 500 Logger.warn("SnipImpl: unable to get XMLContent", e); 501 } catch (Error err) { 502 err.printStackTrace(); 503 tmp = "<span class=\"error\">" + err + "</span>"; 504 } 505 506 return tmp; 507 } 508 509 public Writer appendTo(Writer s) throws IOException { 510 s.write(getXMLContent()); 511 return s; 512 } 513 514 public SnipPath getPath() { 515 return new SnipPath(this); 516 } 517 518 public String 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 534 public boolean equals(Object obj) { 535 if (!(obj instanceof Snip)) { 536 return false; 537 } 538 539 return ((Snip) obj).getName().equals((this.name)); 540 } 541 542 public String toString() { 543 return getName(); 544 } 545 546 public Snip copy(String 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 atts = getAttachments().getAll(); 553 Iterator attsIt = atts.iterator(); 554 File fileStorePath = new File (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 location = att.getLocation(); 560 File attFile = new File (fileStorePath, location); 561 if (attFile.exists()) { 562 try { 563 File newLocation = new File (newSnip.getName(), attFile.getName()); 564 File newAttFile = new File (fileStorePath, newLocation.getPath()); 565 newAttFile.getParentFile().mkdirs(); 566 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (newAttFile)); 567 BufferedInputStream in = new BufferedInputStream (new FileInputStream (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 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 |