1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.announcements.action;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.struts.PortletAction;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.announcements.model.AnnouncementsEntry;
30  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
31  
32  import java.util.Date;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="PreviewEntryAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author David Truong
48   *
49   */
50  public class PreviewEntryAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
58              WebKeys.THEME_DISPLAY);
59  
60          User user = themeDisplay.getUser();
61          Date now = new Date();
62  
63          String[] distributionScopeParts = StringUtil.split(
64              ParamUtil.getString(actionRequest, "distributionScope"));
65  
66          long classNameId = 0;
67          long classPK = 0;
68  
69          if (distributionScopeParts.length == 2) {
70              classNameId = GetterUtil.getLong(distributionScopeParts[0]);
71  
72              if (classNameId > 0) {
73                  classPK = GetterUtil.getLong(distributionScopeParts[1]);
74              }
75          }
76  
77          String title = ParamUtil.getString(actionRequest, "title");
78          String content = ParamUtil.getString(actionRequest, "content");
79          String url = ParamUtil.getString(actionRequest, "url");
80          String type = ParamUtil.getString(actionRequest, "type");
81          int priority = ParamUtil.getInteger(actionRequest, "priority");
82          boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
83  
84          AnnouncementsEntry entry = new AnnouncementsEntryImpl();
85  
86          entry.setCompanyId(user.getCompanyId());
87          entry.setUserId(user.getUserId());
88          entry.setUserName(user.getFullName());
89          entry.setCreateDate(now);
90          entry.setModifiedDate(now);
91          entry.setClassNameId(classNameId);
92          entry.setClassPK(classPK);
93          entry.setTitle(title);
94          entry.setContent(content);
95          entry.setUrl(url);
96          entry.setType(type);
97          entry.setDisplayDate(now);
98          entry.setExpirationDate(now);
99          entry.setPriority(priority);
100         entry.setAlert(alert);
101 
102         actionRequest.setAttribute(WebKeys.ANNOUNCEMENTS_ENTRY, entry);
103     }
104 
105     public ActionForward render(
106             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
107             RenderRequest renderRequest, RenderResponse renderResponse)
108         throws Exception {
109 
110         return mapping.findForward(
111             getForward(renderRequest, "portlet.announcements.preview_entry"));
112     }
113 
114 }