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.comment.CommentManager;
018    import com.liferay.portal.kernel.comment.CommentManagerUtil;
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.PortletDisplay;
030    import com.liferay.portal.theme.ThemeDisplay;
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 int getCommentsCount(BlogsEntry entry) {
057                    CommentManager commentManager = CommentManagerUtil.getCommentManager();
058    
059                    return commentManager.getCommentsCount(
060                            BlogsEntry.class.getName(), entry.getEntryId());
061            }
062    
063            public static Map<String, String> getEmailDefinitionTerms(
064                    PortletRequest portletRequest, String emailFromAddress,
065                    String emailFromName) {
066    
067                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
068                            WebKeys.THEME_DISPLAY);
069    
070                    Map<String, String> definitionTerms = new LinkedHashMap<>();
071    
072                    definitionTerms.put(
073                            "[$BLOGS_ENTRY_CONTENT$]",
074                            LanguageUtil.get(
075                                    themeDisplay.getLocale(), "the-blog-entry-content"));
076                    definitionTerms.put(
077                            "[$BLOGS_ENTRY_CREATE_DATE$]",
078                            LanguageUtil.get(
079                                    themeDisplay.getLocale(),
080                                    "the-date-the-blog-entry-was-created"));
081                    definitionTerms.put(
082                            "[$BLOGS_ENTRY_DESCRIPTION$]",
083                            LanguageUtil.get(
084                                    themeDisplay.getLocale(), "the-blog-entry-description"));
085                    definitionTerms.put(
086                            "[$BLOGS_ENTRY_SITE_NAME$]",
087                            LanguageUtil.get(
088                                    themeDisplay.getLocale(),
089                                    "the-name-of-the-site-where-the-blog-entry-was-created"));
090                    definitionTerms.put(
091                            "[$BLOGS_ENTRY_STATUS_BY_USER_NAME$]",
092                            LanguageUtil.get(
093                                    themeDisplay.getLocale(),
094                                    "the-user-who-updated-the-blog-entry"));
095                    definitionTerms.put(
096                            "[$BLOGS_ENTRY_TITLE$]",
097                            LanguageUtil.get(themeDisplay.getLocale(), "the-blog-entry-title"));
098                    definitionTerms.put(
099                            "[$BLOGS_ENTRY_UPDATE_COMMENT$]",
100                            LanguageUtil.get(
101                                    themeDisplay.getLocale(),
102                                    "the-comment-of-the-user-who-updated-the-blog-entry"));
103                    definitionTerms.put(
104                            "[$BLOGS_ENTRY_USER_ADDRESS$]",
105                            LanguageUtil.get(
106                                    themeDisplay.getLocale(),
107                                    "the-email-address-of-the-user-who-added-the-blog-entry"));
108                    definitionTerms.put(
109                            "[$BLOGS_ENTRY_USER_PORTRAIT_URL$]",
110                            LanguageUtil.get(
111                                    themeDisplay.getLocale(),
112                                    "the-portrait-url-of-the-user-who-added-the-blog-entry"));
113                    definitionTerms.put(
114                            "[$BLOGS_ENTRY_USER_NAME$]",
115                            LanguageUtil.get(
116                                    themeDisplay.getLocale(), "the-user-who-added-the-blog-entry"));
117                    definitionTerms.put(
118                            "[$BLOGS_ENTRY_USER_URL$]",
119                            LanguageUtil.get(
120                                    themeDisplay.getLocale(),
121                                    "the-public-site-url-of-the-user-who-added-the-blog-entry"));
122                    definitionTerms.put(
123                            "[$BLOGS_ENTRY_URL$]",
124                            LanguageUtil.get(themeDisplay.getLocale(), "the-blog-entry-url"));
125                    definitionTerms.put(
126                            "[$COMPANY_ID$]",
127                            LanguageUtil.get(
128                                    themeDisplay.getLocale(),
129                                    "the-company-id-associated-with-the-blog"));
130                    definitionTerms.put(
131                            "[$COMPANY_MX$]",
132                            LanguageUtil.get(
133                                    themeDisplay.getLocale(),
134                                    "the-company-mx-associated-with-the-blog"));
135                    definitionTerms.put(
136                            "[$COMPANY_NAME$]",
137                            LanguageUtil.get(
138                                    themeDisplay.getLocale(),
139                                    "the-company-name-associated-with-the-blog"));
140                    definitionTerms.put(
141                            "[$FROM_ADDRESS$]", HtmlUtil.escape(emailFromAddress));
142                    definitionTerms.put("[$FROM_NAME$]", HtmlUtil.escape(emailFromName));
143    
144                    Company company = themeDisplay.getCompany();
145    
146                    definitionTerms.put("[$PORTAL_URL$]", company.getVirtualHostname());
147    
148                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
149    
150                    definitionTerms.put(
151                            "[$PORTLET_NAME$]", HtmlUtil.escape(portletDisplay.getTitle()));
152    
153                    definitionTerms.put(
154                            "[$SITE_NAME$]",
155                            LanguageUtil.get(
156                                    themeDisplay.getLocale(),
157                                    "the-site-name-associated-with-the-blog"));
158                    definitionTerms.put(
159                            "[$TO_ADDRESS$]",
160                            LanguageUtil.get(
161                                    themeDisplay.getLocale(),
162                                    "the-address-of-the-email-recipient"));
163                    definitionTerms.put(
164                            "[$TO_NAME$]",
165                            LanguageUtil.get(
166                                    themeDisplay.getLocale(), "the-name-of-the-email-recipient"));
167    
168                    return definitionTerms;
169            }
170    
171            public static Map<String, String> getEmailFromDefinitionTerms(
172                    PortletRequest portletRequest, String emailFromAddress,
173                    String emailFromName) {
174    
175                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
176                            WebKeys.THEME_DISPLAY);
177    
178                    Map<String, String> definitionTerms = new LinkedHashMap<>();
179    
180                    definitionTerms.put(
181                            "[$BLOGS_ENTRY_USER_ADDRESS$]",
182                            LanguageUtil.get(
183                                    themeDisplay.getLocale(),
184                                    "the-email-address-of-the-user-who-added-the-blog-entry"));
185                    definitionTerms.put(
186                            "[$BLOGS_ENTRY_USER_NAME$]",
187                            LanguageUtil.get(
188                                    themeDisplay.getLocale(), "the-user-who-added-the-blog-entry"));
189                    definitionTerms.put(
190                            "[$COMPANY_ID$]",
191                            LanguageUtil.get(
192                                    themeDisplay.getLocale(),
193                                    "the-company-id-associated-with-the-blog"));
194                    definitionTerms.put(
195                            "[$COMPANY_MX$]",
196                            LanguageUtil.get(
197                                    themeDisplay.getLocale(),
198                                    "the-company-mx-associated-with-the-blog"));
199                    definitionTerms.put(
200                            "[$COMPANY_NAME$]",
201                            LanguageUtil.get(
202                                    themeDisplay.getLocale(),
203                                    "the-company-name-associated-with-the-blog"));
204    
205                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
206    
207                    definitionTerms.put(
208                            "[$PORTLET_NAME$]", HtmlUtil.escape(portletDisplay.getTitle()));
209    
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.setOrderByCol1("publishDate");
228                    assetEntryQuery.setVisible(Boolean.TRUE);
229    
230                    int total = AssetEntryServiceUtil.getEntriesCount(assetEntryQuery);
231    
232                    assetEntryQuery.setEnd(searchContainer.getEnd());
233                    assetEntryQuery.setStart(searchContainer.getStart());
234    
235                    List<AssetEntry> assetEntries = AssetEntryServiceUtil.getEntries(
236                            assetEntryQuery);
237    
238                    return new SearchContainerResults<>(assetEntries, total);
239            }
240    
241            public static String getUrlTitle(long entryId, String title) {
242                    if (title == null) {
243                            return String.valueOf(entryId);
244                    }
245    
246                    title = StringUtil.toLowerCase(title.trim());
247    
248                    if (Validator.isNull(title) || Validator.isNumber(title) ||
249                            title.equals("rss")) {
250    
251                            title = String.valueOf(entryId);
252                    }
253                    else {
254                            title = FriendlyURLNormalizerUtil.normalize(
255                                    title, _friendlyURLPattern);
256                    }
257    
258                    return ModelHintsUtil.trimString(
259                            BlogsEntry.class.getName(), "urlTitle", title);
260            }
261    
262            private static final Pattern _friendlyURLPattern = Pattern.compile(
263                    "[^a-z0-9_-]");
264    
265    }