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