Innere Klasse UrlFinder aus dem Initalizer ausgelagert.

git-svn-id: https://svn.libreccm.org/ccm/trunk@2204 8810af33-2d31-482b-a856-94f89814c4df
master
jensp 2013-06-13 13:18:37 +00:00
parent 780137c5b5
commit 114dab54a3
2 changed files with 384 additions and 211 deletions

View File

@ -15,7 +15,6 @@
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
package com.arsdigita.navigation; package com.arsdigita.navigation;
import com.arsdigita.categorization.Category; import com.arsdigita.categorization.Category;
@ -86,7 +85,7 @@ import com.arsdigita.xml.XML;
*/ */
public class Initializer extends CompoundInitializer { 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 * Constructor
@ -95,10 +94,8 @@ public class Initializer extends CompoundInitializer {
final String url = RuntimeConfig.getConfig().getJDBCURL(); final String url = RuntimeConfig.getConfig().getJDBCURL();
final int database = DbHelper.getDatabaseFromURL(url); final int database = DbHelper.getDatabaseFromURL(url);
add(new PDLInitializer add(new PDLInitializer(new ManifestSource("ccm-navigation.pdl.mf",
(new ManifestSource new NameFilter(DbHelper.getDatabaseSuffix(database), "pdl"))));
("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! // Use the content-sections configuration directly!
// System.setProperty( NavigationConstants.DEFAULT_CONTENT_SECTION_URL, // System.setProperty( NavigationConstants.DEFAULT_CONTENT_SECTION_URL,
// Navigation.getConfig().getDefaultContentSectionURL() ); // Navigation.getConfig().getDefaultContentSectionURL() );
System.setProperty( NavigationConstants.DEFAULT_CONTENT_SECTION_URL, System.setProperty(NavigationConstants.DEFAULT_CONTENT_SECTION_URL,
ContentSection.getConfig().getDefaultContentSection() ); ContentSection.getConfig().getDefaultContentSection());
e.getFactory().registerInstantiator e.getFactory().registerInstantiator(Template.BASE_DATA_OBJECT_TYPE,
(Template.BASE_DATA_OBJECT_TYPE, new DomainObjectInstantiator() {
new DomainObjectInstantiator() { public DomainObject doNewInstance(DataObject dataObject) {
public DomainObject doNewInstance(DataObject dataObject) { return new Template(dataObject);
return new Template(dataObject); }
}
@Override
public DomainObjectInstantiator
resolveInstantiator(DataObject obj) {
return this;
}
});
e.getFactory().registerInstantiator @Override
(Navigation.BASE_DATA_OBJECT_TYPE, public DomainObjectInstantiator resolveInstantiator(DataObject obj) {
new ACSObjectInstantiator() { return this;
@Override }
public DomainObject doNewInstance(DataObject dataObject) {
return new Navigation(dataObject); });
}
}); e.getFactory().registerInstantiator(Navigation.BASE_DATA_OBJECT_TYPE,
new ACSObjectInstantiator() {
@Override
public DomainObject doNewInstance(DataObject dataObject) {
return new Navigation(dataObject);
}
});
NavigationTreePortlet.registerInstantiator(); NavigationTreePortlet.registerInstantiator();
NavigationTreePortlet.registerResourceTypeConfig(); NavigationTreePortlet.registerResourceTypeConfig();
e.getFactory().registerInstantiator e.getFactory().registerInstantiator(ItemListPortlet.BASE_DATA_OBJECT_TYPE,
(ItemListPortlet.BASE_DATA_OBJECT_TYPE, new ACSObjectInstantiator() {
new ACSObjectInstantiator() { @Override
@Override public DomainObject doNewInstance(DataObject dataObject) {
public DomainObject doNewInstance(DataObject dataObject) { return new ItemListPortlet(dataObject);
return new ItemListPortlet(dataObject); }
}
});
e.getFactory().registerInstantiator });
(ObjectListPortlet.BASE_DATA_OBJECT_TYPE,
new ACSObjectInstantiator() { e.getFactory().registerInstantiator(ObjectListPortlet.BASE_DATA_OBJECT_TYPE,
@Override new ACSObjectInstantiator() {
public DomainObject doNewInstance(DataObject dataObject) { @Override
return new ObjectListPortlet(dataObject); public DomainObject doNewInstance(DataObject dataObject) {
} return new ObjectListPortlet(dataObject);
}); }
e.getFactory().registerInstantiator });
(TemplateMapping.BASE_DATA_OBJECT_TYPE,
new DomainObjectInstantiator() { e.getFactory().registerInstantiator(TemplateMapping.BASE_DATA_OBJECT_TYPE,
public DomainObject doNewInstance(DataObject dataObject) { new DomainObjectInstantiator() {
return new TemplateMapping(dataObject); public DomainObject doNewInstance(DataObject dataObject) {
} return new TemplateMapping(dataObject);
@Override }
public DomainObjectInstantiator
resolveInstantiator(DataObject obj) { @Override
return this; public DomainObjectInstantiator resolveInstantiator(DataObject obj) {
} return this;
}); }
});
new ResourceTypeConfig(ObjectListPortlet.BASE_DATA_OBJECT_TYPE) { new ResourceTypeConfig(ObjectListPortlet.BASE_DATA_OBJECT_TYPE) {
@Override @Override
public ResourceConfigFormSection getCreateFormSection public ResourceConfigFormSection getCreateFormSection(final ResourceType resType,
(final ResourceType resType, final RequestLocal parentAppRL) { final RequestLocal parentAppRL) {
final ResourceConfigFormSection config = final ResourceConfigFormSection config =
new ObjectListPortletEditor(resType, parentAppRL); new ObjectListPortletEditor(resType, parentAppRL);
return config; return config;
} }
@Override @Override
public ResourceConfigFormSection getModifyFormSection public ResourceConfigFormSection getModifyFormSection(final RequestLocal application) {
(final RequestLocal application) {
final ObjectListPortletEditor config = final ObjectListPortletEditor config =
new ObjectListPortletEditor(application); new ObjectListPortletEditor(application);
return config; return config;
} }
}; };
new ResourceTypeConfig(ItemListPortlet.BASE_DATA_OBJECT_TYPE) { new ResourceTypeConfig(ItemListPortlet.BASE_DATA_OBJECT_TYPE) {
@Override @Override
public ResourceConfigFormSection getCreateFormSection public ResourceConfigFormSection getCreateFormSection(final ResourceType resType,
(final ResourceType resType, final RequestLocal parentAppRL) { final RequestLocal parentAppRL) {
final ResourceConfigFormSection config = final ResourceConfigFormSection config =
new ItemListPortletEditor(resType, parentAppRL); new ItemListPortletEditor(resType, parentAppRL);
return config; return config;
} }
@Override @Override
public ResourceConfigFormSection getModifyFormSection public ResourceConfigFormSection getModifyFormSection(final RequestLocal application) {
(final RequestLocal application) {
final ItemListPortletEditor config = final ItemListPortletEditor config =
new ItemListPortletEditor(application); new ItemListPortletEditor(application);
return config; return config;
} }
}; };
URLService.registerFinder(Category.BASE_DATA_OBJECT_TYPE, new NavigationUrlFinder());
URLService.registerFinder( // URLService.registerFinder(Category.BASE_DATA_OBJECT_TYPE,
Category.BASE_DATA_OBJECT_TYPE, // new URLFinder() {
new URLFinder() { //
public String find(OID oid, String context) // public String find(OID oid, String context)
throws NoValidURLException { // throws NoValidURLException {
return find(oid); // return find(oid);
} // }
public String find(OID oid) //
throws NoValidURLException { // public String find(OID oid)
// throws NoValidURLException {
if (s_log.isInfoEnabled()) { //
s_log.info("Locating " + oid); // if (s_log.isInfoEnabled()) {
} // s_log.info("Locating " + oid);
// }
Category cat = (Category)DomainObjectFactory.newInstance(oid); //
CategoryCollection ancestors = cat.getDefaultAscendants(); // Category cat = (Category) DomainObjectFactory.newInstance(oid);
// XXX bz 118313 // CategoryCollection ancestors = cat.getDefaultAscendants();
ancestors.clearOrder(); // // XXX bz 118313
ancestors.addOrder(Category.DEFAULT_ANCESTORS); // ancestors.clearOrder();
// ancestors.addOrder(Category.DEFAULT_ANCESTORS);
List ids = new ArrayList(); //
List paths = new LinkedList(); // List ids = new ArrayList();
boolean first = true; // List paths = new LinkedList();
while (ancestors.next()) { // boolean first = true;
Category anc = ancestors.getCategory(); // while (ancestors.next()) {
if (s_log.isDebugEnabled()) { // Category anc = ancestors.getCategory();
s_log.debug("Process parent " + anc); // if (s_log.isDebugEnabled()) {
} // s_log.debug("Process parent " + anc);
ids.add(anc.getID()); // }
if (first) { // ids.add(anc.getID());
first = false; // if (first) {
paths.add(""); // first = false;
continue; // paths.add("");
} // continue;
if (paths != null) { // }
String url = anc.getURL(); // if (paths != null) {
if (url != null && !"".equals(url)) { // String url = anc.getURL();
s_log.debug("Appending '" + url + "' for anc"); // if (url != null && !"".equals(url)) {
paths.add(url); // s_log.debug("Appending '" + url + "' for anc");
} else { // paths.add(url);
if (s_log.isInfoEnabled()) { // } else {
s_log.info("Cat " + anc + " has no url "); // if (s_log.isInfoEnabled()) {
} // s_log.info("Cat " + anc + " has no url ");
paths = null; // }
} // paths = null;
} else { // }
s_log.debug("Path is null"); // } else {
} // s_log.debug("Path is null");
} // }
// }
if (s_log.isDebugEnabled() && null != paths) { //
StringBuffer buf = new StringBuffer(); // if (s_log.isDebugEnabled() && null != paths) {
Iterator idsit = ids.iterator(); // StringBuffer buf = new StringBuffer();
Iterator pathsit = paths.iterator(); // Iterator idsit = ids.iterator();
while(idsit.hasNext()) { // Iterator pathsit = paths.iterator();
buf.append(idsit.next().toString()).append(' '); // while (idsit.hasNext()) {
buf.append(pathsit.next().toString()).append('/'); // buf.append(idsit.next().toString()).append(' ');
} // buf.append(pathsit.next().toString()).append('/');
s_log.debug("Full path is " + buf.toString()); // }
} // s_log.debug("Full path is " + buf.toString());
// }
TemplateContext tContext = //
Navigation.getContext().getTemplateContext(); // TemplateContext tContext =
String useContext = null == tContext ? // Navigation.getContext().getTemplateContext();
null : tContext.getContext(); // String useContext = null == tContext ? null : tContext.getContext();
if (s_log.isDebugEnabled()) { // if (s_log.isDebugEnabled()) {
s_log.debug("Use Context: " + useContext); // s_log.debug("Use Context: " + useContext);
} // }
//
DataCollection apps = SessionManager.getSession().retrieve // DataCollection apps = SessionManager.getSession().retrieve(Application.BASE_DATA_OBJECT_TYPE);
(Application.BASE_DATA_OBJECT_TYPE); // apps.addEqualsFilter("objectType", Navigation.BASE_DATA_OBJECT_TYPE);
apps.addEqualsFilter("objectType", Navigation.BASE_DATA_OBJECT_TYPE); // apps.addEqualsFilter("rootUseContext.useContext", useContext);
apps.addEqualsFilter("rootUseContext.useContext", useContext); // Filter f = apps.addFilter("rootUseContext.rootCategory in :ids");
Filter f = apps.addFilter("rootUseContext.rootCategory in :ids"); // f.set("ids", ids);
f.set("ids", ids); //
// apps.addPath("rootUseContext.rootCategory.id");
apps.addPath("rootUseContext.rootCategory.id"); //
// String appURL = null;
String appURL = null; // BigDecimal rootCatID = null;
BigDecimal rootCatID = null; //
// if (!apps.next()) {
if (!apps.next()) { // appURL = Navigation.getConfig().getDefaultCategoryRootPath();
appURL = Navigation.getConfig().getDefaultCategoryRootPath(); // // We can only use named paths if the category is mapped
// We can only use named paths if the category is mapped // // to a navigation app instance in the current
// to a navigation app instance in the current // // use context
// use context // paths = null;
paths = null; // if (s_log.isDebugEnabled()) {
if (s_log.isDebugEnabled()) { // s_log.debug("Using default nav path " + appURL);
s_log.debug("Using default nav path " + appURL); // }
} // } else {
} else { // Application app = (Application) DomainObjectFactory.newInstance(apps.getDataObject());
Application app = (Application) // //appURL = app.getPrimaryURL();
DomainObjectFactory.newInstance(apps.getDataObject()); // appURL = String.format("%s/", app.getPath());
appURL = app.getPrimaryURL(); // rootCatID = (BigDecimal) apps.get("rootUseContext.rootCategory.id");
rootCatID = (BigDecimal) apps.get("rootUseContext.rootCategory.id"); // apps.close();
apps.close(); // }
} //
// if (s_log.isInfoEnabled()) {
if (s_log.isInfoEnabled()) { // s_log.info("Application path is " + appURL);
s_log.info("Application path is " + appURL); // }
} //
// Assert.isTrue(appURL.startsWith("/"), "url starts with '/'");
Assert.isTrue(appURL.startsWith("/"), "url starts with '/'"); // Assert.isTrue(appURL.endsWith("/"), "url ends with '/'");
Assert.isTrue(appURL.endsWith("/"), "url ends with '/'"); //
// ParameterMap map = new ParameterMap();
ParameterMap map = new ParameterMap(); // String path;
String path; // if (paths == null) {
if (paths == null) { // map.setParameter("categoryID", cat.getID());
map.setParameter("categoryID", cat.getID()); // path = "category.jsp";
path = "category.jsp"; // } else {
} else { // if (s_log.isDebugEnabled()) {
if (s_log.isDebugEnabled()) { // s_log.debug("Generating path from category " + rootCatID);
s_log.debug("Generating path from category " + // }
rootCatID); // StringBuffer buf = new StringBuffer();
} // Iterator pathsit = paths.iterator();
StringBuffer buf = new StringBuffer(); // Iterator idsit = ids.iterator();
Iterator pathsit = paths.iterator(); // boolean gotRoot = false;
Iterator idsit = ids.iterator(); // while (pathsit.hasNext()) {
boolean gotRoot = false; // String frag = (String) pathsit.next();
while(pathsit.hasNext()) { // BigDecimal id = (BigDecimal) idsit.next();
String frag = (String)pathsit.next(); // if (gotRoot) {
BigDecimal id = (BigDecimal)idsit.next(); // buf.append(frag).append('/');
if (gotRoot) buf.append(frag).append('/'); // } else if (id.equals(rootCatID)) {
else if (id.equals(rootCatID)) gotRoot = true; // gotRoot = true;
} // }
path = buf.toString(); // }
} // path = buf.toString();
// }
String url = URL.there(Web.getRequest(), //
appURL + path, map).toString(); // String url = URL.there(Web.getRequest(), appURL + path, map).toString();
if (s_log.isInfoEnabled()) { // if (s_log.isInfoEnabled()) {
s_log.info("Final url is " + url); // s_log.info("Final url is " + url);
} // }
return url; // return url;
} // }
}); //
// });
XML.parse(Navigation.getConfig().getTraversalAdapters(), XML.parse(Navigation.getConfig().getTraversalAdapters(),
new TraversalHandler()); new TraversalHandler());

View File

@ -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 <jens@jp-digital.de>
* @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<BigDecimal> ids = new ArrayList<BigDecimal>();
List<String> paths = new LinkedList<String>();
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);
}
}