001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.util;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.portlet.LiferayWindowState;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.LocaleUtil;
027: import com.liferay.portal.kernel.util.StringMaker;
028: import com.liferay.portal.kernel.util.StringPool;
029: import com.liferay.portal.kernel.util.StringUtil;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.model.Group;
032: import com.liferay.portal.model.Organization;
033: import com.liferay.portal.model.Role;
034: import com.liferay.portal.model.UserGroup;
035: import com.liferay.portal.service.GroupLocalServiceUtil;
036: import com.liferay.portal.service.OrganizationLocalServiceUtil;
037: import com.liferay.portal.service.RoleLocalServiceUtil;
038: import com.liferay.portal.service.UserGroupLocalServiceUtil;
039: import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
040: import com.liferay.portal.service.UserLocalServiceUtil;
041: import com.liferay.portal.theme.ThemeDisplay;
042: import com.liferay.portal.util.ContentUtil;
043: import com.liferay.portal.util.PropsUtil;
044: import com.liferay.portal.util.PropsValues;
045: import com.liferay.portlet.messageboards.model.MBBan;
046: import com.liferay.portlet.messageboards.model.MBCategory;
047: import com.liferay.portlet.messageboards.model.MBMessage;
048: import com.liferay.portlet.messageboards.model.MBStatsUser;
049: import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
050: import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
051: import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
052: import com.liferay.util.Http;
053: import com.liferay.util.LocalizationUtil;
054:
055: import java.io.IOException;
056:
057: import java.util.Calendar;
058: import java.util.Date;
059:
060: import javax.portlet.PortletPreferences;
061: import javax.portlet.PortletURL;
062: import javax.portlet.RenderRequest;
063: import javax.portlet.RenderResponse;
064: import javax.portlet.WindowState;
065:
066: import javax.servlet.jsp.PageContext;
067:
068: import org.apache.commons.logging.Log;
069: import org.apache.commons.logging.LogFactory;
070:
071: /**
072: * <a href="MBUtil.java.html"><b><i>View Source</i></b></a>
073: *
074: * @author Brian Wing Shun Chan
075: *
076: */
077: public class MBUtil {
078:
079: public static final String POP_PORTLET_PREFIX = "mb.";
080:
081: public static String getBreadcrumbs(long categoryId,
082: long messageId, PageContext pageContext, RenderRequest req,
083: RenderResponse res) throws Exception {
084:
085: if (messageId > 0) {
086: MBMessage message = MBMessageLocalServiceUtil
087: .getMessage(messageId);
088:
089: return getBreadcrumbs(null, message, pageContext, req, res);
090: } else {
091: MBCategory category = null;
092:
093: try {
094: if ((categoryId > 0)
095: && (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
096:
097: category = MBCategoryLocalServiceUtil
098: .getCategory(categoryId);
099: }
100: } catch (Exception e) {
101: }
102:
103: return getBreadcrumbs(category, null, pageContext, req, res);
104: }
105: }
106:
107: public static String getBreadcrumbs(MBCategory category,
108: MBMessage message, PageContext pageContext,
109: RenderRequest req, RenderResponse res) throws Exception {
110:
111: if ((message != null) && (category == null)) {
112: category = message.getCategory();
113: }
114:
115: PortletURL categoriesURL = res.createRenderURL();
116:
117: WindowState windowState = req.getWindowState();
118:
119: if (windowState.equals(LiferayWindowState.POP_UP)) {
120: categoriesURL.setWindowState(LiferayWindowState.POP_UP);
121:
122: categoriesURL.setParameter("struts_action",
123: "/message_boards/select_category");
124: } else {
125: categoriesURL.setWindowState(WindowState.MAXIMIZED);
126:
127: categoriesURL.setParameter("struts_action",
128: "/message_boards/view");
129: categoriesURL
130: .setParameter(
131: "categoryId",
132: String
133: .valueOf(MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID));
134: }
135:
136: String categoriesLink = "<a href=\"" + categoriesURL.toString()
137: + "\">" + LanguageUtil.get(pageContext, "categories")
138: + "</a>";
139:
140: if (category == null) {
141: return categoriesLink;
142: }
143:
144: String breadcrumbs = StringPool.BLANK;
145:
146: if (category != null) {
147: for (int i = 0;; i++) {
148: category = category.toEscapedModel();
149:
150: PortletURL portletURL = res.createRenderURL();
151:
152: if (windowState.equals(LiferayWindowState.POP_UP)) {
153: portletURL
154: .setWindowState(LiferayWindowState.POP_UP);
155:
156: portletURL.setParameter("struts_action",
157: "/message_boards/select_category");
158: portletURL.setParameter("categoryId", String
159: .valueOf(category.getCategoryId()));
160: } else {
161: portletURL.setWindowState(WindowState.MAXIMIZED);
162:
163: portletURL.setParameter("struts_action",
164: "/message_boards/view");
165: portletURL.setParameter("categoryId", String
166: .valueOf(category.getCategoryId()));
167: }
168:
169: String categoryLink = "<a href=\""
170: + portletURL.toString() + "\">"
171: + category.getName() + "</a>";
172:
173: if (i == 0) {
174: breadcrumbs = categoryLink;
175: } else {
176: breadcrumbs = categoryLink + " » "
177: + breadcrumbs;
178: }
179:
180: if (category.isRoot()) {
181: break;
182: }
183:
184: category = MBCategoryLocalServiceUtil
185: .getCategory(category.getParentCategoryId());
186: }
187: }
188:
189: breadcrumbs = categoriesLink + " » " + breadcrumbs;
190:
191: if (message != null) {
192: message = message.toEscapedModel();
193:
194: PortletURL messageURL = res.createRenderURL();
195:
196: messageURL.setWindowState(WindowState.MAXIMIZED);
197:
198: messageURL.setParameter("struts_action",
199: "/message_boards/view_message");
200: messageURL.setParameter("messageId", String.valueOf(message
201: .getMessageId()));
202:
203: String messageLink = "<a href=\"" + messageURL.toString()
204: + "\">" + message.getSubject() + "</a>";
205:
206: breadcrumbs = breadcrumbs + " » " + messageLink;
207: }
208:
209: return breadcrumbs;
210: }
211:
212: public static String getEmailFromAddress(PortletPreferences prefs) {
213: String emailFromAddress = PropsUtil
214: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS);
215:
216: return prefs.getValue("email-from-address", emailFromAddress);
217: }
218:
219: public static String getEmailFromName(PortletPreferences prefs) {
220: String emailFromName = PropsUtil
221: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_FROM_NAME);
222:
223: return prefs.getValue("email-from-name", emailFromName);
224: }
225:
226: public static boolean getEmailMessageAddedEnabled(
227: PortletPreferences prefs) {
228:
229: String emailMessageAddedEnabled = prefs.getValue(
230: "email-message-added-enabled", StringPool.BLANK);
231:
232: if (Validator.isNotNull(emailMessageAddedEnabled)) {
233: return GetterUtil.getBoolean(emailMessageAddedEnabled);
234: } else {
235: return GetterUtil
236: .getBoolean(PropsUtil
237: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED));
238: }
239: }
240:
241: public static String getEmailMessageAddedBody(
242: PortletPreferences prefs) throws IOException {
243:
244: String emailMessageAddedBody = prefs.getValue(
245: "email-message-added-body", StringPool.BLANK);
246:
247: if (Validator.isNotNull(emailMessageAddedBody)) {
248: return emailMessageAddedBody;
249: } else {
250: return ContentUtil
251: .get(PropsUtil
252: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY));
253: }
254: }
255:
256: public static String getEmailMessageAddedSignature(
257: PortletPreferences prefs) throws IOException {
258:
259: String emailMessageAddedSignature = prefs.getValue(
260: "email-message-added-signature", StringPool.BLANK);
261:
262: if (Validator.isNotNull(emailMessageAddedSignature)) {
263: return emailMessageAddedSignature;
264: } else {
265: return ContentUtil
266: .get(PropsUtil
267: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SIGNATURE));
268: }
269: }
270:
271: public static String getEmailMessageAddedSubjectPrefix(
272: PortletPreferences prefs) throws IOException {
273:
274: String emailMessageAddedSubjectPrefix = prefs.getValue(
275: "email-message-added-subject-prefix", StringPool.BLANK);
276:
277: if (Validator.isNotNull(emailMessageAddedSubjectPrefix)) {
278: return emailMessageAddedSubjectPrefix;
279: } else {
280: return ContentUtil
281: .get(PropsUtil
282: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT_PREFIX));
283: }
284: }
285:
286: public static boolean getEmailMessageUpdatedEnabled(
287: PortletPreferences prefs) {
288:
289: String emailMessageUpdatedEnabled = prefs.getValue(
290: "email-message-updated-enabled", StringPool.BLANK);
291:
292: if (Validator.isNotNull(emailMessageUpdatedEnabled)) {
293: return GetterUtil.getBoolean(emailMessageUpdatedEnabled);
294: } else {
295: return GetterUtil
296: .getBoolean(PropsUtil
297: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED));
298: }
299: }
300:
301: public static String getEmailMessageUpdatedBody(
302: PortletPreferences prefs) throws IOException {
303:
304: String emailMessageUpdatedBody = prefs.getValue(
305: "email-message-updated-body", StringPool.BLANK);
306:
307: if (Validator.isNotNull(emailMessageUpdatedBody)) {
308: return emailMessageUpdatedBody;
309: } else {
310: return ContentUtil
311: .get(PropsUtil
312: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY));
313: }
314: }
315:
316: public static String getEmailMessageUpdatedSignature(
317: PortletPreferences prefs) throws IOException {
318:
319: String emailMessageUpdatedSignature = prefs.getValue(
320: "email-message-updated-signature", StringPool.BLANK);
321:
322: if (Validator.isNotNull(emailMessageUpdatedSignature)) {
323: return emailMessageUpdatedSignature;
324: } else {
325: return ContentUtil
326: .get(PropsUtil
327: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SIGNATURE));
328: }
329: }
330:
331: public static String getEmailMessageUpdatedSubjectPrefix(
332: PortletPreferences prefs) throws IOException {
333:
334: String emailMessageUpdatedSubject = prefs.getValue(
335: "email-message-updated-subject-prefix",
336: StringPool.BLANK);
337:
338: if (Validator.isNotNull(emailMessageUpdatedSubject)) {
339: return emailMessageUpdatedSubject;
340: } else {
341: return ContentUtil
342: .get(PropsUtil
343: .get(PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT_PREFIX));
344: }
345: }
346:
347: public static String getMailId(String mx, long categoryId,
348: long messageId) {
349: StringMaker sm = new StringMaker();
350:
351: sm.append(StringPool.LESS_THAN);
352: sm.append(POP_PORTLET_PREFIX);
353: sm.append(categoryId);
354: sm.append(StringPool.PERIOD);
355: sm.append(messageId);
356: sm.append(StringPool.AT);
357: sm.append(PropsValues.POP_SERVER_SUBDOMAIN);
358: sm.append(StringPool.PERIOD);
359: sm.append(mx);
360: sm.append(StringPool.GREATER_THAN);
361:
362: return sm.toString();
363: }
364:
365: public static String getMailingListAddress(long categoryId,
366: long messageId, String mx) {
367:
368: StringMaker sm = new StringMaker();
369:
370: sm.append(POP_PORTLET_PREFIX);
371: sm.append(categoryId);
372: sm.append(StringPool.PERIOD);
373: sm.append(messageId);
374: sm.append(StringPool.AT);
375: sm.append(PropsValues.POP_SERVER_SUBDOMAIN);
376: sm.append(StringPool.PERIOD);
377: sm.append(mx);
378:
379: return sm.toString();
380: }
381:
382: public static long getMessageId(String mailId) {
383: int x = mailId.indexOf(StringPool.LESS_THAN) + 1;
384: int y = mailId.indexOf(StringPool.AT);
385:
386: long messageId = 0;
387:
388: if ((x > 0) && (y != -1)) {
389: String temp = mailId.substring(x, y);
390:
391: int z = temp.indexOf(StringPool.PERIOD);
392:
393: if (z != -1) {
394: messageId = GetterUtil.getLong(temp.substring(z));
395: }
396: }
397:
398: return messageId;
399: }
400:
401: public static String[] getThreadPriority(PortletPreferences prefs,
402: String languageId, double value, ThemeDisplay themeDisplay)
403: throws Exception {
404:
405: String[] priorities = LocalizationUtil.getPrefsValues(prefs,
406: "priorities", languageId);
407:
408: String[] priorityPair = _findThreadPriority(value,
409: themeDisplay, priorities);
410:
411: if (priorityPair == null) {
412: String defaultLanguageId = LocaleUtil
413: .toLanguageId(LocaleUtil.getDefault());
414:
415: priorities = LocalizationUtil.getPrefsValues(prefs,
416: "priorities", defaultLanguageId);
417:
418: priorityPair = _findThreadPriority(value, themeDisplay,
419: priorities);
420: }
421:
422: return priorityPair;
423: }
424:
425: public static Date getUnbanDate(MBBan ban, int expireInterval) {
426: Date banDate = ban.getCreateDate();
427:
428: Calendar cal = Calendar.getInstance();
429:
430: cal.setTime(banDate);
431:
432: cal.add(Calendar.DATE, expireInterval);
433:
434: return cal.getTime();
435: }
436:
437: public static String getUserRank(PortletPreferences prefs,
438: String languageId, int posts) throws Exception {
439:
440: String rank = StringPool.BLANK;
441:
442: String[] ranks = LocalizationUtil.getPrefsValues(prefs,
443: "ranks", languageId);
444:
445: for (int i = 0; i < ranks.length; i++) {
446: String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
447:
448: String kvpName = kvp[0];
449: int kvpPosts = GetterUtil.getInteger(kvp[1]);
450:
451: if (posts >= kvpPosts) {
452: rank = kvpName;
453: } else {
454: break;
455: }
456: }
457:
458: return rank;
459: }
460:
461: public static String getUserRank(PortletPreferences prefs,
462: String languageId, MBStatsUser statsUser) throws Exception {
463:
464: String rank = StringPool.BLANK;
465:
466: Group group = GroupLocalServiceUtil.getGroup(statsUser
467: .getGroupId());
468:
469: long companyId = group.getCompanyId();
470:
471: String[] ranks = LocalizationUtil.getPrefsValues(prefs,
472: "ranks", languageId);
473:
474: for (int i = 0; i < ranks.length; i++) {
475: String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
476:
477: String curRank = kvp[0];
478: String curRankValue = kvp[1];
479:
480: String[] curRankValueKvp = StringUtil.split(curRankValue,
481: StringPool.COLON);
482:
483: if (curRankValueKvp.length <= 1) {
484: int kvpPosts = GetterUtil.getInteger(curRankValue);
485:
486: if (statsUser.getMessageCount() >= kvpPosts) {
487: rank = curRank;
488: }
489:
490: continue;
491: }
492:
493: String entityType = curRankValueKvp[0];
494: String entityValue = curRankValueKvp[1];
495:
496: try {
497: if (_isEntityRank(companyId, statsUser, entityType,
498: entityValue)) {
499:
500: return curRank;
501: }
502: } catch (Exception e) {
503: if (_log.isWarnEnabled()) {
504: _log.warn(e);
505: }
506: }
507: }
508:
509: return rank;
510: }
511:
512: public static boolean isAllowAnonymousPosting(
513: PortletPreferences prefs) {
514: String allowAnonymousPosting = prefs.getValue(
515: "allow-anonymous-posting", StringPool.BLANK);
516:
517: if (Validator.isNotNull(allowAnonymousPosting)) {
518: return GetterUtil.getBoolean(allowAnonymousPosting);
519: } else {
520: return PropsValues.MESSAGE_BOARDS_ANONYMOUS_POSTING_ENABLED;
521: }
522: }
523:
524: private static String[] _findThreadPriority(double value,
525: ThemeDisplay themeDisplay, String[] priorities) {
526:
527: for (int i = 0; i < priorities.length; i++) {
528: String[] priority = StringUtil.split(priorities[i]);
529:
530: try {
531: String priorityName = priority[0];
532: String priorityImage = priority[1];
533: double priorityValue = GetterUtil
534: .getDouble(priority[2]);
535:
536: if (value == priorityValue) {
537: if (!priorityImage.startsWith(Http.HTTP)) {
538: priorityImage = themeDisplay
539: .getPathThemeImages()
540: + priorityImage;
541: }
542:
543: return new String[] { priorityName, priorityImage };
544: }
545: } catch (Exception e) {
546: }
547: }
548:
549: return null;
550: }
551:
552: private static boolean _isEntityRank(long companyId,
553: MBStatsUser statsUser, String entityType, String entityValue)
554: throws Exception {
555:
556: long groupId = statsUser.getGroupId();
557: long userId = statsUser.getUserId();
558:
559: if (entityType.equals("community-role")
560: || entityType.equals("organization-role")) {
561:
562: Role role = RoleLocalServiceUtil.getRole(companyId,
563: entityValue);
564:
565: if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(userId,
566: groupId, role.getRoleId())) {
567:
568: return true;
569: }
570: } else if (entityType.equals("organization")) {
571: Organization organization = OrganizationLocalServiceUtil
572: .getOrganization(companyId, entityValue);
573:
574: if (OrganizationLocalServiceUtil.hasUserOrganization(
575: userId, organization.getOrganizationId())) {
576:
577: return true;
578: }
579: } else if (entityType.equals("regular-role")) {
580: if (RoleLocalServiceUtil.hasUserRole(userId, companyId,
581: entityValue, true)) {
582:
583: return true;
584: }
585: } else if (entityType.equals("user-group")) {
586: UserGroup userGroup = UserGroupLocalServiceUtil
587: .getUserGroup(companyId, entityValue);
588:
589: if (UserLocalServiceUtil.hasUserGroupUser(userGroup
590: .getUserGroupId(), userId)) {
591:
592: return true;
593: }
594: }
595:
596: return false;
597: }
598:
599: private static Log _log = LogFactory.getLog(MBUtil.class);
600:
601: }
|