All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.journal.web.internal.display.context.JournalMoveEntriesDisplayContext Maven / Gradle / Ivy

There is a newer version: 5.0.177
Show newest version
/**
 * 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.journal.web.internal.display.context;

import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.model.JournalFolder;
import com.liferay.journal.service.JournalArticleServiceUtil;
import com.liferay.journal.service.JournalFolderLocalServiceUtil;
import com.liferay.journal.service.JournalFolderServiceUtil;
import com.liferay.journal.web.internal.security.permission.resource.JournalArticlePermission;
import com.liferay.journal.web.internal.security.permission.resource.JournalFolderPermission;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.theme.PortletDisplay;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;

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

import javax.portlet.RenderResponse;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Julio Camarero
 */
public class JournalMoveEntriesDisplayContext {

	public JournalMoveEntriesDisplayContext(
			LiferayPortletRequest liferayPortletRequest,
			LiferayPortletResponse liferayPortletResponse)
		throws PortalException {

		_liferayPortletRequest = liferayPortletRequest;
		_liferayPortletResponse = liferayPortletResponse;

		_httpServletRequest = PortalUtil.getHttpServletRequest(
			_liferayPortletRequest);

		processFolders(getMoveFolders());
		processArticles(getMoveArticles());

		setViewAttributes();
	}

	public List getInvalidMoveArticles() {
		return _invalidMoveArticles;
	}

	public List getInvalidMoveFolders() {
		return _invalidMoveFolders;
	}

	public List getMoveArticles() throws PortalException {
		ThemeDisplay themeDisplay =
			(ThemeDisplay)_liferayPortletRequest.getAttribute(
				WebKeys.THEME_DISPLAY);

		List articles = new ArrayList<>();

		String[] articleIds = ParamUtil.getStringValues(
			_liferayPortletRequest, "rowIdsJournalArticle");

		for (String articleId : articleIds) {
			JournalArticle article = JournalArticleServiceUtil.fetchArticle(
				themeDisplay.getScopeGroupId(), articleId);

			if (article != null) {
				articles.add(article);
			}
		}

		return articles;
	}

	public List getMoveFolders() throws PortalException {
		long[] folderIds = ParamUtil.getLongValues(
			_liferayPortletRequest, "rowIdsJournalFolder");

		List folders = new ArrayList<>();

		for (long folderId : folderIds) {
			JournalFolder folder = JournalFolderServiceUtil.fetchFolder(
				folderId);

			if (folder != null) {
				folders.add(folder);
			}
		}

		return folders;
	}

	public long getNewFolderId() {
		if (_newFolderId > 0) {
			return _newFolderId;
		}

		_newFolderId = ParamUtil.getLong(_liferayPortletRequest, "newFolderId");

		return _newFolderId;
	}

	public String getNewFolderName() throws PortalException {
		if (Validator.isNotNull(_newFolderName)) {
			return _newFolderName;
		}

		if (getNewFolderId() > 0) {
			JournalFolder folder = JournalFolderLocalServiceUtil.getFolder(
				getNewFolderId());

			_newFolderName = folder.getName();
		}
		else {
			_newFolderName = LanguageUtil.get(_httpServletRequest, "home");
		}

		return _newFolderName;
	}

	public PermissionChecker getPermissionChecker() {
		if (_permissionChecker != null) {
			return _permissionChecker;
		}

		ThemeDisplay themeDisplay =
			(ThemeDisplay)_liferayPortletRequest.getAttribute(
				WebKeys.THEME_DISPLAY);

		_permissionChecker = themeDisplay.getPermissionChecker();

		return _permissionChecker;
	}

	public String getRedirect() {
		if (_redirect != null) {
			return _redirect;
		}

		_redirect = ParamUtil.getString(_liferayPortletRequest, "redirect");

		return _redirect;
	}

	public List getValidMoveArticles() {
		return _validMoveArticles;
	}

	public List getValidMoveFolders() {
		return _validMoveFolders;
	}

	public void processArticles(List articles)
		throws PortalException {

		_validMoveArticles = new ArrayList<>();
		_invalidMoveArticles = new ArrayList<>();

		for (JournalArticle curArticle : articles) {
			boolean hasUpdatePermission = JournalArticlePermission.contains(
				getPermissionChecker(), curArticle, ActionKeys.UPDATE);

			if (hasUpdatePermission) {
				_validMoveArticles.add(curArticle);
			}
			else {
				_invalidMoveArticles.add(curArticle);
			}
		}
	}

	public void processFolders(List folders)
		throws PortalException {

		_validMoveFolders = new ArrayList<>();
		_invalidMoveFolders = new ArrayList<>();

		for (JournalFolder curFolder : folders) {
			boolean hasUpdatePermission = JournalFolderPermission.contains(
				getPermissionChecker(), curFolder, ActionKeys.UPDATE);

			if (hasUpdatePermission) {
				_validMoveFolders.add(curFolder);
			}
			else {
				_invalidMoveFolders.add(curFolder);
			}
		}
	}

	public void setViewAttributes() {
		ThemeDisplay themeDisplay =
			(ThemeDisplay)_liferayPortletRequest.getAttribute(
				WebKeys.THEME_DISPLAY);

		PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();

		portletDisplay.setShowBackIcon(true);
		portletDisplay.setURLBack(getRedirect());

		if (_liferayPortletResponse instanceof RenderResponse) {
			RenderResponse renderResponse =
				(RenderResponse)_liferayPortletResponse;

			renderResponse.setTitle(
				LanguageUtil.get(_httpServletRequest, "move-web-content"));
		}
	}

	private final HttpServletRequest _httpServletRequest;
	private List _invalidMoveArticles;
	private List _invalidMoveFolders;
	private final LiferayPortletRequest _liferayPortletRequest;
	private final LiferayPortletResponse _liferayPortletResponse;
	private long _newFolderId;
	private String _newFolderName;
	private PermissionChecker _permissionChecker;
	private String _redirect;
	private List _validMoveArticles;
	private List _validMoveFolders;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy