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