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  public class EditEntryAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
69                  updateEntry(actionRequest);
70              }
71              else if (cmd.equals(Constants.DELETE)) {
72                  deleteEntry(actionRequest);
73              }
74  
75              String redirect = ParamUtil.getString(actionRequest, "redirect");
76  
77              actionResponse.sendRedirect(redirect);
78          }
79          catch (Exception e) {
80              if (e instanceof EntryContentException ||
81                  e instanceof EntryDisplayDateException ||
82                  e instanceof EntryExpirationDateException ||
83                  e instanceof EntryTitleException ||
84                  e instanceof NoSuchEntryException ||
85                  e instanceof PrincipalException) {
86  
87                  SessionErrors.add(actionRequest, e.getClass().getName());
88              }
89              else {
90                  throw e;
91              }
92          }
93      }
94  
95      public ActionForward render(
96              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
97              RenderRequest renderRequest, RenderResponse renderResponse)
98          throws Exception {
99  
100         try {
101             ActionUtil.getEntry(renderRequest);
102         }
103         catch (Exception e) {
104             if (e instanceof NoSuchEntryException ||
105                 e instanceof PrincipalException) {
106 
107                 SessionErrors.add(renderRequest, e.getClass().getName());
108 
109                 return mapping.findForward("portlet.announcements.error");
110             }
111             else {
112                 throw e;
113             }
114         }
115 
116         return mapping.findForward(
117             getForward(renderRequest, "portlet.announcements.edit_entry"));
118     }
119 
120     protected void deleteEntry(ActionRequest actionRequest) throws Exception {
121         long entryId = ParamUtil.getLong(actionRequest, "entryId");
122 
123         AnnouncementsEntryServiceUtil.deleteEntry(entryId);
124     }
125 
126     protected void updateEntry(ActionRequest actionRequest) throws Exception {
127         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
128             WebKeys.THEME_DISPLAY);
129 
130         long entryId = ParamUtil.getLong(actionRequest, "entryId");
131 
132         String[] distributionScopeParts = StringUtil.split(
133             ParamUtil.getString(actionRequest, "distributionScope"));
134 
135         long classNameId = 0;
136         long classPK = 0;
137 
138         if (distributionScopeParts.length == 2) {
139             classNameId = GetterUtil.getLong(distributionScopeParts[0]);
140 
141             if (classNameId > 0) {
142                 classPK = GetterUtil.getLong(distributionScopeParts[1]);
143             }
144         }
145 
146         String title = ParamUtil.getString(actionRequest, "title");
147         String content = ParamUtil.getString(actionRequest, "content");
148         String url = ParamUtil.getString(actionRequest, "url");
149         String type = ParamUtil.getString(actionRequest, "type");
150 
151         int displayDateMonth = ParamUtil.getInteger(
152             actionRequest, "displayDateMonth");
153         int displayDateDay = ParamUtil.getInteger(
154             actionRequest, "displayDateDay");
155         int displayDateYear = ParamUtil.getInteger(
156             actionRequest, "displayDateYear");
157         int displayDateHour = ParamUtil.getInteger(
158             actionRequest, "displayDateHour");
159         int displayDateMinute = ParamUtil.getInteger(
160             actionRequest, "displayDateMinute");
161         int displayDateAmPm = ParamUtil.getInteger(
162             actionRequest, "displayDateAmPm");
163 
164         if (displayDateAmPm == Calendar.PM) {
165             displayDateHour += 12;
166         }
167 
168         int expirationDateMonth = ParamUtil.getInteger(
169             actionRequest, "expirationDateMonth");
170         int expirationDateDay = ParamUtil.getInteger(
171             actionRequest, "expirationDateDay");
172         int expirationDateYear = ParamUtil.getInteger(
173             actionRequest, "expirationDateYear");
174         int expirationDateHour = ParamUtil.getInteger(
175             actionRequest, "expirationDateHour");
176         int expirationDateMinute = ParamUtil.getInteger(
177             actionRequest, "expirationDateMinute");
178         int expirationDateAmPm = ParamUtil.getInteger(
179             actionRequest, "expirationDateAmPm");
180 
181         if (expirationDateAmPm == Calendar.PM) {
182             expirationDateHour += 12;
183         }
184 
185         int priority = ParamUtil.getInteger(actionRequest, "priority");
186         boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
187 
188         if (entryId <= 0) {
189 
190             // Add entry
191 
192             AnnouncementsEntryServiceUtil.addEntry(
193                 themeDisplay.getPlid(), classNameId, classPK, title, content,
194                 url, type, displayDateMonth, displayDateDay, displayDateYear,
195                 displayDateHour, displayDateMinute, expirationDateMonth,
196                 expirationDateDay, expirationDateYear, expirationDateHour,
197                 expirationDateMinute, priority, alert);
198         }
199         else {
200 
201             // Update entry
202 
203             AnnouncementsEntryServiceUtil.updateEntry(
204                 entryId, title, content, url, type, displayDateMonth,
205                 displayDateDay, displayDateYear, displayDateHour,
206                 displayDateMinute, expirationDateMonth, expirationDateDay,
207                 expirationDateYear, expirationDateHour, expirationDateMinute,
208                 priority);
209         }
210     }
211 
212 }