001    /**
002     * Copyright (c) 2000-2013 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.blogs.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.search.Document;
021    import com.liferay.portal.kernel.search.Field;
022    import com.liferay.portal.kernel.search.Hits;
023    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.kernel.util.LocaleUtil;
026    import com.liferay.portal.kernel.util.LocalizationUtil;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.kernel.util.StringPool;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.ModelHintsUtil;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PropsUtil;
033    import com.liferay.portal.util.PropsValues;
034    import com.liferay.portlet.blogs.model.BlogsEntry;
035    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
036    import com.liferay.portlet.messageboards.model.MBMessage;
037    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
038    import com.liferay.util.ContentUtil;
039    
040    import java.util.ArrayList;
041    import java.util.List;
042    import java.util.Locale;
043    import java.util.Map;
044    
045    import javax.portlet.PortletPreferences;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Thiago Moreira
050     */
051    public class BlogsUtil {
052    
053            public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
054    
055            public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
056    
057            public static final String DISPLAY_STYLE_TITLE = "title";
058    
059            public static Map<Locale, String> getEmailEntryAddedBodyMap(
060                    PortletPreferences preferences) {
061    
062                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
063                            preferences, "emailEntryAddedBody");
064    
065                    Locale defaultLocale = LocaleUtil.getDefault();
066    
067                    String defaultValue = map.get(defaultLocale);
068    
069                    if (Validator.isNotNull(defaultValue)) {
070                            return map;
071                    }
072    
073                    map.put(
074                            defaultLocale,
075                            ContentUtil.get(
076                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_BODY)));
077    
078                    return map;
079            }
080    
081            public static boolean getEmailEntryAddedEnabled(
082                    PortletPreferences preferences) {
083    
084                    String emailEntryAddedEnabled = preferences.getValue(
085                            "emailEntryAddedEnabled", StringPool.BLANK);
086    
087                    if (Validator.isNotNull(emailEntryAddedEnabled)) {
088                            return GetterUtil.getBoolean(emailEntryAddedEnabled);
089                    }
090                    else {
091                            return GetterUtil.getBoolean(
092                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_ENABLED));
093                    }
094            }
095    
096            public static Map<Locale, String> getEmailEntryAddedSubjectMap(
097                    PortletPreferences preferences) {
098    
099                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
100                            preferences, "emailEntryAddedSubject");
101    
102                    Locale defaultLocale = LocaleUtil.getDefault();
103    
104                    String defaultValue = map.get(defaultLocale);
105    
106                    if (Validator.isNotNull(defaultValue)) {
107                            return map;
108                    }
109    
110                    map.put(
111                            defaultLocale,
112                            ContentUtil.get(
113                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_SUBJECT)));
114    
115                    return map;
116            }
117    
118            public static Map<Locale, String> getEmailEntryUpdatedBodyMap(
119                    PortletPreferences preferences) {
120    
121                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
122                            preferences, "emailEntryUpdatedBody");
123    
124                    Locale defaultLocale = LocaleUtil.getDefault();
125    
126                    String defaultValue = map.get(defaultLocale);
127    
128                    if (Validator.isNotNull(defaultValue)) {
129                            return map;
130                    }
131    
132                    map.put(
133                            defaultLocale,
134                            ContentUtil.get(
135                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_BODY)));
136    
137                    return map;
138            }
139    
140            public static boolean getEmailEntryUpdatedEnabled(
141                    PortletPreferences preferences) {
142    
143                    String emailEntryUpdatedEnabled = preferences.getValue(
144                            "emailEntryUpdatedEnabled", StringPool.BLANK);
145    
146                    if (Validator.isNotNull(emailEntryUpdatedEnabled)) {
147                            return GetterUtil.getBoolean(emailEntryUpdatedEnabled);
148                    }
149                    else {
150                            return GetterUtil.getBoolean(
151                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_ENABLED));
152                    }
153            }
154    
155            public static Map<Locale, String> getEmailEntryUpdatedSubjectMap(
156                    PortletPreferences preferences) {
157    
158                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
159                            preferences, "emailEntryUpdatedSubject");
160    
161                    Locale defaultLocale = LocaleUtil.getDefault();
162    
163                    String defaultValue = map.get(defaultLocale);
164    
165                    if (Validator.isNotNull(defaultValue)) {
166                            return map;
167                    }
168    
169                    map.put(
170                            defaultLocale,
171                            ContentUtil.get(
172                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_SUBJECT)));
173    
174                    return map;
175            }
176    
177            public static String getEmailFromAddress(
178                            PortletPreferences preferences, long companyId)
179                    throws SystemException {
180    
181                    return PortalUtil.getEmailFromAddress(
182                            preferences, companyId, PropsValues.BLOGS_EMAIL_FROM_ADDRESS);
183            }
184    
185            public static String getEmailFromName(
186                            PortletPreferences preferences, long companyId)
187                    throws SystemException {
188    
189                    return PortalUtil.getEmailFromName(
190                            preferences, companyId, PropsValues.BLOGS_EMAIL_FROM_NAME);
191            }
192    
193            public static List<Object> getEntries(Hits hits) {
194                    List<Object> entries = new ArrayList<Object>();
195    
196                    for (Document document : hits.getDocs()) {
197                            String entryClassName = GetterUtil.getString(
198                                    document.get(Field.ENTRY_CLASS_NAME));
199                            long entryClassPK = GetterUtil.getLong(
200                                    document.get(Field.ENTRY_CLASS_PK));
201    
202                            try {
203                                    Object obj = null;
204    
205                                    if (entryClassName.equals(BlogsEntry.class.getName())) {
206                                            obj = BlogsEntryLocalServiceUtil.getEntry(entryClassPK);
207                                    }
208                                    else if (entryClassName.equals(MBMessage.class.getName())) {
209                                            long classPK = GetterUtil.getLong(
210                                                    document.get(Field.CLASS_PK));
211    
212                                            BlogsEntryLocalServiceUtil.getEntry(classPK);
213    
214                                            obj = MBMessageLocalServiceUtil.getMessage(entryClassPK);
215                                    }
216    
217                                    entries.add(obj);
218                            }
219                            catch (Exception e) {
220                                    if (_log.isWarnEnabled()) {
221                                            _log.warn(
222                                                    "Blogs search index is stale and contains entry " +
223                                                            "{className=" + entryClassName + ", classPK=" +
224                                                                    entryClassPK + "}");
225                                    }
226                            }
227                    }
228    
229                    return entries;
230            }
231    
232            public static String getUrlTitle(long entryId, String title) {
233                    if (title == null) {
234                            return String.valueOf(entryId);
235                    }
236    
237                    title = title.trim().toLowerCase();
238    
239                    if (Validator.isNull(title) || Validator.isNumber(title) ||
240                            title.equals("rss")) {
241    
242                            title = String.valueOf(entryId);
243                    }
244                    else {
245                            title = FriendlyURLNormalizerUtil.normalize(
246                                    title, _URL_TITLE_REPLACE_CHARS);
247                    }
248    
249                    return ModelHintsUtil.trimString(
250                            BlogsEntry.class.getName(), "urlTitle", title);
251            }
252    
253            private static final char[] _URL_TITLE_REPLACE_CHARS = new char[] {
254                    '.', '/'
255            };
256    
257            private static Log _log = LogFactoryUtil.getLog(BlogsUtil.class);
258    
259    }