1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.announcements.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.WebKeys;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portlet.announcements.EntryContentException;
27  import com.liferay.portlet.announcements.EntryDisplayDateException;
28  import com.liferay.portlet.announcements.EntryExpirationDateException;
29  import com.liferay.portlet.announcements.EntryTitleException;
30  import com.liferay.portlet.announcements.NoSuchEntryException;
31  import com.liferay.portlet.announcements.service.AnnouncementsEntryServiceUtil;
32  
33  import java.util.Calendar;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Raymond Augé
49   */
50  public class EditEntryAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58  
59          try {
60              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61                  updateEntry(actionRequest);
62              }
63              else if (cmd.equals(Constants.DELETE)) {
64                  deleteEntry(actionRequest);
65              }
66  
67              String redirect = ParamUtil.getString(actionRequest, "redirect");
68  
69              actionResponse.sendRedirect(redirect);
70          }
71          catch (Exception e) {
72              if (e instanceof EntryContentException ||
73                  e instanceof EntryDisplayDateException ||
74                  e instanceof EntryExpirationDateException ||
75                  e instanceof EntryTitleException ||
76                  e instanceof NoSuchEntryException ||
77                  e instanceof PrincipalException) {
78  
79                  SessionErrors.add(actionRequest, e.getClass().getName());
80              }
81              else {
82                  throw e;
83              }
84          }
85      }
86  
87      public ActionForward render(
88              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
89              RenderRequest renderRequest, RenderResponse renderResponse)
90          throws Exception {
91  
92          try {
93              ActionUtil.getEntry(renderRequest);
94          }
95          catch (Exception e) {
96              if (e instanceof NoSuchEntryException ||
97                  e instanceof PrincipalException) {
98  
99                  SessionErrors.add(renderRequest, e.getClass().getName());
100 
101                 return mapping.findForward("portlet.announcements.error");
102             }
103             else {
104                 throw e;
105             }
106         }
107 
108         return mapping.findForward(
109             getForward(renderRequest, "portlet.announcements.edit_entry"));
110     }
111 
112     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
113         long entryId = ParamUtil.getLong(actionRequest, "entryId");
114 
115         AnnouncementsEntryServiceUtil.deleteEntry(entryId);
116     }
117 
118     protected void updateEntry(ActionRequest actionRequest) throws Exception {
119         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
120             WebKeys.THEME_DISPLAY);
121 
122         long entryId = ParamUtil.getLong(actionRequest, "entryId");
123 
124         String[] distributionScopeParts = StringUtil.split(
125             ParamUtil.getString(actionRequest, "distributionScope"));
126 
127         long classNameId = 0;
128         long classPK = 0;
129 
130         if (distributionScopeParts.length == 2) {
131             classNameId = GetterUtil.getLong(distributionScopeParts[0]);
132 
133             if (classNameId > 0) {
134                 classPK = GetterUtil.getLong(distributionScopeParts[1]);
135             }
136         }
137 
138         String title = ParamUtil.getString(actionRequest, "title");
139         String content = ParamUtil.getString(actionRequest, "content");
140         String url = ParamUtil.getString(actionRequest, "url");
141         String type = ParamUtil.getString(actionRequest, "type");
142 
143         int displayDateMonth = ParamUtil.getInteger(
144             actionRequest, "displayDateMonth");
145         int displayDateDay = ParamUtil.getInteger(
146             actionRequest, "displayDateDay");
147         int displayDateYear = ParamUtil.getInteger(
148             actionRequest, "displayDateYear");
149         int displayDateHour = ParamUtil.getInteger(
150             actionRequest, "displayDateHour");
151         int displayDateMinute = ParamUtil.getInteger(
152             actionRequest, "displayDateMinute");
153         int displayDateAmPm = ParamUtil.getInteger(
154             actionRequest, "displayDateAmPm");
155 
156         if (displayDateAmPm == Calendar.PM) {
157             displayDateHour += 12;
158         }
159 
160         int expirationDateMonth = ParamUtil.getInteger(
161             actionRequest, "expirationDateMonth");
162         int expirationDateDay = ParamUtil.getInteger(
163             actionRequest, "expirationDateDay");
164         int expirationDateYear = ParamUtil.getInteger(
165             actionRequest, "expirationDateYear");
166         int expirationDateHour = ParamUtil.getInteger(
167             actionRequest, "expirationDateHour");
168         int expirationDateMinute = ParamUtil.getInteger(
169             actionRequest, "expirationDateMinute");
170         int expirationDateAmPm = ParamUtil.getInteger(
171             actionRequest, "expirationDateAmPm");
172 
173         if (expirationDateAmPm == Calendar.PM) {
174             expirationDateHour += 12;
175         }
176 
177         int priority = ParamUtil.getInteger(actionRequest, "priority");
178         boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
179 
180         if (entryId <= 0) {
181 
182             // Add entry
183 
184             AnnouncementsEntryServiceUtil.addEntry(
185                 themeDisplay.getPlid(), classNameId, classPK, title, content,
186                 url, type, displayDateMonth, displayDateDay, displayDateYear,
187                 displayDateHour, displayDateMinute, expirationDateMonth,
188                 expirationDateDay, expirationDateYear, expirationDateHour,
189                 expirationDateMinute, priority, alert);
190         }
191         else {
192 
193             // Update entry
194 
195             AnnouncementsEntryServiceUtil.updateEntry(
196                 entryId, title, content, url, type, displayDateMonth,
197                 displayDateDay, displayDateYear, displayDateHour,
198                 displayDateMinute, expirationDateMonth, expirationDateDay,
199                 expirationDateYear, expirationDateHour, expirationDateMinute,
200                 priority);
201         }
202     }
203 
204 }