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