1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
package net.sourceforge.pebble.domain; |
33 | |
|
34 | |
import net.sourceforge.pebble.api.event.blogentry.BlogEntryEvent; |
35 | |
import net.sourceforge.pebble.api.event.comment.CommentEvent; |
36 | |
import net.sourceforge.pebble.api.event.trackback.TrackBackEvent; |
37 | |
import net.sourceforge.pebble.dao.BlogEntryDAO; |
38 | |
import net.sourceforge.pebble.dao.DAOFactory; |
39 | |
import net.sourceforge.pebble.dao.PersistenceException; |
40 | |
import net.sourceforge.pebble.ContentCache; |
41 | |
import org.apache.commons.logging.Log; |
42 | |
import org.apache.commons.logging.LogFactory; |
43 | |
|
44 | |
import java.util.ArrayList; |
45 | |
import java.util.Date; |
46 | |
import java.util.List; |
47 | |
import java.util.Collection; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 1476 | public class BlogService { |
56 | |
|
57 | 4 | private static final Log log = LogFactory.getLog(BlogService.class); |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public BlogEntry getBlogEntry(Blog blog, String blogEntryId) throws BlogServiceException { |
66 | 2132 | BlogEntry blogEntry = null; |
67 | 2132 | ContentCache cache = ContentCache.getInstance(); |
68 | |
|
69 | |
|
70 | 2132 | blogEntry = cache.getBlogEntry(blog, blogEntryId); |
71 | 2132 | if (blogEntry != null) { |
72 | 852 | log.debug("Got blog entry " + blogEntryId + " from cache"); |
73 | |
} else { |
74 | 1280 | log.debug("Loading blog entry " + blogEntryId + " from disk"); |
75 | 1280 | BlogEntryDAO dao = DAOFactory.getConfiguredFactory().getBlogEntryDAO(); |
76 | |
try { |
77 | 1280 | blogEntry = dao.loadBlogEntry(blog, blogEntryId); |
78 | |
|
79 | 1280 | if (blogEntry != null) { |
80 | |
|
81 | 608 | cache.putBlogEntry(blogEntry); |
82 | |
} |
83 | 0 | } catch (PersistenceException pe) { |
84 | 0 | throw new BlogServiceException(blog, pe); |
85 | 1280 | } |
86 | |
} |
87 | |
|
88 | 2132 | if (blogEntry != null) { |
89 | 1460 | blogEntry = (BlogEntry)blogEntry.clone(); |
90 | 1460 | blogEntry.setEventsEnabled(true); |
91 | 1460 | blogEntry.setPersistent(true); |
92 | |
} |
93 | |
|
94 | 2132 | return blogEntry; |
95 | |
} |
96 | |
|
97 | |
public List<BlogEntry> getBlogEntries(Blog blog, int year, int month, int day) throws BlogServiceException { |
98 | 0 | Day d = blog.getBlogForDay(year, month, day); |
99 | 0 | List<String> blogEntryIds = d.getBlogEntries(); |
100 | 0 | return getBlogEntries(blog, blogEntryIds); |
101 | |
} |
102 | |
|
103 | |
public List<BlogEntry> getBlogEntries(Blog blog, int year, int month) throws BlogServiceException { |
104 | 3076 | Month m = blog.getBlogForMonth(year, month); |
105 | 3076 | List<String> blogEntryIds = m.getBlogEntries(); |
106 | 3076 | return getBlogEntries(blog, blogEntryIds); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public Collection<BlogEntry> getBlogEntries(Blog blog) throws BlogServiceException { |
115 | 0 | BlogEntryDAO dao = DAOFactory.getConfiguredFactory().getBlogEntryDAO(); |
116 | |
Collection<BlogEntry> blogEntries; |
117 | |
try { |
118 | 0 | blogEntries = dao.loadBlogEntries(blog); |
119 | 0 | for (BlogEntry blogEntry : blogEntries) { |
120 | 0 | blogEntry.setPersistent(true); |
121 | |
} |
122 | |
|
123 | 0 | } catch (PersistenceException pe) { |
124 | 0 | throw new BlogServiceException(blog, pe); |
125 | 0 | } |
126 | |
|
127 | 0 | return blogEntries; |
128 | |
} |
129 | |
|
130 | |
private List<BlogEntry> getBlogEntries(Blog blog, List<String> blogEntryIds) throws BlogServiceException { |
131 | 3076 | List<BlogEntry> blogEntries = new ArrayList<BlogEntry>(); |
132 | |
|
133 | 3076 | for (String blogEntryId : blogEntryIds) { |
134 | 536 | BlogEntry blogEntry = getBlogEntry(blog, blogEntryId); |
135 | 536 | blogEntries.add(blogEntry); |
136 | 536 | } |
137 | |
|
138 | 3076 | return blogEntries; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void putBlogEntry(BlogEntry blogEntry) throws BlogServiceException { |
145 | 1032 | DAOFactory factory = DAOFactory.getConfiguredFactory(); |
146 | 1032 | BlogEntryDAO dao = factory.getBlogEntryDAO(); |
147 | 1032 | Blog blog = blogEntry.getBlog(); |
148 | 1032 | ContentCache cache = ContentCache.getInstance(); |
149 | |
|
150 | 1032 | synchronized (blog) { |
151 | |
try { |
152 | 1032 | BlogEntry be = getBlogEntry(blog, blogEntry.getId()); |
153 | |
|
154 | 1032 | if (!blogEntry.isPersistent() && be != null) { |
155 | |
|
156 | |
|
157 | 284 | blogEntry.setDate(new Date(blogEntry.getDate().getTime() + 1)); |
158 | 284 | putBlogEntry(blogEntry); |
159 | |
} else { |
160 | 748 | if (!blogEntry.isPersistent()) { |
161 | 592 | dao.storeBlogEntry(blogEntry); |
162 | 592 | blogEntry.insertEvent(new BlogEntryEvent(blogEntry, BlogEntryEvent.BLOG_ENTRY_ADDED)); |
163 | |
|
164 | 592 | for (Comment comment : blogEntry.getComments()) { |
165 | 24 | blogEntry.addEvent(new CommentEvent(comment, CommentEvent.COMMENT_ADDED)); |
166 | |
} |
167 | 592 | for (TrackBack trackBack : blogEntry.getTrackBacks()) { |
168 | 24 | blogEntry.addEvent(new TrackBackEvent(trackBack, TrackBackEvent.TRACKBACK_ADDED)); |
169 | |
} |
170 | |
} else { |
171 | 156 | dao.storeBlogEntry(blogEntry); |
172 | 156 | if (blogEntry.isDirty()) { |
173 | 24 | blogEntry.insertEvent(new BlogEntryEvent(blogEntry, blogEntry.getPropertyChangeEvents())); |
174 | |
} |
175 | |
} |
176 | |
|
177 | 748 | blogEntry.getBlog().getEventDispatcher().fireEvents(blogEntry); |
178 | |
|
179 | |
|
180 | 748 | dao.storeBlogEntry(blogEntry); |
181 | 748 | cache.removeBlogEntry(blogEntry); |
182 | |
} |
183 | |
|
184 | 1032 | blogEntry.setPersistent(true); |
185 | 0 | } catch (PersistenceException pe) { |
186 | 0 | throw new BlogServiceException(blog, pe); |
187 | |
} finally { |
188 | 1032 | blogEntry.clearPropertyChangeEvents(); |
189 | 1032 | blogEntry.clearEvents(); |
190 | 1032 | blogEntry.setEventsEnabled(true); |
191 | 1032 | } |
192 | 1032 | } |
193 | 1032 | } |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public void removeBlogEntry(BlogEntry blogEntry) throws BlogServiceException { |
199 | 36 | Blog blog = blogEntry.getBlog(); |
200 | 36 | ContentCache cache = ContentCache.getInstance(); |
201 | |
|
202 | |
try { |
203 | 36 | DAOFactory factory = DAOFactory.getConfiguredFactory(); |
204 | 36 | BlogEntryDAO dao = factory.getBlogEntryDAO(); |
205 | 36 | dao.removeBlogEntry(blogEntry); |
206 | 36 | blogEntry.setPersistent(false); |
207 | |
|
208 | |
|
209 | 36 | cache.removeBlogEntry(blogEntry); |
210 | |
|
211 | 36 | blogEntry.insertEvent(new BlogEntryEvent(blogEntry, BlogEntryEvent.BLOG_ENTRY_REMOVED)); |
212 | |
|
213 | |
|
214 | |
|
215 | 36 | for (Comment comment : blogEntry.getComments()) { |
216 | 16 | blogEntry.addEvent(new CommentEvent(comment, CommentEvent.COMMENT_REMOVED)); |
217 | |
} |
218 | 36 | for (TrackBack trackBack : blogEntry.getTrackBacks()) { |
219 | 12 | blogEntry.addEvent(new TrackBackEvent(trackBack, TrackBackEvent.TRACKBACK_REMOVED)); |
220 | |
} |
221 | |
|
222 | 36 | blogEntry.getBlog().getEventDispatcher().fireEvents(blogEntry); |
223 | 0 | } catch (PersistenceException pe) { |
224 | 0 | throw new BlogServiceException(blogEntry.getBlog(), pe); |
225 | 36 | } |
226 | 36 | } |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public Response getResponse(Blog blog, String responseId) throws BlogServiceException { |
235 | 0 | String blogEntryId = responseId.substring(responseId.indexOf("/")+1, responseId.lastIndexOf("/")); |
236 | 0 | BlogEntry blogEntry = getBlogEntry(blog, blogEntryId); |
237 | 0 | if (blogEntry != null) { |
238 | 0 | return blogEntry.getResponse(responseId); |
239 | |
} else { |
240 | 0 | return null; |
241 | |
} |
242 | |
} |
243 | |
|
244 | |
} |