1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.announcements.action;
24  
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.WebKeys;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portlet.announcements.EntryContentException;
34  import com.liferay.portlet.announcements.EntryDisplayDateException;
35  import com.liferay.portlet.announcements.EntryExpirationDateException;
36  import com.liferay.portlet.announcements.EntryTitleException;
37  import com.liferay.portlet.announcements.NoSuchEntryException;
38  import com.liferay.portlet.announcements.service.AnnouncementsEntryServiceUtil;
39  import com.liferay.util.servlet.SessionErrors;
40  
41  import java.util.Calendar;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="EditEntryAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Raymond Augé
57   *
58   */
59  public class EditEntryAction extends PortletAction {
60  
61      public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig config,
63              ActionRequest req, ActionResponse res)
64          throws Exception {
65  
66          String cmd = ParamUtil.getString(req, Constants.CMD);
67  
68          try {
69              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
70                  updateEntry(req);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteEntry(req);
74              }
75  
76              String redirect = ParamUtil.getString(req, "redirect");
77  
78              res.sendRedirect(redirect);
79          }
80          catch (Exception e) {
81              if (e instanceof EntryContentException ||
82                  e instanceof EntryDisplayDateException ||
83                  e instanceof EntryExpirationDateException ||
84                  e instanceof EntryTitleException ||
85                  e instanceof NoSuchEntryException ||
86                  e instanceof PrincipalException) {
87  
88                  SessionErrors.add(req, e.getClass().getName());
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig config,
98              RenderRequest req, RenderResponse res)
99          throws Exception {
100 
101         try {
102             ActionUtil.getEntry(req);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchEntryException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(req, e.getClass().getName());
109 
110                 return mapping.findForward("portlet.announcements.error");
111             }
112             else {
113                 throw e;
114             }
115         }
116 
117         return mapping.findForward(
118             getForward(req, "portlet.announcements.edit_entry"));
119     }
120 
121     protected void deleteEntry(ActionRequest req) throws Exception {
122         long entryId = ParamUtil.getLong(req, "entryId");
123 
124         AnnouncementsEntryServiceUtil.deleteEntry(entryId);
125     }
126 
127     protected void updateEntry(ActionRequest req) throws Exception {
128         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
129             WebKeys.THEME_DISPLAY);
130 
131         long entryId = ParamUtil.getLong(req, "entryId");
132 
133         String[] distributionScopeParts = StringUtil.split(
134             ParamUtil.getString(req, "distributionScope"));
135 
136         long classNameId = 0;
137         long classPK = 0;
138 
139         if (distributionScopeParts.length == 2) {
140             classNameId = GetterUtil.getLong(distributionScopeParts[0]);
141 
142             if (classNameId > 0) {
143                 classPK = GetterUtil.getLong(distributionScopeParts[1]);
144             }
145         }
146 
147         String title = ParamUtil.getString(req, "title");
148         String content = ParamUtil.getString(req, "content");
149         String url = ParamUtil.getString(req, "url");
150         String type = ParamUtil.getString(req, "type");
151 
152         int displayDateMonth = ParamUtil.getInteger(req, "displayDateMonth");
153         int displayDateDay = ParamUtil.getInteger(req, "displayDateDay");
154         int displayDateYear = ParamUtil.getInteger(req, "displayDateYear");
155         int displayDateHour = ParamUtil.getInteger(req, "displayDateHour");
156         int displayDateMinute = ParamUtil.getInteger(req, "displayDateMinute");
157         int displayDateAmPm = ParamUtil.getInteger(req, "displayDateAmPm");
158 
159         if (displayDateAmPm == Calendar.PM) {
160             displayDateHour += 12;
161         }
162 
163         int expirationDateMonth = ParamUtil.getInteger(
164             req, "expirationDateMonth");
165         int expirationDateDay = ParamUtil.getInteger(req, "expirationDateDay");
166         int expirationDateYear = ParamUtil.getInteger(
167             req, "expirationDateYear");
168         int expirationDateHour = ParamUtil.getInteger(
169             req, "expirationDateHour");
170         int expirationDateMinute = ParamUtil.getInteger(
171             req, "expirationDateMinute");
172         int expirationDateAmPm = ParamUtil.getInteger(
173             req, "expirationDateAmPm");
174 
175         if (expirationDateAmPm == Calendar.PM) {
176             expirationDateHour += 12;
177         }
178 
179         int priority = ParamUtil.getInteger(req, "priority");
180         boolean alert = ParamUtil.getBoolean(req, "alert");
181 
182         if (entryId <= 0) {
183 
184             // Add entry
185 
186             AnnouncementsEntryServiceUtil.addEntry(
187                 themeDisplay.getPlid(), classNameId, classPK, title, content,
188                 url, type, displayDateMonth, displayDateDay, displayDateYear,
189                 displayDateHour, displayDateMinute, expirationDateMonth,
190                 expirationDateDay, expirationDateYear, expirationDateHour,
191                 expirationDateMinute, priority, alert);
192         }
193         else {
194 
195             // Update entry
196 
197             AnnouncementsEntryServiceUtil.updateEntry(
198                 entryId, title, content, url, type, displayDateMonth,
199                 displayDateDay, displayDateYear, displayDateHour,
200                 displayDateMinute, expirationDateMonth, expirationDateDay,
201                 expirationDateYear, expirationDateHour, expirationDateMinute,
202                 priority);
203         }
204     }
205 
206 }