001    /**
002     * Copyright (c) 2000-2012 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.util.FriendlyURLNormalizerUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.LocalizationUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PropsUtil;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.util.ContentUtil;
029    
030    import java.util.Locale;
031    import java.util.Map;
032    
033    import javax.portlet.PortletPreferences;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Thiago Moreira
038     */
039    public class BlogsUtil {
040    
041            public static Map<Locale, String> getEmailEntryAddedBodyMap(
042                    PortletPreferences preferences) {
043    
044                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
045                            preferences, "emailEntryAddedBody");
046    
047                    Locale defaultLocale = LocaleUtil.getDefault();
048    
049                    String defaultValue = map.get(defaultLocale);
050    
051                    if (Validator.isNotNull(defaultValue)) {
052                            return map;
053                    }
054    
055                    map.put(
056                            defaultLocale,
057                            ContentUtil.get(
058                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_BODY)));
059    
060                    return map;
061            }
062    
063            public static boolean getEmailEntryAddedEnabled(
064                    PortletPreferences preferences) {
065    
066                    String emailEntryAddedEnabled = preferences.getValue(
067                            "emailEntryAddedEnabled", StringPool.BLANK);
068    
069                    if (Validator.isNotNull(emailEntryAddedEnabled)) {
070                            return GetterUtil.getBoolean(emailEntryAddedEnabled);
071                    }
072                    else {
073                            return GetterUtil.getBoolean(PropsUtil.get(
074                                    PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_ENABLED));
075                    }
076            }
077    
078            public static Map<Locale, String> getEmailEntryAddedSubjectMap(
079                    PortletPreferences preferences) {
080    
081                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
082                            preferences, "emailEntryAddedSubject");
083    
084                    Locale defaultLocale = LocaleUtil.getDefault();
085    
086                    String defaultValue = map.get(defaultLocale);
087    
088                    if (Validator.isNotNull(defaultValue)) {
089                            return map;
090                    }
091    
092                    map.put(
093                            defaultLocale,
094                            ContentUtil.get(
095                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_SUBJECT)));
096    
097                    return map;
098            }
099    
100            public static Map<Locale, String> getEmailEntryUpdatedBodyMap(
101                    PortletPreferences preferences) {
102    
103                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
104                            preferences, "emailEntryUpdatedBody");
105    
106                    Locale defaultLocale = LocaleUtil.getDefault();
107    
108                    String defaultValue = map.get(defaultLocale);
109    
110                    if (Validator.isNotNull(defaultValue)) {
111                            return map;
112                    }
113    
114                    map.put(
115                            defaultLocale,
116                            ContentUtil.get(
117                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_BODY)));
118    
119                    return map;
120            }
121    
122            public static boolean getEmailEntryUpdatedEnabled(
123                    PortletPreferences preferences) {
124    
125                    String emailEntryUpdatedEnabled = preferences.getValue(
126                            "emailEntryUpdatedEnabled", StringPool.BLANK);
127    
128                    if (Validator.isNotNull(emailEntryUpdatedEnabled)) {
129                            return GetterUtil.getBoolean(emailEntryUpdatedEnabled);
130                    }
131                    else {
132                            return GetterUtil.getBoolean(PropsUtil.get(
133                                    PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_ENABLED));
134                    }
135            }
136    
137            public static Map<Locale, String> getEmailEntryUpdatedSubjectMap(
138                    PortletPreferences preferences) {
139    
140                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
141                            preferences, "emailEntryUpdatedSubject");
142    
143                    Locale defaultLocale = LocaleUtil.getDefault();
144    
145                    String defaultValue = map.get(defaultLocale);
146    
147                    if (Validator.isNotNull(defaultValue)) {
148                            return map;
149                    }
150    
151                    map.put(
152                            defaultLocale,
153                            ContentUtil.get(
154                                    PropsUtil.get(PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_SUBJECT)));
155    
156                    return map;
157            }
158    
159            public static String getEmailFromAddress(
160                            PortletPreferences preferences, long companyId)
161                    throws SystemException {
162    
163                    return PortalUtil.getEmailFromAddress(
164                            preferences, companyId, PropsValues.BLOGS_EMAIL_FROM_ADDRESS);
165            }
166    
167            public static String getEmailFromName(
168                            PortletPreferences preferences, long companyId)
169                    throws SystemException {
170    
171                    return PortalUtil.getEmailFromName(
172                            preferences, companyId, PropsValues.BLOGS_EMAIL_FROM_NAME);
173            }
174    
175            public static String getUrlTitle(long entryId, String title) {
176                    title = title.trim().toLowerCase();
177    
178                    if (Validator.isNull(title) || Validator.isNumber(title) ||
179                            title.equals("rss")) {
180    
181                            return String.valueOf(entryId);
182                    }
183                    else {
184                            return FriendlyURLNormalizerUtil.normalize(
185                                    title, _URL_TITLE_REPLACE_CHARS);
186                    }
187            }
188    
189            private static final char[] _URL_TITLE_REPLACE_CHARS = new char[] {
190                    '.', '/'
191            };
192    
193    }