1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.util;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.portlet.LiferayWindowState;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.LocaleUtil;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.StringUtil;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.ContentUtil;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.PropsUtil;
37  import com.liferay.portlet.messageboards.model.MBBan;
38  import com.liferay.portlet.messageboards.model.MBCategory;
39  import com.liferay.portlet.messageboards.model.MBMessage;
40  import com.liferay.portlet.messageboards.model.impl.MBCategoryImpl;
41  import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
42  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
43  import com.liferay.util.Http;
44  import com.liferay.util.LocalizationUtil;
45  
46  import java.io.IOException;
47  
48  import java.util.Calendar;
49  import java.util.Date;
50  
51  import javax.portlet.PortletPreferences;
52  import javax.portlet.PortletURL;
53  import javax.portlet.RenderRequest;
54  import javax.portlet.RenderResponse;
55  import javax.portlet.WindowState;
56  
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.jsp.PageContext;
59  
60  /**
61   * <a href="MBUtil.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class MBUtil {
67  
68      public static final String SMTP_PORTLET_PREFIX = "mb.";
69  
70      public static String getBreadcrumbs(
71              long categoryId, long messageId, PageContext pageContext,
72              RenderRequest req, RenderResponse res)
73          throws Exception {
74  
75          if (messageId > 0) {
76              MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
77  
78              return getBreadcrumbs(null, message, pageContext, req, res);
79          }
80          else {
81              MBCategory category = null;
82  
83              try {
84                  if ((categoryId > 0) &&
85                      (categoryId != MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID)) {
86  
87                      category = MBCategoryLocalServiceUtil.getCategory(
88                          categoryId);
89                  }
90              }
91              catch (Exception e) {
92              }
93  
94              return getBreadcrumbs(category, null, pageContext, req, res);
95          }
96      }
97  
98      public static String getBreadcrumbs(
99              MBCategory category, MBMessage message, PageContext pageContext,
100             RenderRequest req, RenderResponse res)
101         throws Exception {
102 
103         if ((message != null) && (category == null)) {
104             category = message.getCategory();
105         }
106 
107         PortletURL categoriesURL = res.createRenderURL();
108 
109         WindowState windowState = req.getWindowState();
110 
111         if (windowState.equals(LiferayWindowState.POP_UP)) {
112             categoriesURL.setWindowState(LiferayWindowState.POP_UP);
113 
114             categoriesURL.setParameter(
115                 "struts_action", "/message_boards/select_category");
116         }
117         else {
118             categoriesURL.setWindowState(WindowState.MAXIMIZED);
119 
120             categoriesURL.setParameter("struts_action", "/message_boards/view");
121             categoriesURL.setParameter(
122                 "categoryId",
123                 String.valueOf(MBCategoryImpl.DEFAULT_PARENT_CATEGORY_ID));
124         }
125 
126         String categoriesLink =
127             "<a href=\"" + categoriesURL.toString() + "\">" +
128                 LanguageUtil.get(pageContext, "categories") + "</a>";
129 
130         if (category == null) {
131             return categoriesLink;
132         }
133 
134         String breadcrumbs = StringPool.BLANK;
135 
136         if (category != null) {
137             for (int i = 0;; i++) {
138                 PortletURL portletURL = res.createRenderURL();
139 
140                 if (windowState.equals(LiferayWindowState.POP_UP)) {
141                     portletURL.setWindowState(LiferayWindowState.POP_UP);
142 
143                     portletURL.setParameter(
144                         "struts_action", "/message_boards/select_category");
145                     portletURL.setParameter(
146                         "categoryId", String.valueOf(category.getCategoryId()));
147                 }
148                 else {
149                     portletURL.setWindowState(WindowState.MAXIMIZED);
150 
151                     portletURL.setParameter(
152                         "struts_action", "/message_boards/view");
153                     portletURL.setParameter(
154                         "categoryId", String.valueOf(category.getCategoryId()));
155                 }
156 
157                 String categoryLink =
158                     "<a href=\"" + portletURL.toString() + "\">" +
159                         category.getName() + "</a>";
160 
161                 if (i == 0) {
162                     breadcrumbs = categoryLink;
163                 }
164                 else {
165                     breadcrumbs = categoryLink + " &raquo; " + breadcrumbs;
166                 }
167 
168                 if (category.isRoot()) {
169                     break;
170                 }
171 
172                 category = MBCategoryLocalServiceUtil.getCategory(
173                     category.getParentCategoryId());
174             }
175         }
176 
177         breadcrumbs = categoriesLink + " &raquo; " + breadcrumbs;
178 
179         if (message != null) {
180             PortletURL messageURL = res.createRenderURL();
181 
182             messageURL.setWindowState(WindowState.MAXIMIZED);
183 
184             messageURL.setParameter(
185                 "struts_action", "/message_boards/view_message");
186             messageURL.setParameter(
187                 "messageId", String.valueOf(message.getMessageId()));
188 
189             String messageLink =
190                 "<a href=\"" + messageURL.toString() + "\">" +
191                     message.getSubject() + "</a>";
192 
193             breadcrumbs = breadcrumbs + " &raquo; " + messageLink;
194         }
195 
196         return breadcrumbs;
197     }
198 
199     public static String getEmailFromAddress(PortletPreferences prefs) {
200         String emailFromAddress = PropsUtil.get(
201             PropsUtil.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS);
202 
203         return prefs.getValue("email-from-address", emailFromAddress);
204     }
205 
206     public static String getEmailFromName(PortletPreferences prefs) {
207         String emailFromName = PropsUtil.get(
208             PropsUtil.MESSAGE_BOARDS_EMAIL_FROM_NAME);
209 
210         return prefs.getValue("email-from-name", emailFromName);
211     }
212 
213     public static boolean getEmailMessageAddedEnabled(
214         PortletPreferences prefs) {
215 
216         String emailMessageAddedEnabled = prefs.getValue(
217             "email-message-added-enabled", StringPool.BLANK);
218 
219         if (Validator.isNotNull(emailMessageAddedEnabled)) {
220             return GetterUtil.getBoolean(emailMessageAddedEnabled);
221         }
222         else {
223             return GetterUtil.getBoolean(PropsUtil.get(
224                 PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED));
225         }
226     }
227 
228     public static String getEmailMessageAddedBody(PortletPreferences prefs)
229         throws IOException {
230 
231         String emailMessageAddedBody = prefs.getValue(
232             "email-message-added-body", StringPool.BLANK);
233 
234         if (Validator.isNotNull(emailMessageAddedBody)) {
235             return emailMessageAddedBody;
236         }
237         else {
238             return ContentUtil.get(PropsUtil.get(
239                 PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY));
240         }
241     }
242 
243     public static String getEmailMessageAddedSignature(PortletPreferences prefs)
244         throws IOException {
245 
246         String emailMessageAddedSignature = prefs.getValue(
247             "email-message-added-signature", StringPool.BLANK);
248 
249         if (Validator.isNotNull(emailMessageAddedSignature)) {
250             return emailMessageAddedSignature;
251         }
252         else {
253             return ContentUtil.get(PropsUtil.get(
254                 PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SIGNATURE));
255         }
256     }
257 
258     public static String getEmailMessageAddedSubjectPrefix(
259             PortletPreferences prefs)
260         throws IOException {
261 
262         String emailMessageAddedSubjectPrefix = prefs.getValue(
263             "email-message-added-subject-prefix", StringPool.BLANK);
264 
265         if (Validator.isNotNull(emailMessageAddedSubjectPrefix)) {
266             return emailMessageAddedSubjectPrefix;
267         }
268         else {
269             return ContentUtil.get(PropsUtil.get(
270                 PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT_PREFIX));
271         }
272     }
273 
274     public static boolean getEmailMessageUpdatedEnabled(
275         PortletPreferences prefs) {
276 
277         String emailMessageUpdatedEnabled = prefs.getValue(
278             "email-message-updated-enabled", StringPool.BLANK);
279 
280         if (Validator.isNotNull(emailMessageUpdatedEnabled)) {
281             return GetterUtil.getBoolean(emailMessageUpdatedEnabled);
282         }
283         else {
284             return GetterUtil.getBoolean(PropsUtil.get(
285                 PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED));
286         }
287     }
288 
289     public static String getEmailMessageUpdatedBody(PortletPreferences prefs)
290         throws IOException {
291 
292         String emailMessageUpdatedBody = prefs.getValue(
293             "email-message-updated-body", StringPool.BLANK);
294 
295         if (Validator.isNotNull(emailMessageUpdatedBody)) {
296             return emailMessageUpdatedBody;
297         }
298         else {
299             return ContentUtil.get(PropsUtil.get(
300                 PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY));
301         }
302     }
303 
304     public static String getEmailMessageUpdatedSignature(
305             PortletPreferences prefs)
306         throws IOException {
307 
308         String emailMessageUpdatedSignature = prefs.getValue(
309             "email-message-updated-signature", StringPool.BLANK);
310 
311         if (Validator.isNotNull(emailMessageUpdatedSignature)) {
312             return emailMessageUpdatedSignature;
313         }
314         else {
315             return ContentUtil.get(PropsUtil.get(
316                 PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SIGNATURE));
317         }
318     }
319 
320     public static String getEmailMessageUpdatedSubjectPrefix(
321             PortletPreferences prefs)
322         throws IOException {
323 
324         String emailMessageUpdatedSubject = prefs.getValue(
325             "email-message-updated-subject-prefix", StringPool.BLANK);
326 
327         if (Validator.isNotNull(emailMessageUpdatedSubject)) {
328             return emailMessageUpdatedSubject;
329         }
330         else {
331             return ContentUtil.get(PropsUtil.get(
332                 PropsUtil.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT_PREFIX));
333         }
334     }
335 
336     public static String getMailId(String mx, long categoryId, long messageId) {
337         StringMaker sm = new StringMaker();
338 
339         sm.append(StringPool.LESS_THAN);
340         sm.append(SMTP_PORTLET_PREFIX);
341         sm.append(categoryId);
342         sm.append(StringPool.PERIOD);
343         sm.append(messageId);
344         sm.append(StringPool.AT);
345         sm.append(PropsUtil.get(PropsUtil.SMTP_SERVER_SUBDOMAIN));
346         sm.append(StringPool.PERIOD);
347         sm.append(mx);
348         sm.append(StringPool.GREATER_THAN);
349 
350         return sm.toString();
351     }
352 
353     public static String getMailingListAddress(
354         long categoryId, long messageId, String mx) {
355 
356         StringMaker sm = new StringMaker();
357 
358         sm.append(SMTP_PORTLET_PREFIX);
359         sm.append(categoryId);
360         sm.append(StringPool.PERIOD);
361         sm.append(messageId);
362         sm.append(StringPool.AT);
363         sm.append(PropsUtil.get(PropsUtil.SMTP_SERVER_SUBDOMAIN));
364         sm.append(StringPool.PERIOD);
365         sm.append(mx);
366 
367         return sm.toString();
368     }
369 
370     public static long getMessageId(String mailId) {
371         int x = mailId.indexOf(StringPool.LESS_THAN) + 1;
372         int y = mailId.indexOf(StringPool.AT);
373 
374         long messageId = 0;
375 
376         if ((x > 0 ) && (y != -1)) {
377             String temp = mailId.substring(x, y);
378 
379             int z = temp.indexOf(StringPool.PERIOD);
380 
381             if (z != -1) {
382                 messageId = GetterUtil.getLong(temp.substring(z));
383             }
384         }
385 
386         return messageId;
387     }
388 
389     public static int getRSSContentLength(PortletPreferences prefs) {
390         String rssContentLength = PropsUtil.get(
391             PropsUtil.MESSAGE_BOARDS_RSS_CONTENT_LENGTH);
392 
393         return GetterUtil.getInteger(
394             prefs.getValue("rss-content-length", rssContentLength), 80);
395     }
396 
397     public static String[] getThreadPriority(
398             PortletPreferences prefs, String languageId, double value,
399             ThemeDisplay themeDisplay)
400         throws Exception {
401 
402         String[] priorities = LocalizationUtil.getPrefsValues(
403             prefs, "priorities", languageId);
404 
405         String[] priorityPair = _findThreadPriority(
406             value, themeDisplay, priorities);
407 
408         if (priorityPair == null) {
409             String defaultLanguageId = LocaleUtil.toLanguageId(
410                 LocaleUtil.getDefault());
411 
412             priorities = LocalizationUtil.getPrefsValues(
413                 prefs, "priorities", defaultLanguageId);
414 
415             priorityPair = _findThreadPriority(value, themeDisplay, priorities);
416         }
417 
418         return priorityPair;
419     }
420 
421     public static Date getUnbanDate(MBBan ban, int expireInterval) {
422         Date banDate = ban.getCreateDate();
423 
424         Calendar cal = Calendar.getInstance();
425 
426         cal.setTime(banDate);
427 
428         cal.add(Calendar.DATE, expireInterval);
429 
430         return cal.getTime();
431     }
432 
433     public static String getUserName(
434         long userId, String defaultUserName, PortletPreferences prefs) {
435 
436         return getUserName(userId, defaultUserName, null, prefs);
437     }
438 
439     public static String getUserName(
440         long userId, String defaultUserName, HttpServletRequest req,
441         PortletPreferences prefs) {
442 
443         String userAttribute = getUserNameAttribute(prefs);
444 
445         return PortalUtil.getUserName(
446             userId, defaultUserName, userAttribute, req);
447     }
448 
449     public static String getUserNameAttribute(PortletPreferences prefs) {
450         String userNameAttribute = PropsUtil.get(
451             PropsUtil.MESSAGE_BOARDS_USER_NAME_ATTRIBUTE);
452 
453         return prefs.getValue("user-name-attribute", userNameAttribute);
454     }
455 
456     public static String getUserRank(
457             PortletPreferences prefs, String languageId, int posts)
458         throws Exception {
459 
460         String rank = StringPool.BLANK;
461 
462         String[] ranks = LocalizationUtil.getPrefsValues(
463             prefs, "ranks", languageId);
464 
465         for (int i = 0; i < ranks.length; i++) {
466             String[] kvp = StringUtil.split(ranks[i], StringPool.EQUAL);
467 
468             String kvpName = kvp[0];
469             int kvpPosts = GetterUtil.getInteger(kvp[1]);
470 
471             if (posts >= kvpPosts) {
472                 rank = kvpName;
473             }
474             else {
475                 break;
476             }
477         }
478 
479         return rank;
480     }
481 
482     private static String[] _findThreadPriority(
483         double value, ThemeDisplay themeDisplay, String[] priorities) {
484 
485         for (int i = 0; i < priorities.length; i++) {
486             String[] priority = StringUtil.split(priorities[i]);
487 
488             try {
489                 String priorityName = priority[0];
490                 String priorityImage = priority[1];
491                 double priorityValue = GetterUtil.getDouble(priority[2]);
492 
493                 if (value == priorityValue) {
494                     if (!priorityImage.startsWith(Http.HTTP)) {
495                         priorityImage =
496                             themeDisplay.getPathThemeImages() + priorityImage;
497                     }
498 
499                     return new String[] {priorityName, priorityImage};
500                 }
501             }
502             catch (Exception e) {
503             }
504         }
505 
506         return null;
507     }
508 
509 }