public void importTheme()

in app/src/main/java/org/apache/roller/weblogger/business/themes/ThemeManagerImpl.java [195:368]


	public void importTheme(Weblog weblog, SharedTheme theme, boolean skipStylesheet)
			throws WebloggerException {

		log.debug("Importing theme [" + theme.getName() + "] to weblog ["
				+ weblog.getName() + "]");

		WeblogManager wmgr = roller.getWeblogManager();
		MediaFileManager fileMgr = roller.getMediaFileManager();

		MediaFileDirectory root = fileMgr.getDefaultMediaFileDirectory(weblog);
        if (root == null) {
            log.warn("Weblog " + weblog.getHandle() + " does not have a root MediaFile directory");
        }

		Set<ComponentType> importedActionTemplates = EnumSet.noneOf(ComponentType.class);
		ThemeTemplate stylesheetTemplate = theme.getStylesheet();
		for (ThemeTemplate themeTemplate : theme.getTemplates()) {
			WeblogTemplate template;

			// if template is an action, lookup by action
			if (themeTemplate.getAction() != null
					&& !themeTemplate.getAction().equals(ComponentType.CUSTOM)) {
				importedActionTemplates.add(themeTemplate.getAction());
				template = wmgr.getTemplateByAction(weblog,
                        themeTemplate.getAction());

				// otherwise, lookup by name
			} else {
				template = wmgr.getTemplateByName(weblog, themeTemplate.getName());
			}

			// Weblog does not have this template, so create it.
			if (template == null) {
				template = new WeblogTemplate();
				template.setWeblog(weblog);
			}

			// update template attributes except leave existing custom stylesheets as-is
			if (!themeTemplate.equals(stylesheetTemplate) || !skipStylesheet) {
				template.setAction(themeTemplate.getAction());
				template.setName(themeTemplate.getName());
				template.setDescription(themeTemplate.getDescription());
				template.setLink(themeTemplate.getLink());
				template.setHidden(themeTemplate.isHidden());
				template.setNavbar(themeTemplate.isNavbar());
                template.setOutputContentType(themeTemplate.getOutputContentType());
				template.setLastModified(new Date());

				// save it
				wmgr.saveTemplate(template);

                // create weblog template code objects and save them
                for (RenditionType type : RenditionType.values()) {

                    // See if we already have some code for this template already (eg previous theme)
                    CustomTemplateRendition weblogTemplateCode = template.getTemplateRendition(type);

                    // Get the template for the new theme
                    TemplateRendition templateCode = themeTemplate.getTemplateRendition(type);
                    if (templateCode != null) {

                        // Check for existing template
                        if (weblogTemplateCode == null) {
                            // Does not exist so create a new one
                            weblogTemplateCode = new CustomTemplateRendition(template, type);
                        }
                        weblogTemplateCode.setType(type);
                        weblogTemplateCode.setTemplate(templateCode.getTemplate());
                        weblogTemplateCode.setTemplateLanguage(templateCode
                                .getTemplateLanguage());
                        WebloggerFactory.getWeblogger().getWeblogManager()
                                .saveTemplateRendition(weblogTemplateCode);
                    }

                }
            }
		}

		// now, see if the weblog has left over non-custom action templates that
		// need to be deleted because they aren't in their new theme
        for (ComponentType action : ComponentType.values()) {
            if (action == ComponentType.CUSTOM) {
                continue;
            }
			// if we didn't import this action then see if it should be deleted
			if (!importedActionTemplates.contains(action)) {
				WeblogTemplate toDelete = wmgr.getTemplateByAction(weblog, action);
				if (toDelete != null) {
					log.debug("Removing stale action template " + toDelete.getId());
					wmgr.removeTemplate(toDelete);
				}
			}
		}

		// set weblog's theme to custom, then save
		weblog.setEditorTheme(WeblogTheme.CUSTOM);
		wmgr.saveWeblog(weblog);

		// now lets import all the theme resources
        for (ThemeResource resource : theme.getResources()) {

			log.debug("Importing resource " + resource.getPath());

			if (resource.isDirectory()) {
				MediaFileDirectory mdir = fileMgr.getMediaFileDirectoryByName(
						weblog, resource.getPath());
				if (mdir == null) {
					log.debug("    Creating directory: " + resource.getPath());
					fileMgr.createMediaFileDirectory(weblog, resource.getPath());
					roller.flush();
				} else {
					log.debug("    No action: directory already exists");
				}

			} else {
				String resourcePath = resource.getPath();

				MediaFileDirectory mdir;
				String justName;
				String justPath;

				if (resourcePath.indexOf('/') == -1) {
					mdir = fileMgr.getDefaultMediaFileDirectory(weblog);
					justPath = "";
					justName = resourcePath;

				} else {
					justPath = resourcePath.substring(0, resourcePath.lastIndexOf('/'));
					if (!justPath.startsWith("/")) {
                        justPath = "/" + justPath;
                    }
					justName = resourcePath.substring(resourcePath.lastIndexOf('/') + 1);
					mdir = fileMgr.getMediaFileDirectoryByName(weblog, justPath);
					if (mdir == null) {
						log.debug("    Creating directory: " + justPath);
						mdir = fileMgr.createMediaFileDirectory(weblog, justPath);
						roller.flush();
					}
				}

				MediaFile oldmf = fileMgr.getMediaFileByOriginalPath(weblog,
						justPath + "/" + justName);
				if (oldmf != null) {
					fileMgr.removeMediaFile(weblog, oldmf);
				}

				// save file without file-type, quota checks, etc.
				InputStream is = resource.getInputStream();
				MediaFile mf = new MediaFile();
				mf.setDirectory(mdir);
				mf.setWeblog(weblog);
				mf.setName(justName);
				mf.setOriginalPath(justPath + "/" + justName);
				mf.setContentType(map.getContentType(justName));
				mf.setInputStream(is);
				mf.setLength(resource.getLength());

				log.debug("    Saving file: " + justName);
				log.debug("    Saving in directory = " + mf.getDirectory());
				RollerMessages errors = new RollerMessages();
				fileMgr.createThemeMediaFile(weblog, mf, errors);
				try {
					is.close();
				} catch (IOException ex) {
					errors.addError("error.closingStream");
					log.debug("ERROR closing inputstream");
				}
				if (errors.getErrorCount() > 0) {
					throw new WebloggerException(errors.toString());
				}
				roller.flush();
			}
		}
	}