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