1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PreviewEntryAction extends PortletAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
60              WebKeys.THEME_DISPLAY);
61  
62          User user = themeDisplay.getUser();
63          Date now = new Date();
64  
65          String[] distributionScopeParts = StringUtil.split(
66              ParamUtil.getString(actionRequest, "distributionScope"));
67  
68          long classNameId = 0;
69          long classPK = 0;
70  
71          if (distributionScopeParts.length == 2) {
72              classNameId = GetterUtil.getLong(distributionScopeParts[0]);
73  
74              if (classNameId > 0) {
75                  classPK = GetterUtil.getLong(distributionScopeParts[1]);
76              }
77          }
78  
79          String title = ParamUtil.getString(actionRequest, "title");
80          String content = ParamUtil.getString(actionRequest, "content");
81          String url = ParamUtil.getString(actionRequest, "url");
82          String type = ParamUtil.getString(actionRequest, "type");
83          int priority = ParamUtil.getInteger(actionRequest, "priority");
84          boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
85  
86          AnnouncementsEntry entry = new AnnouncementsEntryImpl();
87  
88          entry.setCompanyId(user.getCompanyId());
89          entry.setUserId(user.getUserId());
90          entry.setUserName(user.getFullName());
91          entry.setCreateDate(now);
92          entry.setModifiedDate(now);
93          entry.setClassNameId(classNameId);
94          entry.setClassPK(classPK);
95          entry.setTitle(title);
96          entry.setContent(content);
97          entry.setUrl(url);
98          entry.setType(type);
99          entry.setDisplayDate(now);
100         entry.setExpirationDate(now);
101         entry.setPriority(priority);
102         entry.setAlert(alert);
103 
104         actionRequest.setAttribute(WebKeys.ANNOUNCEMENTS_ENTRY, entry);
105     }
106 
107     public ActionForward render(
108             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
109             RenderRequest renderRequest, RenderResponse renderResponse)
110         throws Exception {
111 
112         return mapping.findForward(
113             getForward(renderRequest, "portlet.announcements.preview_entry"));
114     }
115 
116 }