File Source: staticpageservice.java
1 /*
2 * Copyright (c) 2003-2006, Simon Brown
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * - Neither the name of Pebble nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
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 * Service that encompasses all functionality related to getting, putting
51 * and removing static pages.
52 *
53 * @author Simon Brown
54 */
/*
P/P * Method: void net.sourceforge.pebble.service.StaticPageService()
*/
55 public class StaticPageService {
56
/*
P/P * Method: net.sourceforge.pebble.service.StaticPageService__static_init
*
* Postconditions:
* init'ed(log)
*/
57 private static final Log log = LogFactory.getLog(StaticPageService.class);
58
59 /**
60 * Gets the list of static pages for the given blog.
61 *
62 * @param blog the Blog
63 * @return a list of BlogEntry instances
64 * @throws StaticPageServiceException if something goes wrong
65 */
66 public List<StaticPage> getStaticPages(Blog blog) throws StaticPageServiceException {
/*
P/P * Method: List getStaticPages(Blog)
*
* Presumptions:
* net.sourceforge.pebble.dao.DAOFactory:getConfiguredFactory(...)@69 != null
* net.sourceforge.pebble.dao.DAOFactory:getStaticPageDAO(...)@70 != null
*
* Postconditions:
* return_value == &new ArrayList(getStaticPages#1)
* new ArrayList(getStaticPages#1) num objects == 1
*/
67 List<StaticPage> staticPages = new ArrayList<StaticPage>();
68 try {
69 DAOFactory factory = DAOFactory.getConfiguredFactory();
70 StaticPageDAO dao = factory.getStaticPageDAO();
71 staticPages.addAll(dao.loadStaticPages(blog));
72 } catch (PersistenceException pe) {
73 throw new StaticPageServiceException(blog, pe);
74 }
75
76 Collections.sort(staticPages, new StaticPageByNameComparator());
77
78 return staticPages;
79 }
80
81 /**
82 * Gets the page with the specified id.
83 *
84 * @param pageId the id of the static page
85 * @param blog the Blog
86 * @return a Page instance, or null if the page couldn't be found
87 * @throws StaticPageServiceException if something goes wrong
88 */
89 public StaticPage getStaticPageById(Blog blog, String pageId) throws StaticPageServiceException {
90 StaticPage staticPage;
/*
P/P * Method: StaticPage getStaticPageById(Blog, String)
*
* Presumptions:
* net.sourceforge.pebble.ContentCache:getInstance(...)@91 != null
* net.sourceforge.pebble.dao.DAOFactory:getConfiguredFactory(...)@100 != null
* net.sourceforge.pebble.dao.DAOFactory:getStaticPageDAO(...)@101 != null
* org.apache.commons.logging.LogFactory:getLog(...)@57 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* net.sourceforge.pebble.ContentCache:getStaticPage(...)@94: Addr_Set{null}, Inverse{null}
* net.sourceforge.pebble.dao.StaticPageDAO:loadStaticPage(...)@102: Addr_Set{null}, Inverse{null}
*/
91 ContentCache cache = ContentCache.getInstance();
92
93 try {
94 staticPage = cache.getStaticPage(blog, pageId);
95 if (staticPage != null) {
96 log.debug("Got static page " + pageId+ " from cache");
97 } else {
98 log.debug("Loading static page " + pageId+ " from disk");
99
100 DAOFactory factory = DAOFactory.getConfiguredFactory();
101 StaticPageDAO dao = factory.getStaticPageDAO();
102 staticPage = dao.loadStaticPage(blog, pageId);
103 if (staticPage != null) {
104 staticPage.setPersistent(true);
105 cache.putStaticPage(staticPage);
106 }
107 }
108 } catch (PersistenceException pe) {
109 throw new StaticPageServiceException(blog, pe);
110 }
111
112 if (staticPage != null) {
113 staticPage = (StaticPage)staticPage.clone();
114 }
115
116 return staticPage;
117 }
118
119 /**
120 * Gets the static page with the specified name.
121 *
122 * @param name the name of the static page
123 * @param blog the Blog
124 * @return a StaticPage instance, or null if the page couldn't be found
125 * @throws StaticPageServiceException if something goes wrong
126 */
127 public StaticPage getStaticPageByName(Blog blog, String name) throws StaticPageServiceException {
/*
P/P * Method: StaticPage getStaticPageByName(Blog, String)
*
* Preconditions:
* blog != null
*
* Presumptions:
* net.sourceforge.pebble.domain.Blog:getStaticPageIndex(...)@128 != null
*
* Postconditions:
* init'ed(return_value)
*/
128 String id = blog.getStaticPageIndex().getStaticPage(name);
129 return getStaticPageById(blog, id);
130 }
131
132 /**
133 * Puts the static page.
134 *
135 * @param staticPage the StaticPage instance to store
136 * @throws StaticPageServiceException if something goes wrong
137 */
138 public void putStaticPage(StaticPage staticPage) throws StaticPageServiceException {
/*
P/P * Method: void putStaticPage(StaticPage)
*
* Preconditions:
* staticPage != null
*
* Presumptions:
* java.util.Date:getTime(...)@151 <= 264-2
* net.sourceforge.pebble.ContentCache:getInstance(...)@139 != null
* net.sourceforge.pebble.dao.DAOFactory:getConfiguredFactory(...)@140 != null
* net.sourceforge.pebble.dao.DAOFactory:getStaticPageDAO(...)@141 != null
* net.sourceforge.pebble.domain.Blog:getSearchIndex(...)@159 != null
* ...
*
* Test Vectors:
* net.sourceforge.pebble.domain.StaticPage:isPersistent(...)@148: {1}, {0}
*/
139 ContentCache cache = ContentCache.getInstance();
140 DAOFactory factory = DAOFactory.getConfiguredFactory();
141 StaticPageDAO dao = factory.getStaticPageDAO();
142 Blog blog = staticPage.getBlog();
143
144 synchronized (blog) {
145 try {
146 StaticPage sp = getStaticPageById(blog, staticPage.getId());
147
148 if (!staticPage.isPersistent() && sp != null) {
149 // the static page is new but one exists with the same ID already
150 // - increment the date/ID and try again
151 staticPage.setDate(new Date(staticPage.getDate().getTime() + 1));
152 putStaticPage(staticPage);
153 } else {
154 dao.storeStaticPage(staticPage);
155 staticPage.setPersistent(true);
156 cache.removeStaticPage(staticPage);
157 }
158
159 staticPage.getBlog().getSearchIndex().index(staticPage);
160 staticPage.getBlog().getStaticPageIndex().index(staticPage);
161 } catch (PersistenceException pe) {
162 throw new StaticPageServiceException(blog, pe);
163 }
164 }
165 }
166
167 /**
168 * Removes a static page.
169 *
170 * @param staticPage the StaticPage instance to remove
171 * @throws StaticPageServiceException if something goes wrong
172 */
173 public void removeStaticPage(StaticPage staticPage) throws StaticPageServiceException {
/*
P/P * Method: void removeStaticPage(StaticPage)
*
* Preconditions:
* staticPage != null
*
* Presumptions:
* net.sourceforge.pebble.ContentCache:getInstance(...)@174 != null
* net.sourceforge.pebble.dao.DAOFactory:getConfiguredFactory(...)@175 != null
* net.sourceforge.pebble.dao.DAOFactory:getStaticPageDAO(...)@176 != null
* net.sourceforge.pebble.domain.Blog:getSearchIndex(...)@183 != null
* net.sourceforge.pebble.domain.Blog:getStaticPageIndex(...)@184 != null
* ...
*/
174 ContentCache cache = ContentCache.getInstance();
175 DAOFactory factory = DAOFactory.getConfiguredFactory();
176 StaticPageDAO dao = factory.getStaticPageDAO();
177 Blog blog = staticPage.getBlog();
178
179 try {
180 dao.removeStaticPage(staticPage);
181 cache.removeStaticPage(staticPage);
182
183 staticPage.getBlog().getSearchIndex().unindex(staticPage);
184 staticPage.getBlog().getStaticPageIndex().unindex(staticPage);
185 } catch (PersistenceException pe) {
186 // remove from the cache so that it's picked up from storage when accessed next
187 cache.removeStaticPage(staticPage);
188
189 throw new StaticPageServiceException(staticPage.getBlog(), pe);
190 }
191 }
192
193 /**
194 * Locks a given static page.
195 *
196 * @param staticPage the static page to lock
197 * @return true if the page could be locked, false otherwise
198 */
199 public boolean lock(StaticPage staticPage) {
/*
P/P * Method: bool lock(StaticPage)
*
* Preconditions:
* staticPage != null
*
* Presumptions:
* net.sourceforge.pebble.ContentCache:getInstance(...)@202 != null
* net.sourceforge.pebble.dao.DAOFactory:getConfiguredFactory(...)@201 != null
* net.sourceforge.pebble.dao.DAOFactory:getStaticPageDAO(...)@201 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* net.sourceforge.pebble.domain.StaticPage:isPersistent(...)@200: {0}, {1}
*/
200 if (staticPage.isPersistent()) {
201 boolean success = DAOFactory.getConfiguredFactory().getStaticPageDAO().lock(staticPage);
202 ContentCache.getInstance().removeStaticPage(staticPage);
203
204 return success;
205 } else {
206 return true;
207 }
208 }
209
210 /**
211 * Unlocks a given static page.
212 *
213 * @param staticPage the static page to unlock
214 * @return true if the page could be unlocked, false otherwise
215 */
216 public boolean unlock(StaticPage staticPage) {
/*
P/P * Method: bool unlock(StaticPage)
*
* Preconditions:
* staticPage != null
*
* Presumptions:
* net.sourceforge.pebble.ContentCache:getInstance(...)@219 != null
* net.sourceforge.pebble.dao.DAOFactory:getConfiguredFactory(...)@218 != null
* net.sourceforge.pebble.dao.DAOFactory:getStaticPageDAO(...)@218 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* net.sourceforge.pebble.domain.StaticPage:isPersistent(...)@217: {0}, {1}
*/
217 if (staticPage.isPersistent()) {
218 boolean success = DAOFactory.getConfiguredFactory().getStaticPageDAO().unlock(staticPage);
219 ContentCache.getInstance().removeStaticPage(staticPage);
220
221 return success;
222 } else {
223 return true;
224 }
225 }
226
227 }
SofCheck Inspector Build Version : 2.22510
| staticpageservice.java |
2010-Jun-25 19:40:32 |
| staticpageservice.class |
2010-Jul-19 20:23:38 |