001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 actionMapping, ActionForm actionForm,
047                            PortletConfig portletConfig, ActionRequest actionRequest,
048                            ActionResponse actionResponse)
049                    throws Exception {
050    
051                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
052                            WebKeys.THEME_DISPLAY);
053    
054                    User user = themeDisplay.getUser();
055                    Date now = new Date();
056    
057                    String[] distributionScopeParts = StringUtil.split(
058                            ParamUtil.getString(actionRequest, "distributionScope"));
059    
060                    long classNameId = 0;
061                    long classPK = 0;
062    
063                    if (distributionScopeParts.length == 2) {
064                            classNameId = GetterUtil.getLong(distributionScopeParts[0]);
065    
066                            if (classNameId > 0) {
067                                    classPK = GetterUtil.getLong(distributionScopeParts[1]);
068                            }
069                    }
070    
071                    String title = ParamUtil.getString(actionRequest, "title");
072                    String content = ParamUtil.getString(actionRequest, "content");
073                    String url = ParamUtil.getString(actionRequest, "url");
074                    String type = ParamUtil.getString(actionRequest, "type");
075                    int priority = ParamUtil.getInteger(actionRequest, "priority");
076                    boolean alert = ParamUtil.getBoolean(actionRequest, "alert");
077    
078                    AnnouncementsEntry entry = new AnnouncementsEntryImpl();
079    
080                    entry.setCompanyId(user.getCompanyId());
081                    entry.setUserId(user.getUserId());
082                    entry.setUserName(user.getFullName());
083                    entry.setCreateDate(now);
084                    entry.setModifiedDate(now);
085                    entry.setClassNameId(classNameId);
086                    entry.setClassPK(classPK);
087                    entry.setTitle(title);
088                    entry.setContent(content);
089                    entry.setUrl(url);
090                    entry.setType(type);
091                    entry.setDisplayDate(now);
092                    entry.setExpirationDate(now);
093                    entry.setPriority(priority);
094                    entry.setAlert(alert);
095    
096                    actionRequest.setAttribute(WebKeys.ANNOUNCEMENTS_ENTRY, entry);
097            }
098    
099            @Override
100            public ActionForward render(
101                            ActionMapping actionMapping, ActionForm actionForm,
102                            PortletConfig portletConfig, RenderRequest renderRequest,
103                            RenderResponse renderResponse)
104                    throws Exception {
105    
106                    return actionMapping.findForward(
107                            getForward(renderRequest, "portlet.announcements.preview_entry"));
108            }
109    
110    }