001
014
015 package com.liferay.portlet.journal.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.upload.UploadPortletRequest;
019 import com.liferay.portal.kernel.util.Constants;
020 import com.liferay.portal.kernel.util.ParamUtil;
021 import com.liferay.portal.kernel.util.StringUtil;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.security.auth.PrincipalException;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portal.service.ServiceContextFactory;
026 import com.liferay.portal.struts.PortletAction;
027 import com.liferay.portal.theme.ThemeDisplay;
028 import com.liferay.portal.util.PortalUtil;
029 import com.liferay.portal.util.WebKeys;
030 import com.liferay.portlet.ActionRequestImpl;
031 import com.liferay.portlet.PortletURLImpl;
032 import com.liferay.portlet.journal.DuplicateTemplateIdException;
033 import com.liferay.portlet.journal.NoSuchTemplateException;
034 import com.liferay.portlet.journal.RequiredTemplateException;
035 import com.liferay.portlet.journal.TemplateDescriptionException;
036 import com.liferay.portlet.journal.TemplateIdException;
037 import com.liferay.portlet.journal.TemplateNameException;
038 import com.liferay.portlet.journal.TemplateSmallImageNameException;
039 import com.liferay.portlet.journal.TemplateSmallImageSizeException;
040 import com.liferay.portlet.journal.TemplateXslException;
041 import com.liferay.portlet.journal.model.JournalTemplate;
042 import com.liferay.portlet.journal.model.JournalTemplateConstants;
043 import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
044 import com.liferay.portlet.journal.util.JournalUtil;
045 import com.liferay.util.JS;
046
047 import java.io.File;
048
049 import javax.portlet.ActionRequest;
050 import javax.portlet.ActionResponse;
051 import javax.portlet.PortletConfig;
052 import javax.portlet.PortletRequest;
053 import javax.portlet.RenderRequest;
054 import javax.portlet.RenderResponse;
055 import javax.portlet.WindowState;
056
057 import org.apache.struts.action.ActionForm;
058 import org.apache.struts.action.ActionForward;
059 import org.apache.struts.action.ActionMapping;
060
061
065 public class EditTemplateAction extends PortletAction {
066
067 public void processAction(
068 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
069 ActionRequest actionRequest, ActionResponse actionResponse)
070 throws Exception {
071
072 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
073
074 JournalTemplate template = null;
075
076 try {
077 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
078 template = updateTemplate(actionRequest);
079 }
080 else if (cmd.equals(Constants.DELETE)) {
081 deleteTemplates(actionRequest);
082 }
083
084 String redirect = ParamUtil.getString(actionRequest, "redirect");
085
086 if (template != null) {
087 boolean saveAndContinue = ParamUtil.getBoolean(
088 actionRequest, "saveAndContinue");
089
090 if (saveAndContinue) {
091 redirect = getSaveAndContinueRedirect(
092 portletConfig, actionRequest, template, redirect);
093 }
094 }
095
096 sendRedirect(actionRequest, actionResponse, redirect);
097 }
098 catch (Exception e) {
099 if (e instanceof NoSuchTemplateException ||
100 e instanceof PrincipalException) {
101
102 SessionErrors.add(actionRequest, e.getClass().getName());
103
104 setForward(actionRequest, "portlet.journal.error");
105 }
106 else if (e instanceof DuplicateTemplateIdException ||
107 e instanceof RequiredTemplateException ||
108 e instanceof TemplateDescriptionException ||
109 e instanceof TemplateIdException ||
110 e instanceof TemplateNameException ||
111 e instanceof TemplateSmallImageNameException ||
112 e instanceof TemplateSmallImageSizeException ||
113 e instanceof TemplateXslException) {
114
115 SessionErrors.add(actionRequest, e.getClass().getName());
116
117 if (e instanceof RequiredTemplateException) {
118 actionResponse.sendRedirect(
119 ParamUtil.getString(actionRequest, "redirect"));
120 }
121 }
122 else {
123 throw e;
124 }
125 }
126 }
127
128 public ActionForward render(
129 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
130 RenderRequest renderRequest, RenderResponse renderResponse)
131 throws Exception {
132
133 try {
134 String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
135
136 if (!cmd.equals(Constants.ADD)) {
137 ActionUtil.getTemplate(renderRequest);
138 }
139 }
140 catch (NoSuchTemplateException nsse) {
141
142
143
144
145 }
146 catch (Exception e) {
147 if (
148 e instanceof PrincipalException) {
149
150 SessionErrors.add(renderRequest, e.getClass().getName());
151
152 return mapping.findForward("portlet.journal.error");
153 }
154 else {
155 throw e;
156 }
157 }
158
159 return mapping.findForward(
160 getForward(renderRequest, "portlet.journal.edit_template"));
161 }
162
163 protected void deleteTemplates(ActionRequest actionRequest)
164 throws Exception {
165
166 long groupId = ParamUtil.getLong(actionRequest, "groupId");
167
168 String[] deleteTemplateIds = StringUtil.split(
169 ParamUtil.getString(actionRequest, "deleteTemplateIds"));
170
171 for (int i = 0; i < deleteTemplateIds.length; i++) {
172 JournalTemplateServiceUtil.deleteTemplate(
173 groupId, deleteTemplateIds[i]);
174
175 JournalUtil.removeRecentTemplate(
176 actionRequest, deleteTemplateIds[i]);
177 }
178 }
179
180 protected String getSaveAndContinueRedirect(
181 PortletConfig portletConfig, ActionRequest actionRequest,
182 JournalTemplate template, String redirect)
183 throws Exception {
184
185 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
186 WebKeys.THEME_DISPLAY);
187
188 String originalRedirect = ParamUtil.getString(
189 actionRequest, "originalRedirect");
190
191 PortletURLImpl portletURL = new PortletURLImpl(
192 (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
193 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
194
195 portletURL.setWindowState(WindowState.MAXIMIZED);
196
197 portletURL.setParameter("struts_action", "/journal/edit_template");
198 portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
199 portletURL.setParameter("redirect", redirect, false);
200 portletURL.setParameter("originalRedirect", originalRedirect, false);
201 portletURL.setParameter(
202 "groupId", String.valueOf(template.getGroupId()), false);
203 portletURL.setParameter("templateId", template.getTemplateId(), false);
204
205 return portletURL.toString();
206 }
207
208 protected JournalTemplate updateTemplate(ActionRequest actionRequest)
209 throws Exception {
210
211 UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
212 actionRequest);
213
214 String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
215
216 long groupId = ParamUtil.getLong(uploadRequest, "groupId");
217
218 String templateId = ParamUtil.getString(uploadRequest, "templateId");
219 boolean autoTemplateId = ParamUtil.getBoolean(
220 uploadRequest, "autoTemplateId");
221
222 String structureId = ParamUtil.getString(uploadRequest, "structureId");
223 String name = ParamUtil.getString(uploadRequest, "name");
224 String description = ParamUtil.getString(uploadRequest, "description");
225
226 String xsl = ParamUtil.getString(uploadRequest, "xsl");
227 String xslContent = JS.decodeURIComponent(
228 ParamUtil.getString(uploadRequest, "xslContent"));
229 boolean formatXsl = ParamUtil.getBoolean(uploadRequest, "formatXsl");
230
231 if (Validator.isNull(xsl)) {
232 xsl = xslContent;
233 }
234
235 String langType = ParamUtil.getString(
236 uploadRequest, "langType", JournalTemplateConstants.LANG_TYPE_XSL);
237
238 boolean cacheable = ParamUtil.getBoolean(uploadRequest, "cacheable");
239
240 boolean smallImage = ParamUtil.getBoolean(uploadRequest, "smallImage");
241 String smallImageURL = ParamUtil.getString(
242 uploadRequest, "smallImageURL");
243 File smallFile = uploadRequest.getFile("smallFile");
244
245 ServiceContext serviceContext = ServiceContextFactory.getInstance(
246 JournalTemplate.class.getName(), actionRequest);
247
248 JournalTemplate template = null;
249
250 if (cmd.equals(Constants.ADD)) {
251
252
253
254 template = JournalTemplateServiceUtil.addTemplate(
255 groupId, templateId, autoTemplateId, structureId, name,
256 description, xsl, formatXsl, langType, cacheable, smallImage,
257 smallImageURL, smallFile, serviceContext);
258 }
259 else {
260
261
262
263 template = JournalTemplateServiceUtil.updateTemplate(
264 groupId, templateId, structureId, name, description, xsl,
265 formatXsl, langType, cacheable, smallImage, smallImageURL,
266 smallFile, serviceContext);
267 }
268
269
270
271 JournalUtil.addRecentTemplate(actionRequest, template);
272
273 return template;
274 }
275
276 }