KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > dao > AbstractPageDao


1 /* ***** BEGIN LICENSE BLOCK *****
2     * Version: MPL 1.1
3     * The contents of this file are subject to the Mozilla Public License Version
4     * 1.1 (the "License"); you may not use this file except in compliance with
5     * the License. You may obtain a copy of the License at
6     * http://www.mozilla.org/MPL/
7     *
8     * Software distributed under the License is distributed on an "AS IS" basis,
9     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10     * for the specific language governing rights and limitations under the
11     * License.
12     *
13     * The Original Code is Riot.
14     *
15     * The Initial Developer of the Original Code is
16     * Neteye GmbH.
17     * Portions created by the Initial Developer are Copyright (C) 2007
18     * the Initial Developer. All Rights Reserved.
19     *
20     * Contributor(s):
21     * Felix Gnass [fgnass at neteye dot de]
22     *
23     * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.pages.dao;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.riotfamily.components.VersionContainer;
37 import org.riotfamily.components.dao.ComponentDao;
38 import org.riotfamily.pages.Page;
39 import org.riotfamily.pages.PageAlias;
40 import org.riotfamily.pages.PageLocation;
41 import org.riotfamily.pages.PageNode;
42 import org.riotfamily.pages.PageValidationUtils;
43 import org.riotfamily.pages.Site;
44 import org.riotfamily.pages.component.PageComponentListLocator;
45 import org.springframework.beans.factory.InitializingBean;
46 import org.springframework.util.Assert;
47 import org.springframework.util.ObjectUtils;
48
49 /**
50     * Abstract base class for {@link PageDao} implementations.
51     *
52     * @author Felix Gnass [fgnass at neteye dot de]
53     * @author Jan-Frederic Linde [jfl at neteye dot de]
54     * @since 6.5
55     */

56 public abstract class AbstractPageDao implements PageDao, InitializingBean {
57
58     private static final String JavaDoc DEFAULT_SITE_NAME = "default";
59
60     private static final Log log = LogFactory.getLog(AbstractPageDao.class);
61
62     private ComponentDao componentDao;
63
64     private List JavaDoc locales;
65
66     public AbstractPageDao() {
67     }
68
69     public void setComponentDao(ComponentDao componentDao) {
70         this.componentDao = componentDao;
71     }
72
73     public void setLocales(List JavaDoc locales) {
74         this.locales = locales;
75     }
76
77     public final void afterPropertiesSet() throws Exception JavaDoc {
78         Assert.notNull(componentDao, "A ComponentDao must be set.");
79         Assert.notEmpty(locales, "At least one Locale must be configured.");
80         initDao();
81     }
82
83     protected void initDao() {
84     }
85
86     protected abstract Object JavaDoc loadObject(Class JavaDoc clazz, Serializable JavaDoc id);
87
88     protected abstract void saveObject(Object JavaDoc object);
89
90     protected abstract void updateObject(Object JavaDoc object);
91
92     protected abstract void deleteObject(Object JavaDoc object);
93
94     protected abstract void flush();
95     
96
97     public Page loadPage(Long JavaDoc id) {
98         return (Page) loadObject(Page.class, id);
99     }
100
101     public Site loadSite(Long JavaDoc id) {
102         return (Site) loadObject(Site.class, id);
103     }
104
105     public void saveNode(PageNode node) {
106         saveObject(node);
107     }
108     
109     public void savePage(Page parent, Page page) {
110         page.setLocale(parent.getLocale());
111         savePage(parent.getNode(), page);
112     }
113
114     public void savePage(Site site, Page page) {
115         PageNode rootNode = findRootNode(site);
116         savePage(rootNode, page);
117     }
118
119     private void savePage(PageNode parentNode, Page page) {
120         PageNode node = page.getNode();
121         if (node == null) {
122             node = new PageNode();
123         }
124         node.addPage(page); // It could be that the node does not yet contain
125
// the page itself, for example when edited nested...
126

127         if (!PageValidationUtils.isValidChild(parentNode, page)) {
128             log.warn("Page not saved because not valid: " + page);
129             throw new DuplicatePathComponentException("Page '{0}' did not validate", page.toString());
130         }
131
132         parentNode.addChildNode(node);
133         page.setCreationDate(new Date JavaDoc());
134         saveNode(node);
135         deleteAlias(new PageLocation(page));
136         log.debug("Page saved: " + page);
137     }
138
139     public Page addTranslation(Page page, Locale JavaDoc locale) {
140         log.info("Adding translation " + page + " --> " + locale);
141         Page translation = new Page();
142         translation.setLocale(locale);
143         translation.setPathComponent(page.getPathComponent());
144         PageNode node = page.getNode();
145         node.addPage(translation);
146         updateNode(node);
147         deleteAlias(new PageLocation(translation));
148         saveObject(translation);
149         
150         componentDao.copyComponentLists(PageComponentListLocator.TYPE_PAGE,
151                 page.getId().toString(), translation.getId().toString());
152
153         return translation;
154     }
155
156     public void updatePage(Page page) {
157         updateNode(page.getNode());
158         updateObject(page);
159
160         if (!PageValidationUtils.isValidChild(page.getNode().getParent(), page)) {
161             log.warn("Page not saved because not valid: " + page);
162             throw new DuplicatePathComponentException("Page '{0}' did not validate", page.toString());
163         }
164
165         String JavaDoc oldPath = page.getPath();
166         String JavaDoc newPath = page.buildPath();
167         if (!ObjectUtils.nullSafeEquals(oldPath, newPath)) {
168             log.info("Path modified: " + page);
169             PageLocation oldLocation = new PageLocation(page);
170             page.setPath(newPath);
171             createAlias(page, oldLocation);
172             updatePaths(page.getChildPages());
173         }
174     }
175
176     public void updateNode(PageNode node) {
177         updateObject(node);
178     }
179
180     public void moveNode(PageNode node, PageNode newParent) {
181         PageNode parentNode = node.getParent();
182         parentNode.getChildNodes().remove(node);
183         newParent.addChildNode(node);
184         updatePaths(node.getPages());
185     }
186
187     private void updatePaths(Collection JavaDoc pages) {
188         Iterator JavaDoc it = pages.iterator();
189         while (it.hasNext()) {
190             Page page = (Page) it.next();
191             PageLocation oldLocation = new PageLocation(page);
192             page.setPath(page.buildPath());
193             createAlias(page, oldLocation);
194             updateObject(page);
195             updatePaths(page.getChildPages());
196         }
197     }
198
199     protected abstract void clearAliases(Page page);
200
201     protected void deleteAlias(PageLocation location) {
202         PageAlias alias = findPageAlias(location);
203         if (alias != null) {
204             log.info("Deleting " + alias);
205             deleteObject(alias);
206         }
207     }
208
209     protected void createAlias(Page page, PageLocation location) {
210         if (page != null) {
211             deleteAlias(new PageLocation(page));
212         }
213         PageAlias alias = new PageAlias(page, location);
214         log.info("Creating " + alias);
215         saveObject(alias);
216     }
217
218
219     public void deletePage(Page page) {
220         log.info("Deleting page " + page);
221         Collection JavaDoc childPages = page.getChildPages();
222         if (childPages != null) {
223             Iterator JavaDoc it = childPages.iterator();
224             while (it.hasNext()) {
225                 Page child = (Page) it.next();
226                 deletePage(child);
227             }
228         }
229         componentDao.deleteComponentLists(PageComponentListLocator.TYPE_PAGE,
230                 page.getId().toString());
231
232         clearAliases(page);
233
234         PageNode node = page.getNode();
235         node.removePage(page);
236         deleteObject(page);
237         
238         VersionContainer vc = page.getVersionContainer();
239         page.setVersionContainer(null);
240         componentDao.deleteVersionContainer(vc);
241
242         if (node.hasPages()) {
243             updateNode(node);
244         }
245         else {
246             log.debug("Node has no more pages - deleting it ...");
247             PageNode parentNode = node.getParent();
248             if (parentNode != null) {
249                 parentNode.getChildNodes().remove(node);
250                 updateNode(parentNode);
251             }
252             deleteObject(node);
253         }
254     }
255
256     public Site getDefaultSite() {
257         List JavaDoc sites = listSites();
258         if (sites.size() > 0) {
259             return (Site) sites.get(0);
260         }
261         Site site = new Site();
262         site.setName(DEFAULT_SITE_NAME);
263         site.setEnabled(true);
264         site.setLocales(new ArrayList JavaDoc(locales));
265         saveSite(site);
266         flush(); // REVISIT: Flush is required here because this method is
267
// unfortunately called in non-transactional contexts
268
return site;
269     }
270
271     public void saveSite(Site site) {
272         saveObject(site);
273         PageNode rootNode = new PageNode();
274         rootNode.setSite(site);
275         saveObject(rootNode);
276     }
277
278     public void updateSite(Site site) {
279         updateObject(site);
280     }
281
282     public void deleteSite(Site site) {
283         PageNode rootNode = findRootNode(site);
284         Iterator JavaDoc it = rootNode.getPages().iterator();
285         while (it.hasNext()) {
286             Page page = (Page) it.next();
287             deletePage(page);
288         }
289         deleteObject(site);
290     }
291
292     public List JavaDoc getLocales() {
293         return locales;
294     }
295
296 }
297
Popular Tags