001    /**
002     * Copyright (c) 2000-2013 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 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    }