log.debug("Importing theme [" + theme.getName() + "] to weblog [" + website.getName() + "]");
WeblogManager wmgr = roller.getWeblogManager();
MediaFileManager fileMgr = roller.getMediaFileManager();
MediaFileDirectory root = fileMgr.getMediaFileRootDirectory(website);
log.warn("Weblog " + website.getHandle() + " does not have a root MediaFile directory");
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)) {
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);
}
}
}
// always update this weblog's theme and customStylesheet, then save
website.setEditorTheme(WeblogTheme.CUSTOM);
if (theme.getStylesheet() != null) {
website.setCustomStylesheetPath(theme.getStylesheet().getLink());
}
wmgr.saveWeblog(website);
// now lets import all the theme resources
List resources = theme.getResources();
Iterator iterat = resources.iterator();
ThemeResource resource = null;
while (iterat.hasNext()) {
resource = (ThemeResource) iterat.next();
log.debug("Importing resource " + resource.getPath());
if (resource.isDirectory()) {
MediaFileDirectory mdir =
fileMgr.getMediaFileDirectoryByPath(website, resource.getPath());
if (mdir == null) {
log.debug(" Creating directory: " + resource.getPath());
mdir = fileMgr.createMediaFileDirectory(
fileMgr.getMediaFileRootDirectory(website), resource.getPath());
roller.flush();
} else {
log.debug(" No action: directory already exists");
}
} else {
String resourcePath = resource.getPath();
MediaFileDirectory mdir = null;
String justName = null;
String justPath = null;
if (resourcePath.indexOf("/") == -1) {
mdir = fileMgr.getMediaFileRootDirectory(website);