1   /**
2    * Copyright (c) 2000-2009 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.blogs.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.PropsKeys;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.util.ContentUtil;
30  import com.liferay.portal.util.FriendlyURLNormalizer;
31  import com.liferay.portal.util.PropsUtil;
32  import com.liferay.portal.util.PropsValues;
33  
34  import javax.portlet.PortletPreferences;
35  
36  /**
37   * <a href="BlogsUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Thiago Moreira
41   */
42  public class BlogsUtil {
43  
44      public static final String POP_PORTLET_PREFIX = "blogs.";
45  
46      public static String getEmailEntryAddedBody(
47          PortletPreferences preferences) {
48  
49          String emailEntryAddedBody = preferences.getValue(
50              "email-entry-added-body", StringPool.BLANK);
51  
52          if (Validator.isNotNull(emailEntryAddedBody)) {
53              return emailEntryAddedBody;
54          }
55          else {
56              return ContentUtil.get(PropsUtil.get(
57                  PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_BODY));
58          }
59      }
60  
61      public static boolean getEmailEntryAddedEnabled(
62          PortletPreferences preferences) {
63  
64          String emailEntryAddedEnabled = preferences.getValue(
65              "email-entry-added-enabled", StringPool.BLANK);
66  
67          if (Validator.isNotNull(emailEntryAddedEnabled)) {
68              return GetterUtil.getBoolean(emailEntryAddedEnabled);
69          }
70          else {
71              return GetterUtil.getBoolean(PropsUtil.get(
72                  PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_ENABLED));
73          }
74      }
75  
76      public static String getEmailEntryAddedSubject(
77          PortletPreferences preferences) {
78  
79          String emailEntryAddedSubject = preferences.getValue(
80              "email-entry-added-subject", StringPool.BLANK);
81  
82          if (Validator.isNotNull(emailEntryAddedSubject)) {
83              return emailEntryAddedSubject;
84          }
85          else {
86              return ContentUtil.get(PropsUtil.get(
87                  PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_SUBJECT));
88          }
89      }
90  
91      public static String getEmailEntryUpdatedBody(
92          PortletPreferences preferences) {
93  
94          String emailEntryUpdatedBody = preferences.getValue(
95              "email-entry-updated-body", StringPool.BLANK);
96  
97          if (Validator.isNotNull(emailEntryUpdatedBody)) {
98              return emailEntryUpdatedBody;
99          }
100         else {
101             return ContentUtil.get(PropsUtil.get(
102                 PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_BODY));
103         }
104     }
105 
106     public static boolean getEmailEntryUpdatedEnabled(
107         PortletPreferences preferences) {
108 
109         String emailEntryUpdatedEnabled = preferences.getValue(
110             "email-entry-updated-enabled", StringPool.BLANK);
111 
112         if (Validator.isNotNull(emailEntryUpdatedEnabled)) {
113             return GetterUtil.getBoolean(emailEntryUpdatedEnabled);
114         }
115         else {
116             return GetterUtil.getBoolean(PropsUtil.get(
117                 PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_ENABLED));
118         }
119     }
120 
121     public static String getEmailEntryUpdatedSubject(
122         PortletPreferences preferences) {
123 
124         String emailEntryUpdatedSubject = preferences.getValue(
125             "email-entry-updated-subject", StringPool.BLANK);
126 
127         if (Validator.isNotNull(emailEntryUpdatedSubject)) {
128             return emailEntryUpdatedSubject;
129         }
130         else {
131             return ContentUtil.get(PropsUtil.get(
132                 PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_SUBJECT));
133         }
134     }
135 
136     public static String getEmailFromAddress(PortletPreferences preferences) {
137         String emailFromAddress = PropsUtil.get(
138             PropsKeys.BLOGS_EMAIL_FROM_ADDRESS);
139 
140         return preferences.getValue("email-from-address", emailFromAddress);
141     }
142 
143     public static String getEmailFromName(PortletPreferences preferences) {
144         String emailFromName = PropsUtil.get(PropsKeys.BLOGS_EMAIL_FROM_NAME);
145 
146         return preferences.getValue("email-from-name", emailFromName);
147     }
148 
149     public static String getMailId(String mx, long entryId) {
150         StringBuilder sb = new StringBuilder();
151 
152         sb.append(StringPool.LESS_THAN);
153         sb.append(POP_PORTLET_PREFIX);
154         sb.append(entryId);
155         sb.append(StringPool.AT);
156         sb.append(PropsValues.POP_SERVER_SUBDOMAIN);
157         sb.append(StringPool.PERIOD);
158         sb.append(mx);
159         sb.append(StringPool.GREATER_THAN);
160 
161         return sb.toString();
162     }
163 
164     public static String getUrlTitle(long entryId, String title) {
165         title = title.trim().toLowerCase();
166 
167         if (Validator.isNull(title) || Validator.isNumber(title) ||
168             title.equals("rss")) {
169 
170             return String.valueOf(entryId);
171         }
172         else {
173             return FriendlyURLNormalizer.normalize(
174                 title, _URL_TITLE_REPLACE_CHARS);
175         }
176     }
177 
178     private static final char[] _URL_TITLE_REPLACE_CHARS = new char[] {
179         '.', '/'
180     };
181 
182 }