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

com.liferay.change.tracking.spi.display.BaseCTDisplayRenderer Maven / Gradle / Ivy

The newest version!
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */

package com.liferay.change.tracking.spi.display;

import com.liferay.change.tracking.spi.display.context.DisplayContext;
import com.liferay.petra.function.UnsafeSupplier;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.BaseModel;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.CamelCaseUtil;
import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.ResourceBundleUtil;
import com.liferay.portal.kernel.util.WebKeys;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;

import java.sql.Blob;

import java.text.Format;

import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.function.Function;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Preston Crary
 */
public abstract class BaseCTDisplayRenderer>
	implements CTDisplayRenderer {

	@Override
	public String getEditURL(HttpServletRequest httpServletRequest, T model)
		throws Exception {

		return null;
	}

	@Override
	public abstract Class getModelClass();

	@Override
	public abstract String getTitle(Locale locale, T model)
		throws PortalException;

	@Override
	public String getTypeName(Locale locale) {
		Class modelClass = getModelClass();

		return LanguageUtil.get(
			getResourceBundle(locale), "model.resource." + modelClass.getName(),
			modelClass.getName());
	}

	@Override
	public boolean isHideable(T model) {
		return false;
	}

	@Override
	public void render(DisplayContext displayContext) throws Exception {
		HttpServletResponse httpServletResponse =
			displayContext.getHttpServletResponse();

		Writer writer = httpServletResponse.getWriter();

		writer.write("
"); HttpServletRequest httpServletRequest = displayContext.getHttpServletRequest(); ThemeDisplay themeDisplay = (ThemeDisplay)httpServletRequest.getAttribute( WebKeys.THEME_DISPLAY); buildDisplay( new DisplayBuilderImpl<>( displayContext, getResourceBundle(displayContext.getLocale()), themeDisplay)); PermissionChecker permissionChecker = themeDisplay.getPermissionChecker(); if (permissionChecker.isCompanyAdmin(themeDisplay.getCompanyId())) { boolean showAllData = (Boolean)httpServletRequest.getAttribute( "showAllData"); if (showAllData) { T model = displayContext.getModel(); _buildTableContent( httpServletResponse, model.getModelAttributes()); } } writer.write("
"); } protected void buildDisplay(DisplayBuilder displayBuilder) throws PortalException { T model = displayBuilder.getModel(); Map> attributeGetterFunctions = model.getAttributeGetterFunctions(); for (Map.Entry> entry : attributeGetterFunctions.entrySet()) { Function function = entry.getValue(); displayBuilder.display( CamelCaseUtil.fromCamelCase(entry.getKey()), function.apply(model)); } } protected ResourceBundle getResourceBundle(Locale locale) { return ResourceBundleUtil.getBundle(locale, getClass()); } protected interface DisplayBuilder { public DisplayBuilder display(String languageKey, Object value); public DisplayBuilder display( String languageKey, Object value, boolean escape); public DisplayBuilder display( String languageKey, Object value, boolean escape, boolean formatted); public DisplayBuilder display( String languageKey, String value, boolean escape); public DisplayBuilder display( String languageKey, UnsafeSupplier unsafeSupplier); public DisplayBuilder display( String languageKey, UnsafeSupplier unsafeSupplier, boolean escape); public DisplayBuilder display( String languageKey, UnsafeSupplier unsafeSupplier, boolean escape, boolean formatted); public DisplayContext getDisplayContext(); public Locale getLocale(); public T getModel(); } private void _buildTableContent( HttpServletResponse httpServletResponse, Map modelAttributes) { try { Writer writer = httpServletResponse.getWriter(); for (Map.Entry entry : modelAttributes.entrySet()) { writer.write(""); writer.write(entry.getKey()); writer.write(""); Object value = entry.getValue(); if (Objects.equals(value, StringPool.BLANK)) { writer.write("null"); } else { writer.write(String.valueOf(value)); } writer.write(""); } } catch (IOException ioException) { throw new RuntimeException(ioException); } } private static final Log _log = LogFactoryUtil.getLog( BaseCTDisplayRenderer.class); private static class DisplayBuilderImpl implements DisplayBuilder { @Override public DisplayBuilder display(String languageKey, Object value) { return display(languageKey, value, true); } @Override public DisplayBuilder display( String languageKey, Object value, boolean escape) { return display(languageKey, value, escape, false); } @Override public DisplayBuilder display( String languageKey, Object value, boolean escape, boolean formatted) { HttpServletResponse httpServletResponse = _displayContext.getHttpServletResponse(); try { Writer writer = httpServletResponse.getWriter(); writer.write(""); writer.write(LanguageUtil.get(_resourceBundle, languageKey)); writer.write(""); if (formatted) { writer.write("
");
				}

				if (value instanceof Blob) {
					String downloadURL = _displayContext.getDownloadURL(
						languageKey, 0, null);

					if (downloadURL == null) {
						writer.write(
							LanguageUtil.get(_resourceBundle, "no-download"));
					}
					else {
						writer.write("");
						writer.write(
							LanguageUtil.get(_resourceBundle, "download"));
						writer.write("");
					}
				}
				else if (value instanceof Date) {
					Format format = FastDateFormatFactoryUtil.getDateTime(
						_resourceBundle.getLocale(),
						_themeDisplay.getTimeZone());

					writer.write(format.format(value));
				}
				else {
					if (escape) {
						writer.write(HtmlUtil.escape(String.valueOf(value)));
					}
					else {
						writer.write(String.valueOf(value));
					}
				}

				if (formatted) {
					writer.write("
"); } writer.write(""); } catch (IOException ioException) { throw new UncheckedIOException(ioException); } return this; } @Override public DisplayBuilder display( String languageKey, String value, boolean escape) { return display(languageKey, value, escape, false); } @Override public DisplayBuilder display( String languageKey, UnsafeSupplier unsafeSupplier) { return display(languageKey, unsafeSupplier, true); } @Override public DisplayBuilder display( String languageKey, UnsafeSupplier unsafeSupplier, boolean escape) { return display(languageKey, unsafeSupplier, escape, false); } @Override public DisplayBuilder display( String languageKey, UnsafeSupplier unsafeSupplier, boolean escape, boolean formatted) { try { Object value = unsafeSupplier.get(); if (value != null) { display(languageKey, value, escape, formatted); } } catch (Exception exception) { if (_log.isWarnEnabled()) { _log.warn(exception); } } return this; } @Override public DisplayContext getDisplayContext() { return _displayContext; } @Override public Locale getLocale() { return _resourceBundle.getLocale(); } @Override public T getModel() { return _displayContext.getModel(); } private DisplayBuilderImpl( DisplayContext displayContext, ResourceBundle resourceBundle, ThemeDisplay themeDisplay) { _displayContext = displayContext; _resourceBundle = resourceBundle; _themeDisplay = themeDisplay; } private final DisplayContext _displayContext; private final ResourceBundle _resourceBundle; private final ThemeDisplay _themeDisplay; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy