com.liferay.wiki.editor.configuration.internal.BaseWikiAttachmentImageEditorConfigContributor.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.wiki.editor.configuration.internal.BaseWikiAttachmentImageEditorConfigContributor.java

Source

/**
 * Copyright (c) 2000-present 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 com.liferay.wiki.editor.configuration.internal;

import com.liferay.item.selector.ItemSelectorCriterion;
import com.liferay.item.selector.ItemSelectorReturnType;
import com.liferay.item.selector.criteria.FileEntryItemSelectorReturnType;
import com.liferay.item.selector.criteria.URLItemSelectorReturnType;
import com.liferay.item.selector.criteria.image.criterion.ImageItemSelectorCriterion;
import com.liferay.item.selector.criteria.upload.criterion.UploadItemSelectorCriterion;
import com.liferay.item.selector.criteria.url.criterion.URLItemSelectorCriterion;
import com.liferay.portal.kernel.editor.configuration.BaseEditorConfigContributor;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.portlet.RequestBackedPortletURLFactory;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PropsValues;
import com.liferay.wiki.constants.WikiPortletKeys;
import com.liferay.wiki.item.selector.criterion.WikiAttachmentItemSelectorCriterion;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.portlet.ActionRequest;
import javax.portlet.PortletURL;

/**
 * @author Sergio Gonzlez
 * @author Roberto Daz
 */
public abstract class BaseWikiAttachmentImageEditorConfigContributor extends BaseEditorConfigContributor {

    @Override
    public void populateConfigJSONObject(JSONObject jsonObject, Map<String, Object> inputEditorTaglibAttributes,
            ThemeDisplay themeDisplay, RequestBackedPortletURLFactory requestBackedPortletURLFactory) {

        boolean allowBrowseDocuments = GetterUtil
                .getBoolean(inputEditorTaglibAttributes.get("liferay-ui:input-editor:allowBrowseDocuments"));

        if (!allowBrowseDocuments) {
            return;
        }

        Map<String, String> fileBrowserParamsMap = (Map<String, String>) inputEditorTaglibAttributes
                .get("liferay-ui:input-editor:fileBrowserParams");

        long wikiPageResourcePrimKey = 0;

        if (fileBrowserParamsMap != null) {
            wikiPageResourcePrimKey = GetterUtil.getLong(fileBrowserParamsMap.get("wikiPageResourcePrimKey"));
        }

        if (wikiPageResourcePrimKey == 0) {
            String removePlugins = jsonObject.getString("removePlugins");

            if (Validator.isNotNull(removePlugins)) {
                removePlugins += ",ae_addimages";
            } else {
                removePlugins = "ae_addimages";
            }

            jsonObject.put("removePlugins", removePlugins);
        }

        String name = GetterUtil.getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:name"));

        boolean inlineEdit = GetterUtil
                .getBoolean(inputEditorTaglibAttributes.get("liferay-ui:input-editor:inlineEdit"));

        if (!inlineEdit) {
            String namespace = GetterUtil
                    .getString(inputEditorTaglibAttributes.get("liferay-ui:input-editor:namespace"));

            name = namespace + name;
        }

        String itemSelectorURL = getItemSelectorURL(requestBackedPortletURLFactory, name + "selectItem",
                wikiPageResourcePrimKey, themeDisplay);

        jsonObject.put("filebrowserImageBrowseLinkUrl", itemSelectorURL);
        jsonObject.put("filebrowserImageBrowseUrl", itemSelectorURL);
    }

    protected ItemSelectorCriterion getImageItemSelectorCriterion(
            List<ItemSelectorReturnType> desiredItemSelectorReturnTypes) {

        ItemSelectorCriterion imageItemSelectorCriterion = new ImageItemSelectorCriterion();

        imageItemSelectorCriterion.setDesiredItemSelectorReturnTypes(desiredItemSelectorReturnTypes);

        return imageItemSelectorCriterion;
    }

    protected abstract String getItemSelectorURL(RequestBackedPortletURLFactory requestBackedPortletURLFactory,
            String itemSelectedEventName, long wikiPageResourcePrimKey, ThemeDisplay themeDisplay);

    protected ItemSelectorCriterion getUploadItemSelectorCriterion(long wikiPageResourcePrimKey,
            ThemeDisplay themeDisplay, RequestBackedPortletURLFactory requestBackedPortletURLFactory) {

        PortletURL uploadURL = requestBackedPortletURLFactory.createActionURL(WikiPortletKeys.WIKI);

        uploadURL.setParameter(ActionRequest.ACTION_NAME, "/wiki/upload_page_attachment");
        uploadURL.setParameter("mimeTypes", PropsValues.DL_FILE_ENTRY_PREVIEW_IMAGE_MIME_TYPES);
        uploadURL.setParameter("resourcePrimKey", String.valueOf(wikiPageResourcePrimKey));

        ItemSelectorCriterion uploadItemSelectorCriterion = new UploadItemSelectorCriterion(WikiPortletKeys.WIKI,
                uploadURL.toString(), LanguageUtil.get(themeDisplay.getLocale(), "page-attachments"));

        List<ItemSelectorReturnType> uploadDesiredItemSelectorReturnTypes = new ArrayList<>();

        uploadDesiredItemSelectorReturnTypes.add(new FileEntryItemSelectorReturnType());

        uploadItemSelectorCriterion.setDesiredItemSelectorReturnTypes(uploadDesiredItemSelectorReturnTypes);

        return uploadItemSelectorCriterion;
    }

    protected ItemSelectorCriterion getURLItemSelectorCriterion() {
        ItemSelectorCriterion urlItemSelectorCriterion = new URLItemSelectorCriterion();

        List<ItemSelectorReturnType> urlDesiredItemSelectorReturnTypes = new ArrayList<>();

        urlDesiredItemSelectorReturnTypes.add(new URLItemSelectorReturnType());

        urlItemSelectorCriterion.setDesiredItemSelectorReturnTypes(urlDesiredItemSelectorReturnTypes);

        return urlItemSelectorCriterion;
    }

    protected ItemSelectorCriterion getWikiAttachmentItemSelectorCriterion(long wikiPageResourcePrimKey,
            List<ItemSelectorReturnType> desiredItemSelectorReturnTypes) {

        ItemSelectorCriterion attachmentItemSelectorCriterion = new WikiAttachmentItemSelectorCriterion(
                wikiPageResourcePrimKey, PropsValues.DL_FILE_ENTRY_PREVIEW_IMAGE_MIME_TYPES);

        attachmentItemSelectorCriterion.setDesiredItemSelectorReturnTypes(desiredItemSelectorReturnTypes);

        return attachmentItemSelectorCriterion;
    }

}