001    /**
002     * Copyright (c) 2000-2012 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.util.GetterUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.struts.PortletAction;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.WebKeys;
024    import com.liferay.portlet.announcements.model.AnnouncementsEntry;
025    import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
026    
027    import java.util.Date;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.ActionResponse;
031    import javax.portlet.PortletConfig;
032    import javax.portlet.RenderRequest;
033    import javax.portlet.RenderResponse;
034    
035    import org.apache.struts.action.ActionForm;
036    import org.apache.struts.action.ActionForward;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author David Truong
041     */
042    public class PreviewEntryAction extends PortletAction {
043    
044            @Override
045            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
051                            WebKeys.THEME_DISPLAY);
052    
053                    User user = themeDisplay.getUser();
054                    Date now = new Date();
055    
056                    String[] distributionScopeParts = StringUtil.split(
057                            ParamUtil.getString(actionRequest, "distributionScope"));
058    
059                    long classNameId = 0;
060                    long classPK = 0;
061    
062                    if (distributionScopeParts.length == 2) {
063                            classNameId = GetterUtil.getLong(distributionScopeParts[0]);
064    
065                            if (classNameId > 0) {
066                                    classPK = GetterUtil.getLong(distributionScopeParts[1]);
067                            }
068                    }
069    
070                    String title = ParamUtil.getString(actionRequest, "title");
071                    String content = ParamUtil.getString(actionRequest, "content");
072                    String url = ParamUtil.getString(actionRequest, "url");
073                    String type = ParamUtil.getString(actionRequest, "type");
074                    int priority = ParamUtil.getInteger(actionRequest, "priority");
075                    boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
076    
077                    AnnouncementsEntry entry = new AnnouncementsEntryImpl();
078    
079                    entry.setCompanyId(user.getCompanyId());
080                    entry.setUserId(user.getUserId());
081                    entry.setUserName(user.getFullName());
082                    entry.setCreateDate(now);
083                    entry.setModifiedDate(now);
084                    entry.setClassNameId(classNameId);
085                    entry.setClassPK(classPK);
086                    entry.setTitle(title);
087                    entry.setContent(content);
088                    entry.setUrl(url);
089                    entry.setType(type);
090                    entry.setDisplayDate(now);
091                    entry.setExpirationDate(now);
092                    entry.setPriority(priority);
093                    entry.setAlert(alert);
094    
095                    actionRequest.setAttribute(WebKeys.ANNOUNCEMENTS_ENTRY, entry);
096            }
097    
098            @Override
099            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    }