1
22
23 package com.liferay.portlet.messageboards.util;
24
25 import com.liferay.portal.kernel.language.LanguageUtil;
26 import com.liferay.portal.kernel.portlet.LiferayWindowState;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.Http;
29 import com.liferay.portal.kernel.util.LocaleUtil;
30 import com.liferay.portal.kernel.util.ParamUtil;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.StringUtil;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.model.Group;
35 import com.liferay.portal.model.Organization;
36 import com.liferay.portal.model.Role;
37 import com.liferay.portal.model.UserGroup;
38 import com.liferay.portal.service.GroupLocalServiceUtil;
39 import com.liferay.portal.service.OrganizationLocalServiceUtil;
40 import com.liferay.portal.service.RoleLocalServiceUtil;
41 import com.liferay.portal.service.UserGroupLocalServiceUtil;
42 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
43 import com.liferay.portal.service.UserLocalServiceUtil;
44 import com.liferay.portal.theme.ThemeDisplay;
45 import com.liferay.portal.util.ContentUtil;
46 import com.liferay.portal.util.PropsValues;
47 import com.liferay.portlet.messageboards.model.MBBan;
48 import com.liferay.portlet.messageboards.model.MBCategory;
49 import com.liferay.portlet.messageboards.model.MBMessage;
50 import com.liferay.portlet.messageboards.model.MBStatsUser;
51 import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
52 import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
53 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
54 import com.liferay.util.LocalizationUtil;
55
56 import java.util.Calendar;
57 import java.util.Date;
58
59 import javax.portlet.PortletPreferences;
60 import javax.portlet.PortletURL;
61 import javax.portlet.RenderRequest;
62 import javax.portlet.RenderResponse;
63 import javax.portlet.WindowState;
64
65 import javax.servlet.jsp.PageContext;
66
67 import org.apache.commons.logging.Log;
68 import org.apache.commons.logging.LogFactory;
69
70
76 public class MBUtil {
77
78 public static final String POP_PORTLET_PREFIX = "mb.";
79
80 public static String getBreadcrumbs(
81 long categoryId, long messageId, PageContext pageContext,
82 RenderRequest renderRequest, RenderResponse renderResponse)
83 throws Exception {
84
85 if (messageId > 0) {
86 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
87
88 return getBreadcrumbs(
89 null, message, pageContext, renderRequest, renderResponse);
90 }
91 else {
92 MBCategory category = null;
93
94 try {
95 if ((categoryId > 0) &&
96 (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
97
98 category = MBCategoryLocalServiceUtil.getCategory(
99 categoryId);
100 }
101 }
102 catch (Exception e) {
103 }
104
105 return getBreadcrumbs(
106 category, null, pageContext, renderRequest, renderResponse);
107 }
108 }
109
110 public static String getBreadcrumbs(
111 MBCategory category, MBMessage message, PageContext pageContext,
112 RenderRequest renderRequest, RenderResponse renderResponse)
113 throws Exception {
114
115 String strutsAction = ParamUtil.getString(
116 renderRequest, "struts_action");
117
118 boolean selectCategory = strutsAction.equals(
119 "/message_boards/select_category");
120
121 if ((message != null) && (category == null)) {
122 category = message.getCategory();
123 }
124
125 PortletURL categoriesURL = renderResponse.createRenderURL();
126
127 if (selectCategory) {
128 categoriesURL.setWindowState(LiferayWindowState.POP_UP);
129
130 categoriesURL.setParameter(
131 "struts_action", "/message_boards/select_category");
132 }
133 else {
134 categoriesURL.setWindowState(WindowState.MAXIMIZED);
135
136 categoriesURL.setParameter("struts_action", "/message_boards/view");
137 categoriesURL.setParameter(
138 "categoryId",
139 String.valueOf(MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID));
140 }
141
142 String categoriesLink =
143 "<a href=\"" + categoriesURL.toString() + "\">" +
144 LanguageUtil.get(pageContext, "categories") + "</a>";
145
146 if (category == null) {
147 return categoriesLink;
148 }
149
150 String breadcrumbs = StringPool.BLANK;
151
152 if (category != null) {
153 for (int i = 0;; i++) {
154 category = category.toEscapedModel();
155
156 PortletURL portletURL = renderResponse.createRenderURL();
157
158 if (selectCategory) {
159 portletURL.setWindowState(LiferayWindowState.POP_UP);
160
161 portletURL.setParameter(
162 "struts_action", "/message_boards/select_category");
163 portletURL.setParameter(
164 "categoryId", String.valueOf(category.getCategoryId()));
165 }
166 else {
167 portletURL.setWindowState(WindowState.MAXIMIZED);
168
169 portletURL.setParameter(
170 "struts_action", "/message_boards/view");
171 portletURL.setParameter(
172 "categoryId", String.valueOf(category.getCategoryId()));
173 }
174
175 String categoryLink =
176 "<a href=\"" + portletURL.toString() + "\">" +
177 category.getName() + "</a>";
178
179 if (i == 0) {
180 breadcrumbs = categoryLink;
181 }
182 else {
183 breadcrumbs = categoryLink + " » " + breadcrumbs;
184 }
185
186 if (category.isRoot()) {
187 break;
188 }
189
190 category = MBCategoryLocalServiceUtil.getCategory(
191 category.getParentCategoryId());
192 }
193 }
194
195 breadcrumbs = categoriesLink + " » " + breadcrumbs;
196
197 if (message != null) {
198 message = message.toEscapedModel();
199
200 PortletURL messageURL = renderResponse.createRenderURL();
201
202 messageURL.setWindowState(WindowState.MAXIMIZED);
203
204 messageURL.setParameter(
205 "struts_action", "/message_boards/view_message");
206 messageURL.setParameter(
207 "messageId", String.valueOf(message.getMessageId()));
208
209 String messageLink =
210 "<a href=\"" + messageURL.toString() + "\">" +
211 message.getSubject() + "</a>";
212
213 breadcrumbs = breadcrumbs + " » " + messageLink;
214 }
215
216 return breadcrumbs;
217 }
218
219 public static String getEmailFromAddress(PortletPreferences prefs) {
220 String emailFromAddress = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS;
221
222 return prefs.getValue("email-from-address", emailFromAddress);
223 }
224
225 public static String getEmailFromName(PortletPreferences prefs) {
226 String emailFromName = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_NAME;
227
228 return prefs.getValue("email-from-name", emailFromName);
229 }
230
231 public static boolean getEmailHtmlFormat(PortletPreferences prefs) {
232 String emailHtmlFormat = prefs.getValue(
233 "email-html-format", StringPool.BLANK);
234
235 if (Validator.isNotNull(emailHtmlFormat)) {
236 return GetterUtil.getBoolean(emailHtmlFormat);
237 }
238 else {
239 return PropsValues.MESSAGE_BOARDS_EMAIL_HTML_FORMAT;
240 }
241 }
242
243 public static boolean getEmailMessageAddedEnabled(
244 PortletPreferences prefs) {
245
246 String emailMessageAddedEnabled = prefs.getValue(
247 "email-message-added-enabled", StringPool.BLANK);
248
249 if (Validator.isNotNull(emailMessageAddedEnabled)) {
250 return GetterUtil.getBoolean(emailMessageAddedEnabled);
251 }
252 else {
253 return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED;
254 }
255 }
256
257 public static String getEmailMessageAddedBody(PortletPreferences prefs) {
258 String emailMessageAddedBody = prefs.getValue(
259 "email-message-added-body", StringPool.BLANK);
260
261 if (Validator.isNotNull(emailMessageAddedBody)) {
262 return emailMessageAddedBody;
263 }
264 else {
265 return ContentUtil.get(
266 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY);
267 }
268 }
269
270 public static String getEmailMessageAddedSignature(
271 PortletPreferences prefs) {
272
273 String emailMessageAddedSignature = prefs.getValue(
274 "email-message-added-signature", StringPool.BLANK);
275
276 if (Validator.isNotNull(emailMessageAddedSignature)) {
277 return emailMessageAddedSignature;
278 }
279 else {
280 return ContentUtil.get(
281 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SIGNATURE);
282 }
283 }
284
285 public static String getEmailMessageAddedSubjectPrefix(
286 PortletPreferences prefs) {
287
288 String emailMessageAddedSubjectPrefix = prefs.getValue(
289 "email-message-added-subject-prefix", StringPool.BLANK);
290
291 if (Validator.isNotNull(emailMessageAddedSubjectPrefix)) {
292 return emailMessageAddedSubjectPrefix;
293 }
294 else {
295 return ContentUtil.get(
296 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT_PREFIX);
297 }
298 }
299
300 public static boolean getEmailMessageUpdatedEnabled(
301 PortletPreferences prefs) {
302
303 String emailMessageUpdatedEnabled = prefs.getValue(
304 "email-message-updated-enabled", StringPool.BLANK);
305
306 if (Validator.isNotNull(emailMessageUpdatedEnabled)) {
307 return GetterUtil.getBoolean(emailMessageUpdatedEnabled);
308 }
309 else {
310 return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED;
311 }
312 }
313
314 public static String getEmailMessageUpdatedBody(PortletPreferences prefs) {
315 String emailMessageUpdatedBody = prefs.getValue(
316 "email-message-updated-body", StringPool.BLANK);
317
318 if (Validator.isNotNull(emailMessageUpdatedBody)) {
319 return emailMessageUpdatedBody;
320 }
321 else {
322 return ContentUtil.get(
323 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY);
324 }
325 }
326
327 public static String getEmailMessageUpdatedSignature(
328 PortletPreferences prefs) {
329
330 String emailMessageUpdatedSignature = prefs.getValue(
331 "email-message-updated-signature", StringPool.BLANK);
332
333 if (Validator.isNotNull(emailMessageUpdatedSignature)) {
334 return emailMessageUpdatedSignature;
335 }
336 else {
337 return ContentUtil.get(
338 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SIGNATURE);
339 }
340 }
341
342 public static String getEmailMessageUpdatedSubjectPrefix(
343 PortletPreferences prefs) {
344
345 String emailMessageUpdatedSubject = prefs.getValue(
346 "email-message-updated-subject-prefix", StringPool.BLANK);
347
348 if (Validator.isNotNull(emailMessageUpdatedSubject)) {
349 return emailMessageUpdatedSubject;
350 }
351 else {
352 return ContentUtil.get(
353 PropsValues.
354 MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT_PREFIX);
355 }
356 }
357
358 public static String getMailId(String mx, long categoryId, long messageId) {
359 StringBuilder sb = new StringBuilder();
360
361 sb.append(StringPool.LESS_THAN);
362 sb.append(POP_PORTLET_PREFIX);
363 sb.append(categoryId);
364 sb.append(StringPool.PERIOD);
365 sb.append(messageId);
366 sb.append(StringPool.AT);
367 sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
368 sb.append(StringPool.PERIOD);
369 sb.append(mx);
370 sb.append(StringPool.GREATER_THAN);
371
372 return sb.toString();
373 }
374
375 public static String getMailingListAddress(
376 long categoryId, long messageId, String mx) {
377
378 StringBuilder sb = new StringBuilder();
379
380 sb.append(POP_PORTLET_PREFIX);
381 sb.append(categoryId);
382 sb.append(StringPool.PERIOD);
383 sb.append(messageId);
384 sb.append(StringPool.AT);
385 sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
386 sb.append(StringPool.PERIOD);
387 sb.append(mx);
388
389 return sb.toString();
390 }
391
392 public static long getMessageId(String mailId) {
393 int x = mailId.indexOf(StringPool.LESS_THAN) + 1;
394 int y = mailId.indexOf(StringPool.AT);
395
396 long messageId = 0;
397
398 if ((x > 0 ) && (y != -1)) {
399 String temp = mailId.substring(x, y);
400
401 int z = temp.indexOf(StringPool.PERIOD);
402
403 if (z != -1) {
404 messageId = GetterUtil.getLong(temp.substring(z));
405 }
406 }
407
408 return messageId;
409 }
410
411 public static String[] getThreadPriority(
412 PortletPreferences prefs, String languageId, double value,
413 ThemeDisplay themeDisplay)
414 throws Exception {
415
416 String[] priorities = LocalizationUtil.getPrefsValues(
417 prefs, "priorities", languageId);
418
419 String[] priorityPair = _findThreadPriority(
420 value, themeDisplay, priorities);
421
422 if (priorityPair == null) {
423 String defaultLanguageId = LocaleUtil.toLanguageId(
424 LocaleUtil.getDefault());
425
426 priorities = LocalizationUtil.getPrefsValues(
427 prefs, "priorities", defaultLanguageId);
428
429 priorityPair = _findThreadPriority(value, themeDisplay, priorities);
430 }
431
432 return priorityPair;
433 }
434
435 public static Date getUnbanDate(MBBan ban, int expireInterval) {
436 Date banDate = ban.getCreateDate();
437
438 Calendar cal = Calendar.getInstance();
439
440 cal.setTime(banDate);
441
442 cal.add(Calendar.DATE, expireInterval);
443
444 return cal.getTime();
445 }
446
447 public static String getUserRank(
448 PortletPreferences prefs, String languageId, int posts)
449 throws Exception {
450
451 String rank = StringPool.BLANK;
452
453 String[] ranks = LocalizationUtil.getPrefsValues(
454 prefs, "ranks", languageId);
455
456 for (int i = 0; i < ranks.length; i++) {
457 String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
458
459 String kvpName = kvp[0];
460 int kvpPosts = GetterUtil.getInteger(kvp[1]);
461
462 if (posts >= kvpPosts) {
463 rank = kvpName;
464 }
465 else {
466 break;
467 }
468 }
469
470 return rank;
471 }
472
473 public static String getUserRank(
474 PortletPreferences prefs, String languageId, MBStatsUser statsUser)
475 throws Exception {
476
477 String rank = StringPool.BLANK;
478
479 Group group = GroupLocalServiceUtil.getGroup(
480 statsUser.getGroupId());
481
482 long companyId = group.getCompanyId();
483
484 String[] ranks = LocalizationUtil.getPrefsValues(
485 prefs, "ranks", languageId);
486
487 for (int i = 0; i < ranks.length; i++) {
488 String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
489
490 String curRank = kvp[0];
491 String curRankValue = kvp[1];
492
493 String[] curRankValueKvp = StringUtil.split(
494 curRankValue, StringPool.COLON);
495
496 if (curRankValueKvp.length <= 1) {
497 int kvpPosts = GetterUtil.getInteger(curRankValue);
498
499 if (statsUser.getMessageCount() >= kvpPosts) {
500 rank = curRank;
501 }
502
503 continue;
504 }
505
506 String entityType = curRankValueKvp[0];
507 String entityValue = curRankValueKvp[1];
508
509 try {
510 if (_isEntityRank(
511 companyId, statsUser, entityType, entityValue)) {
512
513 return curRank;
514 }
515 }
516 catch (Exception e) {
517 if (_log.isWarnEnabled()) {
518 _log.warn(e);
519 }
520 }
521 }
522
523 return rank;
524 }
525
526 public static boolean isAllowAnonymousPosting(PortletPreferences prefs) {
527 String allowAnonymousPosting = prefs.getValue(
528 "allow-anonymous-posting", StringPool.BLANK);
529
530 if (Validator.isNotNull(allowAnonymousPosting)) {
531 return GetterUtil.getBoolean(allowAnonymousPosting);
532 }
533 else {
534 return PropsValues.MESSAGE_BOARDS_ANONYMOUS_POSTING_ENABLED;
535 }
536 }
537
538 private static String[] _findThreadPriority(
539 double value, ThemeDisplay themeDisplay, String[] priorities) {
540
541 for (int i = 0; i < priorities.length; i++) {
542 String[] priority = StringUtil.split(priorities[i]);
543
544 try {
545 String priorityName = priority[0];
546 String priorityImage = priority[1];
547 double priorityValue = GetterUtil.getDouble(priority[2]);
548
549 if (value == priorityValue) {
550 if (!priorityImage.startsWith(Http.HTTP)) {
551 priorityImage =
552 themeDisplay.getPathThemeImages() + priorityImage;
553 }
554
555 return new String[] {priorityName, priorityImage};
556 }
557 }
558 catch (Exception e) {
559 }
560 }
561
562 return null;
563 }
564
565 private static boolean _isEntityRank(
566 long companyId, MBStatsUser statsUser, String entityType,
567 String entityValue)
568 throws Exception {
569
570 long groupId = statsUser.getGroupId();
571 long userId = statsUser.getUserId();
572
573 if (entityType.equals("community-role") ||
574 entityType.equals("organization-role")) {
575
576 Role role = RoleLocalServiceUtil.getRole(companyId, entityValue);
577
578 if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
579 userId, groupId, role.getRoleId())) {
580
581 return true;
582 }
583 }
584 else if (entityType.equals("organization")) {
585 Organization organization =
586 OrganizationLocalServiceUtil.getOrganization(
587 companyId, entityValue);
588
589 if (OrganizationLocalServiceUtil.hasUserOrganization(
590 userId, organization.getOrganizationId())) {
591
592 return true;
593 }
594 }
595 else if (entityType.equals("regular-role")) {
596 if (RoleLocalServiceUtil.hasUserRole(
597 userId, companyId, entityValue, true)) {
598
599 return true;
600 }
601 }
602 else if (entityType.equals("user-group")) {
603 UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
604 companyId, entityValue);
605
606 if (UserLocalServiceUtil.hasUserGroupUser(
607 userGroup.getUserGroupId(), userId)) {
608
609 return true;
610 }
611 }
612
613 return false;
614 }
615
616 private static Log _log = LogFactory.getLog(MBUtil.class);
617
618 }