surfacing external data through magnolia

27
Surfacing External Data Through Magnolia Sean McMains & Jeff Snider September 10, 2009

Upload: seanmctex

Post on 28-Jun-2015

2.205 views

Category:

Technology


3 download

DESCRIPTION

A technical presentation focusing on how to bring data from other systems into a Magnolia CMS and present it. Most current version at http://blitl.us/zBW5

TRANSCRIPT

Page 1: Surfacing External Data Through Magnolia

Surfacing External Data Through Magnolia

Sean McMains & Jeff SniderSeptember 10, 2009

Page 2: Surfacing External Data Through Magnolia

3 Approaches:JSP/Servlet Data Module RSS Module

Page 3: Surfacing External Data Through Magnolia

JSP/Servlet Approachor "Teaching an 

Old Dog New Tricks"

Photo Credit: Stephen Poff

Page 4: Surfacing External Data Through Magnolia

Department Directory

Page 5: Surfacing External Data Through Magnolia

Department Directory

XML-RPC

Methods:getDepartmentList() getResults()

Page 6: Surfacing External Data Through Magnolia

Implementing a Custom Control    <jsp:scriptlet>        DialogControlImpl control =             (DialogControlImpl)pageContext.getRequest().getAttribute("dialogObject");         XmlRpcClientLite client =            new XmlRpcClientLite("http://apps.its.txstate.edu/people/RPC.mpl");        String method = "getDepartmentList";        Vector params = new Vector();        Vector result = (Vector)client.execute( method, params );        ListIterator i = result.listIterator();

        Select selectControl = new Select( control.getName(), control.getWebsiteNode() );        selectControl.setCssClass( CssConstants.CSSCLASS_SELECT );

        while (i.hasNext() )        {            String department = (String)i.next();            SelectOption option = new SelectOption( department, department );            if ( department.equals( control.getValue() ) ) option.setSelected( true );            selectControl.setOptions( option );        }       out.println( selectControl.getHtml() );    </jsp:scriptlet>

Page 7: Surfacing External Data Through Magnolia

Presenting the Directory InfoXmlRpcClientLite client =   new XmlRpcClientLite("http://apps.its.txstate.edu/people/RPC.mpl"); String department =  Resource.getLocalContentNode(request).getNodeData("department").getString();String filter =   Resource.getLocalContentNode(request).getNodeData("filter").getString(); String method = "getResults";Vector params = new Vector();String searchTerm = "department=\"" + department + "\"";params.addElement(searchTerm); Vector result = (Vector)client.execute( method, params ); ListIterator i = result.listIterator();while ( i.hasNext() ) {  Hashtable person = (Hashtable)i.next();  ...  String firstName = (String)person.get("firstname");  String lastName = (String)person.get("lastname");  html.append( "<div class=\"txst-departmentdirectory-name fn n\">    <span class=\"given-name\">" + firstName + "</span> <span class=\"family-name\">" +    lastName + "</span>&nbsp;</div>" );  ... }                                    

Page 8: Surfacing External Data Through Magnolia

JSP/Servlet Approach

Pros:• Quick & easy• No additional modules

needed, aside from whatever you use to actually fetch the data

• Uses familiar techniques

Cons:• Page caching issues• Performance• At mercy of connection

to data source• You have to write it all

yourself!

Page 9: Surfacing External Data Through Magnolia

The Data Moduleor "How I Learned to Stop

Worrying and Love the JCR"

Photo Credit: Kjunstorm

Page 10: Surfacing External Data Through Magnolia

The Data Module

Info:• Data Module Docs• Footers Example• Advanced Data Module Example (?)

 Requirements:• Magnolia 4.x• Scheduler Module• A Bit of Patience

Page 11: Surfacing External Data Through Magnolia

Creating a New Data Type

Demonstration

Page 12: Surfacing External Data Through Magnolia

Import Handlers

• Built on scheduler module• Provide an automated means to suck in new data• At /modules/data/config/importers

Page 13: Surfacing External Data Through Magnolia

Using Import Handlers

Page 14: Surfacing External Data Through Magnolia

Configuring an Import Handler

Page 15: Surfacing External Data Through Magnolia

ImportTarget Codepublic class ImportTarget {

    protected String name;    protected String targetPath;

    public void setName(String name) {        this.name = name;    }

    public void setTargetPath(String targetPath) {        this.targetPath = targetPath;    }

    public ImportTarget() {    }

    public String getTargetPath() {        return targetPath;    }

    public String getName() {        return name;    }}

Page 16: Surfacing External Data Through Magnolia

ImportHandler Sample

public class DateTimeImportHandler extends ImportHandler {    ...    protected Set doImport(ImportTarget target, Content parent, Set uuids) throws ImportException {            ...            String date = dateFormat.format( new Date() );            String name = "ImportedSampleData" + parent.getChildren("example").size();

            // create new data node.            // FYI: if the name matches existing node and "deleteExisting" is            // set to true, old copy of the node will be automatically deleted.            Content child = parent.createContent( name, "example" );

            // set properties for newly created node            NodeDataUtil.getOrCreate( child, "name").setValue(name);            NodeDataUtil.getOrCreate(child, "comment").setValue("Example of data import.              Generated on " + date);            NodeDataUtil.getOrCreate(child, "data").setValue(date);

            parent.save();

            uuids.add(child.getUUID());            ...        return uuids;    }}

Page 17: Surfacing External Data Through Magnolia

JCR Query

• XPath or SQL-style• Powerful selection capabilities• Can be complicated

 Additional Info:•  JSR-170 Spec

Page 18: Surfacing External Data Through Magnolia

Using the Node Object

            <jsp:scriptlet>

                final String BR = "&lt;br/&gt;";                                HierarchyManager dataHierarchyManager =                     MgnlContext.getHierarchyManager( "data" );                Content eventsNode = dataHierarchyManager.getContent( "/event" );                Iterator eventsNodeIterator = eventsNode.getChildren( "event" ).iterator();                                while ( eventsNodeIterator.hasNext() ) {                    Content event = (Content)eventsNodeIterator.next();                    out.write( event.getNodeData( "name" ).getString() + BR );                    out.write( event.getNodeData( "date" ).getString() + BR + BR );                }                            </jsp:scriptlet>

Page 19: Surfacing External Data Through Magnolia

Caveat: Bootstrapping Data

Add to your project's pom.xml:…<dependencies>    <dependency>      <name>data</name>      <version>1.2.1/*</version>    </dependency></dependencies>…

Page 20: Surfacing External Data Through Magnolia

Data Module

Pros:• Data is cached in JCR• Resilient to temporary

disconnect from data• Can enter data

manually through AdminCentral or import automatically

Cons:• You still have to write it

all yourself• Requires learning

another framework

Page 21: Surfacing External Data Through Magnolia

Bringing in RSS Contentor "All the news that fits, we print!"

Photo Credit: tantrum_dan

Page 22: Surfacing External Data Through Magnolia

The RSS Module

Info:• Official Documentation• Jan's Weblog Entry

 Requires:• Magnolia 4.x• Scheduler Module• Data Module• Built on ROME Project

Page 23: Surfacing External Data Through Magnolia

Sightseeing in the RSS Module

Add new feeds

Configure RSS Module

Page 24: Surfacing External Data Through Magnolia

Setting up RSS Feeds

Demonstration

Page 25: Surfacing External Data Through Magnolia

RSS Module

Pros:• Easy set up• Includes several useful

paragraphs• Can use it without

writing any additional code

Cons:• Can only use content

that's published in RSS format

Page 26: Surfacing External Data Through Magnolia

Questions?

Photo Credit: coldtaxi

Page 27: Surfacing External Data Through Magnolia

Photo Credit: damaradeaella