String newPageTemplatePath = folder.getPath()+(folder.getPath().endsWith(Folder.PATH_SEPARATOR) ? "" : Folder.PATH_SEPARATOR)+templateName+PageTemplate.DOCUMENT_TYPE;
if (pageManager.pageTemplateExists(newPageTemplatePath))
{
throw new IllegalArgumentException("Page template "+newPageTemplatePath+" exists");
}
PageTemplate newPageTemplate = pageManager.newPageTemplate(newPageTemplatePath);
if (newPageTemplate.getRootFragment() instanceof Fragment)
{
Fragment rootFragment = (Fragment)newPageTemplate.getRootFragment();
if (!Utils.isNull(layoutName))
{
rootFragment.setName(layoutName);
}
rootFragment.getFragments().add(pageManager.newPageFragment());
}
if (!Utils.isNull(templateTitle))
{
newPageTemplate.setTitle(templateTitle);
}
if (!Utils.isNull(templateShortTitle))
{
newPageTemplate.setShortTitle(templateShortTitle);
}
String defaultLayoutDecorator = null;
String defaultPortletDecorator = null;
if (page != null)
{
defaultLayoutDecorator = page.getDefaultDecorator(Fragment.LAYOUT);
if (defaultLayoutDecorator != null)
{
newPageTemplate.setDefaultDecorator(defaultLayoutDecorator, Fragment.LAYOUT);
}
defaultPortletDecorator = page.getDefaultDecorator(Fragment.PORTLET);
if (defaultPortletDecorator != null)
{
newPageTemplate.setDefaultDecorator(defaultPortletDecorator, Fragment.PORTLET);
}
}
// update new page template in page manager
pageManager.updatePageTemplate(newPageTemplate);