Surfacing External Data Through Magnolia
-
Upload
seanmctex -
Category
Technology
-
view
2.204 -
download
3
description
Transcript of Surfacing External Data Through Magnolia
Surfacing External Data Through Magnolia
Sean McMains & Jeff SniderSeptember 10, 2009
3 Approaches:JSP/Servlet Data Module RSS Module
JSP/Servlet Approachor "Teaching an
Old Dog New Tricks"
Photo Credit: Stephen Poff
Department Directory
Department Directory
XML-RPC
Methods:getDepartmentList() getResults()
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>
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> </div>" ); ... }
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!
The Data Moduleor "How I Learned to Stop
Worrying and Love the JCR"
Photo Credit: Kjunstorm
The Data Module
Info:• Data Module Docs• Footers Example• Advanced Data Module Example (?)
Requirements:• Magnolia 4.x• Scheduler Module• A Bit of Patience
Creating a New Data Type
Demonstration
Import Handlers
• Built on scheduler module• Provide an automated means to suck in new data• At /modules/data/config/importers
Using Import Handlers
Configuring an Import Handler
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; }}
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; }}
JCR Query
• XPath or SQL-style• Powerful selection capabilities• Can be complicated
Additional Info:• JSR-170 Spec
Using the Node Object
<jsp:scriptlet>
final String BR = "<br/>"; 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>
Caveat: Bootstrapping Data
Add to your project's pom.xml:…<dependencies> <dependency> <name>data</name> <version>1.2.1/*</version> </dependency></dependencies>…
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
Bringing in RSS Contentor "All the news that fits, we print!"
Photo Credit: tantrum_dan
The RSS Module
Info:• Official Documentation• Jan's Weblog Entry
Requires:• Magnolia 4.x• Scheduler Module• Data Module• Built on ROME Project
Sightseeing in the RSS Module
Add new feeds
Configure RSS Module
Setting up RSS Feeds
Demonstration
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
Questions?
Photo Credit: coldtaxi
Photo Credit: damaradeaella