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