001    /**
002     * Copyright (c) 2000-present 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.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.comment.CommentManager;
019    import com.liferay.portal.kernel.dao.search.SearchContainer;
020    import com.liferay.portal.kernel.dao.search.SearchContainerResults;
021    import com.liferay.portal.kernel.exception.PortalException;
022    import com.liferay.portal.kernel.language.LanguageUtil;
023    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
024    import com.liferay.portal.kernel.util.HtmlUtil;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Company;
028    import com.liferay.portal.model.ModelHintsUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.asset.model.AssetEntry;
033    import com.liferay.portlet.asset.service.AssetEntryServiceUtil;
034    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
035    import com.liferay.portlet.blogs.model.BlogsEntry;
036    
037    import java.util.LinkedHashMap;
038    import java.util.List;
039    import java.util.Map;
040    import java.util.regex.Pattern;
041    
042    import javax.portlet.PortletRequest;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Thiago Moreira
047     */
048    public class BlogsUtil {
049    
050            public static final String DISPLAY_STYLE_ABSTRACT = "abstract";
051    
052            public static final String DISPLAY_STYLE_FULL_CONTENT = "full-content";
053    
054            public static final String DISPLAY_STYLE_TITLE = "title";
055    
056            public static CommentManager getCommentManager() {
057                    return (CommentManager)PortalBeanLocatorUtil.locate(
058                            CommentManager.class.getName());
059            }
060    
061            public static int getCommentsCount(BlogsEntry entry) {
062                    CommentManager commentManager = getCommentManager();
063    
064                    return commentManager.getCommentsCount(
065                            BlogsEntry.class.getName(), entry.getEntryId());
066            }
067    
068            public static Map<String, String> getEmailDefinitionTerms(
069                    PortletRequest portletRequest, String emailFromAddress,
070                    String emailFromName) {
071    
072                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
073                            WebKeys.THEME_DISPLAY);
074    
075                    Map<String, String> definitionTerms =
076                            new LinkedHashMap<String, String>();
077    
078                    definitionTerms.put(
079                            "[$BLOGS_ENTRY_CONTENT$]",
080                            LanguageUtil.get(
081                                    themeDisplay.getLocale(), "the-blog-entry-content"));
082                    definitionTerms.put(
083                            "[$BLOGS_ENTRY_CREATE_DATE$]",
084                            LanguageUtil.get(
085                                    themeDisplay.getLocale(),
086                                    "the-date-the-blog-entry-was-created"));
087                    definitionTerms.put(
088                            "[$BLOGS_ENTRY_DESCRIPTION$]",
089                            LanguageUtil.get(
090                                    themeDisplay.getLocale(), "the-blog-entry-description"));
091                    definitionTerms.put(
092                            "[$BLOGS_ENTRY_SITE_NAME$]",
093                            LanguageUtil.get(
094                                    themeDisplay.getLocale(),
095                                    "the-name-of-the-site-where-the-blog-entry-was-created"));
096                    definitionTerms.put(
097                            "[$BLOGS_ENTRY_STATUS_BY_USER_NAME$]",
098                            LanguageUtil.get(
099                                    themeDisplay.getLocale(),
100                                    "the-user-who-updated-the-blog-entry"));
101                    definitionTerms.put(
102                            "[$BLOGS_ENTRY_TITLE$]",
103                            LanguageUtil.get(themeDisplay.getLocale(), "the-blog-entry-title"));
104                    definitionTerms.put(
105                            "[$BLOGS_ENTRY_UPDATE_COMMENT$]",
106                            LanguageUtil.get(
107                                    themeDisplay.getLocale(),
108                                    "the-comment-of-the-user-who-updated-the-blog-entry"));
109                    definitionTerms.put(
110                            "[$BLOGS_ENTRY_USER_ADDRESS$]",
111                            LanguageUtil.get(
112                                    themeDisplay.getLocale(),
113                                    "the-email-address-of-the-user-who-added-the-blog-entry"));
114                    definitionTerms.put(
115                            "[$BLOGS_ENTRY_USER_PORTRAIT_URL$]",
116                            LanguageUtil.get(
117                                    themeDisplay.getLocale(),
118                                    "the-portrait-url-of-the-user-who-added-the-blog-entry"));
119                    definitionTerms.put(
120                            "[$BLOGS_ENTRY_USER_NAME$]",
121                            LanguageUtil.get(
122                                    themeDisplay.getLocale(), "the-user-who-added-the-blog-entry"));
123                    definitionTerms.put(
124                            "[$BLOGS_ENTRY_USER_URL$]",
125                            LanguageUtil.get(
126                                    themeDisplay.getLocale(),
127                                    "the-public-site-url-of-the-user-who-added-the-blog-entry"));
128                    definitionTerms.put(
129                            "[$BLOGS_ENTRY_URL$]",
130                            LanguageUtil.get(themeDisplay.getLocale(), "the-blog-entry-url"));
131                    definitionTerms.put(
132                            "[$COMPANY_ID$]",
133                            LanguageUtil.get(
134                                    themeDisplay.getLocale(),
135                                    "the-company-id-associated-with-the-blog"));
136                    definitionTerms.put(
137                            "[$COMPANY_MX$]",
138                            LanguageUtil.get(
139                                    themeDisplay.getLocale(),
140                                    "the-company-mx-associated-with-the-blog"));
141                    definitionTerms.put(
142                            "[$COMPANY_NAME$]",
143                            LanguageUtil.get(
144                                    themeDisplay.getLocale(),
145                                    "the-company-name-associated-with-the-blog"));
146                    definitionTerms.put(
147                            "[$FROM_ADDRESS$]", HtmlUtil.escape(emailFromAddress));
148                    definitionTerms.put("[$FROM_NAME$]", HtmlUtil.escape(emailFromName));
149    
150                    Company company = themeDisplay.getCompany();
151    
152                    definitionTerms.put("[$PORTAL_URL$]", company.getVirtualHostname());
153    
154                    definitionTerms.put(
155                            "[$PORTLET_NAME$]", PortalUtil.getPortletTitle(portletRequest));
156                    definitionTerms.put(
157                            "[$SITE_NAME$]",
158                            LanguageUtil.get(
159                                    themeDisplay.getLocale(),
160                                    "the-site-name-associated-with-the-blog"));
161                    definitionTerms.put(
162                            "[$TO_ADDRESS$]",
163                            LanguageUtil.get(
164                                    themeDisplay.getLocale(),
165                                    "the-address-of-the-email-recipient"));
166                    definitionTerms.put(
167                            "[$TO_NAME$]",
168                            LanguageUtil.get(
169                                    themeDisplay.getLocale(), "the-name-of-the-email-recipient"));
170    
171                    return definitionTerms;
172            }
173    
174            public static Map<String, String> getEmailFromDefinitionTerms(
175                    PortletRequest portletRequest, String emailFromAddress,
176                    String emailFromName) {
177    
178                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
179                            WebKeys.THEME_DISPLAY);
180    
181                    Map<String, String> definitionTerms =
182                            new LinkedHashMap<String, String>();
183    
184                    definitionTerms.put(
185                            "[$BLOGS_ENTRY_USER_ADDRESS$]",
186                            LanguageUtil.get(
187                                    themeDisplay.getLocale(),
188                                    "the-email-address-of-the-user-who-added-the-blog-entry"));
189                    definitionTerms.put(
190                            "[$BLOGS_ENTRY_USER_NAME$]",
191                            LanguageUtil.get(
192                                    themeDisplay.getLocale(), "the-user-who-added-the-blog-entry"));
193                    definitionTerms.put(
194                            "[$COMPANY_ID$]",
195                            LanguageUtil.get(
196                                    themeDisplay.getLocale(),
197                                    "the-company-id-associated-with-the-blog"));
198                    definitionTerms.put(
199                            "[$COMPANY_MX$]",
200                            LanguageUtil.get(
201                                    themeDisplay.getLocale(),
202                                    "the-company-mx-associated-with-the-blog"));
203                    definitionTerms.put(
204                            "[$COMPANY_NAME$]",
205                            LanguageUtil.get(
206                                    themeDisplay.getLocale(),
207                                    "the-company-name-associated-with-the-blog"));
208                    definitionTerms.put(
209                            "[$PORTLET_NAME$]", PortalUtil.getPortletTitle(portletRequest));
210                    definitionTerms.put(
211                            "[$SITE_NAME$]",
212                            LanguageUtil.get(
213                                    themeDisplay.getLocale(),
214                                    "the-site-name-associated-with-the-blog"));
215    
216                    return definitionTerms;
217            }
218    
219            public static SearchContainerResults<AssetEntry> getSearchContainerResults(
220                            SearchContainer<?> searchContainer)
221                    throws PortalException {
222    
223                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery(
224                            BlogsEntry.class.getName(), searchContainer);
225    
226                    assetEntryQuery.setExcludeZeroViewCount(false);
227                    assetEntryQuery.setVisible(Boolean.TRUE);
228    
229                    int total = AssetEntryServiceUtil.getEntriesCount(assetEntryQuery);
230    
231                    assetEntryQuery.setEnd(searchContainer.getEnd());
232                    assetEntryQuery.setStart(searchContainer.getStart());
233    
234                    List<AssetEntry> assetEntries = AssetEntryServiceUtil.getEntries(
235                            assetEntryQuery);
236    
237                    return new SearchContainerResults<AssetEntry>(assetEntries, total);
238            }
239    
240            public static String getUrlTitle(long entryId, String title) {
241                    if (title == null) {
242                            return String.valueOf(entryId);
243                    }
244    
245                    title = StringUtil.toLowerCase(title.trim());
246    
247                    if (Validator.isNull(title) || Validator.isNumber(title) ||
248                            title.equals("rss")) {
249    
250                            title = String.valueOf(entryId);
251                    }
252                    else {
253                            title = FriendlyURLNormalizerUtil.normalize(
254                                    title, _friendlyURLPattern);
255                    }
256    
257                    return ModelHintsUtil.trimString(
258                            BlogsEntry.class.getName(), "urlTitle", title);
259            }
260    
261            private static final Pattern _friendlyURLPattern = Pattern.compile(
262                    "[^a-z0-9_-]");
263    
264    }