/**
* 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.trash.web.internal.display.context;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.ContainerModel;
import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
import com.liferay.portal.kernel.portlet.PortalPreferences;
import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
import com.liferay.portal.kernel.trash.TrashHandler;
import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
import com.liferay.portal.kernel.trash.TrashRenderer;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.trash.kernel.model.TrashEntry;
import com.liferay.trash.kernel.service.TrashEntryLocalServiceUtil;
import com.liferay.trash.web.internal.constants.TrashPortletKeys;
import java.util.Objects;
import javax.portlet.PortletURL;
import javax.servlet.http.HttpServletRequest;
/**
* Provides utility methods moved from the Recycle Bin portlet's JSP files to
* reduce the complexity of the views.
*
* @author Jürgen Kappler
*/
public class TrashDisplayContext {
public TrashDisplayContext(
HttpServletRequest request,
LiferayPortletResponse liferayPortletResponse) {
_request = request;
_liferayPortletResponse = liferayPortletResponse;
}
public String getClassName() {
TrashEntry trashEntry = getTrashEntry();
if (trashEntry != null) {
return trashEntry.getClassName();
}
String className = StringPool.BLANK;
long classNameId = getClassNameId();
if (classNameId > 0) {
className = PortalUtil.getClassName(getClassNameId());
}
return className;
}
public long getClassNameId() {
TrashEntry trashEntry = getTrashEntry();
if (trashEntry != null) {
return trashEntry.getClassNameId();
}
return ParamUtil.getLong(_request, "classNameId");
}
public long getClassPK() {
TrashEntry trashEntry = getTrashEntry();
if (trashEntry != null) {
return trashEntry.getClassPK();
}
return ParamUtil.getLong(_request, "classPK");
}
public String getContainerModelRedirectURL() throws PortalException {
if (Validator.isNotNull(_containerModelRedirectURL)) {
return _containerModelRedirectURL;
}
String redirect = ParamUtil.getString(_request, "redirect");
if (Validator.isNull(redirect)) {
PortletURL redirectURL = _liferayPortletResponse.createRenderURL();
redirectURL.setParameter("mvcPath", "/view_content.jsp");
long trashEntryId = getTrashEntryId();
long classNameId = getClassNameId();
long classPK = getClassPK();
if (trashEntryId > 0) {
redirectURL.setParameter(
"trashEntryId", String.valueOf(trashEntryId));
}
else if ((classNameId > 0) && (classPK > 0)) {
redirectURL.setParameter(
"classNameId", String.valueOf(classNameId));
redirectURL.setParameter("classPK", String.valueOf(classPK));
}
redirect = redirectURL.toString();
}
_containerModelRedirectURL = redirect;
return redirect;
}
public String getDisplayStyle() {
if (Validator.isNotNull(_displayStyle)) {
return _displayStyle;
}
PortalPreferences portalPreferences =
PortletPreferencesFactoryUtil.getPortalPreferences(_request);
_displayStyle = portalPreferences.getValue(
TrashPortletKeys.TRASH, "display-style", "list");
return _displayStyle;
}
public String getNavigation() {
if (_navigation != null) {
return _navigation;
}
_navigation = ParamUtil.getString(_request, "navigation", "all");
return _navigation;
}
public String getOrderByCol() {
if (Validator.isNotNull(_orderByCol)) {
return _orderByCol;
}
_orderByCol = ParamUtil.getString(
_request, "orderByCol", "removed-date");
return _orderByCol;
}
public String getOrderByType() {
if (Validator.isNotNull(_orderByType)) {
return _orderByType;
}
_orderByType = ParamUtil.getString(_request, "orderByType", "asc");
return _orderByType;
}
public PortletURL getPortletURL() throws PortalException {
PortletURL portletURL = _liferayPortletResponse.createRenderURL();
long trashEntryId = getTrashEntryId();
if (trashEntryId > 0) {
portletURL.setParameter("mvcPath", "/view_content.jsp");
portletURL.setParameter(
"trashEntryId", String.valueOf(trashEntryId));
}
String displayStyle = getDisplayStyle();
if (Validator.isNotNull(displayStyle)) {
portletURL.setParameter("displayStyle", displayStyle);
}
String keywords = ParamUtil.getString(_request, "keywords");
if (Validator.isNotNull(keywords)) {
portletURL.setParameter("keywords", keywords);
}
String navigation = getNavigation();
if (Validator.isNotNull(navigation)) {
portletURL.setParameter("navigation", navigation);
}
return portletURL;
}
public TrashEntry getTrashEntry() {
if (_trashEntry != null) {
return _trashEntry;
}
long trashEntryId = ParamUtil.getLong(_request, "trashEntryId");
long classNameId = ParamUtil.getLong(_request, "classNameId");
long classPK = ParamUtil.getLong(_request, "classPK");
if (trashEntryId > 0) {
_trashEntry = TrashEntryLocalServiceUtil.fetchEntry(trashEntryId);
}
else if ((classNameId > 0) && (classPK > 0)) {
String className = PortalUtil.getClassName(classNameId);
if (Validator.isNotNull(className)) {
_trashEntry = TrashEntryLocalServiceUtil.fetchEntry(
className, classPK);
}
}
return _trashEntry;
}
public long getTrashEntryId() {
TrashEntry trashEntry = getTrashEntry();
if (trashEntry != null) {
return trashEntry.getEntryId();
}
return 0;
}
public TrashHandler getTrashHandler() {
if (_trashHandler != null) {
return _trashHandler;
}
_trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
getClassName());
return _trashHandler;
}
public TrashRenderer getTrashRenderer() throws PortalException {
if (_trashRenderer != null) {
return _trashRenderer;
}
TrashHandler trashHandler = getTrashHandler();
long classPK = getClassPK();
if ((classPK > 0) && (trashHandler != null)) {
_trashRenderer = trashHandler.getTrashRenderer(getClassPK());
}
return _trashRenderer;
}
public String getViewContentRedirectURL() throws PortalException {
String redirect = ParamUtil.getString(_request, "redirect");
if (Validator.isNull(redirect)) {
TrashHandler trashHandler = getTrashHandler();
ContainerModel parentContainerModel =
trashHandler.getParentContainerModel(getClassPK());
PortletURL redirectURL = _liferayPortletResponse.createRenderURL();
if ((parentContainerModel != null) && (getClassNameId() > 0)) {
String parentContainerModelClassName =
parentContainerModel.getModelClassName();
redirectURL.setParameter("mvcPath", "/view_content.jsp");
redirectURL.setParameter(
"classNameId",
String.valueOf(
PortalUtil.getClassNameId(
parentContainerModelClassName)));
redirectURL.setParameter(
"classPK",
String.valueOf(parentContainerModel.getContainerModelId()));
}
redirect = redirectURL.toString();
}
return redirect;
}
public boolean isDescriptiveView() {
if (Objects.equals(getDisplayStyle(), "descriptive")) {
return true;
}
return false;
}
public boolean isIconView() {
if (Objects.equals(getDisplayStyle(), "icon")) {
return true;
}
return false;
}
public boolean isListView() {
if (Objects.equals(getDisplayStyle(), "list")) {
return true;
}
return false;
}
private String _containerModelRedirectURL;
private String _displayStyle;
private final LiferayPortletResponse _liferayPortletResponse;
private String _navigation;
private String _orderByCol;
private String _orderByType;
private final HttpServletRequest _request;
private TrashEntry _trashEntry;
private TrashHandler _trashHandler;
private TrashRenderer _trashRenderer;
}