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