001    /**
002     * Copyright (c) 2000-2010 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.messageboards.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.LiferayWindowState;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.Http;
022    import com.liferay.portal.kernel.util.LocaleUtil;
023    import com.liferay.portal.kernel.util.LocalizationUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.Organization;
031    import com.liferay.portal.model.Role;
032    import com.liferay.portal.model.UserGroup;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.OrganizationLocalServiceUtil;
035    import com.liferay.portal.service.RoleLocalServiceUtil;
036    import com.liferay.portal.service.UserGroupLocalServiceUtil;
037    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
038    import com.liferay.portal.service.UserLocalServiceUtil;
039    import com.liferay.portal.theme.ThemeDisplay;
040    import com.liferay.portal.util.ContentUtil;
041    import com.liferay.portal.util.PortalUtil;
042    import com.liferay.portal.util.PropsValues;
043    import com.liferay.portal.util.WebKeys;
044    import com.liferay.portlet.messageboards.model.MBBan;
045    import com.liferay.portlet.messageboards.model.MBCategory;
046    import com.liferay.portlet.messageboards.model.MBCategoryConstants;
047    import com.liferay.portlet.messageboards.model.MBMailingList;
048    import com.liferay.portlet.messageboards.model.MBMessage;
049    import com.liferay.portlet.messageboards.model.MBStatsUser;
050    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
051    import com.liferay.portlet.messageboards.service.MBMailingListLocalServiceUtil;
052    import com.liferay.util.mail.JavaMailUtil;
053    
054    import java.io.InputStream;
055    
056    import java.util.Calendar;
057    import java.util.Collections;
058    import java.util.Date;
059    import java.util.List;
060    
061    import javax.mail.BodyPart;
062    import javax.mail.Message;
063    import javax.mail.Part;
064    import javax.mail.internet.MimeMessage;
065    import javax.mail.internet.MimeMultipart;
066    
067    import javax.portlet.PortletPreferences;
068    import javax.portlet.PortletURL;
069    import javax.portlet.RenderResponse;
070    
071    import javax.servlet.http.HttpServletRequest;
072    
073    /**
074     * @author Brian Wing Shun Chan
075     */
076    public class MBUtil {
077    
078            public static final String POP_PORTLET_PREFIX = "mb.";
079    
080            public static final int POP_SERVER_SUBDOMAIN_LENGTH =
081                    PropsValues.POP_SERVER_SUBDOMAIN.length();
082    
083            public static void addPortletBreadcrumbEntries(
084                            long categoryId, HttpServletRequest request,
085                            RenderResponse renderResponse)
086                    throws Exception {
087    
088                    if ((categoryId == MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
089                            (categoryId == MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
090    
091                            return;
092                    }
093    
094                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(
095                            categoryId);
096    
097                    addPortletBreadcrumbEntries(category, request, renderResponse);
098            }
099    
100            public static void addPortletBreadcrumbEntries(
101                            MBCategory category, HttpServletRequest request,
102                            RenderResponse renderResponse)
103                    throws Exception {
104    
105                    String strutsAction = ParamUtil.getString(
106                            request, "struts_action");
107    
108                    boolean selectCategory = strutsAction.equals(
109                            "/message_boards/select_category");
110    
111                    PortletURL portletURL = renderResponse.createRenderURL();
112    
113                    if (selectCategory) {
114                            ThemeDisplay themeDisplay =     (ThemeDisplay)request.getAttribute(
115                                    WebKeys.THEME_DISPLAY);
116    
117                            portletURL.setWindowState(LiferayWindowState.POP_UP);
118    
119                            portletURL.setParameter(
120                                    "struts_action", "/message_boards/select_category");
121    
122                            PortalUtil.addPortletBreadcrumbEntry(
123                                    request, themeDisplay.translate("categories"),
124                                    portletURL.toString());
125                    }
126                    else {
127                            portletURL.setParameter("struts_action", "/message_boards/view");
128                            portletURL.setParameter("tabs1", "categories");
129                    }
130    
131                    List<MBCategory> ancestorCategories = category.getAncestors();
132    
133                    Collections.reverse(ancestorCategories);
134    
135                    for (MBCategory curCategory : ancestorCategories) {
136                            portletURL.setParameter(
137                                    "mbCategoryId", String.valueOf(curCategory.getCategoryId()));
138    
139                            PortalUtil.addPortletBreadcrumbEntry(
140                                    request, curCategory.getName(), portletURL.toString());
141                    }
142    
143                    portletURL.setParameter(
144                            "mbCategoryId", String.valueOf(category.getCategoryId()));
145    
146                    PortalUtil.addPortletBreadcrumbEntry(
147                            request, category.getName(), portletURL.toString());
148            }
149    
150            public static void addPortletBreadcrumbEntries(
151                            MBMessage message, HttpServletRequest request,
152                            RenderResponse renderResponse)
153                    throws Exception {
154    
155                    if ((message.getCategoryId() ==
156                                    MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID) ||
157                            (message.getCategoryId() ==
158                                    MBCategoryConstants.DISCUSSION_CATEGORY_ID)) {
159    
160                            return;
161                    }
162    
163                    MBCategory category = message.getCategory();
164    
165                    addPortletBreadcrumbEntries(category, request, renderResponse);
166    
167                    PortletURL portletURL = renderResponse.createRenderURL();
168    
169                    portletURL.setParameter(
170                            "struts_action", "/message_boards/view_message");
171                    portletURL.setParameter("tabs1", "categories");
172                    portletURL.setParameter(
173                            "messageId", String.valueOf(message.getMessageId()));
174    
175                    PortalUtil.addPortletBreadcrumbEntry(
176                            request, message.getSubject(), portletURL.toString());
177            }
178    
179            public static void collectMultipartContent(
180                            MimeMultipart multipart, MBMailMessage collector)
181                    throws Exception {
182    
183                    for (int i = 0; i < multipart.getCount(); i++) {
184                            BodyPart part = multipart.getBodyPart(i);
185    
186                            collectPartContent(part, collector);
187                    }
188            }
189    
190            public static void collectPartContent(Part part, MBMailMessage collector)
191                    throws Exception {
192    
193                    Object partContent = part.getContent();
194    
195                    String contentType = part.getContentType().toLowerCase();
196    
197                    if ((part.getDisposition() != null) &&
198                             (part.getDisposition().equalsIgnoreCase(MimeMessage.ATTACHMENT))) {
199    
200                            if (_log.isDebugEnabled()) {
201                                    _log.debug("Processing attachment");
202                            }
203    
204                            byte[] bytes = null;
205    
206                            if (partContent instanceof String) {
207                                    bytes = ((String)partContent).getBytes();
208                            }
209                            else if (partContent instanceof InputStream) {
210                                    bytes = JavaMailUtil.getBytes(part);
211                            }
212    
213                            collector.addFile(part.getFileName(), bytes);
214                    }
215                    else {
216                            if (partContent instanceof MimeMultipart) {
217                                    collectMultipartContent((MimeMultipart)partContent, collector);
218                            }
219                            else if (partContent instanceof String) {
220                                    if (contentType.startsWith("text/html")) {
221                                            collector.setHtmlBody((String)partContent);
222                                    }
223                                    else {
224                                            collector.setPlainBody((String)partContent);
225                                    }
226                            }
227                    }
228            }
229    
230            public static long getCategoryId(
231                    HttpServletRequest request, MBCategory category) {
232    
233                    long categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
234    
235                    if (category != null) {
236                            categoryId = category.getCategoryId();
237                    }
238    
239                    categoryId = ParamUtil.getLong(request, "mbCategoryId", categoryId);
240    
241                    return categoryId;
242            }
243    
244            public static long getCategoryId(
245                    HttpServletRequest request, MBMessage message) {
246    
247                    long categoryId = MBCategoryConstants.DEFAULT_PARENT_CATEGORY_ID;
248    
249                    if (message != null) {
250                            categoryId = message.getCategoryId();
251                    }
252    
253                    categoryId = ParamUtil.getLong(request, "mbCategoryId", categoryId);
254    
255                    return categoryId;
256            }
257    
258            public static String getEmailFromAddress(PortletPreferences preferences) {
259                    String emailFromAddress = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS;
260    
261                    return preferences.getValue("email-from-address", emailFromAddress);
262            }
263    
264            public static String getEmailFromName(PortletPreferences preferences) {
265                    String emailFromName = PropsValues.MESSAGE_BOARDS_EMAIL_FROM_NAME;
266    
267                    return preferences.getValue("email-from-name", emailFromName);
268            }
269    
270            public static boolean getEmailHtmlFormat(PortletPreferences preferences) {
271                    String emailHtmlFormat = preferences.getValue(
272                            "email-html-format", StringPool.BLANK);
273    
274                    if (Validator.isNotNull(emailHtmlFormat)) {
275                            return GetterUtil.getBoolean(emailHtmlFormat);
276                    }
277                    else {
278                            return PropsValues.MESSAGE_BOARDS_EMAIL_HTML_FORMAT;
279                    }
280            }
281    
282            public static String getEmailMessageAddedBody(
283                    PortletPreferences preferences) {
284    
285                    String emailMessageAddedBody = preferences.getValue(
286                            "email-message-added-body", StringPool.BLANK);
287    
288                    if (Validator.isNotNull(emailMessageAddedBody)) {
289                            return emailMessageAddedBody;
290                    }
291                    else {
292                            return ContentUtil.get(
293                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY);
294                    }
295            }
296    
297            public static boolean getEmailMessageAddedEnabled(
298                    PortletPreferences preferences) {
299    
300                    String emailMessageAddedEnabled = preferences.getValue(
301                            "email-message-added-enabled", StringPool.BLANK);
302    
303                    if (Validator.isNotNull(emailMessageAddedEnabled)) {
304                            return GetterUtil.getBoolean(emailMessageAddedEnabled);
305                    }
306                    else {
307                            return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED;
308                    }
309            }
310    
311            public static String getEmailMessageAddedSignature(
312                    PortletPreferences preferences) {
313    
314                    String emailMessageAddedSignature = preferences.getValue(
315                            "email-message-added-signature", StringPool.BLANK);
316    
317                    if (Validator.isNotNull(emailMessageAddedSignature)) {
318                            return emailMessageAddedSignature;
319                    }
320                    else {
321                            return ContentUtil.get(
322                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SIGNATURE);
323                    }
324            }
325    
326            public static String getEmailMessageAddedSubjectPrefix(
327                    PortletPreferences preferences) {
328    
329                    String emailMessageAddedSubjectPrefix = preferences.getValue(
330                            "email-message-added-subject-prefix", StringPool.BLANK);
331    
332                    if (Validator.isNotNull(emailMessageAddedSubjectPrefix)) {
333                            return emailMessageAddedSubjectPrefix;
334                    }
335                    else {
336                            return ContentUtil.get(
337                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT_PREFIX);
338                    }
339            }
340    
341            public static String getEmailMessageUpdatedBody(
342                    PortletPreferences preferences) {
343    
344                    String emailMessageUpdatedBody = preferences.getValue(
345                            "email-message-updated-body", StringPool.BLANK);
346    
347                    if (Validator.isNotNull(emailMessageUpdatedBody)) {
348                            return emailMessageUpdatedBody;
349                    }
350                    else {
351                            return ContentUtil.get(
352                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY);
353                    }
354            }
355    
356            public static boolean getEmailMessageUpdatedEnabled(
357                    PortletPreferences preferences) {
358    
359                    String emailMessageUpdatedEnabled = preferences.getValue(
360                            "email-message-updated-enabled", StringPool.BLANK);
361    
362                    if (Validator.isNotNull(emailMessageUpdatedEnabled)) {
363                            return GetterUtil.getBoolean(emailMessageUpdatedEnabled);
364                    }
365                    else {
366                            return PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED;
367                    }
368            }
369    
370            public static String getEmailMessageUpdatedSignature(
371                    PortletPreferences preferences) {
372    
373                    String emailMessageUpdatedSignature = preferences.getValue(
374                            "email-message-updated-signature", StringPool.BLANK);
375    
376                    if (Validator.isNotNull(emailMessageUpdatedSignature)) {
377                            return emailMessageUpdatedSignature;
378                    }
379                    else {
380                            return ContentUtil.get(
381                                    PropsValues.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SIGNATURE);
382                    }
383            }
384    
385            public static String getEmailMessageUpdatedSubjectPrefix(
386                    PortletPreferences preferences) {
387    
388                    String emailMessageUpdatedSubject = preferences.getValue(
389                            "email-message-updated-subject-prefix", StringPool.BLANK);
390    
391                    if (Validator.isNotNull(emailMessageUpdatedSubject)) {
392                            return emailMessageUpdatedSubject;
393                    }
394                    else {
395                            return ContentUtil.get(
396                                    PropsValues.
397                                            MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT_PREFIX);
398                    }
399            }
400    
401            public static String getMailId(String mx, long categoryId, long messageId) {
402                    StringBundler sb = new StringBundler(10);
403    
404                    sb.append(StringPool.LESS_THAN);
405                    sb.append(POP_PORTLET_PREFIX);
406                    sb.append(categoryId);
407                    sb.append(StringPool.PERIOD);
408                    sb.append(messageId);
409                    sb.append(StringPool.AT);
410    
411                    if (Validator.isNotNull(PropsValues.POP_SERVER_SUBDOMAIN)) {
412                            sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
413                            sb.append(StringPool.PERIOD);
414                    }
415    
416                    sb.append(mx);
417                    sb.append(StringPool.GREATER_THAN);
418    
419                    return sb.toString();
420            }
421    
422            public static String getMailingListAddress(
423                    long groupId, long categoryId, long messageId, String mx,
424                    String defaultMailingListAddress) {
425    
426                    if (POP_SERVER_SUBDOMAIN_LENGTH <= 0) {
427                            String mailingListAddress = defaultMailingListAddress;
428    
429                            try {
430                                    MBMailingList mailingList =
431                                            MBMailingListLocalServiceUtil.getCategoryMailingList(
432                                                    groupId, categoryId);
433    
434                                    if (mailingList.isActive()) {
435                                            mailingListAddress = mailingList.getEmailAddress();
436                                    }
437                            }
438                            catch (Exception e) {
439                            }
440    
441                            return mailingListAddress;
442                    }
443    
444                    StringBundler sb = new StringBundler(8);
445    
446                    sb.append(POP_PORTLET_PREFIX);
447                    sb.append(categoryId);
448                    sb.append(StringPool.PERIOD);
449                    sb.append(messageId);
450                    sb.append(StringPool.AT);
451                    sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
452                    sb.append(StringPool.PERIOD);
453                    sb.append(mx);
454    
455                    return sb.toString();
456            }
457    
458            public static long getMessageId(String mailId) {
459                    int x = mailId.indexOf(StringPool.LESS_THAN) + 1;
460                    int y = mailId.indexOf(StringPool.AT);
461    
462                    long messageId = 0;
463    
464                    if ((x > 0 ) && (y != -1)) {
465                            String temp = mailId.substring(x, y);
466    
467                            int z = temp.lastIndexOf(StringPool.PERIOD);
468    
469                            if (z != -1) {
470                                    messageId = GetterUtil.getLong(temp.substring(z + 1));
471                            }
472                    }
473    
474                    return messageId;
475            }
476    
477            public static long getParentMessageId(Message message) throws Exception {
478                    long parentMessageId = -1;
479    
480                    String parentHeader = getParentMessageIdString(message);
481    
482                    if (parentHeader != null) {
483                            if (_log.isDebugEnabled()) {
484                                    _log.debug("Parent header " + parentHeader);
485                            }
486    
487                            parentMessageId = getMessageId(parentHeader);
488    
489                            if (_log.isDebugEnabled()) {
490                                    _log.debug("Previous message id " + parentMessageId);
491                            }
492                    }
493    
494                    return parentMessageId;
495            }
496    
497            public static String getParentMessageIdString(Message message)
498                    throws Exception {
499    
500                    // If the previous block failed, try to get the parent message ID from
501                    // the "References" header as explained in
502                    // http://cr.yp.to/immhf/thread.html. Some mail clients such as Yahoo!
503                    // Mail use the "In-Reply-To" header, so we check that as well.
504    
505                    String parentHeader = null;
506    
507                    String[] references = message.getHeader("References");
508    
509                    if ((references != null) && (references.length > 0)) {
510                            String reference = references[0];
511    
512                            int x = reference.lastIndexOf("<mb.");
513    
514                            if (x > -1) {
515                                    int y = reference.indexOf(">", x);
516    
517                                    parentHeader = reference.substring(x, y);
518                            }
519                    }
520    
521                    if (parentHeader == null) {
522                            String[] inReplyToHeaders = message.getHeader("In-Reply-To");
523    
524                            if ((inReplyToHeaders != null) && (inReplyToHeaders.length > 0)) {
525                                    parentHeader = inReplyToHeaders[0];
526                            }
527                    }
528    
529                    if (Validator.isNull(parentHeader) ||
530                            !parentHeader.startsWith(POP_PORTLET_PREFIX, 1)) {
531    
532                            parentHeader = _getParentMessageIdFromSubject(message);
533                    }
534    
535                    return parentHeader;
536            }
537    
538            public static String getSubjectWithoutMessageId(Message message)
539                    throws Exception {
540    
541                    String subject = message.getSubject();
542    
543                    String parentMessageId = _getParentMessageIdFromSubject(message);
544    
545                    if (Validator.isNotNull(parentMessageId)) {
546                            int pos = subject.indexOf(parentMessageId);
547    
548                            if (pos != -1) {
549                                    subject = subject.substring(0, pos);
550                            }
551                    }
552    
553                    return subject;
554            }
555    
556            public static String[] getThreadPriority(
557                            PortletPreferences preferences, String languageId, double value,
558                            ThemeDisplay themeDisplay)
559                    throws Exception {
560    
561                    String[] priorities = LocalizationUtil.getPreferencesValues(
562                            preferences, "priorities", languageId);
563    
564                    String[] priorityPair = _findThreadPriority(
565                            value, themeDisplay, priorities);
566    
567                    if (priorityPair == null) {
568                            String defaultLanguageId = LocaleUtil.toLanguageId(
569                                    LocaleUtil.getDefault());
570    
571                            priorities = LocalizationUtil.getPreferencesValues(
572                                    preferences, "priorities", defaultLanguageId);
573    
574                            priorityPair = _findThreadPriority(value, themeDisplay, priorities);
575                    }
576    
577                    return priorityPair;
578            }
579    
580            public static Date getUnbanDate(MBBan ban, int expireInterval) {
581                    Date banDate = ban.getCreateDate();
582    
583                    Calendar cal = Calendar.getInstance();
584    
585                    cal.setTime(banDate);
586    
587                    cal.add(Calendar.DATE, expireInterval);
588    
589                    return cal.getTime();
590            }
591    
592            public static String getUserRank(
593                            PortletPreferences preferences, String languageId, int posts)
594                    throws Exception {
595    
596                    String rank = StringPool.BLANK;
597    
598                    String[] ranks = LocalizationUtil.getPreferencesValues(
599                            preferences, "ranks", languageId);
600    
601                    for (int i = 0; i < ranks.length; i++) {
602                            String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
603    
604                            String kvpName = kvp[0];
605                            int kvpPosts = GetterUtil.getInteger(kvp[1]);
606    
607                            if (posts >= kvpPosts) {
608                                    rank = kvpName;
609                            }
610                            else {
611                                    break;
612                            }
613                    }
614    
615                    return rank;
616            }
617    
618            public static String[] getUserRank(
619                            PortletPreferences preferences, String languageId,
620                            MBStatsUser statsUser)
621                    throws Exception {
622    
623                    String[] rank = {StringPool.BLANK, StringPool.BLANK};
624    
625                    int maxPosts = 0;
626    
627                    Group group = GroupLocalServiceUtil.getGroup(
628                            statsUser.getGroupId());
629    
630                    long companyId = group.getCompanyId();
631    
632                    String[] ranks = LocalizationUtil.getPreferencesValues(
633                            preferences, "ranks", languageId);
634    
635                    for (int i = 0; i < ranks.length; i++) {
636                            String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
637    
638                            String curRank = kvp[0];
639                            String curRankValue = kvp[1];
640    
641                            String[] curRankValueKvp = StringUtil.split(
642                                    curRankValue, StringPool.COLON);
643    
644                            if (curRankValueKvp.length <= 1) {
645                                    int posts = GetterUtil.getInteger(curRankValue);
646    
647                                    if ((posts <= statsUser.getMessageCount()) &&
648                                            (posts >= maxPosts)) {
649    
650                                            rank[0] = curRank;
651                                            maxPosts = posts;
652                                    }
653    
654                            }
655                            else {
656                                    String entityType = curRankValueKvp[0];
657                                    String entityValue = curRankValueKvp[1];
658    
659                                    try {
660                                            if (_isEntityRank(
661                                                            companyId, statsUser, entityType, entityValue)) {
662    
663                                                    rank[1] = curRank;
664    
665                                                    break;
666                                            }
667                                    }
668                                    catch (Exception e) {
669                                            if (_log.isWarnEnabled()) {
670                                                    _log.warn(e);
671                                            }
672                                    }
673                            }
674                    }
675    
676                    return rank;
677            }
678    
679            public static boolean hasMailIdHeader(Message message) throws Exception {
680                    String[] messageIds = message.getHeader("Message-ID");
681    
682                    if (messageIds == null) {
683                            return false;
684                    }
685    
686                    for (String messageId : messageIds) {
687                            if (Validator.isNotNull(PropsValues.POP_SERVER_SUBDOMAIN) &&
688                                    messageId.contains(PropsValues.POP_SERVER_SUBDOMAIN)) {
689    
690                                    return true;
691                            }
692                    }
693    
694                    return false;
695            }
696    
697            public static boolean isAllowAnonymousPosting(
698                    PortletPreferences preferences) {
699    
700                    String allowAnonymousPosting = preferences.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(), true)) {
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(), false, true,
781                                            false)) {
782    
783                                    return true;
784                            }
785                    }
786                    else if (entityType.equals("regular-role")) {
787                            if (RoleLocalServiceUtil.hasUserRole(
788                                            userId, companyId, entityValue, true)) {
789    
790                                    return true;
791                            }
792                    }
793                    else if (entityType.equals("user-group")) {
794                            UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(
795                                    companyId, entityValue);
796    
797                            if (UserLocalServiceUtil.hasUserGroupUser(
798                                            userGroup.getUserGroupId(), userId)) {
799    
800                                    return true;
801                            }
802                    }
803    
804                    return false;
805            }
806    
807            private static Log _log = LogFactoryUtil.getLog(MBUtil.class);
808    
809    }