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 = ParamUtil.getLong(
160                            request, "groupId", themeDisplay.getScopeGroupId());
161                    long classNameId = ParamUtil.getLong(request, "classNameId");
162                    long classPK = ParamUtil.getLong(request, "classPK");
163                    String articleId = ParamUtil.getString(request, "articleId");
164                    String structureId = ParamUtil.getString(request, "structureId");
165    
166                    JournalArticle article = null;
167    
168                    if (!cmd.equals(Constants.ADD) && Validator.isNotNull(articleId)) {
169                            article = JournalArticleServiceUtil.getLatestArticle(
170                                    groupId, articleId, WorkflowConstants.STATUS_ANY);
171                    }
172                    else if ((classNameId > 0) &&
173                                     (classPK > JournalArticleConstants.CLASSNAME_ID_DEFAULT)) {
174    
175                            String className = PortalUtil.getClassName(classNameId);
176    
177                            article = JournalArticleServiceUtil.getLatestArticle(
178                                    groupId, className, classPK);
179                    }
180                    else if (Validator.isNotNull(structureId)) {
181                            DDMStructure ddmStructure = null;
182    
183                            try {
184                                    ddmStructure = DDMStructureServiceUtil.getStructure(
185                                            groupId, PortalUtil.getClassNameId(JournalArticle.class),
186                                            structureId);
187                            }
188                            catch (NoSuchStructureException nsse1) {
189                                    if (groupId == themeDisplay.getCompanyGroupId()) {
190                                            return;
191                                    }
192    
193                                    try {
194                                            ddmStructure = DDMStructureServiceUtil.getStructure(
195                                                    themeDisplay.getCompanyGroupId(),
196                                                    PortalUtil.getClassNameId(JournalArticle.class),
197                                                    structureId);
198                                    }
199                                    catch (NoSuchStructureException nsse2) {
200                                            return;
201                                    }
202                            }
203    
204                            article = JournalArticleServiceUtil.getArticle(
205                                    groupId, DDMStructure.class.getName(),
206                                    ddmStructure.getStructureId());
207    
208                            article.setNew(true);
209    
210                            article.setId(0);
211                            article.setClassNameId(
212                                    JournalArticleConstants.CLASSNAME_ID_DEFAULT);
213                            article.setClassPK(0);
214                            article.setArticleId(null);
215                            article.setVersion(0);
216                    }
217    
218                    request.setAttribute(WebKeys.JOURNAL_ARTICLE, article);
219            }
220    
221            public static void getArticle(PortletRequest portletRequest)
222                    throws Exception {
223    
224                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
225                            portletRequest);
226    
227                    getArticle(request);
228    
229                    JournalArticle article = (JournalArticle)portletRequest.getAttribute(
230                            WebKeys.JOURNAL_ARTICLE);
231    
232                    JournalUtil.addRecentArticle(portletRequest, article);
233            }
234    
235            public static void getArticles(HttpServletRequest request)
236                    throws Exception {
237    
238                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
239                            WebKeys.THEME_DISPLAY);
240    
241                    List<JournalArticle> articles = new ArrayList<JournalArticle>();
242    
243                    String[] articleIds = StringUtil.split(
244                            ParamUtil.getString(request, "articleIds"));
245    
246                    for (String articleId : articleIds) {
247                            try {
248                                    JournalArticle article = JournalArticleServiceUtil.getArticle(
249                                            themeDisplay.getScopeGroupId(), articleId);
250    
251                                    articles.add(article);
252                            }
253                            catch (NoSuchArticleException nsfee) {
254                            }
255                    }
256    
257                    request.setAttribute(WebKeys.JOURNAL_ARTICLES, articles);
258            }
259    
260            public static void getArticles(PortletRequest portletRequest)
261                    throws Exception {
262    
263                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
264                            portletRequest);
265    
266                    getArticles(request);
267            }
268    
269            public static void getFeed(HttpServletRequest request) throws Exception {
270                    long groupId = ParamUtil.getLong(request, "groupId");
271                    String feedId = ParamUtil.getString(request, "feedId");
272    
273                    JournalFeed feed = null;
274    
275                    if (Validator.isNotNull(feedId)) {
276                            feed = JournalFeedServiceUtil.getFeed(groupId, feedId);
277                    }
278    
279                    request.setAttribute(WebKeys.JOURNAL_FEED, feed);
280            }
281    
282            public static void getFeed(PortletRequest portletRequest) throws Exception {
283                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
284                            portletRequest);
285    
286                    getFeed(request);
287            }
288    
289            public static void getFolder(HttpServletRequest request) throws Exception {
290                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
291                            WebKeys.THEME_DISPLAY);
292    
293                    long folderId = ParamUtil.getLong(request, "folderId");
294    
295                    JournalFolder folder = null;
296    
297                    if ((folderId > 0) &&
298                            (folderId != JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
299    
300                            folder = JournalFolderServiceUtil.getFolder(folderId);
301                    }
302                    else {
303                            JournalPermission.check(
304                                    themeDisplay.getPermissionChecker(),
305                                    themeDisplay.getScopeGroupId(), ActionKeys.VIEW);
306                    }
307    
308                    request.setAttribute(WebKeys.JOURNAL_FOLDER, folder);
309            }
310    
311            public static void getFolder(PortletRequest portletRequest)
312                    throws Exception {
313    
314                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
315                            portletRequest);
316    
317                    getFolder(request);
318            }
319    
320            public static void getFolders(HttpServletRequest request) throws Exception {
321                    long[] folderIds = StringUtil.split(
322                            ParamUtil.getString(request, "folderIds"), 0L);
323    
324                    List<JournalFolder> folders = new ArrayList<JournalFolder>();
325    
326                    for (long folderId : folderIds) {
327                            try {
328                                    JournalFolder folder = JournalFolderServiceUtil.getFolder(
329                                            folderId);
330    
331                                    folders.add(folder);
332                            }
333                            catch (NoSuchFolderException nsfee) {
334                            }
335                    }
336    
337                    request.setAttribute(WebKeys.JOURNAL_FOLDERS, folders);
338            }
339    
340            public static void getFolders(PortletRequest portletRequest)
341                    throws Exception {
342    
343                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
344                            portletRequest);
345    
346                    getFolders(request);
347            }
348    
349            public static void getStructure(HttpServletRequest request)
350                    throws Exception {
351    
352                    long groupId = ParamUtil.getLong(request, "groupId");
353                    long classNameId = ParamUtil.getLong(request, "classNameId");
354                    String structureId = ParamUtil.getString(request, "structureId");
355    
356                    DDMStructure ddmStructure = null;
357    
358                    if (Validator.isNotNull(structureId)) {
359                            ddmStructure = DDMStructureServiceUtil.getStructure(
360                                    groupId, classNameId, structureId);
361                    }
362    
363                    request.setAttribute(WebKeys.JOURNAL_STRUCTURE, ddmStructure);
364            }
365    
366            public static void getStructure(PortletRequest portletRequest)
367                    throws Exception {
368    
369                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
370                            portletRequest);
371    
372                    getStructure(request);
373    
374                    DDMStructure ddmStructure = (DDMStructure)portletRequest.getAttribute(
375                            WebKeys.JOURNAL_STRUCTURE);
376    
377                    JournalUtil.addRecentDDMStructure(portletRequest, ddmStructure);
378            }
379    
380            public static void getTemplate(HttpServletRequest request)
381                    throws Exception {
382    
383                    long groupId = ParamUtil.getLong(request, "groupId");
384                    String templateId = ParamUtil.getString(request, "templateId");
385    
386                    DDMTemplate ddmTemplate = null;
387    
388                    if (Validator.isNotNull(templateId)) {
389                            ddmTemplate = DDMTemplateServiceUtil.getTemplate(
390                                    groupId, PortalUtil.getClassNameId(DDMStructure.class),
391                                    templateId);
392                    }
393    
394                    request.setAttribute(WebKeys.JOURNAL_TEMPLATE, ddmTemplate);
395            }
396    
397            public static void getTemplate(PortletRequest portletRequest)
398                    throws Exception {
399    
400                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
401                            portletRequest);
402    
403                    getTemplate(request);
404    
405                    DDMTemplate ddmTemplate = (DDMTemplate)portletRequest.getAttribute(
406                            WebKeys.JOURNAL_TEMPLATE);
407    
408                    JournalUtil.addRecentDDMTemplate(portletRequest, ddmTemplate);
409            }
410    
411            protected static boolean hasArticle(ActionRequest actionRequest)
412                    throws Exception {
413    
414                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
415                            WebKeys.THEME_DISPLAY);
416    
417                    String articleId = ParamUtil.getString(actionRequest, "articleId");
418    
419                    if (Validator.isNull(articleId)) {
420                            String[] articleIds = StringUtil.split(
421                                    ParamUtil.getString(actionRequest, "articleIds"));
422    
423                            if (articleIds.length <= 0) {
424                                    return false;
425                            }
426    
427                            articleId = articleIds[0];
428                    }
429    
430                    int pos = articleId.lastIndexOf(EditArticleAction.VERSION_SEPARATOR);
431    
432                    if (pos != -1) {
433                            articleId = articleId.substring(0, pos);
434                    }
435    
436                    try {
437                            JournalArticleLocalServiceUtil.getArticle(
438                                    themeDisplay.getScopeGroupId(), articleId);
439                    }
440                    catch (NoSuchArticleException nsae) {
441                            return false;
442                    }
443    
444                    return true;
445            }
446    
447    }