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