Dashboard > Community Wiki > ... > Developing with Magnolia > Creating pages with code
Creating pages with code Log In View a printable version of the current page.

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

Here's a bit of sample code we used to create custom 404 pages programmatically for sites in our system:

First, we iterate through each of the sites in our system, calling our createPage routine (defined below) for each site and, since we're running this as a JSP, flushing the buffer periodically so we can see what progress we're making:

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();
	}
}

For each site, we call our createPage method, which creates the new page, assigns it a page template and a title, and then saves the page. Once the page is created, we call the method (defined below) to create a new content paragraph within the newly saved page:

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/>" );
		}			
}

Lastly, the createParagraph methods creates the paragraph with the default text in it. It first creates the contentParagraph node, and then paragraph "0" within it. The text and the paragraph template are set, and the contentParagraph node is then saved to persist these changes. (Note that in a lot of cases, you have to save a parent item to persist a new sub-item.)

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/>" );
}

Just like for Counting pages, this duplicates some of what's achievable with ContentUtil.visit() - essentially, the processSites method can be replaced by a call to ContentUtil.visit(), passing it a ContentUtil.Visitor() whose visit() method will do what createPage() does.
Javadoc:

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