1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.util;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.PropsKeys;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.util.ContentUtil;
23  import com.liferay.portal.util.FriendlyURLNormalizer;
24  import com.liferay.portal.util.PropsUtil;
25  import com.liferay.portal.util.PropsValues;
26  
27  import javax.portlet.PortletPreferences;
28  
29  /**
30   * <a href="BlogsUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   * @author Thiago Moreira
34   */
35  public class BlogsUtil {
36  
37      public static final String POP_PORTLET_PREFIX = "blogs.";
38  
39      public static String getEmailEntryAddedBody(
40          PortletPreferences preferences) {
41  
42          String emailEntryAddedBody = preferences.getValue(
43              "email-entry-added-body", StringPool.BLANK);
44  
45          if (Validator.isNotNull(emailEntryAddedBody)) {
46              return emailEntryAddedBody;
47          }
48          else {
49              return ContentUtil.get(PropsUtil.get(
50                  PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_BODY));
51          }
52      }
53  
54      public static boolean getEmailEntryAddedEnabled(
55          PortletPreferences preferences) {
56  
57          String emailEntryAddedEnabled = preferences.getValue(
58              "email-entry-added-enabled", StringPool.BLANK);
59  
60          if (Validator.isNotNull(emailEntryAddedEnabled)) {
61              return GetterUtil.getBoolean(emailEntryAddedEnabled);
62          }
63          else {
64              return GetterUtil.getBoolean(PropsUtil.get(
65                  PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_ENABLED));
66          }
67      }
68  
69      public static String getEmailEntryAddedSubject(
70          PortletPreferences preferences) {
71  
72          String emailEntryAddedSubject = preferences.getValue(
73              "email-entry-added-subject", StringPool.BLANK);
74  
75          if (Validator.isNotNull(emailEntryAddedSubject)) {
76              return emailEntryAddedSubject;
77          }
78          else {
79              return ContentUtil.get(PropsUtil.get(
80                  PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_SUBJECT));
81          }
82      }
83  
84      public static String getEmailEntryUpdatedBody(
85          PortletPreferences preferences) {
86  
87          String emailEntryUpdatedBody = preferences.getValue(
88              "email-entry-updated-body", StringPool.BLANK);
89  
90          if (Validator.isNotNull(emailEntryUpdatedBody)) {
91              return emailEntryUpdatedBody;
92          }
93          else {
94              return ContentUtil.get(PropsUtil.get(
95                  PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_BODY));
96          }
97      }
98  
99      public static boolean getEmailEntryUpdatedEnabled(
100         PortletPreferences preferences) {
101 
102         String emailEntryUpdatedEnabled = preferences.getValue(
103             "email-entry-updated-enabled", StringPool.BLANK);
104 
105         if (Validator.isNotNull(emailEntryUpdatedEnabled)) {
106             return GetterUtil.getBoolean(emailEntryUpdatedEnabled);
107         }
108         else {
109             return GetterUtil.getBoolean(PropsUtil.get(
110                 PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_ENABLED));
111         }
112     }
113 
114     public static String getEmailEntryUpdatedSubject(
115         PortletPreferences preferences) {
116 
117         String emailEntryUpdatedSubject = preferences.getValue(
118             "email-entry-updated-subject", StringPool.BLANK);
119 
120         if (Validator.isNotNull(emailEntryUpdatedSubject)) {
121             return emailEntryUpdatedSubject;
122         }
123         else {
124             return ContentUtil.get(PropsUtil.get(
125                 PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_SUBJECT));
126         }
127     }
128 
129     public static String getEmailFromAddress(PortletPreferences preferences) {
130         String emailFromAddress = PropsUtil.get(
131             PropsKeys.BLOGS_EMAIL_FROM_ADDRESS);
132 
133         return preferences.getValue("email-from-address", emailFromAddress);
134     }
135 
136     public static String getEmailFromName(PortletPreferences preferences) {
137         String emailFromName = PropsUtil.get(PropsKeys.BLOGS_EMAIL_FROM_NAME);
138 
139         return preferences.getValue("email-from-name", emailFromName);
140     }
141 
142     public static String getMailId(String mx, long entryId) {
143         StringBundler sb = new StringBundler(8);
144 
145         sb.append(StringPool.LESS_THAN);
146         sb.append(POP_PORTLET_PREFIX);
147         sb.append(entryId);
148         sb.append(StringPool.AT);
149         sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
150         sb.append(StringPool.PERIOD);
151         sb.append(mx);
152         sb.append(StringPool.GREATER_THAN);
153 
154         return sb.toString();
155     }
156 
157     public static String getUrlTitle(long entryId, String title) {
158         title = title.trim().toLowerCase();
159 
160         if (Validator.isNull(title) || Validator.isNumber(title) ||
161             title.equals("rss")) {
162 
163             return String.valueOf(entryId);
164         }
165         else {
166             return FriendlyURLNormalizer.normalize(
167                 title, _URL_TITLE_REPLACE_CHARS);
168         }
169     }
170 
171     private static final char[] _URL_TITLE_REPLACE_CHARS = new char[] {
172         '.', '/'
173     };
174 
175 }