1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.announcements.action;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.model.User;
21  import com.liferay.portal.struts.PortletAction;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portlet.announcements.model.AnnouncementsEntry;
25  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
26  
27  import java.util.Date;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  
39  /**
40   * <a href="PreviewEntryAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author David Truong
43   */
44  public class PreviewEntryAction extends PortletAction {
45  
46      public void processAction(
47              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
48              ActionRequest actionRequest, ActionResponse actionResponse)
49          throws Exception {
50  
51          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
52              WebKeys.THEME_DISPLAY);
53  
54          User user = themeDisplay.getUser();
55          Date now = new Date();
56  
57          String[] distributionScopeParts = StringUtil.split(
58              ParamUtil.getString(actionRequest, "distributionScope"));
59  
60          long classNameId = 0;
61          long classPK = 0;
62  
63          if (distributionScopeParts.length == 2) {
64              classNameId = GetterUtil.getLong(distributionScopeParts[0]);
65  
66              if (classNameId > 0) {
67                  classPK = GetterUtil.getLong(distributionScopeParts[1]);
68              }
69          }
70  
71          String title = ParamUtil.getString(actionRequest, "title");
72          String content = ParamUtil.getString(actionRequest, "content");
73          String url = ParamUtil.getString(actionRequest, "url");
74          String type = ParamUtil.getString(actionRequest, "type");
75          int priority = ParamUtil.getInteger(actionRequest, "priority");
76          boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
77  
78          AnnouncementsEntry entry = new AnnouncementsEntryImpl();
79  
80          entry.setCompanyId(user.getCompanyId());
81          entry.setUserId(user.getUserId());
82          entry.setUserName(user.getFullName());
83          entry.setCreateDate(now);
84          entry.setModifiedDate(now);
85          entry.setClassNameId(classNameId);
86          entry.setClassPK(classPK);
87          entry.setTitle(title);
88          entry.setContent(content);
89          entry.setUrl(url);
90          entry.setType(type);
91          entry.setDisplayDate(now);
92          entry.setExpirationDate(now);
93          entry.setPriority(priority);
94          entry.setAlert(alert);
95  
96          actionRequest.setAttribute(WebKeys.ANNOUNCEMENTS_ENTRY, entry);
97      }
98  
99      public ActionForward render(
100             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101             RenderRequest renderRequest, RenderResponse renderResponse)
102         throws Exception {
103 
104         return mapping.findForward(
105             getForward(renderRequest, "portlet.announcements.preview_entry"));
106     }
107 
108 }