KickJava   Java API By Example, From Geeks To Geeks.

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


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

46 public class LocaleRiotDao extends RiotDaoAdapter
47         implements ParentChildDao, InitializingBean {
48
49     private static final String JavaDoc ID_SEPARATOR = ",";
50
51     private PageDao pageDao;
52
53     public LocaleRiotDao() {
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 Class JavaDoc getEntityClass() {
65         return SiteLocale.class;
66     }
67
68     public Collection JavaDoc list(Object JavaDoc parent, ListParams params) throws DataAccessException {
69         Site site = (Site) parent;
70         if (site == null) {
71             site = pageDao.getDefaultSite();
72         }
73         List JavaDoc locales = pageDao.getLocales();
74         ArrayList JavaDoc result = new ArrayList JavaDoc(locales.size());
75         Iterator JavaDoc it = locales.iterator();
76         while (it.hasNext()) {
77             Locale JavaDoc locale = (Locale JavaDoc) it.next();
78             result.add(new SiteLocale(site, locale));
79         }
80         return result;
81     }
82
83     public String JavaDoc getObjectId(Object JavaDoc entity) {
84         SiteLocale siteLocale = (SiteLocale) entity;
85         return siteLocale.getSite().getId().toString()
86                 + ID_SEPARATOR + siteLocale.getLocale().toString();
87     }
88
89     public Object JavaDoc load(String JavaDoc id) throws DataAccessException {
90         String JavaDoc[] s = StringUtils.split(id, ID_SEPARATOR);
91         Site site = pageDao.loadSite(new Long JavaDoc(s[0]));
92         Locale JavaDoc locale = StringUtils.parseLocaleString(s[1]);
93         return new SiteLocale(site, locale);
94     }
95
96     public Object JavaDoc getParent(Object JavaDoc entity) {
97         SiteLocale siteLocale = (SiteLocale) entity;
98         return siteLocale.getSite();
99     }
100 }
101
Popular Tags