From: <mb...@re...> - 2004-11-24 16:49:46
|
Author: mbooth Date: 2004-11-24 17:41:39 +0100 (Wed, 24 Nov 2004) New Revision: 125 Added: ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/ ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/ImagePropertyRenderer.java ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/SingleImageProperty.java Modified: ccm-ldn-image-step/trunk/application.xml Log: Add SingleImageProperty to efficiently (no additional queries) pull out a an image with a specific use context in a navigation widget. Modified: ccm-ldn-image-step/trunk/application.xml =================================================================== --- ccm-ldn-image-step/trunk/application.xml 2004-11-24 16:40:33 UTC (rev 124) +++ ccm-ldn-image-step/trunk/application.xml 2004-11-24 16:41:39 UTC (rev 125) @@ -10,6 +10,7 @@ <ccm:dependencies> <ccm:requires name="ccm-core" version="6.1.0"/> <ccm:requires name="ccm-cms" version="6.1.0"/> + <ccm:requires name="ccm-ldn-navigation" version="1.4.3"/> </ccm:dependencies> <ccm:contacts> Added: ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/ImagePropertyRenderer.java =================================================================== --- ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/ImagePropertyRenderer.java 2004-11-24 16:40:33 UTC (rev 124) +++ ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/ImagePropertyRenderer.java 2004-11-24 16:41:39 UTC (rev 125) @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2001-2004 Red Hat Inc. All Rights Reserved. + * + * 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.cms.contenttypes.imagestep; + +import com.arsdigita.london.navigation.DataCollectionPropertyRenderer; + +import com.arsdigita.cms.ReusableImageAsset; +import com.arsdigita.cms.contenttypes.ItemImageAttachment; + +import com.arsdigita.persistence.DataAssociation; +import com.arsdigita.persistence.DataAssociationCursor; +import com.arsdigita.persistence.DataCollection; +import com.arsdigita.persistence.DataObject; +import com.arsdigita.util.UncheckedWrapperException; +import com.arsdigita.xml.Element; + +/** + * Classes implementing this interface will render in XML a property which + * will normally have been previously added to a + * <code>DataCollectionDefinition</code>. + */ +public class ImagePropertyRenderer implements DataCollectionPropertyRenderer { + public static final String XML_NS = + "http://ccm.redhat.com/london/image_attachments"; + + /** + * Called from DataCollectionRenderer for every returned item. This method + * will add XML for the property to the renderer's output. + */ + public void render( DataCollection dc, Element parent ) { + + Object images = dc.get( ItemImageAttachment.IMAGE_ATTACHMENTS ); + if( null == images ) return; + + if( images instanceof DataObject ) { + Element root = rootElement( parent ); + render( (DataObject) images, root ); + } + + else if( images instanceof DataAssociation ) { + // XXX: Unused, untested code path + DataAssociationCursor cursor = ((DataAssociation) images).cursor(); + + Element root = null; + while( cursor.next() ) { + if( null == root ) root = rootElement( parent ); + render( cursor.getDataObject(), root ); + } + } + + else { + throw new UncheckedWrapperException( + "While trying to render image property, " + + ItemImageAttachment.IMAGE_ATTACHMENTS + + " association returned a " + images.getClass().getName() + + " (" + images.toString() + + "). Expected either a DataObject or a DataAssociation." + ); + } + } + + private void render( DataObject obj, Element root ) { + Element ia = root.newChildElement( "ia:imageAttachment", XML_NS ); + + DataObject image = (DataObject) obj.get( ItemImageAttachment.IMAGE ); + + Object context = obj.get( ItemImageAttachment.USE_CONTEXT ); + Object caption = obj.get( ItemImageAttachment.CAPTION ); + Object imageID = image.get( ReusableImageAsset.ID ); + Object width = image.get( ReusableImageAsset.WIDTH ); + Object height = image.get( ReusableImageAsset.HEIGHT ); + + Element imageIDE = ia.newChildElement( "ia:imageID", XML_NS ); + imageIDE.setText( imageID.toString() ); + + if( null != context ) { + Element contextE = ia.newChildElement( "ia:context", XML_NS ); + contextE.setText( context.toString() ); + } + + if( null != caption ) { + Element captionE = ia.newChildElement( "ia:caption", XML_NS ); + captionE.setText( caption.toString() ); + } + + if( null != width ) { + Element widthE = ia.newChildElement( "ia:width", XML_NS ); + widthE.setText( width.toString() ); + } + + if( null != height ) { + Element heightE = ia.newChildElement( "ia:height", XML_NS ); + heightE.setText( height.toString() ); + } + } + + private Element rootElement( Element parent ) { + return parent.newChildElement( "ia:imageAttachments", XML_NS ); + } +} Added: ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/SingleImageProperty.java =================================================================== --- ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/SingleImageProperty.java 2004-11-24 16:40:33 UTC (rev 124) +++ ccm-ldn-image-step/trunk/src/com/arsdigita/cms/contenttypes/imagestep/SingleImageProperty.java 2004-11-24 16:41:39 UTC (rev 125) @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2001-2004 Red Hat Inc. All Rights Reserved. + * + * 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.cms.contenttypes.imagestep; + +import com.arsdigita.london.navigation.DataCollectionProperty; + +import com.arsdigita.persistence.CompoundFilter; +import com.arsdigita.persistence.DataCollection; +import com.arsdigita.persistence.Filter; +import com.arsdigita.persistence.FilterFactory; + +import com.arsdigita.cms.ReusableImageAsset; +import com.arsdigita.cms.contenttypes.ItemImageAttachment; + +/** + * Add a single image with a specific context. + */ +public class SingleImageProperty implements DataCollectionProperty { + private String m_context; + + private static final String IMAGE_ID = + ItemImageAttachment.IMAGE_ATTACHMENTS + "." + + ItemImageAttachment.IMAGE + "." + + ReusableImageAsset.ID; + + private static final String WIDTH = + ItemImageAttachment.IMAGE_ATTACHMENTS + "." + + ItemImageAttachment.IMAGE + "." + + ReusableImageAsset.WIDTH; + + private static final String HEIGHT = + ItemImageAttachment.IMAGE_ATTACHMENTS + "." + + ItemImageAttachment.IMAGE + "." + + ReusableImageAsset.HEIGHT; + + private static final String CAPTION = + ItemImageAttachment.IMAGE_ATTACHMENTS + "." + + ItemImageAttachment.CAPTION; + + private static final String CONTEXT = + ItemImageAttachment.IMAGE_ATTACHMENTS + "." + + ItemImageAttachment.USE_CONTEXT; + + /** + * Create a SingleImageProperty which will pull out a single + * ItemImageAttachment with the given use context. + */ + public SingleImageProperty( String context ) { + m_context = context; + } + + public void addProperty( DataCollection dc ) { + dc.addPath( IMAGE_ID ); + dc.addPath( WIDTH ); + dc.addPath( HEIGHT ); + dc.addPath( CAPTION ); + dc.addPath( CONTEXT ); + + FilterFactory ff = dc.getFilterFactory(); + CompoundFilter or = ff.or(); + + or.addFilter( ff.equals( CONTEXT, m_context ) ); + or.addFilter( ff.equals( IMAGE_ID, null ) ); + + dc.addFilter( or ); + } +} |