Last active
December 11, 2015 17:38
-
-
Save nshaw/4635866 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. | |
| * | |
| * This library is free software; you can redistribute it and/or modify it under | |
| * the terms of the GNU Lesser General Public License as published by the Free | |
| * Software Foundation; either version 2.1 of the License, or (at your option) | |
| * any later version. | |
| * | |
| * This library is distributed in the hope that it will be useful, but WITHOUT | |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | |
| * details. | |
| */ | |
| package org.sesameworkshop.portlet.learning.portlet; | |
| import com.liferay.portal.kernel.portlet.PortletResponseUtil; | |
| import com.liferay.portal.kernel.util.MimeTypesUtil; | |
| import com.liferay.portal.util.PortalUtil; | |
| import com.liferay.util.bridges.mvc.MVCPortlet; | |
| import org.sesameworkshop.portlet.learning.service.CurriculumExporterLocalServiceUtil; | |
| import javax.portlet.PortletException; | |
| import javax.portlet.ResourceRequest; | |
| import javax.portlet.ResourceResponse; | |
| import java.io.File; | |
| import java.io.FileInputStream; | |
| import java.io.IOException; | |
| public class AdminPortlet extends MVCPortlet { | |
| public void serveResource(ResourceRequest resourceRequest, | |
| ResourceResponse resourceResponse) | |
| throws IOException, PortletException { | |
| super.serveResource(resourceRequest, resourceResponse); | |
| try { | |
| String resourceID = resourceRequest.getResourceID(); | |
| if (resourceID.equals("download")) { | |
| serveDownload(resourceRequest, resourceResponse); | |
| } | |
| } | |
| catch (IOException ioe) { | |
| throw ioe; | |
| } | |
| catch (PortletException pe) { | |
| throw pe; | |
| } | |
| catch (Exception e) { | |
| throw new PortletException(e); | |
| } | |
| } | |
| protected void serveDownload( | |
| ResourceRequest request, ResourceResponse response) | |
| throws Exception { | |
| long groupId = PortalUtil.getScopeGroupId(request); | |
| File file = CurriculumExporterLocalServiceUtil.exportCurriculum(groupId); | |
| String fileName = file.getName(); | |
| String contentType = MimeTypesUtil.getContentType(fileName); | |
| FileInputStream fis = new FileInputStream(file); | |
| PortletResponseUtil.sendFile(request, response, fileName, fis, contentType); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved. | |
| * | |
| * This library is free software; you can redistribute it and/or modify it under | |
| * the terms of the GNU Lesser General Public License as published by the Free | |
| * Software Foundation; either version 2.1 of the License, or (at your option) | |
| * any later version. | |
| * | |
| * This library is distributed in the hope that it will be useful, but WITHOUT | |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
| * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | |
| * details. | |
| */ | |
| package org.sesameworkshop.portlet.learning.service.impl; | |
| import com.liferay.portal.kernel.dao.orm.QueryUtil; | |
| import com.liferay.portal.kernel.exception.PortalException; | |
| import com.liferay.portal.kernel.exception.SystemException; | |
| import com.liferay.portal.kernel.log.Log; | |
| import com.liferay.portal.kernel.log.LogFactoryUtil; | |
| import com.liferay.portal.kernel.util.FileUtil; | |
| import com.liferay.portal.kernel.util.HttpUtil; | |
| import com.liferay.portal.kernel.zip.ZipWriter; | |
| import com.liferay.portal.kernel.zip.ZipWriterFactoryUtil; | |
| import com.liferay.portal.model.Company; | |
| import com.liferay.portal.model.Layout; | |
| import com.liferay.portal.service.CompanyLocalServiceUtil; | |
| import com.liferay.portal.service.LayoutLocalServiceUtil; | |
| import com.liferay.portal.theme.ThemeDisplay; | |
| import com.liferay.portlet.documentlibrary.NoSuchFolderException; | |
| import com.liferay.portlet.documentlibrary.model.DLFileEntry; | |
| import com.liferay.portlet.documentlibrary.model.DLFolder; | |
| import com.liferay.portlet.documentlibrary.model.DLFolderConstants; | |
| import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil; | |
| import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil; | |
| import com.liferay.portlet.journal.model.JournalArticle; | |
| import com.liferay.portlet.journal.model.JournalArticleDisplay; | |
| import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil; | |
| import org.sesameworkshop.portlet.learning.service.base.CurriculumExporterLocalServiceBaseImpl; | |
| import org.sesameworkshop.portlet.learning.service.util.CurriculumExporterThreadLocal; | |
| import org.sesameworkshop.portlet.learning.util.LearningPortletPropsValues; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Locale; | |
| /** | |
| * The implementation of the curriculum exporter local service. | |
| * | |
| * <p> | |
| * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link org.sesameworkshop.portlet.learning.service.CurriculumExporterLocalService} interface. | |
| * | |
| * <p> | |
| * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM. | |
| * </p> | |
| * | |
| * @author Nathan Shaw | |
| * @see org.sesameworkshop.portlet.learning.service.base.CurriculumExporterLocalServiceBaseImpl | |
| * @see org.sesameworkshop.portlet.learning.service.CurriculumExporterLocalServiceUtil | |
| */ | |
| public class CurriculumExporterLocalServiceImpl | |
| extends CurriculumExporterLocalServiceBaseImpl { | |
| /* | |
| * NOTE FOR DEVELOPERS: | |
| * | |
| * Never reference this interface directly. Always use {@link org.sesameworkshop.portlet.learning.service.CurriculumExporterLocalServiceUtil} to access the curriculum exporter local service. | |
| */ | |
| /** | |
| * Export the complete curriculum for this group | |
| * @return a zip file containing the curriculum content and assets | |
| * @throws PortalException | |
| * @throws SystemException | |
| */ | |
| public File exportCurriculum(long groupId) | |
| throws PortalException, SystemException { | |
| File file = FileUtil.createTempFile("zip"); | |
| ZipWriter writer = ZipWriterFactoryUtil.getZipWriter(file); | |
| try { | |
| CurriculumExporterThreadLocal.setExportProcess(true); | |
| addArticles(writer, groupId); | |
| addAssets(writer, groupId); | |
| } | |
| finally { | |
| CurriculumExporterThreadLocal.setExportProcess(false); | |
| } | |
| file = writer.getFile(); | |
| return file; | |
| } | |
| /** | |
| * Return the name of the configured base assets folder, by default "assets" | |
| * @return | |
| */ | |
| public String getAssetsBaseFolderName() { | |
| return LearningPortletPropsValues.EXPORT_ASSET_BASE_FOLDER; | |
| } | |
| /** | |
| * Indicate whether the export process is currently running so | |
| * templates can choose how to transform content accordingly | |
| * @return | |
| * @throws PortalException | |
| * @throws SystemException | |
| */ | |
| public boolean isExportProcess() | |
| throws PortalException, SystemException { | |
| return CurriculumExporterThreadLocal.isExportProcess(); | |
| } | |
| protected void addArticles(ZipWriter writer, long groupId) | |
| throws PortalException, SystemException { | |
| List<JournalArticle> list = | |
| journalArticleLocalService.getArticles(groupId); | |
| list = filterArticles(list); | |
| long plid = LayoutLocalServiceUtil.getDefaultPlid(groupId, false); | |
| Layout layout = LayoutLocalServiceUtil.getLayout(plid); | |
| Company company = CompanyLocalServiceUtil.getCompany( | |
| layout.getCompanyId()); | |
| ThemeDisplay themeDisplay = new ThemeDisplay(); | |
| themeDisplay.setCompany(company); | |
| themeDisplay.setLayout(layout); | |
| for (JournalArticle article : list) { | |
| // May have to deal with localization at some point but for now | |
| // assume we can use the US locale | |
| String languageId = Locale.US.toString(); | |
| String articleId = article.getArticleId(); | |
| if (_log.isDebugEnabled()) { | |
| _log.debug("Adding article to zip: " + articleId + " -" + | |
| article.getTitle()); | |
| } | |
| JournalArticleDisplay display = | |
| JournalArticleLocalServiceUtil.getArticleDisplay( | |
| groupId, articleId, | |
| article.getTemplateId(), null, languageId, 0, | |
| null, themeDisplay); | |
| String content = display.getContent(); | |
| String path = display.getTitle(); | |
| // Make sure the title is converted to a path-safe version, | |
| // replace spaces, etc. | |
| path = HttpUtil.encodePath(path); | |
| try { | |
| writer.addEntry(path, content); | |
| } | |
| catch (IOException e) { | |
| String msg = "Unable to add article: " + articleId; | |
| if (_log.isDebugEnabled()) { | |
| _log.warn(msg, e); | |
| } | |
| else { | |
| _log.warn(msg + " - " + e.getMessage()); | |
| } | |
| throw new PortalException(msg, e); | |
| } | |
| } | |
| } | |
| protected void addAssets(ZipWriter writer, long groupId) | |
| throws PortalException, SystemException { | |
| DLFolder dlFolder; | |
| try { | |
| String baseFolderName = | |
| LearningPortletPropsValues.EXPORT_ASSET_BASE_FOLDER; | |
| dlFolder = DLFolderLocalServiceUtil.getFolder(groupId, | |
| DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, baseFolderName); | |
| } | |
| catch (NoSuchFolderException e) { | |
| _log.info("No curriculum assets found."); | |
| return; | |
| } | |
| addAssets(writer, dlFolder); | |
| } | |
| protected void addAssets(ZipWriter writer, DLFolder parentFolder) | |
| throws PortalException, SystemException { | |
| if (_log.isDebugEnabled()) { | |
| _log.debug("Adding folder to zip " + parentFolder); | |
| } | |
| long groupId = parentFolder.getGroupId(); | |
| long folderId = parentFolder.getFolderId(); | |
| String path = parentFolder.getPath(); | |
| // Add any assets in this folder to the zip | |
| List<DLFileEntry> assets = | |
| DLFileEntryLocalServiceUtil.getFileEntries(groupId, folderId, | |
| QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); | |
| for (DLFileEntry asset : assets) { | |
| String name = asset.getTitle(); | |
| InputStream is = asset.getContentStream(); | |
| String fullName = path + "/" + name; | |
| try { | |
| if (_log.isDebugEnabled()) { | |
| _log.debug("Adding entry to zip: " + fullName); | |
| } | |
| writer.addEntry(fullName, is); | |
| } | |
| catch (IOException e) { | |
| //Note - should we just skip assets that can't be | |
| // exported? Write an audit message? | |
| _log.warn("Unable to add asset to zip: " + name, e); | |
| } | |
| finally { | |
| try { | |
| is.close(); | |
| } | |
| catch (IOException e) { | |
| //no-op | |
| } | |
| } | |
| } | |
| // Find any child folders and also add those | |
| List<DLFolder> folders = | |
| DLFolderLocalServiceUtil.getFolders(groupId, folderId); | |
| for (DLFolder folder : folders) { | |
| addAssets(writer, folder); | |
| } | |
| } | |
| protected List<JournalArticle> filterArticles(List<JournalArticle> list) | |
| throws PortalException,SystemException { | |
| List<JournalArticle> filtered = new ArrayList<JournalArticle>(); | |
| for (JournalArticle article : list) { | |
| String type = article.getType(); | |
| if(LearningPortletPropsValues.EXPORT_ARTICLE_TYPE.equalsIgnoreCase(type)) { | |
| filtered.add(article); | |
| } | |
| } | |
| return filtered; | |
| } | |
| private static Log _log = | |
| LogFactoryUtil.getLog(CurriculumExporterLocalServiceImpl.class); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment