001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2007
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.pages.setup;
025:
026: import java.util.Collections;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Locale;
030:
031: import org.riotfamily.pages.dao.PageDao;
032: import org.riotfamily.pages.dao.PageDefinition;
033: import org.riotfamily.pages.model.PageNode;
034: import org.riotfamily.pages.model.Site;
035: import org.springframework.beans.factory.BeanFactoryUtils;
036: import org.springframework.beans.factory.InitializingBean;
037: import org.springframework.context.ApplicationContext;
038: import org.springframework.context.ApplicationContextAware;
039: import org.springframework.transaction.PlatformTransactionManager;
040: import org.springframework.transaction.TransactionStatus;
041: import org.springframework.transaction.support.TransactionCallbackWithoutResult;
042: import org.springframework.transaction.support.TransactionTemplate;
043:
044: /**
045: * @author Felix Gnass [fgnass at neteye dot de]
046: * @since 6.5
047: */
048: public class PageSetupBean implements InitializingBean,
049: ApplicationContextAware {
050:
051: private List sites;
052:
053: private List definitions;
054:
055: private PageDao pageDao;
056:
057: private PlatformTransactionManager transactionManager;
058:
059: private ApplicationContext applicationContext;
060:
061: public void setPageDao(PageDao pageDao) {
062: this .pageDao = pageDao;
063: }
064:
065: public void setTransactionManager(
066: PlatformTransactionManager transactionManager) {
067: this .transactionManager = transactionManager;
068: }
069:
070: public void setSites(List sites) {
071: this .sites = sites;
072: }
073:
074: public void setDefinitions(List definitions) {
075: this .definitions = definitions;
076: }
077:
078: public void setApplicationContext(
079: ApplicationContext applicationContext) {
080: this .applicationContext = applicationContext;
081: }
082:
083: public void afterPropertiesSet() throws Exception {
084: if (transactionManager == null) {
085: transactionManager = (PlatformTransactionManager) BeanFactoryUtils
086: .beanOfTypeIncludingAncestors(applicationContext,
087: PlatformTransactionManager.class);
088: }
089: if (pageDao == null) {
090: pageDao = (PageDao) BeanFactoryUtils
091: .beanOfTypeIncludingAncestors(applicationContext,
092: PageDao.class);
093: }
094: new TransactionTemplate(transactionManager)
095: .execute(new TransactionCallbackWithoutResult() {
096: protected void doInTransactionWithoutResult(
097: TransactionStatus status) {
098: createNodes();
099: }
100: });
101: }
102:
103: protected void createNodes() {
104: if (pageDao.listSites().isEmpty()) {
105: createSites();
106: PageNode rootNode = pageDao.getRootNode();
107: Iterator it = definitions.iterator();
108: while (it.hasNext()) {
109: PageDefinition definition = (PageDefinition) it.next();
110: definition.createNode(rootNode, sites, pageDao);
111: }
112: pageDao.updateNode(rootNode);
113: }
114: }
115:
116: protected void createSites() {
117: if (sites == null || sites.isEmpty()) {
118: Site site = new Site();
119: site.setLocale(Locale.ENGLISH);
120: site.setEnabled(true);
121: sites = Collections.singletonList(site);
122: }
123: Iterator it = sites.iterator();
124: while (it.hasNext()) {
125: Site site = (Site) it.next();
126: pageDao.saveSite(site);
127: }
128: }
129:
130: }
|