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