From 6b8b421da02a171c7d5730f37448dd2be16c141c Mon Sep 17 00:00:00 2001 From: quasi Date: Thu, 20 Jun 2013 10:49:30 +0000 Subject: [PATCH] Ticket #131: Navigation Indexpage nicht (mehr) Subsite aware MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Erste Version. Scheint aber schon gut zu funktionieren. Intensiver Test steht noch aus. Hinzugefügt: * SubsiteItemURLFinder ** Neuen Subsite-aware URLFinder geschreiben und eingebunden * Geändert: ItemURLFinder ** Teil der find Methode in getCategories ausgelagert git-svn-id: https://svn.libreccm.org/ccm/trunk@2222 8810af33-2d31-482b-a856-94f89814c4df --- .../cms/dispatcher/ItemURLFinder.java | 110 +++++++++++------- .../com/arsdigita/subsite/Initializer.java | 25 ++-- .../dispatcher/SubsiteItemURLFinder.java | 54 +++++++++ 3 files changed, 133 insertions(+), 56 deletions(-) create mode 100644 ccm-subsite/src/com/arsdigita/subsite/dispatcher/SubsiteItemURLFinder.java diff --git a/ccm-cms/src/com/arsdigita/cms/dispatcher/ItemURLFinder.java b/ccm-cms/src/com/arsdigita/cms/dispatcher/ItemURLFinder.java index 8899f5349..4322e6f29 100755 --- a/ccm-cms/src/com/arsdigita/cms/dispatcher/ItemURLFinder.java +++ b/ccm-cms/src/com/arsdigita/cms/dispatcher/ItemURLFinder.java @@ -32,21 +32,24 @@ import com.arsdigita.kernel.URLFinderNotFoundException; import com.arsdigita.kernel.URLService; import com.arsdigita.persistence.DataAssociation; import com.arsdigita.persistence.DataAssociationCursor; +import com.arsdigita.persistence.DataObject; import com.arsdigita.persistence.OID; import com.arsdigita.web.ParameterMap; import com.arsdigita.web.URL; import com.arsdigita.web.Web; +import java.util.ArrayList; +import java.util.List; import org.apache.log4j.Logger; -/** - * Implementation of {@link com.arsdigita.kernel.URLFinder} for content types, +/** + * Implementation of {@link com.arsdigita.kernel.URLFinder} for content types, * necessary for ccm dispatcher to find a concrete content item for an url * provided by a client request. - * - * Specifically it is a helper class for {@link com.arsdigita.web.OIDRedirectServlet} - * to map an OID to an URL. - * + * + * Specifically it is a helper class for + * {@link com.arsdigita.web.OIDRedirectServlet} to map an OID to an URL. + * */ public class ItemURLFinder implements URLFinder { @@ -67,13 +70,12 @@ public class ItemURLFinder implements URLFinder { ContentItem item; try { - item = (ContentItem)DomainObjectFactory - .newInstance(oid); + item = (ContentItem) DomainObjectFactory + .newInstance(oid); } catch (DataObjectNotFoundException ex) { throw new NoValidURLException( - "cannot instantiate item " + oid + - " message: " + ex.getMessage() - ); + "cannot instantiate item " + oid + + " message: " + ex.getMessage()); } if (ContentItem.LIVE.equals(context)) { @@ -87,8 +89,7 @@ public class ItemURLFinder implements URLFinder { } else { s_log.debug("Item was not live"); throw new NoValidURLException( - "item " + oid + " is not live" - ); + "item " + oid + " is not live"); } } @@ -96,7 +97,7 @@ public class ItemURLFinder implements URLFinder { } /** - * + * * @param oid * @return * @throws NoValidURLException @@ -109,13 +110,12 @@ public class ItemURLFinder implements URLFinder { ContentItem item; try { - item = (ContentItem)DomainObjectFactory - .newInstance(oid); + item = (ContentItem) DomainObjectFactory + .newInstance(oid); } catch (DataObjectNotFoundException ex) { throw new NoValidURLException( - "cannot instantiate item " + oid + - " message: " + ex.getMessage() - ); + "cannot instantiate item " + oid + + " message: " + ex.getMessage()); } if (s_log.isDebugEnabled()) { @@ -145,15 +145,15 @@ public class ItemURLFinder implements URLFinder { } /** - * + * * @param item * @param context publication status ['live'|'draft'] * @return * @throws NoValidURLException */ private String find(ContentItem item, String context) - throws NoValidURLException { - + throws NoValidURLException { + ContentSection section = item.getContentSection(); ItemResolver resolver = section.getItemResolver(); @@ -170,44 +170,44 @@ public class ItemURLFinder implements URLFinder { bundle = (ContentBundle) ((ContentBundle) parent).getDraftVersion(); } if (bundle != null) { - DataAssociationCursor categories = - ((DataAssociation) DomainServiceInterfaceExposer. - get(bundle, Category.CATEGORIES)).cursor(); - // XXX pb (2010.12.15) ToDO - // solution is currently not subsite aware! - // provides an index url even if the category tree is not part - // of subsite (which results in a resource not found exception) - categories.addEqualsFilter("link." + Category.IS_INDEX, Boolean.TRUE); - if (categories.next()) { + List categories = getCategories(bundle); + + /* For all associated categories, try to get a url. Stop at + * first successful try.*/ + for(DataObject dobj:categories) { + String url; Category indexCat = (Category) DomainObjectFactory. - newInstance(categories.getDataObject()); - categories.close(); + newInstance(dobj); try { if (s_log.isDebugEnabled()) { - s_log.debug(item + " is a Category index item. " + - "Resolving URL for " + indexCat); + s_log.debug(item + " is a Category index item. " + + "Resolving URL for " + indexCat); } - return URLService.locate(indexCat.getOID(), context); + url = URLService.locate(indexCat.getOID(), context); } catch (URLFinderNotFoundException ufnfe) { if (s_log.isDebugEnabled()) { - s_log.debug("Could not find URLFinder for " + indexCat + - ", continuing with URL resolution for " + item, - ufnfe); + s_log.debug("Could not find URLFinder for " + indexCat + + ", continuing with URL resolution for " + item, + ufnfe); } + continue; } catch (NoValidURLException nvue) { if (s_log.isDebugEnabled()) { - s_log.debug("Could not find valid URL for " + indexCat + - ", continuing with URL resolution for " + item, - nvue); + s_log.debug("Could not find valid URL for " + indexCat + + ", continuing with URL resolution for " + item, + nvue); } + continue; } + return url; + } } } else { // DRAFT context // public users get 404 when item gets unpublished // if com.arsdigita.cms.unpublished_not_found=true if (ContentSection.getConfig().isUnpublishedNotFound() - && !Web.getUserContext().isLoggedIn()) { + && !Web.getUserContext().isLoggedIn()) { throw new NoValidURLException("user must be logged-in to get draft"); } else { // force the switch to draft version at this point @@ -233,8 +233,7 @@ public class ItemURLFinder implements URLFinder { if (sep == -1) { destination = URL.there(url, null); } else { - final ParameterMap params = ParameterMap.fromString - (url.substring(sep + 1)); + final ParameterMap params = ParameterMap.fromString(url.substring(sep + 1)); destination = URL.there(url.substring(0, sep), params); } @@ -246,4 +245,25 @@ public class ItemURLFinder implements URLFinder { return destination.toString(); } + /** + * Get all categories for a content bundle, where the content bundle is an + * index item. + * + * @param bundle The content bundle to test for + * @return a list of associated categories + */ + protected List getCategories(ContentBundle bundle) { + List catList = new ArrayList(); + DataAssociationCursor categories = + ((DataAssociation) DomainServiceInterfaceExposer. + get(bundle, Category.CATEGORIES)).cursor(); + categories.addEqualsFilter("link." + Category.IS_INDEX, Boolean.TRUE); + + while(categories.next()) { + catList.add(categories.getDataObject()); + } + categories.close(); + + return catList; + } } diff --git a/ccm-subsite/src/com/arsdigita/subsite/Initializer.java b/ccm-subsite/src/com/arsdigita/subsite/Initializer.java index 952290c1d..027fb24da 100755 --- a/ccm-subsite/src/com/arsdigita/subsite/Initializer.java +++ b/ccm-subsite/src/com/arsdigita/subsite/Initializer.java @@ -18,24 +18,22 @@ package com.arsdigita.subsite; +import com.arsdigita.cms.ContentBundle; +import com.arsdigita.cms.ContentPage; import com.arsdigita.db.DbHelper; - -import com.arsdigita.domain.xml.TraversalHandler; import com.arsdigita.domain.DomainObject; - +import com.arsdigita.domain.xml.TraversalHandler; import com.arsdigita.kernel.ACSObjectInstantiator; - -import com.arsdigita.persistence.pdl.NameFilter; -import com.arsdigita.persistence.pdl.ManifestSource; +import com.arsdigita.kernel.URLService; import com.arsdigita.persistence.DataObject; - -import com.arsdigita.runtime.RuntimeConfig; -import com.arsdigita.runtime.PDLInitializer; +import com.arsdigita.persistence.pdl.ManifestSource; +import com.arsdigita.persistence.pdl.NameFilter; import com.arsdigita.runtime.CompoundInitializer; import com.arsdigita.runtime.DomainInitEvent; - +import com.arsdigita.runtime.PDLInitializer; +import com.arsdigita.runtime.RuntimeConfig; +import com.arsdigita.subsite.dispatcher.SubsiteItemURLFinder; import com.arsdigita.templating.PatternStylesheetResolver; - import com.arsdigita.xml.XML; /** @@ -70,6 +68,11 @@ public class Initializer extends CompoundInitializer { XML.parse(Subsite.getConfig().getTraversalAdapters(), new TraversalHandler()); + URLService.registerFinder(ContentPage.BASE_DATA_OBJECT_TYPE, + new SubsiteItemURLFinder()); + URLService.registerFinder(ContentBundle.BASE_DATA_OBJECT_TYPE, + new SubsiteItemURLFinder()); + PatternStylesheetResolver.registerPatternGenerator( "subsite", new SubsitePatternGenerator() ); diff --git a/ccm-subsite/src/com/arsdigita/subsite/dispatcher/SubsiteItemURLFinder.java b/ccm-subsite/src/com/arsdigita/subsite/dispatcher/SubsiteItemURLFinder.java new file mode 100644 index 000000000..23b3275a4 --- /dev/null +++ b/ccm-subsite/src/com/arsdigita/subsite/dispatcher/SubsiteItemURLFinder.java @@ -0,0 +1,54 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package com.arsdigita.subsite.dispatcher; + +import com.arsdigita.categorization.Category; +import com.arsdigita.cms.ContentBundle; +import com.arsdigita.cms.dispatcher.ItemURLFinder; +import com.arsdigita.domain.DomainObjectFactory; +import com.arsdigita.persistence.DataObject; +import com.arsdigita.subsite.Site; +import com.arsdigita.subsite.Subsite; +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author Sören Bernstein (quasimodo) + */ +public class SubsiteItemURLFinder extends ItemURLFinder { + + /** + * Get all categories for a content bundle, where the content bundle is an + * index item in the current subsite. + * + * @param bundle + * @return a list of categories from this subsite + */ + @Override + protected List getCategories(ContentBundle bundle) { + + List categories = super.getCategories(bundle); + List subsiteCategories = new ArrayList(); + + // If there is a current subsite, filter all categories which not belong + // to this subsites root category + if (Subsite.getContext().hasSite()) { + Site site = Subsite.getContext().getSite(); + Category subsiteRootCat = site.getRootCategory(); + + if (subsiteRootCat != null) { + for (DataObject dobj : categories) { + Category cat = (Category) DomainObjectFactory.newInstance(dobj); + if (subsiteRootCat.isMemberOfSubtree(cat)) { + subsiteCategories.add(dobj); + } + } + return subsiteCategories; + } + } + return categories; + } +}