View Javadoc

1   package net.sourceforge.blogentis.om;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: Post.java,v 1.2 2004/10/28 10:41:38 tassos Exp $
23  //
24  
25  import java.io.IOException;
26  import java.sql.Connection;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import net.sourceforge.blogentis.rss.RSSItem;
32  import net.sourceforge.blogentis.utils.AbsoluteLinkURL;
33  import net.sourceforge.blogentis.utils.BlogManagerService;
34  import net.sourceforge.blogentis.utils.DateUtils;
35  import net.sourceforge.blogentis.utils.LinkFactoryService;
36  import net.sourceforge.blogentis.utils.MappedConfiguration;
37  import net.sourceforge.blogentis.utils.StringUtils;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.apache.ecs.xml.XML;
42  import org.apache.torque.TorqueException;
43  import org.apache.torque.om.Persistent;
44  import org.apache.torque.util.Criteria;
45  import org.apache.turbine.om.security.User;
46  import org.apache.turbine.services.security.TurbineSecurity;
47  
48  public class Post
49          extends BasePost
50          implements Persistent, RSSItem {
51  
52      private static Log log = LogFactory.getLog(Post.class);
53  
54      private MappedConfiguration properties = null;
55      private Blog blog = null;
56  
57      public BaseConfigurablePeer getConfigurablePeer() {
58          return getPeer();
59      }
60  
61      public Blog getBlog()
62              throws TorqueException {
63          if (blog == null)
64              blog = BlogManagerService.getBlog(getBlogId());
65          return blog;
66      }
67  
68      public List getSections()
69              throws TorqueException {
70          List ret = new ArrayList(3);
71          List sections = getPostSectionsJoinSection(new Criteria()
72              .addAscendingOrderByColumn(SectionPeer.TITLE));
73          for(Iterator i = sections.iterator(); i.hasNext();) {
74              PostSection ps = (PostSection)i.next();
75              ret.add(ps.getSection());
76          }
77          return ret;
78      }
79  
80      public List getComments()
81              throws TorqueException {
82          return getComments(new Criteria()
83              .addAscendingOrderByColumn(CommentPeer.POSTED_TIME));
84      }
85  
86      public User getAuthor()
87              throws TorqueException {
88          try {
89              return TurbineSecurity.getUser(getAuthorId());
90          } catch (Exception e) {
91              throw new TorqueException("Could not find user u", e);
92          }
93      }
94  
95      public List getItemDescription() {
96          List l = new ArrayList(4);
97          AbsoluteLinkURL link = LinkFactoryService.getLink();
98          l.add(new XML("blogentis:title").addElement(getTitle()));
99          l.add(new XML("blogentis:link").addElement(getRDFIdentifier()));
100         l.add(new XML("blogentis:description").addElement(StringUtils
101             .entityfyHTML(getShortDescription())));
102         String full = "<p>" + getShortDescription() + "</p>" + getFullText();
103         l.add(new XML("blogentis:contents").addElement(StringUtils
104             .entityfyHTML(full)));
105         //l.add(new XML("blogentis:contents-xml").addElement(full));
106         l.add(new XML("blogentis:guid").addElement(getRDFIdentifier())
107             .addAttribute("isPermaLink", "true"));
108         l.add(new XML("blogentis:pubDate").addElement(DateUtils
109             .formatRFC822(getPostedTime())));
110         l.add(new XML("blogentis:pubDate-iso").addElement(DateUtils
111             .formatIso8601(getPostedTime())));
112         l.add(new XML("blogentis:comments").addElement(getRDFIdentifier()
113                                                        + "#Comments"));
114         l.add(new XML("blogentis:trackback-link").addElement(link
115             .permaLink(this).setPage("TrackBack").toString()));
116         l.add(new XML("blogentis:comment-rss").addElement(link.permaLink(this)
117             .setPage("RSS").toString()));
118         try {
119             List sections = getSections();
120             l.add(new XML("blogentis:source").addElement(getBlog().getTitle())
121                 .addAttribute("url", link.permaLink(getBlog()).toString()));
122             for(Iterator i = sections.iterator(); i.hasNext();) {
123                 Section s = (Section)i.next();
124                 l.add(new XML("blogentis:category").addElement(s.getTitle())
125                     .addAttribute("domain",
126                                   link.permaLink(getBlog()).toString()));
127             }
128             l.add(new XML("blogentis:author")
129                 .addElement(getAuthor().getEmail()));
130             l.add(new XML("blogentis:comment-count").addElement(String
131                 .valueOf(getComments().size())));
132         } catch (TorqueException e) {
133             log.warn("getItemDescription", e);
134         }
135         return l;
136     }
137 
138     public String getRDFIdentifier() {
139         return LinkFactoryService.getLink().permaLink(this).toString();
140     }
141 
142     public boolean isPublished() {
143         return getPostType() == PostPeer.PUBLISHED_TYPE;
144     }
145 
146     /***
147      * Replace the list of section this post belongs to with the specified list.
148      * 
149      * A database connection must be specified in order to delete the previous
150      * set of sections. Sections that do not belong to this blog will be
151      * silently ignored.
152      * 
153      * @param con
154      *            The transaction-supporting connection that should be used to
155      *            save the sections.
156      * @param sections
157      *            the list of sections this post should belong to. Empty list
158      *            accepted.
159      * @throws TorqueException
160      *             when the torque layes encounters an exception.
161      */
162     public void replaceSections(Connection con, List sections)
163             throws TorqueException {
164         // Clear any existing sections
165         Criteria c = new Criteria();
166         c.add(PostSectionPeer.POST_ID, getPostId());
167         PostSectionPeer.doDelete(c, con);
168         this.collPostSections = null;
169         // make and save the post-sections
170         for(Iterator i = sections.iterator(); i.hasNext();) {
171             Section s = (Section)i.next();
172             if (s.getBlogId() == getBlogId()) {
173                 PostSection ps = new PostSection();
174                 ps.setSectionId(s.getSectionId());
175                 if (isNew()) {
176                     addPostSection(ps);
177                 } else {
178                     ps.setPostId(getPostId());
179                     ps.save(con);
180                 }
181             }
182         }
183     }
184 
185     public synchronized MappedConfiguration getProperties() {
186         if (properties == null)
187             properties = new MappedConfiguration(this);
188         return properties;
189     }
190 
191     public void save(Connection con)
192             throws TorqueException {
193         try {
194             if (properties != null && properties.isModified())
195                 properties.saveTo(this);
196         } catch (IOException e) {
197             throw new TorqueException("Could not serialize properties", e);
198         }
199         super.save(con);
200     }
201 
202     public boolean isAllowComments() {
203         return getProperties().getBoolean("allowComments", true);
204     }
205 }