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.service; |
33 | |
|
34 | |
import net.sourceforge.pebble.ContentCache; |
35 | |
import net.sourceforge.pebble.comparator.StaticPageByNameComparator; |
36 | |
import net.sourceforge.pebble.dao.DAOFactory; |
37 | |
import net.sourceforge.pebble.dao.PersistenceException; |
38 | |
import net.sourceforge.pebble.dao.StaticPageDAO; |
39 | |
import net.sourceforge.pebble.domain.Blog; |
40 | |
import net.sourceforge.pebble.domain.StaticPage; |
41 | |
import org.apache.commons.logging.Log; |
42 | |
import org.apache.commons.logging.LogFactory; |
43 | |
|
44 | |
import java.util.ArrayList; |
45 | |
import java.util.Collections; |
46 | |
import java.util.Date; |
47 | |
import java.util.List; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 0 | public class StaticPageService { |
56 | |
|
57 | 0 | private static final Log log = LogFactory.getLog(StaticPageService.class); |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public List<StaticPage> getStaticPages(Blog blog) throws StaticPageServiceException { |
67 | 0 | List<StaticPage> staticPages = new ArrayList<StaticPage>(); |
68 | |
try { |
69 | 0 | DAOFactory factory = DAOFactory.getConfiguredFactory(); |
70 | 0 | StaticPageDAO dao = factory.getStaticPageDAO(); |
71 | 0 | staticPages.addAll(dao.loadStaticPages(blog)); |
72 | 0 | } catch (PersistenceException pe) { |
73 | 0 | throw new StaticPageServiceException(blog, pe); |
74 | 0 | } |
75 | |
|
76 | 0 | Collections.sort(staticPages, new StaticPageByNameComparator()); |
77 | |
|
78 | 0 | return staticPages; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public StaticPage getStaticPageById(Blog blog, String pageId) throws StaticPageServiceException { |
90 | |
StaticPage staticPage; |
91 | 0 | ContentCache cache = ContentCache.getInstance(); |
92 | |
|
93 | |
try { |
94 | 0 | staticPage = cache.getStaticPage(blog, pageId); |
95 | 0 | if (staticPage != null) { |
96 | 0 | log.debug("Got static page " + pageId+ " from cache"); |
97 | |
} else { |
98 | 0 | log.debug("Loading static page " + pageId+ " from disk"); |
99 | |
|
100 | 0 | DAOFactory factory = DAOFactory.getConfiguredFactory(); |
101 | 0 | StaticPageDAO dao = factory.getStaticPageDAO(); |
102 | 0 | staticPage = dao.loadStaticPage(blog, pageId); |
103 | 0 | if (staticPage != null) { |
104 | 0 | staticPage.setPersistent(true); |
105 | 0 | cache.putStaticPage(staticPage); |
106 | |
} |
107 | |
} |
108 | 0 | } catch (PersistenceException pe) { |
109 | 0 | throw new StaticPageServiceException(blog, pe); |
110 | 0 | } |
111 | |
|
112 | 0 | if (staticPage != null) { |
113 | 0 | staticPage = (StaticPage)staticPage.clone(); |
114 | |
} |
115 | |
|
116 | 0 | return staticPage; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public StaticPage getStaticPageByName(Blog blog, String name) throws StaticPageServiceException { |
128 | 0 | String id = blog.getStaticPageIndex().getStaticPage(name); |
129 | 0 | return getStaticPageById(blog, id); |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public void putStaticPage(StaticPage staticPage) throws StaticPageServiceException { |
139 | 0 | ContentCache cache = ContentCache.getInstance(); |
140 | 0 | DAOFactory factory = DAOFactory.getConfiguredFactory(); |
141 | 0 | StaticPageDAO dao = factory.getStaticPageDAO(); |
142 | 0 | Blog blog = staticPage.getBlog(); |
143 | |
|
144 | 0 | synchronized (blog) { |
145 | |
try { |
146 | 0 | StaticPage sp = getStaticPageById(blog, staticPage.getId()); |
147 | |
|
148 | 0 | if (!staticPage.isPersistent() && sp != null) { |
149 | |
|
150 | |
|
151 | 0 | staticPage.setDate(new Date(staticPage.getDate().getTime() + 1)); |
152 | 0 | putStaticPage(staticPage); |
153 | |
} else { |
154 | 0 | dao.storeStaticPage(staticPage); |
155 | 0 | staticPage.setPersistent(true); |
156 | 0 | cache.removeStaticPage(staticPage); |
157 | |
} |
158 | |
|
159 | 0 | staticPage.getBlog().getSearchIndex().index(staticPage); |
160 | 0 | staticPage.getBlog().getStaticPageIndex().index(staticPage); |
161 | 0 | } catch (PersistenceException pe) { |
162 | 0 | throw new StaticPageServiceException(blog, pe); |
163 | 0 | } |
164 | 0 | } |
165 | 0 | } |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public void removeStaticPage(StaticPage staticPage) throws StaticPageServiceException { |
174 | 0 | ContentCache cache = ContentCache.getInstance(); |
175 | 0 | DAOFactory factory = DAOFactory.getConfiguredFactory(); |
176 | 0 | StaticPageDAO dao = factory.getStaticPageDAO(); |
177 | 0 | Blog blog = staticPage.getBlog(); |
178 | |
|
179 | |
try { |
180 | 0 | dao.removeStaticPage(staticPage); |
181 | 0 | cache.removeStaticPage(staticPage); |
182 | |
|
183 | 0 | staticPage.getBlog().getSearchIndex().unindex(staticPage); |
184 | 0 | staticPage.getBlog().getStaticPageIndex().unindex(staticPage); |
185 | 0 | } catch (PersistenceException pe) { |
186 | |
|
187 | 0 | cache.removeStaticPage(staticPage); |
188 | |
|
189 | 0 | throw new StaticPageServiceException(staticPage.getBlog(), pe); |
190 | 0 | } |
191 | 0 | } |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public boolean lock(StaticPage staticPage) { |
200 | 0 | if (staticPage.isPersistent()) { |
201 | 0 | boolean success = DAOFactory.getConfiguredFactory().getStaticPageDAO().lock(staticPage); |
202 | 0 | ContentCache.getInstance().removeStaticPage(staticPage); |
203 | |
|
204 | 0 | return success; |
205 | |
} else { |
206 | 0 | return true; |
207 | |
} |
208 | |
} |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
public boolean unlock(StaticPage staticPage) { |
217 | 0 | if (staticPage.isPersistent()) { |
218 | 0 | boolean success = DAOFactory.getConfiguredFactory().getStaticPageDAO().unlock(staticPage); |
219 | 0 | ContentCache.getInstance().removeStaticPage(staticPage); |
220 | |
|
221 | 0 | return success; |
222 | |
} else { |
223 | 0 | return true; |
224 | |
} |
225 | |
} |
226 | |
|
227 | |
} |