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