001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.announcements.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portlet.announcements.EntryContentException;
029    import com.liferay.portlet.announcements.EntryDisplayDateException;
030    import com.liferay.portlet.announcements.EntryExpirationDateException;
031    import com.liferay.portlet.announcements.EntryTitleException;
032    import com.liferay.portlet.announcements.EntryURLException;
033    import com.liferay.portlet.announcements.NoSuchEntryException;
034    import com.liferay.portlet.announcements.service.AnnouncementsEntryServiceUtil;
035    
036    import java.util.Calendar;
037    
038    import javax.portlet.ActionRequest;
039    import javax.portlet.ActionResponse;
040    import javax.portlet.PortletConfig;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionForward;
046    import org.apache.struts.action.ActionMapping;
047    
048    /**
049     * @author Raymond Aug??
050     */
051    public class EditEntryAction extends PortletAction {
052    
053            @Override
054            public void processAction(
055                            ActionMapping actionMapping, ActionForm actionForm,
056                            PortletConfig portletConfig, ActionRequest actionRequest,
057                            ActionResponse actionResponse)
058                    throws Exception {
059    
060                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
061    
062                    try {
063                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
064                                    updateEntry(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.DELETE)) {
067                                    deleteEntry(actionRequest);
068                            }
069    
070                            String redirect = PortalUtil.escapeRedirect(
071                                    ParamUtil.getString(actionRequest, "redirect"));
072    
073                            if (Validator.isNotNull(redirect)) {
074                                    actionResponse.sendRedirect(redirect);
075                            }
076                    }
077                    catch (Exception e) {
078                            if (e instanceof EntryContentException ||
079                                    e instanceof EntryDisplayDateException ||
080                                    e instanceof EntryExpirationDateException ||
081                                    e instanceof EntryTitleException ||
082                                    e instanceof EntryURLException ||
083                                    e instanceof NoSuchEntryException ||
084                                    e instanceof PrincipalException) {
085    
086                                    SessionErrors.add(actionRequest, e.getClass());
087                            }
088                            else {
089                                    throw e;
090                            }
091                    }
092            }
093    
094            @Override
095            public ActionForward render(
096                            ActionMapping actionMapping, ActionForm actionForm,
097                            PortletConfig portletConfig, RenderRequest renderRequest,
098                            RenderResponse renderResponse)
099                    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());
109    
110                                    return actionMapping.findForward("portlet.announcements.error");
111                            }
112                            else {
113                                    throw e;
114                            }
115                    }
116    
117                    return actionMapping.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                    boolean autoDisplayDate = ParamUtil.getBoolean(
170                            actionRequest, "autoDisplayDate");
171    
172                    int expirationDateMonth = ParamUtil.getInteger(
173                            actionRequest, "expirationDateMonth");
174                    int expirationDateDay = ParamUtil.getInteger(
175                            actionRequest, "expirationDateDay");
176                    int expirationDateYear = ParamUtil.getInteger(
177                            actionRequest, "expirationDateYear");
178                    int expirationDateHour = ParamUtil.getInteger(
179                            actionRequest, "expirationDateHour");
180                    int expirationDateMinute = ParamUtil.getInteger(
181                            actionRequest, "expirationDateMinute");
182                    int expirationDateAmPm = ParamUtil.getInteger(
183                            actionRequest, "expirationDateAmPm");
184    
185                    if (expirationDateAmPm == Calendar.PM) {
186                            expirationDateHour += 12;
187                    }
188    
189                    int priority = ParamUtil.getInteger(actionRequest, "priority");
190                    boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
191    
192                    if (entryId <= 0) {
193    
194                            // Add entry
195    
196                            AnnouncementsEntryServiceUtil.addEntry(
197                                    themeDisplay.getPlid(), classNameId, classPK, title, content,
198                                    url, type, displayDateMonth, displayDateDay, displayDateYear,
199                                    displayDateHour, displayDateMinute, autoDisplayDate,
200                                    expirationDateMonth, expirationDateDay, expirationDateYear,
201                                    expirationDateHour, expirationDateMinute, priority, alert);
202                    }
203                    else {
204    
205                            // Update entry
206    
207                            AnnouncementsEntryServiceUtil.updateEntry(
208                                    entryId, title, content, url, type, displayDateMonth,
209                                    displayDateDay, displayDateYear, displayDateHour,
210                                    displayDateMinute, expirationDateMonth, expirationDateDay,
211                                    expirationDateYear, expirationDateHour, expirationDateMinute,
212                                    priority);
213                    }
214            }
215    
216    }