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