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