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