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.OrderByComparator;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.kernel.util.WebKeys;
029    import com.liferay.portal.model.Company;
030    import com.liferay.portal.model.ModelHintsUtil;
031    import com.liferay.portal.theme.PortletDisplay;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portlet.asset.model.AssetEntry;
034    import com.liferay.portlet.asset.service.AssetEntryServiceUtil;
035    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
036    import com.liferay.portlet.blogs.model.BlogsEntry;
037    import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
038    import com.liferay.portlet.blogs.util.comparator.EntryTitleComparator;
039    
040    import java.util.LinkedHashMap;
041    import java.util.List;
042    import java.util.Map;
043    import java.util.regex.Pattern;
044    
045    import javax.portlet.PortletRequest;
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 int getCommentsCount(BlogsEntry entry) {
060                    CommentManager commentManager = CommentManagerUtil.getCommentManager();
061    
062                    return commentManager.getCommentsCount(
063                            BlogsEntry.class.getName(), entry.getEntryId());
064            }
065    
066            public static Map<String, String> getEmailDefinitionTerms(
067                    PortletRequest portletRequest, String emailFromAddress,
068                    String emailFromName) {
069    
070                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
071                            WebKeys.THEME_DISPLAY);
072    
073                    Map<String, String> definitionTerms = new LinkedHashMap<>();
074    
075                    definitionTerms.put(
076                            "[$BLOGS_ENTRY_CONTENT$]",
077                            LanguageUtil.get(
078                                    themeDisplay.getLocale(), "the-blog-entry-content"));
079                    definitionTerms.put(
080                            "[$BLOGS_ENTRY_CREATE_DATE$]",
081                            LanguageUtil.get(
082                                    themeDisplay.getLocale(),
083                                    "the-date-the-blog-entry-was-created"));
084                    definitionTerms.put(
085                            "[$BLOGS_ENTRY_DESCRIPTION$]",
086                            LanguageUtil.get(
087                                    themeDisplay.getLocale(), "the-blog-entry-description"));
088                    definitionTerms.put(
089                            "[$BLOGS_ENTRY_SITE_NAME$]",
090                            LanguageUtil.get(
091                                    themeDisplay.getLocale(),
092                                    "the-name-of-the-site-where-the-blog-entry-was-created"));
093                    definitionTerms.put(
094                            "[$BLOGS_ENTRY_STATUS_BY_USER_NAME$]",
095                            LanguageUtil.get(
096                                    themeDisplay.getLocale(),
097                                    "the-user-who-updated-the-blog-entry"));
098                    definitionTerms.put(
099                            "[$BLOGS_ENTRY_TITLE$]",
100                            LanguageUtil.get(themeDisplay.getLocale(), "the-blog-entry-title"));
101                    definitionTerms.put(
102                            "[$BLOGS_ENTRY_UPDATE_COMMENT$]",
103                            LanguageUtil.get(
104                                    themeDisplay.getLocale(),
105                                    "the-comment-of-the-user-who-updated-the-blog-entry"));
106                    definitionTerms.put(
107                            "[$BLOGS_ENTRY_USER_ADDRESS$]",
108                            LanguageUtil.get(
109                                    themeDisplay.getLocale(),
110                                    "the-email-address-of-the-user-who-added-the-blog-entry"));
111                    definitionTerms.put(
112                            "[$BLOGS_ENTRY_USER_PORTRAIT_URL$]",
113                            LanguageUtil.get(
114                                    themeDisplay.getLocale(),
115                                    "the-portrait-url-of-the-user-who-added-the-blog-entry"));
116                    definitionTerms.put(
117                            "[$BLOGS_ENTRY_USER_NAME$]",
118                            LanguageUtil.get(
119                                    themeDisplay.getLocale(), "the-user-who-added-the-blog-entry"));
120                    definitionTerms.put(
121                            "[$BLOGS_ENTRY_USER_URL$]",
122                            LanguageUtil.get(
123                                    themeDisplay.getLocale(),
124                                    "the-public-site-url-of-the-user-who-added-the-blog-entry"));
125                    definitionTerms.put(
126                            "[$BLOGS_ENTRY_URL$]",
127                            LanguageUtil.get(themeDisplay.getLocale(), "the-blog-entry-url"));
128                    definitionTerms.put(
129                            "[$COMPANY_ID$]",
130                            LanguageUtil.get(
131                                    themeDisplay.getLocale(),
132                                    "the-company-id-associated-with-the-blog"));
133                    definitionTerms.put(
134                            "[$COMPANY_MX$]",
135                            LanguageUtil.get(
136                                    themeDisplay.getLocale(),
137                                    "the-company-mx-associated-with-the-blog"));
138                    definitionTerms.put(
139                            "[$COMPANY_NAME$]",
140                            LanguageUtil.get(
141                                    themeDisplay.getLocale(),
142                                    "the-company-name-associated-with-the-blog"));
143                    definitionTerms.put(
144                            "[$FROM_ADDRESS$]", HtmlUtil.escape(emailFromAddress));
145                    definitionTerms.put("[$FROM_NAME$]", HtmlUtil.escape(emailFromName));
146    
147                    Company company = themeDisplay.getCompany();
148    
149                    definitionTerms.put("[$PORTAL_URL$]", company.getVirtualHostname());
150    
151                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
152    
153                    definitionTerms.put(
154                            "[$PORTLET_NAME$]", HtmlUtil.escape(portletDisplay.getTitle()));
155    
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 = new LinkedHashMap<>();
182    
183                    definitionTerms.put(
184                            "[$BLOGS_ENTRY_USER_ADDRESS$]",
185                            LanguageUtil.get(
186                                    themeDisplay.getLocale(),
187                                    "the-email-address-of-the-user-who-added-the-blog-entry"));
188                    definitionTerms.put(
189                            "[$BLOGS_ENTRY_USER_NAME$]",
190                            LanguageUtil.get(
191                                    themeDisplay.getLocale(), "the-user-who-added-the-blog-entry"));
192                    definitionTerms.put(
193                            "[$COMPANY_ID$]",
194                            LanguageUtil.get(
195                                    themeDisplay.getLocale(),
196                                    "the-company-id-associated-with-the-blog"));
197                    definitionTerms.put(
198                            "[$COMPANY_MX$]",
199                            LanguageUtil.get(
200                                    themeDisplay.getLocale(),
201                                    "the-company-mx-associated-with-the-blog"));
202                    definitionTerms.put(
203                            "[$COMPANY_NAME$]",
204                            LanguageUtil.get(
205                                    themeDisplay.getLocale(),
206                                    "the-company-name-associated-with-the-blog"));
207    
208                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
209    
210                    definitionTerms.put(
211                            "[$PORTLET_NAME$]", HtmlUtil.escape(portletDisplay.getTitle()));
212    
213                    definitionTerms.put(
214                            "[$SITE_NAME$]",
215                            LanguageUtil.get(
216                                    themeDisplay.getLocale(),
217                                    "the-site-name-associated-with-the-blog"));
218    
219                    return definitionTerms;
220            }
221    
222            public static OrderByComparator<BlogsEntry> getOrderByComparator(
223                    String orderByCol, String orderByType) {
224    
225                    boolean orderByAsc = true;
226    
227                    if (orderByType.equals("desc")) {
228                            orderByAsc = false;
229                    }
230    
231                    OrderByComparator<BlogsEntry> orderByComparator = null;
232    
233                    if (orderByCol.equals("display-date")) {
234                            orderByComparator = new EntryDisplayDateComparator(orderByAsc);
235                    }
236                    else {
237                            orderByComparator = new EntryTitleComparator(orderByAsc);
238                    }
239    
240                    return orderByComparator;
241            }
242    
243            public static SearchContainerResults<AssetEntry> getSearchContainerResults(
244                            SearchContainer<?> searchContainer)
245                    throws PortalException {
246    
247                    AssetEntryQuery assetEntryQuery = new AssetEntryQuery(
248                            BlogsEntry.class.getName(), searchContainer);
249    
250                    assetEntryQuery.setExcludeZeroViewCount(false);
251                    assetEntryQuery.setOrderByCol1("publishDate");
252                    assetEntryQuery.setVisible(Boolean.TRUE);
253    
254                    int total = AssetEntryServiceUtil.getEntriesCount(assetEntryQuery);
255    
256                    assetEntryQuery.setEnd(searchContainer.getEnd());
257                    assetEntryQuery.setStart(searchContainer.getStart());
258    
259                    List<AssetEntry> assetEntries = AssetEntryServiceUtil.getEntries(
260                            assetEntryQuery);
261    
262                    return new SearchContainerResults<>(assetEntries, total);
263            }
264    
265            public static String getUrlTitle(long entryId, String title) {
266                    if (title == null) {
267                            return String.valueOf(entryId);
268                    }
269    
270                    title = StringUtil.toLowerCase(title.trim());
271    
272                    if (Validator.isNull(title) || Validator.isNumber(title) ||
273                            title.equals("rss")) {
274    
275                            title = String.valueOf(entryId);
276                    }
277                    else {
278                            title = FriendlyURLNormalizerUtil.normalize(
279                                    title, _friendlyURLPattern);
280                    }
281    
282                    return ModelHintsUtil.trimString(
283                            BlogsEntry.class.getName(), "urlTitle", title);
284            }
285    
286            private static final Pattern _friendlyURLPattern = Pattern.compile(
287                    "[^a-z0-9_-]");
288    
289    }