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