throws WebloggerException {
log.debug("Importing theme ["+theme.getName()+"] to weblog ["+website.getName()+"]");
try {
UserManager userMgr = roller.getUserManager();
Set importedActionTemplates = new HashSet();
ThemeTemplate themeTemplate = null;
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)) {
template = userMgr.getPageByAction(website, themeTemplate.getAction());
if(template != null) {
importedActionTemplates.add(themeTemplate.getAction());
}
// otherwise, lookup by name
} else {
template = userMgr.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
userMgr.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 = userMgr.getPageByAction(website, action);
if(toDelete != null) {
log.debug("Removing stale action template "+toDelete.getId());
userMgr.removePage(toDelete);
}
}
}
// always update this weblog's theme and customStylesheet, then save
website.setEditorTheme(WeblogTheme.CUSTOM);
if(theme.getStylesheet() != null) {
website.setCustomStylesheetPath(theme.getStylesheet().getLink());
}
userMgr.saveWebsite(website);
// now lets import all the theme resources
FileManager fileMgr = roller.getFileManager();