[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/comments ...

Christian Bauer christian at hibernate.org
Thu Apr 5 10:38:25 EDT 2007


  User: cbauer  
  Date: 07/04/05 10:38:25

  Added:       examples/wiki/src/main/org/jboss/seam/wiki/plugin/comments  
                        Comment.java CommentHome.java
  Log:
  Added broken user comment plugin
  
  Revision  Changes    Path
  1.1      date: 2007/04/05 14:38:25;  author: cbauer;  state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/comments/Comment.java
  
  Index: Comment.java
  ===================================================================
  package org.jboss.seam.wiki.plugin.comments;
  
  import org.hibernate.validator.Length;
  import org.hibernate.validator.Email;
  import org.jboss.seam.wiki.core.model.Document;
  import org.jboss.seam.wiki.core.model.User;
  
  import javax.persistence.*;
  import java.util.Date;
  import java.io.Serializable;
  
  @Entity
  @Table(name = "COMMENTS")
  public class Comment implements Serializable {
  
      @Id
      @GeneratedValue
      @Column(name = "COMMENT_ID")
      private Long id = null;
  
      @Version
      @Column(name = "OBJ_VERSION")
      private int version = 0;
  
      @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
      @JoinColumn(name = "DOCUMENT_ID", nullable = false)
      private Document document;
  
      @Column(name = "SUBJECT", nullable = false)
      @Length(min = 3, max = 255)
      private String subject;
  
      @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
      @JoinColumn(name = "FROM_USER_ID", nullable = true)
      private User fromUser;
  
      @Column(name = "FROM_USER_NAME", nullable = false)
      @Length(min = 3, max = 100)
      private String fromUserName;
  
      @Column(name = "FROM_USER_EMAIL", nullable = true)
      @Length(min = 0, max = 255)
      @Email
      private String fromUserEmail;
  
      @Column(name = "FROM_USER_HOMEPAGE", nullable = true)
      @Length(min = 0, max = 1000)
      private String fromUserHomepage;
  
      @Column(name = "COMMENT_TEXT", nullable = false)
      //@Length(min = 1, max = 32768)
      private String text;
  
      @Column(name = "CREATED_ON", nullable = false, updatable = false)
      private Date createdOn = new Date();
      
      public Comment () {}
  
      // Immutable properties
  
      public Long getId() { return id; }
      public Integer getVersion() { return version; }
      public Date getCreatedOn() { return createdOn; }
  
      // Mutable properties
  
      public Document getDocument() {
          return document;
      }
  
      public void setDocument(Document document) {
          this.document = document;
      }
  
      public String getSubject() {
          return subject;
      }
  
      public void setSubject(String subject) {
          this.subject = subject;
      }
  
      public String getFromUserName() {
          System.out.println("########## GET COMMENT FROM: " +fromUserName);
          return fromUserName;
      }
  
      public void setFromUserName(String fromUserName) {
          System.out.println("########## SET COMMENT FROM: " +fromUserName);
          this.fromUserName = fromUserName;
      }
  
      public String getFromUserEmail() {
          return fromUserEmail;
      }
  
      public void setFromUserEmail(String fromUserEmail) {
          this.fromUserEmail = fromUserEmail;
      }
  
      public String getFromUserHomepage() {
          return fromUserHomepage;
      }
  
      public void setFromUserHomepage(String fromUserHomepage) {
          this.fromUserHomepage = fromUserHomepage;
      }
  
      public User getFromUser() {
          return fromUser;
      }
  
      public void setFromUser(User fromUser) {
          this.fromUser = fromUser;
      }
  
      public String getText() {
          return text;
      }
  
      public void setText(String text) {
          this.text = text;
      }
  
      // Misc methods
  
      public String toString() {
          return  "Comment ('" + getId() + "'), " +
                  "Subject: '" + getSubject() + "'";
      }
  }
  
  
  
  1.1      date: 2007/04/05 14:38:25;  author: cbauer;  state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/plugin/comments/CommentHome.java
  
  Index: CommentHome.java
  ===================================================================
  package org.jboss.seam.wiki.plugin.comments;
  
  import org.jboss.seam.annotations.*;
  import org.jboss.seam.ScopeType;
  import org.jboss.seam.Component;
  import org.jboss.seam.core.FacesMessages;
  import org.jboss.seam.wiki.core.model.Document;
  import org.jboss.seam.wiki.core.model.User;
  import org.jboss.seam.wiki.core.model.Directory;
  import org.jboss.seam.wiki.core.action.prefs.WikiPreferences;
  import org.jboss.seam.wiki.core.engine.WikiLinkResolver;
  import org.jboss.seam.wiki.util.WikiUtil;
  
  import javax.persistence.EntityManager;
  import javax.faces.application.FacesMessage;
  import java.io.Serializable;
  import java.util.List;
  import java.util.ArrayList;
  
  @Name("commentHome")
  @Scope(ScopeType.PAGE)
  public class CommentHome implements Serializable {
  
      @In
      EntityManager entityManager;
  
      @In
      FacesMessages facesMessages;
  
      @In
      Document currentDocument;
  
      @In
      Directory currentDirectory;
  
      @In
      User currentUser;
  
      @In
      User guestUser;
  
      private Comment comment;
      private List<Comment> comments;
  
      private String formContent;
      private boolean enabledPreview = false;
  
      @Create
      public void initialize() {
          System.out.println("########################### CREATE COMMENT HOME ##################################");
  
          refreshComments();
      }
  
      //@Observer("Preferences.blogDirectoryPreferences")
      @Transactional
      public void refreshComments() {
          System.out.println("########################### REFRESH COMMENTS ##################################");
          entityManager.joinTransaction();
  
          comments = new ArrayList<Comment>();
          //noinspection unchecked
          comments = entityManager
                  .createQuery("select c from Comment c where c.document is :doc order by c.createdOn asc")
                  .setParameter("doc", currentDocument)
                  .getResultList();
  
          comment = new Comment();
          if (!currentUser.getId().equals(guestUser.getId())) {
              comment.setFromUserName(currentUser.getFirstname() + " " + currentUser.getLastname());
              comment.setFromUserEmail(currentUser.getEmail());
              comment.setFromUserHomepage(
                  currentUser.getMemberHome() != null
                      ? ((WikiPreferences)Component.getInstance("wikiPreferences")).getBaseUrl()+WikiUtil.renderHomeURL(currentUser)
                      : null);
          }
          formContent = null;
      }
  
      @Transactional
      public void persist() {
          System.out.println("###################################### PERSIST #####################################");
  
          syncFormToInstance(currentDirectory);
  
          entityManager.joinTransaction();
          comment.setDocument(entityManager.merge(currentDocument));
          if (!currentUser.getId().equals(guestUser.getId()))
              comment.setFromUser(entityManager.merge(currentUser));
  
          // Null out the property so that the @Email validator doesn't fall over it...
          // I hate JSF and it's "let's set an empty string" behavior
          comment.setFromUserEmail(
              comment.getFromUserEmail()!=null && comment.getFromUserEmail().length()>0
                  ? comment.getFromUserEmail()
                  : null
          );
  
          entityManager.persist(comment);
  
  
          facesMessages.addFromResourceBundleOrDefault(
              FacesMessage.SEVERITY_INFO,
              "comment.persist",
              "Your comment has been saved."
          );
          refreshComments();
      }
  
      @Transactional
      public void remove(Long commentId) {
          entityManager.joinTransaction();
  
          Comment foundCommment = entityManager.find(Comment.class, commentId);
          if (foundCommment != null) {
              entityManager.remove(foundCommment);
              facesMessages.addFromResourceBundleOrDefault(
                  FacesMessage.SEVERITY_INFO,
                  "comment.remove",
                  "Comment with subject '" + foundCommment.getSubject() + "' has been removed."
              );
          }
  
          refreshComments();
      }
  
      public Comment getComment() {
          return comment;
      }
  
      public void setComment(Comment comment) {
          this.comment = comment;
      }
  
      public List<Comment> getComments() {
          return comments;
      }
  
      /* Wiki text editing */
  
      public String getFormContent() {
          // Load the text content and resolve links
          if (formContent == null) syncInstanceToForm(currentDirectory);
          return formContent;
      }
  
      public void setFormContent(String formContent) {
          this.formContent = formContent;
      }
  
      private void syncFormToInstance(Directory dir) {
          WikiLinkResolver wikiLinkResolver = (WikiLinkResolver)Component.getInstance("wikiLinkResolver");
          comment.setText(
              wikiLinkResolver.convertToWikiProtocol(dir.getAreaNumber(), formContent)
          );
      }
  
      private void syncInstanceToForm(Directory dir) {
          WikiLinkResolver wikiLinkResolver = (WikiLinkResolver)Component.getInstance("wikiLinkResolver");
          formContent = wikiLinkResolver.convertFromWikiProtocol(dir.getAreaNumber(), comment.getText());
      }
  
      public boolean isEnabledPreview() {
          return enabledPreview;
      }
  
      public void setEnabledPreview(boolean enabledPreview) {
          this.enabledPreview = enabledPreview;
          syncFormToInstance(currentDirectory);
      }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list