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.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.announcements.model.AnnouncementsEntry;
33  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
34  
35  import java.util.Date;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="PreviewEntryAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author David Truong
51   *
52   */
53  public class PreviewEntryAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
61              WebKeys.THEME_DISPLAY);
62  
63          User user = themeDisplay.getUser();
64          Date now = new Date();
65  
66          String[] distributionScopeParts = StringUtil.split(
67              ParamUtil.getString(actionRequest, "distributionScope"));
68  
69          long classNameId = 0;
70          long classPK = 0;
71  
72          if (distributionScopeParts.length == 2) {
73              classNameId = GetterUtil.getLong(distributionScopeParts[0]);
74  
75              if (classNameId > 0) {
76                  classPK = GetterUtil.getLong(distributionScopeParts[1]);
77              }
78          }
79  
80          String title = ParamUtil.getString(actionRequest, "title");
81          String content = ParamUtil.getString(actionRequest, "content");
82          String url = ParamUtil.getString(actionRequest, "url");
83          String type = ParamUtil.getString(actionRequest, "type");
84          int priority = ParamUtil.getInteger(actionRequest, "priority");
85          boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
86  
87          AnnouncementsEntry entry = new AnnouncementsEntryImpl();
88  
89          entry.setCompanyId(user.getCompanyId());
90          entry.setUserId(user.getUserId());
91          entry.setUserName(user.getFullName());
92          entry.setCreateDate(now);
93          entry.setModifiedDate(now);
94          entry.setClassNameId(classNameId);
95          entry.setClassPK(classPK);
96          entry.setTitle(title);
97          entry.setContent(content);
98          entry.setUrl(url);
99          entry.setType(type);
100         entry.setDisplayDate(now);
101         entry.setExpirationDate(now);
102         entry.setPriority(priority);
103         entry.setAlert(alert);
104 
105         actionRequest.setAttribute(WebKeys.ANNOUNCEMENTS_ENTRY, entry);
106     }
107 
108     public ActionForward render(
109             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
110             RenderRequest renderRequest, RenderResponse renderResponse)
111         throws Exception {
112 
113         return mapping.findForward(
114             getForward(renderRequest, "portlet.announcements.preview_entry"));
115     }
116 
117 }