Dashboard > Community Wiki > ... > Template development > Mass Add Page
Mass Add Page Log In View a printable version of the current page.

Added by GrĂ©gory Joseph , last edited by Boris Kraft on Mar 14, 2008  (view change)
Labels: 

In order to support custom 404 messages for the 70-ish sites on our system, we needed to create a 404 page beneath the home page of each of our sites. (We use a custom cache in front of Magnolia which we taught to look for custom 404 pages in this location.) We wanted the pages to have some boilerplate text, which site owners could then modify as they saw fit, and to be marked "Do Not Show In Navigation" by default. Here's the code we used to achieve this.

First off, we grab a hierarchy manager, grab the root of the WEBSITE repository, and feed it to our method that processes the sites:

<jsp:scriptlet>
	<![CDATA[
	HierarchyManager hierarchyManager=MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
	MgnlContext.initAsWebContext( request );
	Content rootNode = hierarchyManager.getContent("/");
	
	response.getWriter().println("Processing sites...<br/>");
	processSites( rootNode, request, response );
	response.getWriter().println("**** ADDING 404s COMPLETE **** <br/>");	
	]]>
</jsp:scriptlet>

Then we iterate through each of the sites and catch any errors that the other methods might throw:

public void processSites( Content c, HttpServletRequest request, HttpServletResponse response )  {	
	try {
		Iterator childContentNodesIterator = c.getChildren( ItemType.CONTENT ).iterator();
				
		while ( childContentNodesIterator.hasNext() ) {
				Content childNode = (Content)childContentNodesIterator.next();
				createPage( childNode, request, response );
				response.flushBuffer();
		}				
				
	} catch (Exception e) {
		e.printStackTrace();
	}
}

Now that we have the root for each site, we can check to see if it already has a 404 page, and if not, create it, setting the appropriate page template, title, and the hideInNav directive along the way:

public void createPage( Content c, HttpServletRequest request, HttpServletResponse response ) throws Exception {
		response.getWriter().println("Processing Site: " + c.getHandle() + "<br/>");
		HierarchyManager hierarchyManager=MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
		ValueFactory valueFactory = hierarchyManager.getWorkspace().getSession().getValueFactory();

		if ( !hierarchyManager.isExist( c.getHandle() + "/404" ) ) {
			Content newPage = c.createContent("404", ItemType.CONTENT.getSystemName() );
			response.getWriter().println("Created new page at " + newPage.getHandle() + "<br/>" ) ;
			MetaData meta = newPage.getMetaData();
			meta.setAuthorId( Authenticator.getUserId( request ) );
			meta.setCreationDate();
			meta.setModificationDate();
			meta.setTemplate( "texas-state-standard" );
			newPage.setNodeData("title", valueFactory.createValue( "Page Not Found" ) );
			newPage.setNodeData("hideInNav", valueFactory.createValue( "true" ) ); 
			c.save();
			
			createParagraph( newPage, request, response );
		} else {
			response.getWriter().println( c.getHandle() + "/404 already exists<br/>" );
		}			
}

Finally, with the 404 page created, we create the contentParagraph node, create a paragraph node within it, set the template for the paragraph, and pop some default text into it.

public void createParagraph( Content page, HttpServletRequest request, HttpServletResponse response ) throws Exception {
	HierarchyManager hierarchyManager=MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
	ValueFactory valueFactory = hierarchyManager.getWorkspace().getSession().getValueFactory();
	
	String text = "Unfortunately, the page you're looking for doesn't seem to exist. If you've followed a " +
		"link to get here, the owner of the page with that link probably needs to update the it " + 
		"to point to the correct place on our site.<br /> <br /> To get where you're " +
		"going, try using the site navigation or the search engine above.";

	Content collection = page.createContent( "contentParagraph", ItemType.CONTENTNODE.getSystemName() );
	page.save();

	Content paragraph = collection.createContent( "0", ItemType.CONTENTNODE.getSystemName() );
	paragraph.setNodeData("content", valueFactory.createValue( text ));
	MetaData meta = paragraph.getMetaData();
	meta.setTemplate( "texasEditor" );
	collection.save();
	
	response.getWriter().println( "Paragraph Created At: " + paragraph.getHandle() + "<br/>" );
}

Once all of this is done, we have our default 404 page set for any site that didn't already have one. To execute this code, we just stick the JSP in the docroot directory and call the appropriate URL. The complete JSP is included for your reading pleasure.

In its previous incarnation on JspWiki, this page was last edited on Mar 6, 2007 3:23:12 PM by SeanMcMains

Powered by a free Atlassian Confluence Open Source Project License granted to Magnolia International. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators