From 114dab54a390890fdac00513e18b71992a00a7c3 Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 13 Jun 2013 13:18:37 +0000 Subject: [PATCH] Innere Klasse UrlFinder aus dem Initalizer ausgelagert. git-svn-id: https://svn.libreccm.org/ccm/trunk@2204 8810af33-2d31-482b-a856-94f89814c4df --- .../com/arsdigita/navigation/Initializer.java | 421 +++++++++--------- .../navigation/NavigationUrlFinder.java | 174 ++++++++ 2 files changed, 384 insertions(+), 211 deletions(-) create mode 100644 ccm-navigation/src/com/arsdigita/navigation/NavigationUrlFinder.java diff --git a/ccm-navigation/src/com/arsdigita/navigation/Initializer.java b/ccm-navigation/src/com/arsdigita/navigation/Initializer.java index b9758fe5f..62ff8e91a 100755 --- a/ccm-navigation/src/com/arsdigita/navigation/Initializer.java +++ b/ccm-navigation/src/com/arsdigita/navigation/Initializer.java @@ -15,7 +15,6 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ - package com.arsdigita.navigation; import com.arsdigita.categorization.Category; @@ -86,7 +85,7 @@ import com.arsdigita.xml.XML; */ public class Initializer extends CompoundInitializer { - public static final Logger s_log = Logger.getLogger(Initializer.class); + private static final Logger s_log = Logger.getLogger(Initializer.class); /** * Constructor @@ -95,10 +94,8 @@ public class Initializer extends CompoundInitializer { final String url = RuntimeConfig.getConfig().getJDBCURL(); final int database = DbHelper.getDatabaseFromURL(url); - add(new PDLInitializer - (new ManifestSource - ("ccm-navigation.pdl.mf", - new NameFilter(DbHelper.getDatabaseSuffix(database), "pdl")))); + add(new PDLInitializer(new ManifestSource("ccm-navigation.pdl.mf", + new NameFilter(DbHelper.getDatabaseSuffix(database), "pdl")))); } /** @@ -114,244 +111,246 @@ public class Initializer extends CompoundInitializer { // Use the content-sections configuration directly! // System.setProperty( NavigationConstants.DEFAULT_CONTENT_SECTION_URL, // Navigation.getConfig().getDefaultContentSectionURL() ); - System.setProperty( NavigationConstants.DEFAULT_CONTENT_SECTION_URL, - ContentSection.getConfig().getDefaultContentSection() ); + System.setProperty(NavigationConstants.DEFAULT_CONTENT_SECTION_URL, + ContentSection.getConfig().getDefaultContentSection()); - e.getFactory().registerInstantiator - (Template.BASE_DATA_OBJECT_TYPE, - new DomainObjectInstantiator() { - public DomainObject doNewInstance(DataObject dataObject) { - return new Template(dataObject); - } - @Override - public DomainObjectInstantiator - resolveInstantiator(DataObject obj) { - return this; - } - }); + e.getFactory().registerInstantiator(Template.BASE_DATA_OBJECT_TYPE, + new DomainObjectInstantiator() { + public DomainObject doNewInstance(DataObject dataObject) { + return new Template(dataObject); + } - e.getFactory().registerInstantiator - (Navigation.BASE_DATA_OBJECT_TYPE, - new ACSObjectInstantiator() { - @Override - public DomainObject doNewInstance(DataObject dataObject) { - return new Navigation(dataObject); - } - }); + @Override + public DomainObjectInstantiator resolveInstantiator(DataObject obj) { + return this; + } + + }); + + e.getFactory().registerInstantiator(Navigation.BASE_DATA_OBJECT_TYPE, + new ACSObjectInstantiator() { + @Override + public DomainObject doNewInstance(DataObject dataObject) { + return new Navigation(dataObject); + } + + }); NavigationTreePortlet.registerInstantiator(); NavigationTreePortlet.registerResourceTypeConfig(); - e.getFactory().registerInstantiator - (ItemListPortlet.BASE_DATA_OBJECT_TYPE, - new ACSObjectInstantiator() { - @Override - public DomainObject doNewInstance(DataObject dataObject) { - return new ItemListPortlet(dataObject); - } - }); + e.getFactory().registerInstantiator(ItemListPortlet.BASE_DATA_OBJECT_TYPE, + new ACSObjectInstantiator() { + @Override + public DomainObject doNewInstance(DataObject dataObject) { + return new ItemListPortlet(dataObject); + } - e.getFactory().registerInstantiator - (ObjectListPortlet.BASE_DATA_OBJECT_TYPE, - new ACSObjectInstantiator() { - @Override - public DomainObject doNewInstance(DataObject dataObject) { - return new ObjectListPortlet(dataObject); - } - }); - - e.getFactory().registerInstantiator - (TemplateMapping.BASE_DATA_OBJECT_TYPE, - new DomainObjectInstantiator() { - public DomainObject doNewInstance(DataObject dataObject) { - return new TemplateMapping(dataObject); - } - @Override - public DomainObjectInstantiator - resolveInstantiator(DataObject obj) { - return this; - } - }); + }); + + e.getFactory().registerInstantiator(ObjectListPortlet.BASE_DATA_OBJECT_TYPE, + new ACSObjectInstantiator() { + @Override + public DomainObject doNewInstance(DataObject dataObject) { + return new ObjectListPortlet(dataObject); + } + + }); + + e.getFactory().registerInstantiator(TemplateMapping.BASE_DATA_OBJECT_TYPE, + new DomainObjectInstantiator() { + public DomainObject doNewInstance(DataObject dataObject) { + return new TemplateMapping(dataObject); + } + + @Override + public DomainObjectInstantiator resolveInstantiator(DataObject obj) { + return this; + } + + }); new ResourceTypeConfig(ObjectListPortlet.BASE_DATA_OBJECT_TYPE) { @Override - public ResourceConfigFormSection getCreateFormSection - (final ResourceType resType, final RequestLocal parentAppRL) { + public ResourceConfigFormSection getCreateFormSection(final ResourceType resType, + final RequestLocal parentAppRL) { final ResourceConfigFormSection config = - new ObjectListPortletEditor(resType, parentAppRL); - + new ObjectListPortletEditor(resType, parentAppRL); + return config; } - + @Override - public ResourceConfigFormSection getModifyFormSection - (final RequestLocal application) { + public ResourceConfigFormSection getModifyFormSection(final RequestLocal application) { final ObjectListPortletEditor config = - new ObjectListPortletEditor(application); - + new ObjectListPortletEditor(application); + return config; } + }; new ResourceTypeConfig(ItemListPortlet.BASE_DATA_OBJECT_TYPE) { @Override - public ResourceConfigFormSection getCreateFormSection - (final ResourceType resType, final RequestLocal parentAppRL) { + public ResourceConfigFormSection getCreateFormSection(final ResourceType resType, + final RequestLocal parentAppRL) { final ResourceConfigFormSection config = - new ItemListPortletEditor(resType, parentAppRL); - + new ItemListPortletEditor(resType, parentAppRL); + return config; } - + @Override - public ResourceConfigFormSection getModifyFormSection - (final RequestLocal application) { + public ResourceConfigFormSection getModifyFormSection(final RequestLocal application) { final ItemListPortletEditor config = - new ItemListPortletEditor(application); - + new ItemListPortletEditor(application); + return config; } + }; + URLService.registerFinder(Category.BASE_DATA_OBJECT_TYPE, new NavigationUrlFinder()); - URLService.registerFinder( - Category.BASE_DATA_OBJECT_TYPE, - new URLFinder() { - public String find(OID oid, String context) - throws NoValidURLException { - return find(oid); - } - public String find(OID oid) - throws NoValidURLException { - - if (s_log.isInfoEnabled()) { - s_log.info("Locating " + oid); - } - - Category cat = (Category)DomainObjectFactory.newInstance(oid); - CategoryCollection ancestors = cat.getDefaultAscendants(); - // XXX bz 118313 - ancestors.clearOrder(); - ancestors.addOrder(Category.DEFAULT_ANCESTORS); - - List ids = new ArrayList(); - List paths = new LinkedList(); - boolean first = true; - while (ancestors.next()) { - Category anc = ancestors.getCategory(); - if (s_log.isDebugEnabled()) { - s_log.debug("Process parent " + anc); - } - ids.add(anc.getID()); - if (first) { - first = false; - paths.add(""); - continue; - } - if (paths != null) { - String url = anc.getURL(); - if (url != null && !"".equals(url)) { - s_log.debug("Appending '" + url + "' for anc"); - paths.add(url); - } else { - if (s_log.isInfoEnabled()) { - s_log.info("Cat " + anc + " has no url "); - } - paths = null; - } - } else { - s_log.debug("Path is null"); - } - } - - if (s_log.isDebugEnabled() && null != paths) { - StringBuffer buf = new StringBuffer(); - Iterator idsit = ids.iterator(); - Iterator pathsit = paths.iterator(); - while(idsit.hasNext()) { - buf.append(idsit.next().toString()).append(' '); - buf.append(pathsit.next().toString()).append('/'); - } - s_log.debug("Full path is " + buf.toString()); - } - - TemplateContext tContext = - Navigation.getContext().getTemplateContext(); - String useContext = null == tContext ? - null : tContext.getContext(); - if (s_log.isDebugEnabled()) { - s_log.debug("Use Context: " + useContext); - } - - DataCollection apps = SessionManager.getSession().retrieve - (Application.BASE_DATA_OBJECT_TYPE); - apps.addEqualsFilter("objectType", Navigation.BASE_DATA_OBJECT_TYPE); - apps.addEqualsFilter("rootUseContext.useContext", useContext); - Filter f = apps.addFilter("rootUseContext.rootCategory in :ids"); - f.set("ids", ids); - - apps.addPath("rootUseContext.rootCategory.id"); - - String appURL = null; - BigDecimal rootCatID = null; - - if (!apps.next()) { - appURL = Navigation.getConfig().getDefaultCategoryRootPath(); - // We can only use named paths if the category is mapped - // to a navigation app instance in the current - // use context - paths = null; - if (s_log.isDebugEnabled()) { - s_log.debug("Using default nav path " + appURL); - } - } else { - Application app = (Application) - DomainObjectFactory.newInstance(apps.getDataObject()); - appURL = app.getPrimaryURL(); - rootCatID = (BigDecimal) apps.get("rootUseContext.rootCategory.id"); - apps.close(); - } - - if (s_log.isInfoEnabled()) { - s_log.info("Application path is " + appURL); - } - - Assert.isTrue(appURL.startsWith("/"), "url starts with '/'"); - Assert.isTrue(appURL.endsWith("/"), "url ends with '/'"); - - ParameterMap map = new ParameterMap(); - String path; - if (paths == null) { - map.setParameter("categoryID", cat.getID()); - path = "category.jsp"; - } else { - if (s_log.isDebugEnabled()) { - s_log.debug("Generating path from category " + - rootCatID); - } - StringBuffer buf = new StringBuffer(); - Iterator pathsit = paths.iterator(); - Iterator idsit = ids.iterator(); - boolean gotRoot = false; - while(pathsit.hasNext()) { - String frag = (String)pathsit.next(); - BigDecimal id = (BigDecimal)idsit.next(); - if (gotRoot) buf.append(frag).append('/'); - else if (id.equals(rootCatID)) gotRoot = true; - } - path = buf.toString(); - } - - String url = URL.there(Web.getRequest(), - appURL + path, map).toString(); - if (s_log.isInfoEnabled()) { - s_log.info("Final url is " + url); - } - return url; - } - }); +// URLService.registerFinder(Category.BASE_DATA_OBJECT_TYPE, +// new URLFinder() { +// +// public String find(OID oid, String context) +// throws NoValidURLException { +// return find(oid); +// } +// +// public String find(OID oid) +// throws NoValidURLException { +// +// if (s_log.isInfoEnabled()) { +// s_log.info("Locating " + oid); +// } +// +// Category cat = (Category) DomainObjectFactory.newInstance(oid); +// CategoryCollection ancestors = cat.getDefaultAscendants(); +// // XXX bz 118313 +// ancestors.clearOrder(); +// ancestors.addOrder(Category.DEFAULT_ANCESTORS); +// +// List ids = new ArrayList(); +// List paths = new LinkedList(); +// boolean first = true; +// while (ancestors.next()) { +// Category anc = ancestors.getCategory(); +// if (s_log.isDebugEnabled()) { +// s_log.debug("Process parent " + anc); +// } +// ids.add(anc.getID()); +// if (first) { +// first = false; +// paths.add(""); +// continue; +// } +// if (paths != null) { +// String url = anc.getURL(); +// if (url != null && !"".equals(url)) { +// s_log.debug("Appending '" + url + "' for anc"); +// paths.add(url); +// } else { +// if (s_log.isInfoEnabled()) { +// s_log.info("Cat " + anc + " has no url "); +// } +// paths = null; +// } +// } else { +// s_log.debug("Path is null"); +// } +// } +// +// if (s_log.isDebugEnabled() && null != paths) { +// StringBuffer buf = new StringBuffer(); +// Iterator idsit = ids.iterator(); +// Iterator pathsit = paths.iterator(); +// while (idsit.hasNext()) { +// buf.append(idsit.next().toString()).append(' '); +// buf.append(pathsit.next().toString()).append('/'); +// } +// s_log.debug("Full path is " + buf.toString()); +// } +// +// TemplateContext tContext = +// Navigation.getContext().getTemplateContext(); +// String useContext = null == tContext ? null : tContext.getContext(); +// if (s_log.isDebugEnabled()) { +// s_log.debug("Use Context: " + useContext); +// } +// +// DataCollection apps = SessionManager.getSession().retrieve(Application.BASE_DATA_OBJECT_TYPE); +// apps.addEqualsFilter("objectType", Navigation.BASE_DATA_OBJECT_TYPE); +// apps.addEqualsFilter("rootUseContext.useContext", useContext); +// Filter f = apps.addFilter("rootUseContext.rootCategory in :ids"); +// f.set("ids", ids); +// +// apps.addPath("rootUseContext.rootCategory.id"); +// +// String appURL = null; +// BigDecimal rootCatID = null; +// +// if (!apps.next()) { +// appURL = Navigation.getConfig().getDefaultCategoryRootPath(); +// // We can only use named paths if the category is mapped +// // to a navigation app instance in the current +// // use context +// paths = null; +// if (s_log.isDebugEnabled()) { +// s_log.debug("Using default nav path " + appURL); +// } +// } else { +// Application app = (Application) DomainObjectFactory.newInstance(apps.getDataObject()); +// //appURL = app.getPrimaryURL(); +// appURL = String.format("%s/", app.getPath()); +// rootCatID = (BigDecimal) apps.get("rootUseContext.rootCategory.id"); +// apps.close(); +// } +// +// if (s_log.isInfoEnabled()) { +// s_log.info("Application path is " + appURL); +// } +// +// Assert.isTrue(appURL.startsWith("/"), "url starts with '/'"); +// Assert.isTrue(appURL.endsWith("/"), "url ends with '/'"); +// +// ParameterMap map = new ParameterMap(); +// String path; +// if (paths == null) { +// map.setParameter("categoryID", cat.getID()); +// path = "category.jsp"; +// } else { +// if (s_log.isDebugEnabled()) { +// s_log.debug("Generating path from category " + rootCatID); +// } +// StringBuffer buf = new StringBuffer(); +// Iterator pathsit = paths.iterator(); +// Iterator idsit = ids.iterator(); +// boolean gotRoot = false; +// while (pathsit.hasNext()) { +// String frag = (String) pathsit.next(); +// BigDecimal id = (BigDecimal) idsit.next(); +// if (gotRoot) { +// buf.append(frag).append('/'); +// } else if (id.equals(rootCatID)) { +// gotRoot = true; +// } +// } +// path = buf.toString(); +// } +// +// String url = URL.there(Web.getRequest(), appURL + path, map).toString(); +// if (s_log.isInfoEnabled()) { +// s_log.info("Final url is " + url); +// } +// return url; +// } +// +// }); XML.parse(Navigation.getConfig().getTraversalAdapters(), new TraversalHandler()); diff --git a/ccm-navigation/src/com/arsdigita/navigation/NavigationUrlFinder.java b/ccm-navigation/src/com/arsdigita/navigation/NavigationUrlFinder.java new file mode 100644 index 000000000..0cc563030 --- /dev/null +++ b/ccm-navigation/src/com/arsdigita/navigation/NavigationUrlFinder.java @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2013 Jens Pelzetter + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +package com.arsdigita.navigation; + +import com.arsdigita.categorization.Category; +import com.arsdigita.categorization.CategoryCollection; +import com.arsdigita.cms.TemplateContext; +import com.arsdigita.domain.DomainObjectFactory; +import com.arsdigita.kernel.ACSObject; +import com.arsdigita.kernel.NoValidURLException; +import com.arsdigita.kernel.URLFinder; +import com.arsdigita.persistence.DataCollection; +import com.arsdigita.persistence.Filter; +import com.arsdigita.persistence.OID; +import com.arsdigita.persistence.SessionManager; +import com.arsdigita.util.Assert; +import com.arsdigita.web.Application; +import com.arsdigita.web.ParameterMap; +import com.arsdigita.web.URL; +import com.arsdigita.web.Web; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import org.apache.log4j.Logger; + +/** + * + * @author Jens Pelzetter + * @version $Id$ + */ +public class NavigationUrlFinder implements URLFinder { + + private final static Logger LOGGER = Logger.getLogger(NavigationUrlFinder.class); + + public String find(final OID oid) throws NoValidURLException { + LOGGER.info(String.format("Locating %s", oid.toString())); + + final Category category = (Category) DomainObjectFactory.newInstance(oid); + final CategoryCollection ancestors = category.getDefaultAscendants(); + ancestors.clearOrder(); + ancestors.addOrder(Category.DEFAULT_ANCESTORS); + + final List ids = new ArrayList(); + List paths = new LinkedList(); + boolean first = true; + + while (ancestors.next()) { + final Category ancestor = ancestors.getCategory(); + LOGGER.debug(String.format("Process parent %s", ancestor.toString())); + + ids.add(ancestor.getID()); + if (first) { + first = false; + paths.add(""); + continue; + } + if (paths != null) { + String url = ancestor.getURL(); + if (url != null && !"".equals(url)) { + LOGGER.debug("Appending '" + url + "' for anc"); + paths.add(url); + } else { + if (LOGGER.isInfoEnabled()) { + LOGGER.info("Cat " + ancestor + " has no url "); + } + paths = null; + } + } else { + LOGGER.debug("Path is null"); + } + } + + if (LOGGER.isDebugEnabled() && null != paths) { + final StringBuilder builder = new StringBuilder(); + for (int i = 0; i < ids.size(); i++) { + builder.append(ids.get(i).toString()).append(' '); + builder.append(paths.get(i).toString()).append('/'); + } + + LOGGER.debug(String.format("Full path is %s.", builder.toString())); + } + + final TemplateContext templateContext = Navigation.getContext().getTemplateContext(); + final String useContext; + if (templateContext == null) { + useContext = null; + } else { + useContext = templateContext.getContext(); + } + LOGGER.debug(String.format("Use Context: %s", useContext)); + + final DataCollection applications = SessionManager.getSession().retrieve(Application.BASE_DATA_OBJECT_TYPE); + applications.addEqualsFilter(ACSObject.OBJECT_TYPE, Navigation.BASE_DATA_OBJECT_TYPE); + applications.addEqualsFilter("rootUseContext.useContext", useContext); + final Filter filter = applications.addFilter("rootUseContext.rootCategory in :ids"); + filter.set("ids", ids); + applications.addPath("rootUseContext.rootCategory.id"); + + final String applicationUrl; + final BigDecimal rootCatId; + if (applications.next()) { + final Application application = (Application) DomainObjectFactory.newInstance(applications.getDataObject()); + applicationUrl = String.format("%s/", application.getPath()); + rootCatId = (BigDecimal) applications.get("rootUseContext.rootCategory.id"); + applications.close(); + } else { + applicationUrl = Navigation.getConfig().getDefaultCategoryRootPath(); + rootCatId = null; + // We can only use named paths if the category is mapped + // to a navigation app instance in the current + // use context + paths = null; + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(String.format("Using default nav path " + applicationUrl)); + } + } + + if (LOGGER.isInfoEnabled()) { + LOGGER.info(String.format("Application path is %s", applicationUrl)); + } + + Assert.isTrue(applicationUrl.charAt(0) == '/', "Assert failed: url starts not with '/'"); + Assert.isTrue(applicationUrl.endsWith("/"), "Assert failed: url ends not with '/'"); + + final ParameterMap parameterMap = new ParameterMap(); + final String path; + if (paths == null) { + parameterMap.setParameter("categoryID", category.getID()); + path = "category.jsp"; + } else { + LOGGER.debug(String.format("Generating path from category %s", rootCatId)); + + final StringBuilder builder = new StringBuilder(); + for (int i = 0; i < paths.size(); i++) { + if (!ids.get(i).equals(rootCatId)) { + builder.append(paths.get(i)); + builder.append('/'); + } + } + + path = builder.toString(); + } + + final String url = URL.there(Web.getRequest(), String.format("%s%s", applicationUrl, path), parameterMap). + toString(); + LOGGER.info(String.format("Final URL is: %s", url)); + + return url; + + } + + public String find(final OID oid, final String context) throws NoValidURLException { + return find(oid); + } + +}