1
22
23 package com.liferay.portlet.portletconfiguration.action;
24
25 import com.liferay.portal.kernel.json.JSONFactoryUtil;
26 import com.liferay.portal.kernel.json.JSONObject;
27 import com.liferay.portal.kernel.language.LanguageUtil;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.LocaleUtil;
32 import com.liferay.portal.kernel.util.ParamUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.model.Layout;
35 import com.liferay.portal.security.permission.ActionKeys;
36 import com.liferay.portal.security.permission.PermissionChecker;
37 import com.liferay.portal.service.permission.PortletPermissionUtil;
38 import com.liferay.portal.struts.JSONAction;
39 import com.liferay.portal.theme.ThemeDisplay;
40 import com.liferay.portal.util.WebKeys;
41 import com.liferay.portlet.InvokerPortletImpl;
42 import com.liferay.portlet.PortletPreferencesFactoryUtil;
43
44 import java.util.Locale;
45
46 import javax.portlet.PortletPreferences;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50 import javax.servlet.http.HttpSession;
51
52 import org.apache.struts.action.ActionForm;
53 import org.apache.struts.action.ActionMapping;
54
55
62 public class UpdateLookAndFeelAction extends JSONAction {
63
64 public String getJSON(
65 ActionMapping mapping, ActionForm form, HttpServletRequest request,
66 HttpServletResponse response)
67 throws Exception {
68
69 HttpSession session = request.getSession();
70
71 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
72 WebKeys.THEME_DISPLAY);
73
74 Layout layout = themeDisplay.getLayout();
75
76 PermissionChecker permissionChecker =
77 themeDisplay.getPermissionChecker();
78
79 String portletId = ParamUtil.getString(request, "portletId");
80
81 if (!PortletPermissionUtil.contains(
82 permissionChecker, themeDisplay.getPlid(), portletId,
83 ActionKeys.CONFIGURATION)) {
84
85 return null;
86 }
87
88 PortletPreferences portletSetup =
89 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
90 layout, portletId);
91
92 String css = ParamUtil.getString(request, "css");
93
94 if (_log.isDebugEnabled()) {
95 _log.debug("Updating css " + css);
96 }
97
98 JSONObject jsonObj = JSONFactoryUtil.createJSONObject(css);
99
100 JSONObject portletData = jsonObj.getJSONObject("portletData");
101
102 jsonObj.remove("portletData");
103
104 css = jsonObj.toString();
105
106 boolean useCustomTitle = portletData.getBoolean("useCustomTitle");
107 boolean showBorders = portletData.getBoolean("showBorders");
108 long linkToLayoutId = GetterUtil.getLong(
109 portletData.getString("portletLinksTarget"));
110
111 JSONObject titles = portletData.getJSONObject("titles");
112
113 Locale[] locales = LanguageUtil.getAvailableLocales();
114
115 for (int i = 0; i < locales.length; i++) {
116 String languageId = LocaleUtil.toLanguageId(locales[i]);
117
118 String title = null;
119
120 if (titles.has(languageId)) {
121 title = GetterUtil.getString(titles.getString(languageId));
122 }
123
124 if (Validator.isNotNull(title)) {
125 portletSetup.setValue(
126 "portlet-setup-title-" + languageId, title);
127 }
128 else {
129 portletSetup.reset("portlet-setup-title-" + languageId);
130 }
131 }
132
133 portletSetup.setValue(
134 "portlet-setup-use-custom-title", String.valueOf(useCustomTitle));
135 portletSetup.setValue(
136 "portlet-setup-show-borders", String.valueOf(showBorders));
137
138 if (linkToLayoutId > 0) {
139 portletSetup.setValue(
140 "portlet-setup-link-to-layout-id",
141 String.valueOf(linkToLayoutId));
142 }
143 else {
144 portletSetup.reset("portlet-setup-link-to-layout-id");
145 }
146
147 portletSetup.setValue("portlet-setup-css", css);
148
149 JSONObject wapData = jsonObj.getJSONObject("wapData");
150
151 String wapTitle = wapData.getString("title");
152 String wapInitialWindowState = wapData.getString("initialWindowState");
153
154 portletSetup.setValue("lfr-wap-title", wapTitle);
155 portletSetup.setValue(
156 "lfr-wap-initial-window-state", wapInitialWindowState);
157
158 portletSetup.store();
159
160 InvokerPortletImpl.clearResponse(
161 session, layout.getPrimaryKey(), portletId,
162 LanguageUtil.getLanguageId(request));
163
164 return null;
165 }
166
167 private static Log _log =
168 LogFactoryUtil.getLog(UpdateLookAndFeelAction.class);
169
170 }