1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.messageboards.util;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.portlet.LiferayWindowState;
21  import com.liferay.portal.kernel.util.GetterUtil;
22  import com.liferay.portal.kernel.util.Http;
23  import com.liferay.portal.kernel.util.LocaleUtil;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.StringBundler;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.model.Organization;
31  import com.liferay.portal.model.Role;
32  import com.liferay.portal.model.UserGroup;
33  import com.liferay.portal.service.GroupLocalServiceUtil;
34  import com.liferay.portal.service.OrganizationLocalServiceUtil;
35  import com.liferay.portal.service.RoleLocalServiceUtil;
36  import com.liferay.portal.service.UserGroupLocalServiceUtil;
37  import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
38  import com.liferay.portal.service.UserLocalServiceUtil;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.ContentUtil;
41  import com.liferay.portal.util.PropsValues;
42  import com.liferay.portlet.messageboards.model.MBBan;
43  import com.liferay.portlet.messageboards.model.MBCategory;
44  import com.liferay.portlet.messageboards.model.MBMessage;
45  import com.liferay.portlet.messageboards.model.MBStatsUser;
46  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
47  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
48  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
49  import com.liferay.util.LocalizationUtil;
50  import com.liferay.util.mail.JavaMailUtil;
51  
52  import java.io.InputStream;
53  
54  import java.util.Calendar;
55  import java.util.Date;
56  
57  import javax.mail.BodyPart;
58  import javax.mail.Message;
59  import javax.mail.Part;
60  import javax.mail.internet.MimeMessage;
61  import javax.mail.internet.MimeMultipart;
62  
63  import javax.portlet.PortletPreferences;
64  import javax.portlet.PortletURL;
65  import javax.portlet.RenderRequest;
66  import javax.portlet.RenderResponse;
67  
68  import javax.servlet.jsp.PageContext;
69  
70  /**
71   * <a href="MBUtil.java.html"><b><i>View Source</i></b></a>
72   *
73   * @author Brian Wing Shun Chan
74   */
75  public class MBUtil {
76  
77      public static final String POP_PORTLET_PREFIX = "mb.";
78  
79      public static final int POP_SERVER_SUBDOMAIN_LENGTH =
80          PropsValues.POP_SERVER_SUBDOMAIN.length();
81  
82      public static void collectMultipartContent(
83              MimeMultipart multipart, MBMailMessage collector)
84          throws Exception {
85  
86          for (int i = 0; i < multipart.getCount(); i++) {
87              BodyPart part = multipart.getBodyPart(i);
88  
89              collectPartContent(part, collector);
90          }
91      }
92  
93      public static void collectPartContent(Part part, MBMailMessage collector)
94          throws Exception {
95  
96          Object partContent = part.getContent();
97  
98          String contentType = part.getContentType().toLowerCase();
99  
100         if ((part.getDisposition() != null) &&
101              (part.getDisposition().equalsIgnoreCase(MimeMessage.ATTACHMENT))) {
102 
103             if (_log.isDebugEnabled()) {
104                 _log.debug("Processing attachment");
105             }
106 
107             byte[] bytes = null;
108 
109             if (partContent instanceof String) {
110                 bytes = ((String)partContent).getBytes();
111             }
112             else if (partContent instanceof InputStream) {
113                 bytes = JavaMailUtil.getBytes(part);
114             }
115 
116             collector.addFile(part.getFileName(), bytes);
117         }
118         else {
119             if (partContent instanceof MimeMultipart) {
120                 collectMultipartContent((MimeMultipart)partContent, collector);
121             }
122             else if (partContent instanceof String) {
123                 if (contentType.startsWith("text/html")) {
124                     collector.setHtmlBody((String)partContent);
125                 }
126                 else {
127                     collector.setPlainBody((String)partContent);
128                 }
129             }
130         }
131     }
132 
133     public static String getBreadcrumbs(
134             long categoryId, long messageId, PageContext pageContext,
135             RenderRequest renderRequest, RenderResponse renderResponse)
136         throws Exception {
137 
138         if (messageId > 0) {
139             MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
140 
141             return getBreadcrumbs(
142                 null, message, pageContext, renderRequest, renderResponse);
143         }
144         else {
145             MBCategory category = null;
146 
147             try {
148                 if ((categoryId > 0) &&
149                     (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
150 
151                     category = MBCategoryLocalServiceUtil.getCategory(
152                         categoryId);
153                 }
154             }
155             catch (Exception e) {
156                 _log.error("Unable to retrieve category " + categoryId, e);
157             }
158 
159             return getBreadcrumbs(
160                 category, null, pageContext, renderRequest, renderResponse);
161         }
162     }
163 
164     public static String getBreadcrumbs(
165             MBCategory category, MBMessage message, PageContext pageContext,
166             RenderRequest renderRequest, RenderResponse renderResponse)
167         throws Exception {
168 
169         String strutsAction = ParamUtil.getString(
170             renderRequest, "struts_action");
171 
172         boolean selectCategory = strutsAction.equals(
173             "/message_boards/select_category");
174 
175         if ((message != null) && (category == null)) {
176             category = message.getCategory();
177         }
178 
179         PortletURL categoriesURL = renderResponse.createRenderURL();
180 
181         if (selectCategory) {
182             categoriesURL.setWindowState(LiferayWindowState.POP_UP);
183 
184             categoriesURL.setParameter(
185                 "struts_action", "/message_boards/select_category");
186         }
187         else {
188             //categoriesURL.setWindowState(WindowState.MAXIMIZED);
189 
190             categoriesURL.setParameter("struts_action", "/message_boards/view");
191             categoriesURL.setParameter(
192                 "categoryId",
193                 String.valueOf(MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID));
194         }
195 
196         String categoriesLink =
197             "<a href=\"" + categoriesURL.toString() + "\">" +
198                 LanguageUtil.get(pageContext, "categories") + "</a>";
199 
200         if (category == null) {
201             return "<span class=\"first last\">" + categoriesLink + "</span>";
202         }
203 
204         String breadcrumbs = StringPool.BLANK;
205 
206         for (int i = 0;; i++) {
207             category = category.toEscapedModel();
208 
209             PortletURL portletURL = renderResponse.createRenderURL();
210 
211             if (selectCategory) {
212                 portletURL.setWindowState(LiferayWindowState.POP_UP);
213 
214                 portletURL.setParameter(
215                     "struts_action", "/message_boards/select_category");
216                 portletURL.setParameter(
217                     "categoryId", String.valueOf(category.getCategoryId()));
218             }
219             else {
220                 //portletURL.setWindowState(WindowState.MAXIMIZED);
221 
222                 portletURL.setParameter(
223                     "struts_action", "/message_boards/view");
224                 portletURL.setParameter(
225                     "categoryId", String.valueOf(category.getCategoryId()));
226             }
227 
228             String categoryLink =
229                 "<a href=\"" + portletURL.toString() + "\">" +
230                     category.getName() + "</a>";
231 
232             if (i == 0) {
233                 if (message != null) {
234                     breadcrumbs += categoryLink;
235                 }
236                 else {
237                     breadcrumbs =
238                         "<span class=\"last\">" + categoryLink + "</span>";
239                 }
240             }
241             else {
242                 breadcrumbs = categoryLink + " &raquo; " + breadcrumbs;
243             }
244 
245             if (category.isRoot()) {
246                 break;
247             }
248 
249             category = MBCategoryLocalServiceUtil.getCategory(
250                 category.getParentCategoryId());
251         }
252 
253         breadcrumbs =
254             "<span class=\"first\">" + categoriesLink + " &raquo; </span>" +
255                 breadcrumbs;
256 
257         if (message != null) {
258             message = message.toEscapedModel();
259 
260             PortletURL messageURL = renderResponse.createRenderURL();
261 
262             //messageURL.setWindowState(WindowState.MAXIMIZED);
263 
264             messageURL.setParameter(
265                 "struts_action", "/message_boards/view_message");
266             messageURL.setParameter(
267                 "messageId", String.valueOf(message.getMessageId()));
268 
269             String messageLink =
270                 "<span class=\"last\"><a href=\"" + messageURL.toString() +
271                     "\">" + message.getSubject() + "</a></span>";
272 
273             breadcrumbs = breadcrumbs + " &raquo; " + messageLink;
274         }
275 
276         return breadcrumbs;
277     }
278 
279     public static String getEmailFromAddress(PortletPreferences prefs) {
280         String emailFromAddress = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS;
281 
282         return prefs.getValue("email-from-address", emailFromAddress);
283     }
284 
285     public static String getEmailFromName(PortletPreferences prefs) {
286         String emailFromName = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_NAME;
287 
288         return prefs.getValue("email-from-name", emailFromName);
289     }
290 
291     public static boolean getEmailHtmlFormat(PortletPreferences prefs) {
292         String emailHtmlFormat = prefs.getValue(
293             "email-html-format", StringPool.BLANK);
294 
295         if (Validator.isNotNull(emailHtmlFormat)) {
296             return GetterUtil.getBoolean(emailHtmlFormat);
297         }
298         else {
299             return PropsValues.MESSAGE_BOARDS_EMAIL_HTML_FORMAT;
300         }
301     }
302 
303     public static boolean getEmailMessageAddedEnabled(
304         PortletPreferences prefs) {
305 
306         String emailMessageAddedEnabled = prefs.getValue(
307             "email-message-added-enabled", StringPool.BLANK);
308 
309         if (Validator.isNotNull(emailMessageAddedEnabled)) {
310             return GetterUtil.getBoolean(emailMessageAddedEnabled);
311         }
312         else {
313             return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED;
314         }
315     }
316 
317     public static String getEmailMessageAddedBody(PortletPreferences prefs) {
318         String emailMessageAddedBody = prefs.getValue(
319             "email-message-added-body", StringPool.BLANK);
320 
321         if (Validator.isNotNull(emailMessageAddedBody)) {
322             return emailMessageAddedBody;
323         }
324         else {
325             return ContentUtil.get(
326                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY);
327         }
328     }
329 
330     public static String getEmailMessageAddedSignature(
331         PortletPreferences prefs) {
332 
333         String emailMessageAddedSignature = prefs.getValue(
334             "email-message-added-signature", StringPool.BLANK);
335 
336         if (Validator.isNotNull(emailMessageAddedSignature)) {
337             return emailMessageAddedSignature;
338         }
339         else {
340             return ContentUtil.get(
341                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SIGNATURE);
342         }
343     }
344 
345     public static String getEmailMessageAddedSubjectPrefix(
346         PortletPreferences prefs) {
347 
348         String emailMessageAddedSubjectPrefix = prefs.getValue(
349             "email-message-added-subject-prefix", StringPool.BLANK);
350 
351         if (Validator.isNotNull(emailMessageAddedSubjectPrefix)) {
352             return emailMessageAddedSubjectPrefix;
353         }
354         else {
355             return ContentUtil.get(
356                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT_PREFIX);
357         }
358     }
359 
360     public static boolean getEmailMessageUpdatedEnabled(
361         PortletPreferences prefs) {
362 
363         String emailMessageUpdatedEnabled = prefs.getValue(
364             "email-message-updated-enabled", StringPool.BLANK);
365 
366         if (Validator.isNotNull(emailMessageUpdatedEnabled)) {
367             return GetterUtil.getBoolean(emailMessageUpdatedEnabled);
368         }
369         else {
370             return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED;
371         }
372     }
373 
374     public static String getEmailMessageUpdatedBody(PortletPreferences prefs) {
375         String emailMessageUpdatedBody = prefs.getValue(
376             "email-message-updated-body", StringPool.BLANK);
377 
378         if (Validator.isNotNull(emailMessageUpdatedBody)) {
379             return emailMessageUpdatedBody;
380         }
381         else {
382             return ContentUtil.get(
383                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY);
384         }
385     }
386 
387     public static String getEmailMessageUpdatedSignature(
388         PortletPreferences prefs) {
389 
390         String emailMessageUpdatedSignature = prefs.getValue(
391             "email-message-updated-signature", StringPool.BLANK);
392 
393         if (Validator.isNotNull(emailMessageUpdatedSignature)) {
394             return emailMessageUpdatedSignature;
395         }
396         else {
397             return ContentUtil.get(
398                 PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SIGNATURE);
399         }
400     }
401 
402     public static String getEmailMessageUpdatedSubjectPrefix(
403         PortletPreferences prefs) {
404 
405         String emailMessageUpdatedSubject = prefs.getValue(
406             "email-message-updated-subject-prefix", StringPool.BLANK);
407 
408         if (Validator.isNotNull(emailMessageUpdatedSubject)) {
409             return emailMessageUpdatedSubject;
410         }
411         else {
412             return ContentUtil.get(
413                 PropsValues.
414                     MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT_PREFIX);
415         }
416     }
417 
418     public static String getMailId(String mx, long categoryId, long messageId) {
419         StringBundler sb = new StringBundler(10);
420 
421         sb.append(StringPool.LESS_THAN);
422         sb.append(POP_PORTLET_PREFIX);
423         sb.append(categoryId);
424         sb.append(StringPool.PERIOD);
425         sb.append(messageId);
426         sb.append(StringPool.AT);
427 
428         if (Validator.isNotNull(PropsValues.POP_SERVER_SUBDOMAIN)) {
429             sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
430             sb.append(StringPool.PERIOD);
431         }
432 
433         sb.append(mx);
434         sb.append(StringPool.GREATER_THAN);
435 
436         return sb.toString();
437     }
438 
439     public static String getMailingListAddress(
440         long categoryId, long messageId, String mx,
441         String defaultMailingListAddress) {
442 
443         if (POP_SERVER_SUBDOMAIN_LENGTH <= 0) {
444             return defaultMailingListAddress;
445         }
446 
447         StringBundler sb = new StringBundler(8);
448 
449         sb.append(POP_PORTLET_PREFIX);
450         sb.append(categoryId);
451         sb.append(StringPool.PERIOD);
452         sb.append(messageId);
453         sb.append(StringPool.AT);
454         sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
455         sb.append(StringPool.PERIOD);
456         sb.append(mx);
457 
458         return sb.toString();
459     }
460 
461     public static long getMessageId(String mailId) {
462         int x = mailId.indexOf(StringPool.LESS_THAN) + 1;
463         int y = mailId.indexOf(StringPool.AT);
464 
465         long messageId = 0;
466 
467         if ((x > 0 ) && (y != -1)) {
468             String temp = mailId.substring(x, y);
469 
470             int z = temp.lastIndexOf(StringPool.PERIOD);
471 
472             if (z != -1) {
473                 messageId = GetterUtil.getLong(temp.substring(z + 1));
474             }
475         }
476 
477         return messageId;
478     }
479 
480     public static long getParentMessageId(Message message) throws Exception {
481         long parentMessageId = -1;
482 
483         String parentHeader = getParentMessageIdString(message);
484 
485         if (parentHeader != null) {
486             if (_log.isDebugEnabled()) {
487                 _log.debug("Parent header " + parentHeader);
488             }
489 
490             parentMessageId = getMessageId(parentHeader);
491 
492             if (_log.isDebugEnabled()) {
493                 _log.debug("Previous message id " + parentMessageId);
494             }
495         }
496 
497         return parentMessageId;
498     }
499 
500     public static String getParentMessageIdString(Message message)
501         throws Exception {
502 
503         // If the previous block failed, try to get the parent message ID from
504         // the "References" header as explained in
505         // http://cr.yp.to/immhf/thread.html. Some mail clients such as Yahoo!
506         // Mail use the "In-Reply-To" header, so we check that as well.
507 
508         String parentHeader = null;
509 
510         String[] references = message.getHeader("References");
511 
512         if ((references != null) && (references.length > 0)) {
513             String reference = references[0];
514 
515             int x = reference.lastIndexOf("<mb.");
516 
517             if (x > -1) {
518                 int y = reference.indexOf(">", x);
519 
520                 parentHeader = reference.substring(x, y);
521             }
522         }
523 
524         if (parentHeader == null) {
525             String[] inReplyToHeaders = message.getHeader("In-Reply-To");
526 
527             if ((inReplyToHeaders != null) && (inReplyToHeaders.length > 0)) {
528                 parentHeader = inReplyToHeaders[0];
529             }
530         }
531 
532         if (Validator.isNull(parentHeader) ||
533             !parentHeader.startsWith(POP_PORTLET_PREFIX, 1)) {
534 
535             parentHeader = _getParentMessageIdFromSubject(message);
536         }
537 
538         return parentHeader;
539     }
540 
541     public static String getSubjectWithoutMessageId(Message message)
542         throws Exception {
543 
544         String subject = message.getSubject();
545 
546         String parentMessageId = _getParentMessageIdFromSubject(message);
547 
548         if (Validator.isNotNull(parentMessageId)) {
549             int pos = subject.indexOf(parentMessageId);
550 
551             if (pos != -1) {
552                 subject = subject.substring(0, pos);
553             }
554         }
555 
556         return subject;
557     }
558 
559     public static String[] getThreadPriority(
560             PortletPreferences prefs, String languageId, double value,
561             ThemeDisplay themeDisplay)
562         throws Exception {
563 
564         String[] priorities = LocalizationUtil.getPreferencesValues(
565             prefs, "priorities", languageId);
566 
567         String[] priorityPair = _findThreadPriority(
568             value, themeDisplay, priorities);
569 
570         if (priorityPair == null) {
571             String defaultLanguageId = LocaleUtil.toLanguageId(
572                 LocaleUtil.getDefault());
573 
574             priorities = LocalizationUtil.getPreferencesValues(
575                 prefs, "priorities", defaultLanguageId);
576 
577             priorityPair = _findThreadPriority(value, themeDisplay, priorities);
578         }
579 
580         return priorityPair;
581     }
582 
583     public static Date getUnbanDate(MBBan ban, int expireInterval) {
584         Date banDate = ban.getCreateDate();
585 
586         Calendar cal = Calendar.getInstance();
587 
588         cal.setTime(banDate);
589 
590         cal.add(Calendar.DATE, expireInterval);
591 
592         return cal.getTime();
593     }
594 
595     public static String getUserRank(
596             PortletPreferences prefs, String languageId, int posts)
597         throws Exception {
598 
599         String rank = StringPool.BLANK;
600 
601         String[] ranks = LocalizationUtil.getPreferencesValues(
602             prefs, "ranks", languageId);
603 
604         for (int i = 0; i < ranks.length; i++) {
605             String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
606 
607             String kvpName = kvp[0];
608             int kvpPosts = GetterUtil.getInteger(kvp[1]);
609 
610             if (posts >= kvpPosts) {
611                 rank = kvpName;
612             }
613             else {
614                 break;
615             }
616         }
617 
618         return rank;
619     }
620 
621     public static String[] getUserRank(
622             PortletPreferences prefs, String languageId, MBStatsUser statsUser)
623         throws Exception {
624 
625         String[] rank = {StringPool.BLANK, StringPool.BLANK};
626 
627         int maxPosts = 0;
628 
629         Group group = GroupLocalServiceUtil.getGroup(
630             statsUser.getGroupId());
631 
632         long companyId = group.getCompanyId();
633 
634         String[] ranks = LocalizationUtil.getPreferencesValues(
635             prefs, "ranks", languageId);
636 
637         for (int i = 0; i < ranks.length; i++) {
638             String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
639 
640             String curRank = kvp[0];
641             String curRankValue = kvp[1];
642 
643             String[] curRankValueKvp = StringUtil.split(
644                 curRankValue, StringPool.COLON);
645 
646             if (curRankValueKvp.length <= 1) {
647                 int posts = GetterUtil.getInteger(curRankValue);
648 
649                 if ((posts <= statsUser.getMessageCount()) &&
650                     (posts >= maxPosts)) {
651 
652                     rank[0] = curRank;
653                     maxPosts = posts;
654                 }
655 
656             }
657             else {
658                 String entityType = curRankValueKvp[0];
659                 String entityValue = curRankValueKvp[1];
660 
661                 try {
662                     if (_isEntityRank(
663                             companyId, statsUser, entityType, entityValue)) {
664 
665                         rank[1] = curRank;
666 
667                         break;
668                     }
669                 }
670                 catch (Exception e) {
671                     if (_log.isWarnEnabled()) {
672                         _log.warn(e);
673                     }
674                 }
675             }
676         }
677 
678         return rank;
679     }
680 
681     public static boolean hasMailIdHeader(Message message) throws Exception {
682         String[] messageIds = message.getHeader("Message-ID");
683 
684         if (messageIds == null) {
685             return false;
686         }
687 
688         for (String messageId : messageIds) {
689             if (Validator.isNotNull(PropsValues.POP_SERVER_SUBDOMAIN) &&
690                 messageId.contains(PropsValues.POP_SERVER_SUBDOMAIN)) {
691 
692                 return true;
693             }
694         }
695 
696         return false;
697     }
698 
699     public static boolean isAllowAnonymousPosting(PortletPreferences prefs) {
700         String allowAnonymousPosting = prefs.getValue(
701             "allow-anonymous-posting", StringPool.BLANK);
702 
703         if (Validator.isNotNull(allowAnonymousPosting)) {
704             return GetterUtil.getBoolean(allowAnonymousPosting);
705         }
706         else {
707             return PropsValues.MESSAGE_BOARDS_ANONYMOUS_POSTING_ENABLED;
708         }
709     }
710 
711     private static String[] _findThreadPriority(
712         double value, ThemeDisplay themeDisplay, String[] priorities) {
713 
714         for (int i = 0; i < priorities.length; i++) {
715             String[] priority = StringUtil.split(priorities[i]);
716 
717             try {
718                 String priorityName = priority[0];
719                 String priorityImage = priority[1];
720                 double priorityValue = GetterUtil.getDouble(priority[2]);
721 
722                 if (value == priorityValue) {
723                     if (!priorityImage.startsWith(Http.HTTP)) {
724                         priorityImage =
725                             themeDisplay.getPathThemeImages() + priorityImage;
726                     }
727 
728                     return new String[] {priorityName, priorityImage};
729                 }
730             }
731             catch (Exception e) {
732                 _log.error("Unable to determine thread priority", e);
733             }
734         }
735 
736         return null;
737     }
738 
739     private static String _getParentMessageIdFromSubject(Message message)
740         throws Exception {
741 
742         String parentMessageId = null;
743 
744         String subject = StringUtil.reverse(message.getSubject());
745 
746         int pos = subject.indexOf(StringPool.LESS_THAN);
747 
748         if (pos != -1) {
749             parentMessageId = StringUtil.reverse(subject.substring(0, pos + 1));
750         }
751 
752         return parentMessageId;
753     }
754 
755     private static boolean _isEntityRank(
756             long companyId, MBStatsUser statsUser, String entityType,
757             String entityValue)
758         throws Exception {
759 
760         long groupId = statsUser.getGroupId();
761         long userId = statsUser.getUserId();
762 
763         if (entityType.equals("community-role") ||
764             entityType.equals("organization-role")) {
765 
766             Role role = RoleLocalServiceUtil.getRole(companyId, entityValue);
767 
768             if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(
769                     userId, groupId, role.getRoleId())) {
770 
771                 return true;
772             }
773         }
774         else if (entityType.equals("organization")) {
775             Organization organization =
776                 OrganizationLocalServiceUtil.getOrganization(
777                     companyId, entityValue);
778 
779             if (OrganizationLocalServiceUtil.hasUserOrganization(
780                     userId, organization.getOrganizationId(), true)) {
781 
782                 return true;
783             }
784         }
785         else if (entityType.equals("regular-role")) {
786             if (RoleLocalServiceUtil.hasUserRole(
787                     userId, companyId, entityValue, true)) {
788 
789                 return true;
790             }
791         }
792         else if (entityType.equals("user-group")) {
793             UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
794                 companyId, entityValue);
795 
796             if (UserLocalServiceUtil.hasUserGroupUser(
797                     userGroup.getUserGroupId(), userId)) {
798 
799                 return true;
800             }
801         }
802 
803         return false;
804     }
805 
806     private static Log _log = LogFactoryUtil.getLog(MBUtil.class);
807 
808 }