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.journal.action;
016    
017    import com.liferay.portal.kernel.util.Constants;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.ServiceContextFactory;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.journal.NoSuchArticleException;
030    import com.liferay.portlet.journal.NoSuchFolderException;
031    import com.liferay.portlet.journal.NoSuchStructureException;
032    import com.liferay.portlet.journal.model.JournalArticle;
033    import com.liferay.portlet.journal.model.JournalFeed;
034    import com.liferay.portlet.journal.model.JournalFolder;
035    import com.liferay.portlet.journal.model.JournalFolderConstants;
036    import com.liferay.portlet.journal.model.JournalStructure;
037    import com.liferay.portlet.journal.model.JournalTemplate;
038    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
039    import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
040    import com.liferay.portlet.journal.service.JournalFolderServiceUtil;
041    import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
042    import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
043    import com.liferay.portlet.journal.service.permission.JournalPermission;
044    import com.liferay.portlet.journal.util.JournalUtil;
045    
046    import java.util.ArrayList;
047    import java.util.List;
048    
049    import javax.portlet.ActionRequest;
050    import javax.portlet.PortletRequest;
051    
052    import javax.servlet.http.HttpServletRequest;
053    
054    /**
055     * @author Brian Wing Shun Chan
056     */
057    public class ActionUtil {
058    
059            public static void deleteArticle(
060                            ActionRequest actionRequest, String deleteArticleId)
061                    throws Exception {
062    
063                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
064                            WebKeys.THEME_DISPLAY);
065    
066                    String articleId = deleteArticleId;
067                    String articleURL = ParamUtil.getString(actionRequest, "articleURL");
068                    double version = 0;
069    
070                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
071                            JournalArticle.class.getName(), actionRequest);
072    
073                    int pos = deleteArticleId.lastIndexOf(
074                            EditArticleAction.VERSION_SEPARATOR);
075    
076                    if (pos == -1) {
077                            JournalArticleServiceUtil.deleteArticle(
078                                    themeDisplay.getScopeGroupId(), articleId, articleURL,
079                                    serviceContext);
080                    }
081                    else {
082                            articleId = articleId.substring(0, pos);
083                            version = GetterUtil.getDouble(
084                                    deleteArticleId.substring(
085                                            pos + EditArticleAction.VERSION_SEPARATOR.length()));
086    
087                            JournalArticleServiceUtil.deleteArticle(
088                                    themeDisplay.getScopeGroupId(), articleId, version, articleURL,
089                                    serviceContext);
090                    }
091    
092                    JournalUtil.removeRecentArticle(actionRequest, articleId, version);
093            }
094    
095            public static void expireArticle(
096                            ActionRequest actionRequest, String expireArticleId)
097                    throws Exception {
098    
099                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
100                            WebKeys.THEME_DISPLAY);
101    
102                    String articleId = expireArticleId;
103                    String articleURL = ParamUtil.getString(actionRequest, "articleURL");
104                    double version = 0;
105    
106                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
107                            JournalArticle.class.getName(), actionRequest);
108    
109                    int pos = expireArticleId.lastIndexOf(
110                            EditArticleAction.VERSION_SEPARATOR);
111    
112                    if (pos == -1) {
113                            JournalArticleServiceUtil.expireArticle(
114                                    themeDisplay.getScopeGroupId(), articleId, articleURL,
115                                    serviceContext);
116                    }
117                    else {
118                            articleId = articleId.substring(0, pos);
119                            version = GetterUtil.getDouble(
120                                    expireArticleId.substring(
121                                            pos + EditArticleAction.VERSION_SEPARATOR.length()));
122    
123                            JournalArticleServiceUtil.expireArticle(
124                                    themeDisplay.getScopeGroupId(), articleId, version, articleURL,
125                                    serviceContext);
126                    }
127    
128                    JournalUtil.removeRecentArticle(actionRequest, articleId, version);
129            }
130    
131            public static void expireFolder(
132                            long groupId, long parentFolderId, ServiceContext serviceContext)
133                    throws Exception {
134    
135                    List<JournalFolder> folders = JournalFolderServiceUtil.getFolders(
136                            groupId, parentFolderId);
137    
138                    for (JournalFolder folder : folders) {
139                            expireFolder(groupId, folder.getFolderId(), serviceContext);
140                    }
141    
142                    List<JournalArticle> articles = JournalArticleServiceUtil.getArticles(
143                            groupId, parentFolderId);
144    
145                    for (JournalArticle article : articles) {
146                            JournalArticleServiceUtil.expireArticle(
147                                    groupId, article.getArticleId(), null, serviceContext);
148                    }
149            }
150    
151            public static void getArticle(HttpServletRequest request) throws Exception {
152                    String cmd = ParamUtil.getString(request, Constants.CMD);
153    
154                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
155                            WebKeys.THEME_DISPLAY);
156    
157                    long groupId = themeDisplay.getScopeGroupId();
158                    long classNameId = ParamUtil.getLong(request, "classNameId");
159                    long classPK = ParamUtil.getLong(request, "classPK");
160                    String articleId = ParamUtil.getString(request, "articleId");
161                    String structureId = ParamUtil.getString(request, "structureId");
162    
163                    JournalArticle article = null;
164    
165                    if (!cmd.equals(Constants.ADD) && Validator.isNotNull(articleId)) {
166                            article = JournalArticleServiceUtil.getLatestArticle(
167                                    groupId, articleId, WorkflowConstants.STATUS_ANY);
168                    }
169                    else if ((classNameId > 0) && (classPK > 0)) {
170                            String className = PortalUtil.getClassName(classNameId);
171    
172                            article = JournalArticleServiceUtil.getLatestArticle(
173                                    groupId, className, classPK);
174                    }
175                    else if (Validator.isNotNull(structureId)) {
176                            JournalStructure structure = null;
177    
178                            try {
179                                    structure = JournalStructureServiceUtil.getStructure(
180                                            groupId, structureId);
181                            }
182                            catch (NoSuchStructureException nsse1) {
183                                    if (groupId == themeDisplay.getCompanyGroupId()) {
184                                            return;
185                                    }
186    
187                                    try {
188                                            structure = JournalStructureServiceUtil.getStructure(
189                                                    themeDisplay.getCompanyGroupId(), structureId);
190                                    }
191                                    catch (NoSuchStructureException nsse2) {
192                                            return;
193                                    }
194                            }
195    
196                            article = JournalArticleServiceUtil.getArticle(
197                                    groupId, JournalStructure.class.getName(), structure.getId());
198    
199                            article.setNew(true);
200    
201                            article.setId(0);
202                            article.setClassNameId(0);
203                            article.setClassPK(0);
204                            article.setArticleId(null);
205                            article.setVersion(0);
206                    }
207    
208                    request.setAttribute(WebKeys.JOURNAL_ARTICLE, article);
209            }
210    
211            public static void getArticle(PortletRequest portletRequest)
212                    throws Exception {
213    
214                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
215                            portletRequest);
216    
217                    getArticle(request);
218    
219                    JournalArticle article = (JournalArticle)portletRequest.getAttribute(
220                            WebKeys.JOURNAL_ARTICLE);
221    
222                    JournalUtil.addRecentArticle(portletRequest, article);
223            }
224    
225            public static void getArticles(HttpServletRequest request)
226                    throws Exception {
227    
228                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
229                            WebKeys.THEME_DISPLAY);
230    
231                    List<JournalArticle> articles = new ArrayList<JournalArticle>();
232    
233                    String[] articleIds = StringUtil.split(
234                            ParamUtil.getString(request, "articleIds"));
235    
236                    for (String articleId : articleIds) {
237                            try {
238                                    JournalArticle article = JournalArticleServiceUtil.getArticle(
239                                            themeDisplay.getScopeGroupId(), articleId);
240    
241                                    articles.add(article);
242                            }
243                            catch (NoSuchArticleException nsfee) {
244                            }
245                    }
246    
247                    request.setAttribute(WebKeys.JOURNAL_ARTICLES, articles);
248            }
249    
250            public static void getArticles(PortletRequest portletRequest)
251                    throws Exception {
252    
253                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
254                            portletRequest);
255    
256                    getArticles(request);
257            }
258    
259            public static void getFeed(HttpServletRequest request) throws Exception {
260                    long groupId = ParamUtil.getLong(request, "groupId");
261                    String feedId = ParamUtil.getString(request, "feedId");
262    
263                    JournalFeed feed = null;
264    
265                    if (Validator.isNotNull(feedId)) {
266                            feed = JournalFeedServiceUtil.getFeed(groupId, feedId);
267                    }
268    
269                    request.setAttribute(WebKeys.JOURNAL_FEED, feed);
270            }
271    
272            public static void getFeed(PortletRequest portletRequest) throws Exception {
273                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
274                            portletRequest);
275    
276                    getFeed(request);
277            }
278    
279            public static void getFolder(HttpServletRequest request) throws Exception {
280                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
281                            WebKeys.THEME_DISPLAY);
282    
283                    long folderId = ParamUtil.getLong(request, "folderId");
284    
285                    JournalFolder folder = null;
286    
287                    if ((folderId > 0) &&
288                            (folderId != JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
289    
290                            folder = JournalFolderServiceUtil.getFolder(folderId);
291                    }
292                    else {
293                            JournalPermission.check(
294                                    themeDisplay.getPermissionChecker(),
295                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
296                    }
297    
298                    request.setAttribute(WebKeys.JOURNAL_FOLDER, folder);
299            }
300    
301            public static void getFolder(PortletRequest portletRequest)
302                    throws Exception {
303    
304                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
305                            portletRequest);
306    
307                    getFolder(request);
308            }
309    
310            public static void getFolders(HttpServletRequest request) throws Exception {
311                    long[] folderIds = StringUtil.split(
312                            ParamUtil.getString(request, "folderIds"), 0L);
313    
314                    List<JournalFolder> folders = new ArrayList<JournalFolder>();
315    
316                    for (long folderId : folderIds) {
317                            try {
318                                    JournalFolder folder = JournalFolderServiceUtil.getFolder(
319                                            folderId);
320    
321                                    folders.add(folder);
322                            }
323                            catch (NoSuchFolderException nsfee) {
324                            }
325                    }
326    
327                    request.setAttribute(WebKeys.JOURNAL_FOLDERS, folders);
328            }
329    
330            public static void getFolders(PortletRequest portletRequest)
331                    throws Exception {
332    
333                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
334                            portletRequest);
335    
336                    getFolders(request);
337            }
338    
339            public static void getStructure(HttpServletRequest request)
340                    throws Exception {
341    
342                    long groupId = ParamUtil.getLong(request, "groupId");
343                    String structureId = ParamUtil.getString(request, "structureId");
344    
345                    JournalStructure structure = null;
346    
347                    if (Validator.isNotNull(structureId)) {
348                            structure = JournalStructureServiceUtil.getStructure(
349                                    groupId, structureId);
350                    }
351    
352                    request.setAttribute(WebKeys.JOURNAL_STRUCTURE, structure);
353            }
354    
355            public static void getStructure(PortletRequest portletRequest)
356                    throws Exception {
357    
358                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
359                            portletRequest);
360    
361                    getStructure(request);
362    
363                    JournalStructure structure =
364                            (JournalStructure)portletRequest.getAttribute(
365                                    WebKeys.JOURNAL_STRUCTURE);
366    
367                    JournalUtil.addRecentStructure(portletRequest, structure);
368            }
369    
370            public static void getTemplate(HttpServletRequest request)
371                    throws Exception {
372    
373                    long groupId = ParamUtil.getLong(request, "groupId");
374                    String templateId = ParamUtil.getString(request, "templateId");
375    
376                    JournalTemplate template = null;
377    
378                    if (Validator.isNotNull(templateId)) {
379                            template = JournalTemplateServiceUtil.getTemplate(
380                                    groupId, templateId);
381                    }
382    
383                    request.setAttribute(WebKeys.JOURNAL_TEMPLATE, template);
384            }
385    
386            public static void getTemplate(PortletRequest portletRequest)
387                    throws Exception {
388    
389                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
390                            portletRequest);
391    
392                    getTemplate(request);
393    
394                    JournalTemplate template = (JournalTemplate)portletRequest.getAttribute(
395                            WebKeys.JOURNAL_TEMPLATE);
396    
397                    JournalUtil.addRecentTemplate(portletRequest, template);
398            }
399    
400    }