From: <mb...@re...> - 2005-02-11 12:07:58
|
Author: mbooth Date: 2005-02-11 13:07:06 +0100 (Fri, 11 Feb 2005) New Revision: 207 Modified: ccm-cms/trunk/pdl/com/arsdigita/portlet/ContentItemPortlet.pdl ccm-cms/trunk/src/com/arsdigita/cms/ui/portlet/ContentItemPortletEditor.java ccm-cms/trunk/src/com/arsdigita/cms/ui/portlet/ContentItemPortletRenderer.java Log: Fix ContentItemPortlet. Association is no longer deleted when item is unpublished. Use can paste complete URL of a published item and it will be recognised. User can also use preview URL. When item is not currently published, preview URL will always be used, and portlet will not display anything to public. Modified: ccm-cms/trunk/pdl/com/arsdigita/portlet/ContentItemPortlet.pdl =================================================================== --- ccm-cms/trunk/pdl/com/arsdigita/portlet/ContentItemPortlet.pdl 2005-02-10 15:55:32 UTC (rev 206) +++ ccm-cms/trunk/pdl/com/arsdigita/portlet/ContentItemPortlet.pdl 2005-02-11 12:07:06 UTC (rev 207) @@ -23,12 +23,19 @@ import com.arsdigita.portal.Portlet; object type ContentItemPortlet extends Portlet { + ContentItem[0..1] item = + join portlet_content_item.item_id to cms_items.item_id; + reference key (portlet_content_item.portlet_id); } -association { - ContentItem[0..1] item = - join portlet_content_item.item_id to cms_items.item_id; - ContentItemPortlet[0..n] itemPortlets = - join cms_items.item_id to portlet_content_item.item_id; -} +// Removed the 2 way association, as it was causing the association to be +// deleted when the item was unpublished even though the association is with +// the draft item. + +//association { +// ContentItem[0..1] item = +// join portlet_content_item.item_id to cms_items.item_id; +// ContentItemPortlet[0..n] itemPortlets = +// join cms_items.item_id to portlet_content_item.item_id; +//} Modified: ccm-cms/trunk/src/com/arsdigita/cms/ui/portlet/ContentItemPortletEditor.java =================================================================== --- ccm-cms/trunk/src/com/arsdigita/cms/ui/portlet/ContentItemPortletEditor.java 2005-02-10 15:55:32 UTC (rev 206) +++ ccm-cms/trunk/src/com/arsdigita/cms/ui/portlet/ContentItemPortletEditor.java 2005-02-11 12:07:06 UTC (rev 207) @@ -28,17 +28,26 @@ import com.arsdigita.bebop.parameters.StringInRangeValidationListener; import com.arsdigita.bebop.parameters.StringParameter; import com.arsdigita.bebop.portal.PortletConfigFormSection; -import com.arsdigita.cms.ContentItem; -import com.arsdigita.cms.ContentSection; -import com.arsdigita.cms.dispatcher.ItemResolver; -import com.arsdigita.cms.dispatcher.MultilingualItemResolver; -import com.arsdigita.cms.portlet.ContentItemPortlet; import com.arsdigita.domain.DataObjectNotFoundException; import com.arsdigita.kernel.ResourceType; import com.arsdigita.kernel.SiteNode; import com.arsdigita.portal.Portlet; +import com.arsdigita.util.UncheckedWrapperException; import com.arsdigita.web.Application; import com.arsdigita.web.URL; +import com.arsdigita.web.Web; + +import com.arsdigita.cms.ContentItem; +import com.arsdigita.cms.ContentSection; +import com.arsdigita.cms.ContentSectionCollection; +import com.arsdigita.cms.dispatcher.CMSDispatcher; +import com.arsdigita.cms.dispatcher.ItemResolver; +import com.arsdigita.cms.dispatcher.MultilingualItemResolver; +import com.arsdigita.cms.portlet.ContentItemPortlet; + +import java.net.MalformedURLException; +import java.util.StringTokenizer; + import org.apache.log4j.Logger; public class ContentItemPortletEditor extends PortletConfigFormSection { @@ -49,44 +58,76 @@ private TextField m_url; private RequestLocal m_contentItem = new RequestLocal() { - protected Object initialValue( PageState ps ) { - String url = (String) m_url.getValue(ps); - String dp = URL.getDispatcherPath(); + protected Object initialValue( PageState ps ) { + String userURL = (String) m_url.getValue(ps); + java.net.URL contextURL; + try { + contextURL = new java.net.URL( Web.getRequest().getRequestURL().toString() ); + } catch ( MalformedURLException ex ) { + throw new UncheckedWrapperException( ex ); + } - if ( url.startsWith(dp) ) { - url = url.substring(dp.length()); - } + java.net.URL url; + try { + url = new java.net.URL( contextURL, userURL ); + } catch( MalformedURLException ex ) { + s_log.info( "Malformed URL " + userURL ); + return null; + } - SiteNode node = null; + String dp = URL.getDispatcherPath(); + String path = url.getPath(); + if( path.startsWith( dp ) ) { + path = path.substring(dp.length()); + } - try { - node = SiteNode.getSiteNode(url); - } catch (DataObjectNotFoundException ex) { - s_log.debug( "Couldn't fetch sitenode for " + url ); - return null; - } + StringTokenizer tok = new StringTokenizer( path, "/" ); + if( !tok.hasMoreTokens() ) { + s_log.info( "Couldn't find a content section for " + path + + " in " + userURL ); + return null; + } - ContentSection section = null; - section = ContentSection.getSectionFromNode(node); - ItemResolver resolver = section.getItemResolver(); + String sectionPath = '/' + tok.nextToken() + '/'; - String base = node.getURL(); - url = url.substring(base.length(), url.length()); + String context = ContentItem.LIVE; + if( tok.hasMoreTokens() && + CMSDispatcher.PREVIEW.equals( tok.nextToken() ) ) { - if (url.endsWith(".jsp")) { - url = url.substring(0, url.length()-4); - } + context = CMSDispatcher.PREVIEW; + } - ContentItem item = resolver.getItem(section, url, ContentItem.LIVE); - if (item == null) { - s_log.debug( "Couldn't resolve item " + url ); - return null; - } + ContentSectionCollection sections = ContentSection.getAllSections(); + sections.addEqualsFilter( Application.PRIMARY_URL, sectionPath ); - return item; + ContentSection section; + if( sections.next() ) { + section = sections.getContentSection(); + sections.close(); + } else { + s_log.info( "Content section " + sectionPath + " in " + + userURL + " doesn't exist." ); + return null; } - }; + ItemResolver resolver = section.getItemResolver(); + + path = path.substring( sectionPath.length() ); + + if (path.endsWith(".jsp")) { + path = path.substring(0, path.length()-4); + } + + ContentItem item = resolver.getItem(section, path, context); + if (item == null) { + s_log.debug( "Couldn't resolve item " + path ); + return null; + } + + return item.getDraftVersion(); + } + }; + public ContentItemPortletEditor(ResourceType resType, RequestLocal parentAppRL) { super(resType, parentAppRL); @@ -117,9 +158,16 @@ ItemResolver resolver = new MultilingualItemResolver(); ContentItem item = myportlet.getContentItem(); if (item != null) { + String context; + if( item.isLive() ) { + item = item.getPublicVersion(); + context = ContentItem.LIVE; + } + else context = CMSDispatcher.PREVIEW; + m_url.setValue(state, resolver.generateItemURL(state, item, item.getContentSection(), - ContentItem.LIVE)); + context)); } else { m_url.setValue(state, ""); } Modified: ccm-cms/trunk/src/com/arsdigita/cms/ui/portlet/ContentItemPortletRenderer.java =================================================================== --- ccm-cms/trunk/src/com/arsdigita/cms/ui/portlet/ContentItemPortletRenderer.java 2005-02-10 15:55:32 UTC (rev 206) +++ ccm-cms/trunk/src/com/arsdigita/cms/ui/portlet/ContentItemPortletRenderer.java 2005-02-11 12:07:06 UTC (rev 207) @@ -33,7 +33,6 @@ private static org.apache.log4j.Logger s_log = org.apache.log4j.Logger.getLogger(ContentItemPortletRenderer.class); - private RequestLocalSelectionModel m_item = new RequestLocalSelectionModel(); private ContentItemPortlet m_portlet; public ContentItemPortletRenderer(ContentItemPortlet portlet) { @@ -46,26 +45,38 @@ "http://www.arsdigita.com/portlet/1.0"); ContentItem item = m_portlet.getContentItem(); - if (item != null) { - m_item.setSelectedObject(state, item); + if( null == item ) { + s_log.warn( "No content item for content item portlet " + + m_portlet.getOID() ); + return; + } + if( !item.isLive() ) { + s_log.info( "No live version for content item portlet " + + m_portlet.getOID() ); + return; + } - Element contentItem = content.newChildElement - ( "cms:item", CMS.CMS_XML_NS ); + Element contentItem = content.newChildElement + ( "cms:item", CMS.CMS_XML_NS ); - DomainObjectXMLRenderer renderer = - new DomainObjectXMLRenderer(contentItem); + DomainObjectXMLRenderer renderer = + new DomainObjectXMLRenderer(contentItem); - renderer.setWrapAttributes( true ); - renderer.setWrapRoot( false ); - renderer.setWrapObjects( false ); + renderer.setWrapAttributes( true ); + renderer.setWrapRoot( false ); + renderer.setWrapObjects( false ); - renderer.walk( item, SimpleXMLGenerator.ADAPTER_CONTEXT ); - } + renderer.walk( item.getPublicVersion(), + SimpleXMLGenerator.ADAPTER_CONTEXT ); } public Object getCacheKey() { ContentItem item = m_portlet.getContentItem(); - return item == null ? null : item; + if( null == item ) return null; + if( item.isLive() ) return item.getPublicVersion().getOID(); + + // Don't cache it if it's not live + return null; } // For a given cache key a contnet item is *never* dirty, |