diff --git a/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/ArticleSectionXMLGenerator.java b/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/ArticleSectionXMLGenerator.java new file mode 100644 index 000000000..35b537ef1 --- /dev/null +++ b/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/ArticleSectionXMLGenerator.java @@ -0,0 +1,230 @@ +package com.arsdigita.cms.contenttypes; + +import com.arsdigita.bebop.Page; +import com.arsdigita.bebop.PageState; +import com.arsdigita.cms.CMS; +import com.arsdigita.cms.CMSExcursion; +import com.arsdigita.cms.ContentItem; +import com.arsdigita.cms.ContentSection; +import com.arsdigita.cms.ExtraXMLGenerator; +import com.arsdigita.cms.dispatcher.XMLGenerator; +import com.arsdigita.util.UncheckedWrapperException; +import com.arsdigita.xml.Element; +import java.io.IOException; +import java.util.ArrayList; +import javax.servlet.ServletException; + +/** + * + * @author Jens Pelzetter + * @version $Id$ + */ +public class ArticleSectionXMLGenerator implements ExtraXMLGenerator { + + public static final String PAGE_NUMBER_PARAM = "page"; + //private final PageParameter pageParam = new PageParameter(PAGE_NUMBER_PARAM); + + public ArticleSectionXMLGenerator() { + } + + public void generateXML(final ContentItem item, + final Element element, + final PageState state) { + Element content = element.newChildElement("cms:articleSectionPanel", + CMS.CMS_XML_NS); + + XMLGenerator xmlGenerator = getXMLGenerator(state, item); + + ArticleSection sections[] = getSections(item, state); + for (int i = 0; i < sections.length; i++) { + generateSectionXML(state, content, sections[i], xmlGenerator); + } + } + + public void addGlobalStateParams(final Page page) { + } + + protected void generateSectionXML(final PageState state, + final Element parent, + final ContentItem section, + final XMLGenerator xmlGenerator) { + CMSExcursion excursion = new CMSExcursion() { + + public void excurse() { + setContentItem(section); + xmlGenerator.generateXML(state, parent, null); + } + }; + try { + excursion.run(); + } catch (ServletException ex) { + throw new UncheckedWrapperException("excursion failed", ex); + } catch (IOException ex) { + throw new UncheckedWrapperException("excursion failed", ex); + } + } + + protected ArticleSection[] getSections(ContentItem item, + final PageState state) { + PageNumber number = null; + //try { + + + //number = (PageNumber) state.getValue(pageParam); + //} catch (IllegalArgumentException e) { + // probably viewing an index item on a category, + // get the parameter from the request and set it + String value = state.getRequest().getParameter(PAGE_NUMBER_PARAM); + if (value == null) { + value = "1"; + } + number = new PageNumber(value); + //state.setValue(pageParam, number); + //} + if (number == null) { + number = new PageNumber("1"); + } + + MultiPartArticle mpa = (MultiPartArticle) item; + if (!number.wantAllSections()) { + return getSections(item, state, number.getPageNumber()); + } else { + ArticleSectionCollection sections = mpa.getSections(); + ArticleSection[] page = new ArticleSection[(int) sections.size()]; + int i = 0; + while (sections.next()) { + page[i] = (ArticleSection) sections.getArticleSection(); + i++; + } + return page; + } + } + + // Get the section based on position in list of sections + protected ArticleSection[] getSections(ContentItem item, + PageState state, + Integer number) { + MultiPartArticle mpa = (MultiPartArticle) item; + ArticleSectionCollection sections = mpa.getSections(); + int current = 1; + int desired = number.intValue(); + + ArrayList page; + try { + // Skip over sections until we hit the desired page + while (current < desired) { + if (!sections.next()) { + + return new ArticleSection[]{}; + } + ArticleSection section = (ArticleSection) sections. + getArticleSection(); + + if (section.isPageBreak()) { + current++; + } + } + + // Now capture sections until we hit the next page (or end of sections) + int subsequent = desired + 1; + page = new ArrayList(); + while (current < subsequent) { + if (!sections.next()) { + break; + } + ArticleSection section = (ArticleSection) sections. + getArticleSection(); + page.add(section); + if (section.isPageBreak()) { + current++; + } + } + } finally { + sections.close(); + } + + return (ArticleSection[]) page.toArray(new ArticleSection[page.size()]); + } + + /** + * Try to get the section from the context + * if there isn't (eg if we are looking at an index + * item on a category), guess the section from the item + * @param state + * @param item + * @return + */ + protected XMLGenerator getXMLGenerator(PageState state, ContentItem item) { + ContentSection section = null; + try { + section = CMS.getContext().getContentSection(); + } catch (Exception e) { + } + if (section == null) { + section = item.getContentSection(); + CMS.getContext().setContentSection(section); + } + return section.getXMLGenerator(); + } + + // A class representing either an Integer number indicating + // the position in the list of sections, or the string 'all' + private class PageNumber { + + private boolean m_all; + private Integer m_number; + + public PageNumber(String number) + throws NumberFormatException { + + if ("all".equals(number.toLowerCase())) { + m_all = true; + m_number = null; + } else { + m_all = false; + m_number = new Integer(number); + } + } + + public boolean wantAllSections() { + return m_all; + } + + public Integer getPageNumber() { + return m_number; + } + } + // A parameter which is either an Integer number indicating + // the position in the list of sections, or the string 'all' + /*private class PageParameter extends ParameterModel { + + public PageParameter(String name) { + super(name); + } + + public Object transformValue(HttpServletRequest request) + throws IllegalArgumentException { + return transformSingleValue(request); + } + + public Object unmarshal(String encoded) + throws IllegalArgumentException { + + if (encoded == null || encoded.length() == 0) { + return null; + } + try { + return new PageNumber(encoded); + } catch (NumberFormatException e) { + e.printStackTrace(); + throw new IllegalArgumentException(getName() + + " should be a BigDecimal: '" + + encoded + "'"); + } + } + + public Class getValueClass() { + return PageNumber.class; + } + }*/ +} diff --git a/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/MultiPartArticle.java b/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/MultiPartArticle.java index 2f4794f07..11558bea4 100755 --- a/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/MultiPartArticle.java +++ b/ccm-cms-types-mparticle/src/com/arsdigita/cms/contenttypes/MultiPartArticle.java @@ -19,6 +19,8 @@ package com.arsdigita.cms.contenttypes; import com.arsdigita.cms.ContentPage; +import com.arsdigita.cms.ExtraXMLGenerator; +import com.arsdigita.cms.contenttypes.ui.mparticle.ArticleSectionPanel; import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.domain.DomainObjectFactory; import com.arsdigita.persistence.DataAssociation; @@ -29,8 +31,7 @@ import com.arsdigita.persistence.OID; import org.apache.log4j.Logger; import java.math.BigDecimal; -import java.util.Collection; -import java.util.HashSet; +import java.util.List; /** * CMS content type that represents a multi-part article. @@ -292,5 +293,14 @@ public class MultiPartArticle extends ContentPage { } } + @Override + public List getExtraXMLGenerators() { + final List generators = super.getExtraXMLGenerators(); + + //generators.add(new ArticleSectionPanel()); + generators.add(new ArticleSectionXMLGenerator()); + + return generators; + } } diff --git a/ccm-cms-types-mparticle/web/static/content-types/com/arsdigita/cms/contenttypes/MultiPartArticle.xsl b/ccm-cms-types-mparticle/web/static/content-types/com/arsdigita/cms/contenttypes/MultiPartArticle.xsl index 5cad11059..c2ade73bc 100755 --- a/ccm-cms-types-mparticle/web/static/content-types/com/arsdigita/cms/contenttypes/MultiPartArticle.xsl +++ b/ccm-cms-types-mparticle/web/static/content-types/com/arsdigita/cms/contenttypes/MultiPartArticle.xsl @@ -59,7 +59,7 @@ - + - + diff --git a/ccm-cms/src/com/arsdigita/cms/ContentItem.java b/ccm-cms/src/com/arsdigita/cms/ContentItem.java index 95835f942..d7c098482 100755 --- a/ccm-cms/src/com/arsdigita/cms/ContentItem.java +++ b/ccm-cms/src/com/arsdigita/cms/ContentItem.java @@ -241,7 +241,7 @@ public class ContentItem extends VersionedACSObject implements CustomCopy { private VersionCache m_live; private boolean m_wasNew; private Reporter m_reporter; - private BasicAuditTrail m_audit_trail; + private BasicAuditTrail m_audit_trail; /** * Default constructor. This creates a new content item. @@ -1548,7 +1548,7 @@ public class ContentItem extends VersionedACSObject implements CustomCopy { public ContentItem copy() { return copy(null, false); } - + public ContentItem copy(String lang) { return copy(null, false, lang); } @@ -1578,7 +1578,7 @@ public class ContentItem extends VersionedACSObject implements CustomCopy { } return newItem; } - + final public ContentItem copy(final ContentItem newParent, final boolean copyServices, final String lang) { @@ -1591,8 +1591,6 @@ public class ContentItem extends VersionedACSObject implements CustomCopy { } return newItem; } - - /** * Performs the actual mechanics of copying a content item. @@ -2171,14 +2169,13 @@ public class ContentItem extends VersionedACSObject implements CustomCopy { public String getLastModifiedIP() { return m_audit_trail.getLastModifiedIP(); } - protected static List extraXMLGenerators = new ArrayList(); - + /** * Override this to explicit that your content items - * have extra XML to generate. - * ATM This is used in navigation for GreetingItems. + * have extra XML to generate. + * @return A list of all extra XML Generators for this content item. */ - public List getExtraXMLGenerators() { - return extraXMLGenerators; + public List getExtraXMLGenerators() { + return new ArrayList(); } } diff --git a/ccm-cms/src/com/arsdigita/cms/ExtraXMLGenerator.java b/ccm-cms/src/com/arsdigita/cms/ExtraXMLGenerator.java index 7ee635f34..234a1032a 100755 --- a/ccm-cms/src/com/arsdigita/cms/ExtraXMLGenerator.java +++ b/ccm-cms/src/com/arsdigita/cms/ExtraXMLGenerator.java @@ -6,7 +6,6 @@ package com.arsdigita.cms; import com.arsdigita.bebop.Page; import com.arsdigita.bebop.PageState; -import com.arsdigita.cms.ContentItem; import com.arsdigita.xml.Element; /** @@ -20,11 +19,15 @@ public interface ExtraXMLGenerator { /** * Specify the XML for a given content item. + * @param item The content item to render + * @param element The element to add the rendered content to + * @param state The current page state */ public void generateXML(ContentItem item, Element element, PageState state); /** * Add all required global parameters. + * @param p The page which contains to item to render */ public void addGlobalStateParams(Page p); } diff --git a/ccm-cms/src/com/arsdigita/cms/dispatcher/SimpleXMLGenerator.java b/ccm-cms/src/com/arsdigita/cms/dispatcher/SimpleXMLGenerator.java index 5ee30e058..f34a2653a 100755 --- a/ccm-cms/src/com/arsdigita/cms/dispatcher/SimpleXMLGenerator.java +++ b/ccm-cms/src/com/arsdigita/cms/dispatcher/SimpleXMLGenerator.java @@ -18,11 +18,13 @@ */ package com.arsdigita.cms.dispatcher; +import com.arsdigita.bebop.Page; import com.arsdigita.bebop.PageState; import com.arsdigita.cms.CMS; import com.arsdigita.cms.ContentItem; import com.arsdigita.cms.ContentItemXMLRenderer; import com.arsdigita.cms.ContentSection; +import com.arsdigita.cms.ExtraXMLGenerator; import com.arsdigita.cms.SecurityManager; import com.arsdigita.cms.UserDefinedContentItem; import com.arsdigita.cms.util.GlobalizationUtil; @@ -54,7 +56,7 @@ import java.util.Iterator; public class SimpleXMLGenerator implements XMLGenerator { private static final Logger s_log = - Logger.getLogger(SimpleXMLGenerator.class); + Logger.getLogger(SimpleXMLGenerator.class); public static final String ADAPTER_CONTEXT = SimpleXMLGenerator.class. getName(); @@ -101,13 +103,15 @@ public class SimpleXMLGenerator implements XMLGenerator { // Note that the xml that is generated is only of use if you DO NOT CACHE content pages. // cg. - PermissionDescriptor edit = new PermissionDescriptor(PrivilegeDescriptor. - get(SecurityManager.CMS_EDIT_ITEM), item, currentParty); + PermissionDescriptor edit = + new PermissionDescriptor(PrivilegeDescriptor.get( + SecurityManager.CMS_EDIT_ITEM), item, currentParty); if (PermissionService.checkPermission(edit)) { parent.addAttribute("canEdit", "true"); } - PermissionDescriptor publish = new PermissionDescriptor(PrivilegeDescriptor. - get(SecurityManager.CMS_PUBLISH), item, currentParty); + PermissionDescriptor publish = + new PermissionDescriptor(PrivilegeDescriptor.get( + SecurityManager.CMS_PUBLISH), item, currentParty); if (PermissionService.checkPermission(publish)) { parent.addAttribute("canPublish", "true"); } @@ -117,7 +121,9 @@ public class SimpleXMLGenerator implements XMLGenerator { if (!item.getClass().getName().equals(className)) { s_log.info("Specializing item"); try { - item = (ContentItem) DomainObjectFactory.newInstance(new OID(item. getObjectType().getQualifiedName(), item.getID())); + item = + (ContentItem) DomainObjectFactory.newInstance(new OID(item. + getObjectType().getQualifiedName(), item.getID())); } catch (DataObjectNotFoundException ex) { throw new UncheckedWrapperException( (String) GlobalizationUtil.globalize( @@ -143,7 +149,8 @@ public class SimpleXMLGenerator implements XMLGenerator { // This is the preferred method Element content = startElement(useContext); - ContentItemXMLRenderer renderer = new ContentItemXMLRenderer(content); + ContentItemXMLRenderer renderer = + new ContentItemXMLRenderer(content); renderer.setWrapAttributes(true); renderer.setWrapRoot(false); @@ -153,11 +160,15 @@ public class SimpleXMLGenerator implements XMLGenerator { renderer.walk(item, ADAPTER_CONTEXT); parent.addContent(content); + + for (ExtraXMLGenerator generator : item.getExtraXMLGenerators()) { + generator.generateXML(item, parent, state); + } } } /** - * Fetches the current content item. This method can be overidden to + * Fetches the current content item. This method can be overridden to * fetch any {@link com.arsdigita.cms.ContentItem}, but by default, * it fetches the ContentItem that is set in the page state * by the dispatcher. diff --git a/ccm-ldn-aplaws/src/com/arsdigita/aplaws/ui/SimplePage.java b/ccm-ldn-aplaws/src/com/arsdigita/aplaws/ui/SimplePage.java index bafa8be3c..31f296bcd 100755 --- a/ccm-ldn-aplaws/src/com/arsdigita/aplaws/ui/SimplePage.java +++ b/ccm-ldn-aplaws/src/com/arsdigita/aplaws/ui/SimplePage.java @@ -42,6 +42,7 @@ public class SimplePage extends com.arsdigita.ui.SimplePage { } + @Override public Element generateXMLHelper(PageState state, Document parent) { Element page = super.generateXMLHelper(state, parent);