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