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