/**
* 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.expando.web.internal.portlet;
import com.liferay.expando.kernel.exception.ColumnNameException;
import com.liferay.expando.kernel.exception.ColumnTypeException;
import com.liferay.expando.kernel.exception.DuplicateColumnNameException;
import com.liferay.expando.kernel.exception.NoSuchColumnException;
import com.liferay.expando.kernel.exception.ValueDataException;
import com.liferay.expando.kernel.model.ExpandoBridge;
import com.liferay.expando.kernel.model.ExpandoColumnConstants;
import com.liferay.expando.kernel.service.ExpandoColumnService;
import com.liferay.expando.kernel.util.ExpandoBridgeFactoryUtil;
import com.liferay.expando.kernel.util.ExpandoPresetUtil;
import com.liferay.expando.web.internal.constants.ExpandoPortletKeys;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.LocalizationUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnicodeProperties;
import com.liferay.portal.kernel.util.WebKeys;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Raymond Augé
* @author Drew Brokke
*/
@Component(
immediate = true,
property = {
"com.liferay.portlet.add-default-resource=true",
"com.liferay.portlet.css-class-wrapper=portlet-expando",
"com.liferay.portlet.display-category=category.hidden",
"com.liferay.portlet.header-portlet-css=/css/main.css",
"com.liferay.portlet.icon=/icons/expando.png",
"com.liferay.portlet.preferences-owned-by-group=true",
"com.liferay.portlet.private-request-attributes=false",
"com.liferay.portlet.private-session-attributes=false",
"com.liferay.portlet.render-weight=50",
"com.liferay.portlet.system=true",
"com.liferay.portlet.use-default-template=true",
"javax.portlet.display-name=Custom Fields",
"javax.portlet.expiration-cache=0",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.name=" + ExpandoPortletKeys.EXPANDO,
"javax.portlet.portlet-mode=text/html",
"javax.portlet.resource-bundle=content.Language"
},
service = Portlet.class
)
public class ExpandoPortlet extends MVCPortlet {
public void addExpando(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
WebKeys.THEME_DISPLAY);
String modelResource = ParamUtil.getString(
actionRequest, "modelResource");
long resourcePrimKey = ParamUtil.getLong(
actionRequest, "resourcePrimKey");
String name = ParamUtil.getString(actionRequest, "name");
String preset = ParamUtil.getString(actionRequest, "type");
ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
if (preset.startsWith("Preset")) {
ExpandoPresetUtil.addPresetExpando(expandoBridge, preset, name);
}
else {
int type = ParamUtil.getInteger(actionRequest, "type");
expandoBridge.addAttribute(name, type);
updateProperties(actionRequest, expandoBridge, name);
}
}
public void deleteExpando(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
long columnId = ParamUtil.getLong(actionRequest, "columnId");
_expandoColumnService.deleteColumn(columnId);
}
public void deleteExpandos(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
long[] columnIds = StringUtil.split(
ParamUtil.getString(actionRequest, "columnIds"), 0L);
for (long columnId : columnIds) {
_expandoColumnService.deleteColumn(columnId);
}
}
public void updateExpando(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
WebKeys.THEME_DISPLAY);
String modelResource = ParamUtil.getString(
actionRequest, "modelResource");
long resourcePrimKey = ParamUtil.getLong(
actionRequest, "resourcePrimKey");
String name = ParamUtil.getString(actionRequest, "name");
int type = ParamUtil.getInteger(actionRequest, "type");
Serializable defaultValue = getValue(
actionRequest, "defaultValue", type);
ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
expandoBridge.setAttributeDefault(name, defaultValue);
updateProperties(actionRequest, expandoBridge, name);
}
@Override
protected void doDispatch(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
if (SessionErrors.contains(
renderRequest, ColumnNameException.class.getName()) ||
SessionErrors.contains(
renderRequest, ColumnTypeException.class.getName()) ||
SessionErrors.contains(
renderRequest, DuplicateColumnNameException.class.getName()) ||
SessionErrors.contains(
renderRequest, ValueDataException.class.getName())) {
include("/edit_expando.jsp", renderRequest, renderResponse);
}
else if (SessionErrors.contains(
renderRequest, NoSuchColumnException.class.getName()) ||
SessionErrors.contains(
renderRequest, PrincipalException.getNestedClasses())) {
include("/error.jsp", renderRequest, renderResponse);
}
else {
super.doDispatch(renderRequest, renderResponse);
}
}
protected Serializable getValue(
PortletRequest portletRequest, String name, int type)
throws PortalException {
String delimiter = StringPool.COMMA;
Serializable value = null;
if (type == ExpandoColumnConstants.BOOLEAN) {
value = ParamUtil.getBoolean(portletRequest, name);
}
else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
}
else if (type == ExpandoColumnConstants.DATE) {
User user = _portal.getUser(portletRequest);
int valueDateMonth = ParamUtil.getInteger(
portletRequest, name + "Month");
int valueDateDay = ParamUtil.getInteger(
portletRequest, name + "Day");
int valueDateYear = ParamUtil.getInteger(
portletRequest, name + "Year");
int valueDateHour = ParamUtil.getInteger(
portletRequest, name + "Hour");
int valueDateMinute = ParamUtil.getInteger(
portletRequest, name + "Minute");
int valueDateAmPm = ParamUtil.getInteger(
portletRequest, name + "AmPm");
if (valueDateAmPm == Calendar.PM) {
valueDateHour += 12;
}
value = _portal.getDate(
valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
valueDateMinute, user.getTimeZone(), ValueDataException.class);
}
else if (type == ExpandoColumnConstants.DATE_ARRAY) {
}
else if (type == ExpandoColumnConstants.DOUBLE) {
value = ParamUtil.getDouble(portletRequest, name);
}
else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
String paramValue = ParamUtil.getString(portletRequest, name);
if (paramValue.contains(StringPool.NEW_LINE)) {
delimiter = StringPool.NEW_LINE;
}
String[] values = StringUtil.split(paramValue, delimiter);
value = GetterUtil.getDoubleValues(values);
}
else if (type == ExpandoColumnConstants.FLOAT) {
value = ParamUtil.getFloat(portletRequest, name);
}
else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
String paramValue = ParamUtil.getString(portletRequest, name);
if (paramValue.contains(StringPool.NEW_LINE)) {
delimiter = StringPool.NEW_LINE;
}
String[] values = StringUtil.split(paramValue, delimiter);
value = GetterUtil.getFloatValues(values);
}
else if (type == ExpandoColumnConstants.INTEGER) {
value = ParamUtil.getInteger(portletRequest, name);
}
else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
String paramValue = ParamUtil.getString(portletRequest, name);
if (paramValue.contains(StringPool.NEW_LINE)) {
delimiter = StringPool.NEW_LINE;
}
String[] values = StringUtil.split(paramValue, delimiter);
value = GetterUtil.getIntegerValues(values);
}
else if (type == ExpandoColumnConstants.LONG) {
value = ParamUtil.getLong(portletRequest, name);
}
else if (type == ExpandoColumnConstants.LONG_ARRAY) {
String paramValue = ParamUtil.getString(portletRequest, name);
if (paramValue.contains(StringPool.NEW_LINE)) {
delimiter = StringPool.NEW_LINE;
}
String[] values = StringUtil.split(paramValue, delimiter);
value = GetterUtil.getLongValues(values);
}
else if (type == ExpandoColumnConstants.NUMBER) {
value = ParamUtil.getNumber(portletRequest, name);
}
else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
String paramValue = ParamUtil.getString(portletRequest, name);
if (paramValue.contains(StringPool.NEW_LINE)) {
delimiter = StringPool.NEW_LINE;
}
String[] values = StringUtil.split(paramValue, delimiter);
value = GetterUtil.getNumberValues(values);
}
else if (type == ExpandoColumnConstants.SHORT) {
value = ParamUtil.getShort(portletRequest, name);
}
else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
String paramValue = ParamUtil.getString(portletRequest, name);
if (paramValue.contains(StringPool.NEW_LINE)) {
delimiter = StringPool.NEW_LINE;
}
String[] values = StringUtil.split(paramValue, delimiter);
value = GetterUtil.getShortValues(values);
}
else if (type == ExpandoColumnConstants.STRING_ARRAY) {
String paramValue = ParamUtil.getString(portletRequest, name);
if (paramValue.contains(StringPool.NEW_LINE)) {
delimiter = StringPool.NEW_LINE;
}
value = StringUtil.split(paramValue, delimiter);
}
else if (type == ExpandoColumnConstants.STRING_LOCALIZED) {
value = (Serializable)LocalizationUtil.getLocalizationMap(
portletRequest, name);
}
else {
value = ParamUtil.getString(portletRequest, name);
}
return value;
}
@Override
protected boolean isSessionErrorException(Throwable cause) {
if (cause instanceof ColumnNameException ||
cause instanceof ColumnTypeException ||
cause instanceof DuplicateColumnNameException ||
cause instanceof NoSuchColumnException ||
cause instanceof PrincipalException ||
cause instanceof ValueDataException) {
return true;
}
return false;
}
@Reference(unbind = "-")
protected void setExpandoColumnService(
ExpandoColumnService expandoColumnService) {
_expandoColumnService = expandoColumnService;
}
protected void updateProperties(
ActionRequest actionRequest, ExpandoBridge expandoBridge,
String name)
throws Exception {
Enumeration<String> enu = actionRequest.getParameterNames();
UnicodeProperties properties = expandoBridge.getAttributeProperties(
name);
List<String> propertyNames = new ArrayList<>();
while (enu.hasMoreElements()) {
String param = enu.nextElement();
if (param.contains("PropertyName--")) {
String propertyName = ParamUtil.getString(actionRequest, param);
propertyNames.add(propertyName);
}
}
for (String propertyName : propertyNames) {
String value = ParamUtil.getString(
actionRequest, "Property--" + propertyName + "--");
properties.setProperty(propertyName, value);
}
expandoBridge.setAttributeProperties(name, properties);
}
private ExpandoColumnService _expandoColumnService;
@Reference
private Portal _portal;
}