Dashboard > Community Wiki > ... > Template development > Mass Update Templates
Mass Update Templates 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: 

We had been using a variety of the sample paragraph types that Magnolia provides in the Sample Module for a long while with about 65 sites. We eventually decided to move these to our own custom paragraph type in order to ensure that our existing sites wouldn't be affected by changes to the Samples module. In order to do so, we wrote a JSP that would iterate through the database and make the necessary changes. In order to run it, we simply put the file into docroot and called the appropriate URL.

Here are the interesting bits of the code:

The first bit kicks things off by grabbing a hierarchy manager, getting the root note of the WEBSITE repository, and passing it off to a recursive processChildren method:

<jsp:scriptlet>
	<![CDATA[
	response.getWriter().println("Getting hierarchy manager...<br/>");
	HierarchyManager hm=MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
	
	response.getWriter().println("Getting root node...<br/>");
	Content rootNode = hm.getContent("/");
	
	response.flushBuffer();
	response.getWriter().println("Processing children...<br/>");
	processChildren( rootNode, response );
	response.getWriter().println("**** DATABASE SCRUB COMPLETE **** <br/>");	
	]]>
	</jsp:scriptlet>

The processChildren method then gets the metadata for the node that it is passed, makes the change to the template if necessary, and then calls itself again with the different kinds of children of the current content node:

<jsp:declaration>
		<![^CDATA[
		public void processChildren( Content c, ServletResponse response ) {
			try {
				response.getWriter().println("Processing "" + c.getHandle() + ""<br/>" );
				MetaData meta = c.getMetaData();

				if ( meta.getTemplate().equals( "samplesTextImage" ) ) {
					meta.setTemplate( "texasTextImage" );
					response.getWriter().println( "<span style="color: red">Changing from samplesTextImage to txTextImage.</span><br/>" );
				}

				else if ( meta.getTemplate().equals( "texasTextImage" ) ) {
					response.getWriter().println( "<span style="color: green">txTextImage already set.</span><br/>" );
				}
				...
				c.save();

				response.flushBuffer();
				
				Iterator childContentNodesIterator = c.getChildren( ItemType.CONTENTNODE ).iterator();
				while ( childContentNodesIterator.hasNext() ) {
					Content childNode = (Content)childContentNodesIterator.next();
					processChildren( childNode, response );
					
				}				
				
				Iterator childNodesIterator = c.getChildren( ItemType.CONTENT ).iterator();
				while ( childNodesIterator.hasNext() ) {
					Content childNode = (Content)childNodesIterator.next();
					processChildren( childNode, response );
					
				}
				
			}
			catch ( Exception e ) {}
			
		}
		]]>
	</jsp:declaration>

This worked like a charm, and took only a minute or two to chew through the thousands of pages in our system.

The complete JSP file is attached.

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

Have a look at info.magnolia.cms.util.ContentUtil.visit(), which provides a possibly cleaner/simpler solution for the same problem.

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