001    /**
002     * Copyright (c) 2000-2010 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.WebKeys;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portlet.announcements.EntryContentException;
027    import com.liferay.portlet.announcements.EntryDisplayDateException;
028    import com.liferay.portlet.announcements.EntryExpirationDateException;
029    import com.liferay.portlet.announcements.EntryTitleException;
030    import com.liferay.portlet.announcements.EntryURLException;
031    import com.liferay.portlet.announcements.NoSuchEntryException;
032    import com.liferay.portlet.announcements.service.AnnouncementsEntryServiceUtil;
033    
034    import java.util.Calendar;
035    
036    import javax.portlet.ActionRequest;
037    import javax.portlet.ActionResponse;
038    import javax.portlet.PortletConfig;
039    import javax.portlet.RenderRequest;
040    import javax.portlet.RenderResponse;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Raymond Augé
048     */
049    public class EditEntryAction extends PortletAction {
050    
051            public void processAction(
052                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
060                                    updateEntry(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.DELETE)) {
063                                    deleteEntry(actionRequest);
064                            }
065    
066                            String redirect = ParamUtil.getString(actionRequest, "redirect");
067    
068                            actionResponse.sendRedirect(redirect);
069                    }
070                    catch (Exception e) {
071                            if (e instanceof EntryContentException ||
072                                    e instanceof EntryDisplayDateException ||
073                                    e instanceof EntryExpirationDateException ||
074                                    e instanceof EntryTitleException ||
075                                    e instanceof EntryURLException ||
076                                    e instanceof NoSuchEntryException ||
077                                    e instanceof PrincipalException) {
078    
079                                    SessionErrors.add(actionRequest, e.getClass().getName());
080                            }
081                            else {
082                                    throw e;
083                            }
084                    }
085            }
086    
087            public ActionForward render(
088                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
089                            RenderRequest renderRequest, RenderResponse renderResponse)
090                    throws Exception {
091    
092                    try {
093                            ActionUtil.getEntry(renderRequest);
094                    }
095                    catch (Exception e) {
096                            if (e instanceof NoSuchEntryException ||
097                                    e instanceof PrincipalException) {
098    
099                                    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    }