Search in sources :

Example 1 with NoSuchPageException

use of com.liferay.portlet.wiki.NoSuchPageException in project liferay-ide by liferay.

the class MediaWikiImporter method importPage.

protected void importPage(long userId, String author, WikiNode node, String title, String content, String summary, Map<String, String> usersMap, boolean strictImportMode) throws PortalException {
    try {
        long authorUserId = getUserId(userId, node, author, usersMap);
        String parentTitle = readParentTitle(content);
        String redirectTitle = readRedirectTitle(content);
        ServiceContext serviceContext = new ServiceContext();
        serviceContext.setAddGroupPermissions(true);
        serviceContext.setAddGuestPermissions(true);
        serviceContext.setAssetTagNames(readAssetTagNames(userId, node, content));
        if (Validator.isNull(redirectTitle)) {
            _translator.setStrictImportMode(strictImportMode);
            content = _translator.translate(content);
        } else {
            content = StringPool.DOUBLE_OPEN_BRACKET + redirectTitle + StringPool.DOUBLE_CLOSE_BRACKET;
        }
        WikiPage page = null;
        try {
            page = WikiPageLocalServiceUtil.getPage(node.getNodeId(), title);
        } catch (NoSuchPageException nspe) {
            page = WikiPageLocalServiceUtil.addPage(authorUserId, node.getNodeId(), title, WikiPageConstants.NEW, null, true, serviceContext);
        }
        WikiPageLocalServiceUtil.updatePage(authorUserId, node.getNodeId(), title, page.getVersion(), content, summary, true, "creole", parentTitle, redirectTitle, serviceContext);
    } catch (Exception e) {
        throw new PortalException("Error importing page " + title, e);
    }
}
Also used : NoSuchPageException(com.liferay.portlet.wiki.NoSuchPageException) ServiceContext(com.liferay.portal.service.ServiceContext) WikiPage(com.liferay.portlet.wiki.model.WikiPage) PortalException(com.liferay.portal.kernel.exception.PortalException) NoSuchPageException(com.liferay.portlet.wiki.NoSuchPageException) ImportFilesException(com.liferay.portlet.wiki.ImportFilesException) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) DocumentException(com.liferay.portal.kernel.xml.DocumentException) IOException(java.io.IOException) NoSuchTagException(com.liferay.portlet.asset.NoSuchTagException)

Example 2 with NoSuchPageException

use of com.liferay.portlet.wiki.NoSuchPageException in project liferay-ide by liferay.

the class MediaWikiImporter method processImages.

protected void processImages(long userId, WikiNode node, InputStream imagesInputStream) throws Exception {
    if (imagesInputStream == null) {
        return;
    }
    ProgressTracker progressTracker = ProgressTrackerThreadLocal.getProgressTracker();
    int count = 0;
    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(imagesInputStream);
    List<String> entries = zipReader.getEntries();
    int total = entries.size();
    if (total > 0) {
        try {
            WikiPageLocalServiceUtil.getPage(node.getNodeId(), SHARED_IMAGES_TITLE);
        } catch (NoSuchPageException nspe) {
            ServiceContext serviceContext = new ServiceContext();
            serviceContext.setAddGroupPermissions(true);
            serviceContext.setAddGuestPermissions(true);
            WikiPageLocalServiceUtil.addPage(userId, node.getNodeId(), SHARED_IMAGES_TITLE, SHARED_IMAGES_CONTENT, null, true, serviceContext);
        }
    }
    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = new ArrayList<ObjectValuePair<String, InputStream>>();
    try {
        int percentage = 50;
        for (int i = 0; i < entries.size(); i++) {
            String entry = entries.get(i);
            String key = entry;
            InputStream inputStream = zipReader.getEntryAsInputStream(entry);
            String[] paths = StringUtil.split(key, CharPool.SLASH);
            if (!isValidImage(paths, inputStream)) {
                if (_log.isInfoEnabled()) {
                    _log.info("Ignoring " + key);
                }
                continue;
            }
            String fileName = StringUtil.toLowerCase(paths[paths.length - 1]);
            ObjectValuePair<String, InputStream> inputStreamOVP = new ObjectValuePair<String, InputStream>(fileName, inputStream);
            inputStreamOVPs.add(inputStreamOVP);
            count++;
            if ((i % 5) == 0) {
                WikiPageLocalServiceUtil.addPageAttachments(userId, node.getNodeId(), SHARED_IMAGES_TITLE, inputStreamOVPs);
                inputStreamOVPs.clear();
                percentage = Math.min(50 + (i * 50) / total, 99);
                progressTracker.setPercent(percentage);
            }
        }
        if (!inputStreamOVPs.isEmpty()) {
            WikiPageLocalServiceUtil.addPageAttachments(userId, node.getNodeId(), SHARED_IMAGES_TITLE, inputStreamOVPs);
        }
    } finally {
        for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {
            InputStream inputStream = inputStreamOVP.getValue();
            StreamUtil.cleanUp(inputStream);
        }
    }
    zipReader.close();
    if (_log.isInfoEnabled()) {
        _log.info("Imported " + count + " images into " + node.getName());
    }
}
Also used : ProgressTracker(com.liferay.portal.kernel.util.ProgressTracker) NoSuchPageException(com.liferay.portlet.wiki.NoSuchPageException) ServiceContext(com.liferay.portal.service.ServiceContext) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ZipReader(com.liferay.portal.kernel.zip.ZipReader) ObjectValuePair(com.liferay.portal.kernel.util.ObjectValuePair)

Aggregations

ServiceContext (com.liferay.portal.service.ServiceContext)2 NoSuchPageException (com.liferay.portlet.wiki.NoSuchPageException)2 PortalException (com.liferay.portal.kernel.exception.PortalException)1 SystemException (com.liferay.portal.kernel.exception.SystemException)1 ObjectValuePair (com.liferay.portal.kernel.util.ObjectValuePair)1 ProgressTracker (com.liferay.portal.kernel.util.ProgressTracker)1 DocumentException (com.liferay.portal.kernel.xml.DocumentException)1 ZipReader (com.liferay.portal.kernel.zip.ZipReader)1 NoSuchTagException (com.liferay.portlet.asset.NoSuchTagException)1 ImportFilesException (com.liferay.portlet.wiki.ImportFilesException)1 WikiPage (com.liferay.portlet.wiki.model.WikiPage)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1