com.liferay.journal.web.internal.display.context.JournalEditDDMTemplateDisplayContext Maven / Gradle / Ivy
/**
* 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.journal.web.internal.display.context;
import com.liferay.change.tracking.spi.history.util.CTTimelineUtil;
import com.liferay.dynamic.data.mapping.model.DDMStructure;
import com.liferay.dynamic.data.mapping.model.DDMTemplate;
import com.liferay.dynamic.data.mapping.service.DDMStructureLocalServiceUtil;
import com.liferay.dynamic.data.mapping.service.DDMTemplateLocalServiceUtil;
import com.liferay.dynamic.data.mapping.util.DDMTemplateHelper;
import com.liferay.journal.configuration.JournalFileUploadsConfiguration;
import com.liferay.journal.configuration.JournalServiceConfiguration;
import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.web.internal.helper.JournalDDMTemplateHelper;
import com.liferay.portal.configuration.module.configuration.ConfigurationProviderUtil;
import com.liferay.portal.kernel.bean.BeanParamUtil;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.json.JSONUtil;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.module.configuration.ConfigurationException;
import com.liferay.portal.kernel.portlet.LiferayWindowState;
import com.liferay.portal.kernel.portlet.url.builder.PortletURLBuilder;
import com.liferay.portal.kernel.template.TemplateConstants;
import com.liferay.portal.kernel.template.TemplateHandler;
import com.liferay.portal.kernel.template.TemplateHandlerRegistryUtil;
import com.liferay.portal.kernel.template.TemplateVariableDefinition;
import com.liferay.portal.kernel.template.TemplateVariableGroup;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.HashMapBuilder;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.ResourceBundleUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.template.engine.TemplateContextHelper;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
/**
* @author Eudaldo Alonso
*/
public class JournalEditDDMTemplateDisplayContext {
public JournalEditDDMTemplateDisplayContext(
DDMTemplateHelper ddmTemplateHelper,
JournalDDMTemplateHelper journalDDMTemplateHelper, Portal portal,
RenderRequest renderRequest, RenderResponse renderResponse)
throws ConfigurationException {
_ddmTemplateHelper = ddmTemplateHelper;
_journalDDMTemplateHelper = journalDDMTemplateHelper;
_portal = portal;
_renderResponse = renderResponse;
_httpServletRequest = portal.getHttpServletRequest(renderRequest);
_journalFileUploadsConfiguration =
(JournalFileUploadsConfiguration)renderRequest.getAttribute(
JournalFileUploadsConfiguration.class.getName());
_themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
WebKeys.THEME_DISPLAY);
_journalServiceConfiguration =
ConfigurationProviderUtil.getCompanyConfiguration(
JournalServiceConfiguration.class,
_themeDisplay.getCompanyId());
}
public boolean autogenerateDDMTemplateKey() {
if (_journalServiceConfiguration == null) {
return true;
}
return _journalServiceConfiguration.autogenerateDDMTemplateKey();
}
public JSONObject getAutocompleteJSONObject() throws Exception {
return JSONFactoryUtil.createJSONObject(
_ddmTemplateHelper.getAutocompleteJSON(
_httpServletRequest, TemplateConstants.LANG_TYPE_FTL));
}
public long getClassPK() {
if (_classPK != null) {
return _classPK;
}
_classPK = BeanParamUtil.getLong(
getDDMTemplate(), _httpServletRequest, "classPK");
return _classPK;
}
public DDMStructure getDDMStructure() {
if (_ddmStructure != null) {
return _ddmStructure;
}
_ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
getClassPK());
if (_ddmStructure != null) {
return _ddmStructure;
}
DDMTemplate ddmTemplate = getDDMTemplate();
if (ddmTemplate != null) {
_ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(
ddmTemplate.getClassPK());
}
return _ddmStructure;
}
public DDMTemplate getDDMTemplate() {
if (_ddmTemplate != null) {
return _ddmTemplate;
}
_ddmTemplate = DDMTemplateLocalServiceUtil.fetchDDMTemplate(
getDDMTemplateId());
return _ddmTemplate;
}
public HashMap getDDMTemplateEditorContext(
long scopeGroupId)
throws Exception {
return HashMapBuilder.put(
"editorAutocompleteData", getAutocompleteJSONObject()
).put(
"propertiesViewURL",
() -> PortletURLBuilder.createRenderURL(
_renderResponse
).setMVCPath(
"/ddm_template/edit_properties.jsp"
).setParameter(
"classPK", getClassPK()
).setParameter(
"ddmTemplateId", getDDMTemplateId()
).setWindowState(
LiferayWindowState.EXCLUSIVE
).buildString()
).put(
"script", getScript()
).put(
"showCacheableWarning", isCacheable()
).put(
"showPropertiesPanel",
() -> {
DDMTemplate ddmTemplate = getDDMTemplate();
if ((ddmTemplate == null) || (ddmTemplate.getClassPK() <= 0)) {
return true;
}
return false;
}
).put(
"showTemplateWarning",
(_ddmTemplate != null) && (getGroupId() != scopeGroupId)
).put(
"templateVariableGroups", getTemplateVariableGroupJSONArray()
).build();
}
public long getDDMTemplateId() {
if (_ddmTemplateId != null) {
return _ddmTemplateId;
}
_ddmTemplateId = ParamUtil.getLong(
_httpServletRequest, "ddmTemplateId");
CTTimelineUtil.setCTTimelineKeys(
_httpServletRequest, DDMTemplate.class, _ddmTemplateId);
return _ddmTemplateId;
}
public long getGroupId() {
if (_groupId != null) {
return _groupId;
}
ThemeDisplay themeDisplay =
(ThemeDisplay)_httpServletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
_groupId = BeanParamUtil.getLong(
getDDMTemplate(), _httpServletRequest, "groupId",
themeDisplay.getScopeGroupId());
return _groupId;
}
public String getRedirect() {
if (_redirect != null) {
return _redirect;
}
_redirect = ParamUtil.getString(_httpServletRequest, "redirect");
return _redirect;
}
public String getScript() {
if (_script != null) {
return _script;
}
String script = BeanParamUtil.getString(
getDDMTemplate(), _httpServletRequest, "script");
if (Validator.isNotNull(script)) {
_script = new String(Base64.decode(script));
}
if (Validator.isNull(_script)) {
TemplateHandler templateHandler =
TemplateHandlerRegistryUtil.getTemplateHandler(
_portal.getClassNameId(JournalArticle.class));
_script = templateHandler.getTemplatesHelpContent(
TemplateConstants.LANG_TYPE_FTL);
}
String scriptContent = ParamUtil.getString(
_httpServletRequest, "scriptContent");
if (Validator.isNotNull(scriptContent)) {
_script = new String(Base64.decode(scriptContent));
}
return _script;
}
public String getSmallImageSource() {
if (Validator.isNotNull(_smallImageSource)) {
return _smallImageSource;
}
DDMTemplate ddmTemplate = getDDMTemplate();
if (ddmTemplate == null) {
_smallImageSource = "none";
return _smallImageSource;
}
_smallImageSource = ParamUtil.getString(
_httpServletRequest, "smallImageSource");
if (Validator.isNotNull(_smallImageSource)) {
return _smallImageSource;
}
if (!ddmTemplate.isSmallImage()) {
_smallImageSource = "none";
}
else if (Validator.isNotNull(ddmTemplate.getSmallImageURL())) {
_smallImageSource = "url";
}
else if ((ddmTemplate.getSmallImageId() > 0) &&
Validator.isNull(ddmTemplate.getSmallImageURL())) {
_smallImageSource = "file";
}
return _smallImageSource;
}
public ResourceBundle getTemplateHandlerResourceBundle() {
ThemeDisplay themeDisplay =
(ThemeDisplay)_httpServletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
TemplateHandler templateHandler =
TemplateHandlerRegistryUtil.getTemplateHandler(
_portal.getClassNameId(JournalArticle.class));
Class> clazz = getClass();
if (templateHandler != null) {
clazz = templateHandler.getClass();
}
return ResourceBundleUtil.getBundle(themeDisplay.getLocale(), clazz);
}
public JSONArray getTemplateVariableGroupJSONArray() throws Exception {
JSONArray templateVariableGroupJSONArray =
JSONFactoryUtil.createJSONArray();
ResourceBundle resourceBundle = getTemplateHandlerResourceBundle();
for (TemplateVariableGroup templateVariableGroup :
getTemplateVariableGroups()) {
if (templateVariableGroup.isEmpty()) {
continue;
}
JSONArray templateVariableDefinitionJSONArray =
JSONFactoryUtil.createJSONArray();
for (TemplateVariableDefinition templateVariableDefinition :
templateVariableGroup.getTemplateVariableDefinitions()) {
templateVariableDefinitionJSONArray.put(
JSONUtil.put(
"content",
_journalDDMTemplateHelper.getDataContent(
templateVariableDefinition)
).put(
"label",
LanguageUtil.get(
_httpServletRequest, resourceBundle,
templateVariableDefinition.getLabel())
).put(
"repeatable",
templateVariableDefinition.isCollection() ||
templateVariableDefinition.isRepeatable()
).put(
"tooltip",
_journalDDMTemplateHelper.getPaletteItemTitle(
_httpServletRequest, resourceBundle,
templateVariableDefinition)
));
}
templateVariableGroupJSONArray.put(
JSONUtil.put(
"items", templateVariableDefinitionJSONArray
).put(
"label",
LanguageUtil.get(
_httpServletRequest, resourceBundle,
templateVariableGroup.getLabel())
));
}
return templateVariableGroupJSONArray;
}
public Collection getTemplateVariableGroups()
throws Exception {
ThemeDisplay themeDisplay =
(ThemeDisplay)_httpServletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
Map templateVariableGroups =
TemplateContextHelper.getTemplateVariableGroups(
_portal.getClassNameId(JournalArticle.class), getClassPK(),
TemplateConstants.LANG_TYPE_FTL, themeDisplay.getLocale());
return templateVariableGroups.values();
}
public String getTitle() {
ThemeDisplay themeDisplay =
(ThemeDisplay)_httpServletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
DDMStructure ddmStructure = getDDMStructure();
DDMTemplate ddmTemplate = getDDMTemplate();
if ((ddmStructure != null) && (ddmTemplate != null)) {
return StringUtil.appendParentheticalSuffix(
ddmTemplate.getName(themeDisplay.getLocale()),
ddmStructure.getName(themeDisplay.getLocale()));
}
if (ddmStructure != null) {
return LanguageUtil.format(
_httpServletRequest, "new-template-for-structure-x",
ddmStructure.getName(themeDisplay.getLocale()), false);
}
if (ddmTemplate != null) {
return ddmTemplate.getName(themeDisplay.getLocale());
}
return LanguageUtil.get(_httpServletRequest, "new-template");
}
public String[] imageExtensions() {
return _journalFileUploadsConfiguration.imageExtensions();
}
public boolean isCacheable() {
if (_cacheable != null) {
return _cacheable;
}
_cacheable = BeanParamUtil.getBoolean(
getDDMTemplate(), _httpServletRequest, "cacheable", true);
return _cacheable;
}
public boolean isSmallImage() {
if (_smallImage != null) {
return _smallImage;
}
_smallImage = BeanParamUtil.getBoolean(
getDDMTemplate(), _httpServletRequest, "smallImage");
return _smallImage;
}
public long smallImageMaxSize() {
return _journalFileUploadsConfiguration.smallImageMaxSize();
}
private Boolean _cacheable;
private Long _classPK;
private DDMStructure _ddmStructure;
private DDMTemplate _ddmTemplate;
private final DDMTemplateHelper _ddmTemplateHelper;
private Long _ddmTemplateId;
private Long _groupId;
private final HttpServletRequest _httpServletRequest;
private final JournalDDMTemplateHelper _journalDDMTemplateHelper;
private final JournalFileUploadsConfiguration
_journalFileUploadsConfiguration;
private final JournalServiceConfiguration _journalServiceConfiguration;
private final Portal _portal;
private String _redirect;
private final RenderResponse _renderResponse;
private String _script;
private Boolean _smallImage;
private String _smallImageSource;
private final ThemeDisplay _themeDisplay;
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy