KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > riot > dao > PageRiotDao


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.riot.dao;
25
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 import org.riotfamily.pages.Page;
31 import org.riotfamily.pages.PageNode;
32 import org.riotfamily.pages.Site;
33 import org.riotfamily.pages.dao.PageDao;
34 import org.riotfamily.riot.dao.CutAndPasteEnabledDao;
35 import org.riotfamily.riot.dao.ListParams;
36 import org.riotfamily.riot.dao.ParentChildDao;
37 import org.riotfamily.riot.dao.SwappableItemDao;
38 import org.springframework.beans.factory.InitializingBean;
39 import org.springframework.dao.DataAccessException;
40 import org.springframework.util.Assert;
41
42 /**
43  * @author Felix Gnass [fgnass at neteye dot de]
44  * @since 6.5
45  */

46 public class PageRiotDao implements ParentChildDao, SwappableItemDao,
47         CutAndPasteEnabledDao, InitializingBean {
48
49     private PageDao pageDao;
50
51     private Locale JavaDoc masterLocale;
52
53     public PageRiotDao() {
54     }
55
56     public void setPageDao(PageDao pageDao) {
57         this.pageDao = pageDao;
58     }
59
60     public void afterPropertiesSet() throws Exception JavaDoc {
61         Assert.notNull(pageDao, "A PageDao must be set.");
62     }
63
64     public Locale JavaDoc getMasterLocale() {
65         return this.masterLocale;
66     }
67
68     public void setMasterLocale(Locale JavaDoc masterLocale) {
69         this.masterLocale = masterLocale;
70     }
71
72     public Object JavaDoc getParent(Object JavaDoc entity) {
73         Page page = (Page) entity;
74         Page parentPage = page.getParentPage();
75         if (parentPage != null) {
76             return parentPage;
77         }
78         return new SiteLocale(page.getNode().getSite(), page.getLocale());
79     }
80
81     public void delete(Object JavaDoc entity, Object JavaDoc parent) throws DataAccessException {
82         Page page = (Page) entity;
83         pageDao.deletePage(page);
84     }
85
86     public Class JavaDoc getEntityClass() {
87         return Page.class;
88     }
89
90     public int getListSize(Object JavaDoc parent, ListParams params)
91             throws DataAccessException {
92
93         return -1;
94     }
95
96     public String JavaDoc getObjectId(Object JavaDoc entity) {
97         Page page = (Page) entity;
98         return page.getId().toString();
99     }
100
101     public Collection JavaDoc list(Object JavaDoc parent, ListParams params)
102             throws DataAccessException {
103
104         if (parent instanceof Page) {
105             Page parentPage = (Page) parent;
106             return parentPage.getChildPages(masterLocale);
107         }
108         else {
109             Site site = null;
110             Locale JavaDoc locale = null;
111             if (parent instanceof SiteLocale) {
112                 SiteLocale siteLocale = (SiteLocale) parent;
113                 site = siteLocale.getSite();
114                 locale = siteLocale.getLocale();
115             }
116             else {
117                 site = pageDao.getDefaultSite();
118                 locale = getFixedLocale();
119             }
120             return pageDao.findRootNode(site).getChildPages(locale, masterLocale);
121         }
122     }
123
124     public Object JavaDoc load(String JavaDoc id) throws DataAccessException {
125         return pageDao.loadPage(new Long JavaDoc(id));
126     }
127
128     public void save(Object JavaDoc entity, Object JavaDoc parent) throws DataAccessException {
129         Page page = (Page) entity;
130         if (parent instanceof Page) {
131             Page parentPage = (Page) parent;
132             pageDao.savePage(parentPage, page);
133         }
134         else {
135             Site site = null;
136             Locale JavaDoc locale = null;
137             if (parent instanceof SiteLocale) {
138                 SiteLocale siteLocale = (SiteLocale) parent;
139                 site = siteLocale.getSite();
140                 locale = siteLocale.getLocale();
141             }
142             else {
143                 site = pageDao.getDefaultSite();
144                 locale = getFixedLocale();
145             }
146             page.setLocale(locale);
147             pageDao.savePage(site, page);
148         }
149     }
150
151     public void update(Object JavaDoc entity) throws DataAccessException {
152         pageDao.updatePage((Page) entity);
153     }
154
155     public void swapEntity(Object JavaDoc entity, Object JavaDoc parent, ListParams params,
156             int swapWith) {
157
158         Page page = (Page) entity;
159         PageNode node = page.getNode();
160         PageNode parentNode = node.getParent();
161         List JavaDoc nodes = parentNode.getChildNodes();
162
163         int pos = nodes.indexOf(node);
164         Object JavaDoc otherNode = nodes.get(swapWith);
165
166         nodes.remove(node);
167         nodes.remove(otherNode);
168
169         if (pos < swapWith) {
170             nodes.add(pos, otherNode);
171             nodes.add(swapWith, node);
172         }
173         else {
174             nodes.add(swapWith, node);
175             nodes.add(pos, otherNode);
176         }
177
178         pageDao.updateNode(parentNode);
179     }
180
181     public void addChild(Object JavaDoc entity, Object JavaDoc parent) {
182         Page page = (Page) entity;
183         PageNode node = page.getNode();
184         PageNode parentNode = null;
185         if (parent instanceof Page) {
186             Page parentPage = (Page) parent;
187             parentNode = parentPage.getNode();
188         }
189         else {
190             Site site = null;
191             if (parent instanceof SiteLocale) {
192                 SiteLocale siteLocale = (SiteLocale) parent;
193                 site = siteLocale.getSite();
194             }
195             else {
196                 site = pageDao.getDefaultSite();
197             }
198             parentNode = pageDao.findRootNode(site);
199         }
200         pageDao.moveNode(node, parentNode);
201     }
202
203     public void removeChild(Object JavaDoc entity, Object JavaDoc parent) {
204     }
205
206     private Locale JavaDoc getFixedLocale() {
207         List JavaDoc locales = pageDao.getLocales();
208         if (locales.size() > 1) {
209             throw new IllegalStateException JavaDoc("More than one Locale is " +
210                     "configured but null was passed as parent");
211         }
212         return (Locale JavaDoc) locales.get(0);
213     }
214
215 }
216
Popular Tags