ThemeTemplate stylesheetTemplate = theme.getStylesheet();
Iterator iter = theme.getTemplates().iterator();
while (iter.hasNext()) {
themeTemplate = (ThemeTemplate) iter.next();
WeblogTemplate template = null;
// if template is an action, lookup by action
if (themeTemplate.getAction() != null
&& !themeTemplate.getAction().equals(WeblogTemplate.ACTION_CUSTOM)) {
importedActionTemplates.add(themeTemplate.getAction());
template = wmgr.getPageByAction(website, themeTemplate.getAction());
// otherwise, lookup by name
} else {
template = wmgr.getPageByName(website, themeTemplate.getName());
}
// Weblog does not have this template, so create it.
boolean newTmpl = false;
if (template == null) {
template = new WeblogTemplate();
template.setWebsite(website);
newTmpl = true;
}
// TODO: fix conflict situation
// it's possible that someone has defined a theme template which
// matches 2 existing templates, 1 by action, the other by name
// update template attributes
// NOTE: we don't want to copy the template data for an existing stylesheet
if (newTmpl || !themeTemplate.equals(stylesheetTemplate)) {
template.setAction(themeTemplate.getAction());
template.setName(themeTemplate.getName());
template.setDescription(themeTemplate.getDescription());
template.setLink(themeTemplate.getLink());
template.setContents(themeTemplate.getContents());
template.setHidden(themeTemplate.isHidden());
template.setNavbar(themeTemplate.isNavbar());
template.setTemplateLanguage(themeTemplate.getTemplateLanguage());
// NOTE: decorators are deprecated starting in 4.0
template.setDecoratorName(null);
template.setLastModified(new Date());
// save it
wmgr.savePage(template);
}
}
// now, see if the weblog has left over action templates that
// need to be deleted because they aren't in their new theme
for (int i = 0; i < WeblogTemplate.ACTIONS.length; i++) {
String action = WeblogTemplate.ACTIONS[i];
// if we didn't import this action then see if it should be deleted
if (!importedActionTemplates.contains(action)) {
WeblogTemplate toDelete = wmgr.getPageByAction(website, action);
if (toDelete != null) {
log.debug("Removing stale action template " + toDelete.getId());
wmgr.removePage(toDelete);
}
}
}