1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.upload.UploadPortletRequest;
24  import com.liferay.portal.kernel.util.Constants;
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.ActionRequestImpl;
35  import com.liferay.portlet.PortletURLImpl;
36  import com.liferay.portlet.journal.DuplicateTemplateIdException;
37  import com.liferay.portlet.journal.NoSuchTemplateException;
38  import com.liferay.portlet.journal.RequiredTemplateException;
39  import com.liferay.portlet.journal.TemplateDescriptionException;
40  import com.liferay.portlet.journal.TemplateIdException;
41  import com.liferay.portlet.journal.TemplateNameException;
42  import com.liferay.portlet.journal.TemplateSmallImageNameException;
43  import com.liferay.portlet.journal.TemplateSmallImageSizeException;
44  import com.liferay.portlet.journal.TemplateXslException;
45  import com.liferay.portlet.journal.model.JournalTemplate;
46  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
47  import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
48  import com.liferay.portlet.journal.util.JournalUtil;
49  import com.liferay.util.JS;
50  
51  import java.io.File;
52  
53  import javax.portlet.ActionRequest;
54  import javax.portlet.ActionResponse;
55  import javax.portlet.PortletConfig;
56  import javax.portlet.PortletRequest;
57  import javax.portlet.RenderRequest;
58  import javax.portlet.RenderResponse;
59  import javax.portlet.WindowState;
60  
61  import org.apache.struts.action.ActionForm;
62  import org.apache.struts.action.ActionForward;
63  import org.apache.struts.action.ActionMapping;
64  
65  /**
66   * <a href="EditTemplateAction.java.html"><b><i>View Source</i></b></a>
67   *
68   * @author Brian Wing Shun Chan
69   *
70   */
71  public class EditTemplateAction extends PortletAction {
72  
73      public void processAction(
74              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
75              ActionRequest actionRequest, ActionResponse actionResponse)
76          throws Exception {
77  
78          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
79  
80          JournalTemplate template = null;
81  
82          try {
83              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
84                  template = updateTemplate(actionRequest);
85              }
86              else if (cmd.equals(Constants.DELETE)) {
87                  deleteTemplates(actionRequest);
88              }
89  
90              String redirect = ParamUtil.getString(actionRequest, "redirect");
91  
92              if (template != null) {
93                  boolean saveAndContinue = ParamUtil.getBoolean(
94                      actionRequest, "saveAndContinue");
95  
96                  if (saveAndContinue) {
97                      redirect = getSaveAndContinueRedirect(
98                          portletConfig, actionRequest, template, redirect);
99                  }
100             }
101 
102             sendRedirect(actionRequest, actionResponse, redirect);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchTemplateException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(actionRequest, e.getClass().getName());
109 
110                 setForward(actionRequest, "portlet.journal.error");
111             }
112             else if (e instanceof DuplicateTemplateIdException ||
113                      e instanceof RequiredTemplateException ||
114                      e instanceof TemplateDescriptionException ||
115                      e instanceof TemplateIdException ||
116                      e instanceof TemplateNameException ||
117                      e instanceof TemplateSmallImageNameException ||
118                      e instanceof TemplateSmallImageSizeException ||
119                      e instanceof TemplateXslException) {
120 
121                 SessionErrors.add(actionRequest, e.getClass().getName());
122 
123                 if (e instanceof RequiredTemplateException) {
124                     actionResponse.sendRedirect(
125                         ParamUtil.getString(actionRequest, "redirect"));
126                 }
127             }
128             else {
129                 throw e;
130             }
131         }
132     }
133 
134     public ActionForward render(
135             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
136             RenderRequest renderRequest, RenderResponse renderResponse)
137         throws Exception {
138 
139         try {
140             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
141 
142             if (!cmd.equals(Constants.ADD)) {
143                 ActionUtil.getTemplate(renderRequest);
144             }
145         }
146         catch (NoSuchTemplateException nsse) {
147 
148             // Let this slide because the user can manually input a template id
149             // for a new template that does not yet exist.
150 
151         }
152         catch (Exception e) {
153             if (//e instanceof NoSuchTemplateException ||
154                 e instanceof PrincipalException) {
155 
156                 SessionErrors.add(renderRequest, e.getClass().getName());
157 
158                 return mapping.findForward("portlet.journal.error");
159             }
160             else {
161                 throw e;
162             }
163         }
164 
165         return mapping.findForward(
166             getForward(renderRequest, "portlet.journal.edit_template"));
167     }
168 
169     protected void deleteTemplates(ActionRequest actionRequest)
170         throws Exception {
171 
172         long groupId = ParamUtil.getLong(actionRequest, "groupId");
173 
174         String[] deleteTemplateIds = StringUtil.split(
175             ParamUtil.getString(actionRequest, "deleteTemplateIds"));
176 
177         for (int i = 0; i < deleteTemplateIds.length; i++) {
178             JournalTemplateServiceUtil.deleteTemplate(
179                 groupId, deleteTemplateIds[i]);
180 
181             JournalUtil.removeRecentTemplate(
182                 actionRequest, deleteTemplateIds[i]);
183         }
184     }
185 
186     protected String getSaveAndContinueRedirect(
187             PortletConfig portletConfig, ActionRequest actionRequest,
188             JournalTemplate template, String redirect)
189         throws Exception {
190 
191         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
192             WebKeys.THEME_DISPLAY);
193 
194         String originalRedirect = ParamUtil.getString(
195             actionRequest, "originalRedirect");
196 
197         PortletURLImpl portletURL = new PortletURLImpl(
198             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
199             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
200 
201         portletURL.setWindowState(WindowState.MAXIMIZED);
202 
203         portletURL.setParameter("struts_action", "/journal/edit_template");
204         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
205         portletURL.setParameter("redirect", redirect, false);
206         portletURL.setParameter("originalRedirect", originalRedirect, false);
207         portletURL.setParameter(
208             "groupId", String.valueOf(template.getGroupId()), false);
209         portletURL.setParameter("templateId", template.getTemplateId(), false);
210 
211         return portletURL.toString();
212     }
213 
214     protected JournalTemplate updateTemplate(ActionRequest actionRequest)
215         throws Exception {
216 
217         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
218             actionRequest);
219 
220         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
221 
222         Layout layout = (Layout)uploadRequest.getAttribute(WebKeys.LAYOUT);
223 
224         long groupId = ParamUtil.getLong(uploadRequest, "groupId");
225 
226         String templateId = ParamUtil.getString(uploadRequest, "templateId");
227         boolean autoTemplateId = ParamUtil.getBoolean(
228             uploadRequest, "autoTemplateId");
229 
230         String structureId = ParamUtil.getString(uploadRequest, "structureId");
231         String name = ParamUtil.getString(uploadRequest, "name");
232         String description = ParamUtil.getString(uploadRequest, "description");
233 
234         String xsl = ParamUtil.getString(uploadRequest, "xsl");
235         String xslContent = JS.decodeURIComponent(
236             ParamUtil.getString(uploadRequest, "xslContent"));
237         boolean formatXsl = ParamUtil.getBoolean(uploadRequest, "formatXsl");
238 
239         if (Validator.isNull(xsl)) {
240             xsl = xslContent;
241         }
242 
243         String langType = ParamUtil.getString(
244             uploadRequest, "langType", JournalTemplateImpl.LANG_TYPE_XSL);
245 
246         boolean cacheable = ParamUtil.getBoolean(uploadRequest, "cacheable");
247 
248         boolean smallImage = ParamUtil.getBoolean(uploadRequest, "smallImage");
249         String smallImageURL = ParamUtil.getString(
250             uploadRequest, "smallImageURL");
251         File smallFile = uploadRequest.getFile("smallFile");
252 
253         String[] communityPermissions = uploadRequest.getParameterValues(
254             "communityPermissions");
255         String[] guestPermissions = uploadRequest.getParameterValues(
256             "guestPermissions");
257 
258         JournalTemplate template = null;
259 
260         if (cmd.equals(Constants.ADD)) {
261 
262             // Add template
263 
264             template = JournalTemplateServiceUtil.addTemplate(
265                 templateId, autoTemplateId, layout.getPlid(), structureId, name,
266                 description, xsl, formatXsl, langType, cacheable, smallImage,
267                 smallImageURL, smallFile, communityPermissions,
268                 guestPermissions);
269         }
270         else {
271 
272             // Update template
273 
274             template = JournalTemplateServiceUtil.updateTemplate(
275                 groupId, templateId, structureId, name, description, xsl,
276                 formatXsl, langType, cacheable, smallImage, smallImageURL,
277                 smallFile);
278         }
279 
280         // Recent templates
281 
282         JournalUtil.addRecentTemplate(actionRequest, template);
283 
284         return template;
285     }
286 
287 }