1   /**
2    * Copyright (c) 2000-2009 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.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.WebKeys;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portlet.announcements.EntryContentException;
35  import com.liferay.portlet.announcements.EntryDisplayDateException;
36  import com.liferay.portlet.announcements.EntryExpirationDateException;
37  import com.liferay.portlet.announcements.EntryTitleException;
38  import com.liferay.portlet.announcements.NoSuchEntryException;
39  import com.liferay.portlet.announcements.service.AnnouncementsEntryServiceUtil;
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 portletConfig,
63              ActionRequest actionRequest, ActionResponse actionResponse)
64          throws Exception {
65  
66          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
67  
68          try {
69              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
70                  updateEntry(actionRequest);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteEntry(actionRequest);
74              }
75  
76              String redirect = ParamUtil.getString(actionRequest, "redirect");
77  
78              actionResponse.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(actionRequest, e.getClass().getName());
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         try {
102             ActionUtil.getEntry(renderRequest);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchEntryException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(renderRequest, 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(renderRequest, "portlet.announcements.edit_entry"));
119     }
120 
121     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
122         long entryId = ParamUtil.getLong(actionRequest, "entryId");
123 
124         AnnouncementsEntryServiceUtil.deleteEntry(entryId);
125     }
126 
127     protected void updateEntry(ActionRequest actionRequest) throws Exception {
128         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
129             WebKeys.THEME_DISPLAY);
130 
131         long entryId = ParamUtil.getLong(actionRequest, "entryId");
132 
133         String[] distributionScopeParts = StringUtil.split(
134             ParamUtil.getString(actionRequest, "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(actionRequest, "title");
148         String content = ParamUtil.getString(actionRequest, "content");
149         String url = ParamUtil.getString(actionRequest, "url");
150         String type = ParamUtil.getString(actionRequest, "type");
151 
152         int displayDateMonth = ParamUtil.getInteger(
153             actionRequest, "displayDateMonth");
154         int displayDateDay = ParamUtil.getInteger(
155             actionRequest, "displayDateDay");
156         int displayDateYear = ParamUtil.getInteger(
157             actionRequest, "displayDateYear");
158         int displayDateHour = ParamUtil.getInteger(
159             actionRequest, "displayDateHour");
160         int displayDateMinute = ParamUtil.getInteger(
161             actionRequest, "displayDateMinute");
162         int displayDateAmPm = ParamUtil.getInteger(
163             actionRequest, "displayDateAmPm");
164 
165         if (displayDateAmPm == Calendar.PM) {
166             displayDateHour += 12;
167         }
168 
169         int expirationDateMonth = ParamUtil.getInteger(
170             actionRequest, "expirationDateMonth");
171         int expirationDateDay = ParamUtil.getInteger(
172             actionRequest, "expirationDateDay");
173         int expirationDateYear = ParamUtil.getInteger(
174             actionRequest, "expirationDateYear");
175         int expirationDateHour = ParamUtil.getInteger(
176             actionRequest, "expirationDateHour");
177         int expirationDateMinute = ParamUtil.getInteger(
178             actionRequest, "expirationDateMinute");
179         int expirationDateAmPm = ParamUtil.getInteger(
180             actionRequest, "expirationDateAmPm");
181 
182         if (expirationDateAmPm == Calendar.PM) {
183             expirationDateHour += 12;
184         }
185 
186         int priority = ParamUtil.getInteger(actionRequest, "priority");
187         boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
188 
189         if (entryId <= 0) {
190 
191             // Add entry
192 
193             AnnouncementsEntryServiceUtil.addEntry(
194                 themeDisplay.getPlid(), classNameId, classPK, title, content,
195                 url, type, displayDateMonth, displayDateDay, displayDateYear,
196                 displayDateHour, displayDateMinute, expirationDateMonth,
197                 expirationDateDay, expirationDateYear, expirationDateHour,
198                 expirationDateMinute, priority, alert);
199         }
200         else {
201 
202             // Update entry
203 
204             AnnouncementsEntryServiceUtil.updateEntry(
205                 entryId, title, content, url, type, displayDateMonth,
206                 displayDateDay, displayDateYear, displayDateHour,
207                 displayDateMinute, expirationDateMonth, expirationDateDay,
208                 expirationDateYear, expirationDateHour, expirationDateMinute,
209                 priority);
210         }
211     }
212 
213 }