001    /**
002     * Copyright (c) 2000-2013 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.bookmarks.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.portlet.LiferayWindowState;
022    import com.liferay.portal.kernel.search.Document;
023    import com.liferay.portal.kernel.search.Field;
024    import com.liferay.portal.kernel.search.Hits;
025    import com.liferay.portal.kernel.util.GetterUtil;
026    import com.liferay.portal.kernel.util.LocaleUtil;
027    import com.liferay.portal.kernel.util.LocalizationUtil;
028    import com.liferay.portal.kernel.util.OrderByComparator;
029    import com.liferay.portal.kernel.util.ParamUtil;
030    import com.liferay.portal.kernel.util.PropsKeys;
031    import com.liferay.portal.kernel.util.StringBundler;
032    import com.liferay.portal.kernel.util.StringPool;
033    import com.liferay.portal.kernel.util.Validator;
034    import com.liferay.portal.theme.ThemeDisplay;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portal.util.PortletKeys;
037    import com.liferay.portal.util.PropsUtil;
038    import com.liferay.portal.util.PropsValues;
039    import com.liferay.portal.util.WebKeys;
040    import com.liferay.portlet.PortletURLFactoryUtil;
041    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
042    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
043    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
044    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
045    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
046    import com.liferay.portlet.bookmarks.util.comparator.EntryCreateDateComparator;
047    import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
048    import com.liferay.portlet.bookmarks.util.comparator.EntryNameComparator;
049    import com.liferay.portlet.bookmarks.util.comparator.EntryPriorityComparator;
050    import com.liferay.portlet.bookmarks.util.comparator.EntryURLComparator;
051    import com.liferay.portlet.bookmarks.util.comparator.EntryVisitsComparator;
052    import com.liferay.util.ContentUtil;
053    
054    import java.util.ArrayList;
055    import java.util.Collections;
056    import java.util.List;
057    import java.util.Locale;
058    import java.util.Map;
059    
060    import javax.portlet.PortletPreferences;
061    import javax.portlet.PortletRequest;
062    import javax.portlet.PortletURL;
063    import javax.portlet.RenderResponse;
064    
065    import javax.servlet.http.HttpServletRequest;
066    
067    /**
068     * @author Brian Wing Shun Chan
069     */
070    public class BookmarksUtil {
071    
072            public static void addPortletBreadcrumbEntries(
073                            BookmarksEntry entry, HttpServletRequest request,
074                            RenderResponse renderResponse)
075                    throws Exception {
076    
077                    BookmarksFolder folder = entry.getFolder();
078    
079                    if (folder.getFolderId() !=
080                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
081    
082                            addPortletBreadcrumbEntries(folder, request, renderResponse);
083                    }
084    
085                    BookmarksEntry unescapedEntry = entry.toUnescapedModel();
086    
087                    PortletURL portletURL = renderResponse.createRenderURL();
088    
089                    portletURL.setParameter("struts_action", "/bookmarks/view_entry");
090                    portletURL.setParameter("entryId", String.valueOf(entry.getEntryId()));
091    
092                    PortalUtil.addPortletBreadcrumbEntry(
093                            request, unescapedEntry.getName(), portletURL.toString());
094            }
095    
096            public static void addPortletBreadcrumbEntries(
097                            BookmarksFolder folder, HttpServletRequest request,
098                            RenderResponse renderResponse)
099                    throws Exception {
100    
101                    String strutsAction = ParamUtil.getString(request, "struts_action");
102    
103                    PortletURL portletURL = renderResponse.createRenderURL();
104    
105                    if (strutsAction.equals("/bookmarks/select_folder")) {
106                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
107                                    WebKeys.THEME_DISPLAY);
108    
109                            portletURL.setParameter(
110                                    "struts_action", "/bookmarks/select_folder");
111                            portletURL.setWindowState(LiferayWindowState.POP_UP);
112    
113                            PortalUtil.addPortletBreadcrumbEntry(
114                                    request, themeDisplay.translate("home"), portletURL.toString());
115                    }
116                    else {
117                            portletURL.setParameter("struts_action", "/bookmarks/view");
118                    }
119    
120                    List<BookmarksFolder> ancestorFolders = folder.getAncestors();
121    
122                    Collections.reverse(ancestorFolders);
123    
124                    for (BookmarksFolder ancestorFolder : ancestorFolders) {
125                            portletURL.setParameter(
126                                    "folderId", String.valueOf(ancestorFolder.getFolderId()));
127    
128                            PortalUtil.addPortletBreadcrumbEntry(
129                                    request, ancestorFolder.getName(), portletURL.toString());
130                    }
131    
132                    portletURL.setParameter(
133                            "folderId", String.valueOf(folder.getFolderId()));
134    
135                    if (folder.getFolderId() !=
136                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
137    
138                            BookmarksFolder unescapedFolder = folder.toUnescapedModel();
139    
140                            PortalUtil.addPortletBreadcrumbEntry(
141                                    request, unescapedFolder.getName(), portletURL.toString());
142                    }
143            }
144    
145            public static void addPortletBreadcrumbEntries(
146                            long folderId, HttpServletRequest request,
147                            RenderResponse renderResponse)
148                    throws Exception {
149    
150                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
151                            return;
152                    }
153    
154                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
155                            folderId);
156    
157                    addPortletBreadcrumbEntries(folder, request, renderResponse);
158            }
159    
160            public static String getAbsolutePath(
161                            PortletRequest portletRequest, long folderId)
162                    throws PortalException, SystemException {
163    
164                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
165                            WebKeys.THEME_DISPLAY);
166    
167                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
168                            return themeDisplay.translate("home");
169                    }
170    
171                    BookmarksFolder folder =
172                            BookmarksFolderLocalServiceUtil.fetchBookmarksFolder(folderId);
173    
174                    List<BookmarksFolder> folders = folder.getAncestors();
175    
176                    StringBundler sb = new StringBundler((folders.size() * 3) + 5);
177    
178                    sb.append(themeDisplay.translate("home"));
179                    sb.append(StringPool.SPACE);
180    
181                    Collections.reverse(folders);
182    
183                    for (BookmarksFolder curFolder : folders) {
184                            sb.append("\u00bb");
185                            sb.append(StringPool.SPACE);
186                            sb.append(curFolder.getName());
187                    }
188    
189                    sb.append("\u00bb");
190                    sb.append(StringPool.SPACE);
191                    sb.append(folder.getName());
192    
193                    return sb.toString();
194            }
195    
196            public static String getControlPanelLink(
197                            PortletRequest portletRequest, long folderId)
198                    throws PortalException, SystemException {
199    
200                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
201                            WebKeys.THEME_DISPLAY);
202    
203                    PortletURL portletURL = PortletURLFactoryUtil.create(
204                            portletRequest, PortletKeys.BOOKMARKS,
205                            PortalUtil.getControlPanelPlid(themeDisplay.getCompanyId()),
206                            PortletRequest.RENDER_PHASE);
207    
208                    portletURL.setParameter("struts_action", "/bookmarks/view");
209                    portletURL.setParameter("folderId", String.valueOf(folderId));
210    
211                    return portletURL.toString();
212            }
213    
214            public static Map<Locale, String> getEmailEntryAddedBodyMap(
215                    PortletPreferences preferences) {
216    
217                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
218                            preferences, "emailEntryAddedBody");
219    
220                    Locale defaultLocale = LocaleUtil.getSiteDefault();
221    
222                    String defaultValue = map.get(defaultLocale);
223    
224                    if (Validator.isNotNull(defaultValue)) {
225                            return map;
226                    }
227    
228                    map.put(
229                            defaultLocale,
230                            ContentUtil.get(
231                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_BODY)));
232    
233                    return map;
234            }
235    
236            public static boolean getEmailEntryAddedEnabled(
237                    PortletPreferences preferences) {
238    
239                    String emailEntryAddedEnabled = preferences.getValue(
240                            "emailEntryAddedEnabled", StringPool.BLANK);
241    
242                    if (Validator.isNotNull(emailEntryAddedEnabled)) {
243                            return GetterUtil.getBoolean(emailEntryAddedEnabled);
244                    }
245                    else {
246                            return GetterUtil.getBoolean(
247                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_ENABLED));
248                    }
249            }
250    
251            public static Map<Locale, String> getEmailEntryAddedSubjectMap(
252                    PortletPreferences preferences) {
253    
254                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
255                            preferences, "emailEntryAddedSubject");
256    
257                    Locale defaultLocale = LocaleUtil.getSiteDefault();
258    
259                    String defaultValue = map.get(defaultLocale);
260    
261                    if (Validator.isNotNull(defaultValue)) {
262                            return map;
263                    }
264    
265                    map.put(
266                            defaultLocale,
267                            ContentUtil.get(
268                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_SUBJECT)));
269    
270                    return map;
271            }
272    
273            public static Map<Locale, String> getEmailEntryUpdatedBodyMap(
274                    PortletPreferences preferences) {
275    
276                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
277                            preferences, "emailEntryUpdatedBody");
278    
279                    Locale defaultLocale = LocaleUtil.getSiteDefault();
280    
281                    String defaultValue = map.get(defaultLocale);
282    
283                    if (Validator.isNotNull(defaultValue)) {
284                            return map;
285                    }
286    
287                    map.put(
288                            defaultLocale,
289                            ContentUtil.get(
290                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_BODY)));
291    
292                    return map;
293            }
294    
295            public static boolean getEmailEntryUpdatedEnabled(
296                    PortletPreferences preferences) {
297    
298                    String emailEntryUpdatedEnabled = preferences.getValue(
299                            "emailEntryUpdatedEnabled", StringPool.BLANK);
300    
301                    if (Validator.isNotNull(emailEntryUpdatedEnabled)) {
302                            return GetterUtil.getBoolean(emailEntryUpdatedEnabled);
303                    }
304                    else {
305                            return GetterUtil.getBoolean(
306                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_ENABLED));
307                    }
308            }
309    
310            public static Map<Locale, String> getEmailEntryUpdatedSubjectMap(
311                    PortletPreferences preferences) {
312    
313                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
314                            preferences, "emailEntryUpdatedSubject");
315    
316                    Locale defaultLocale = LocaleUtil.getSiteDefault();
317    
318                    String defaultValue = map.get(defaultLocale);
319    
320                    if (Validator.isNotNull(defaultValue)) {
321                            return map;
322                    }
323    
324                    map.put(
325                            defaultLocale,
326                            ContentUtil.get(
327                                    PropsUtil.get(
328                                            PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_SUBJECT)));
329    
330                    return map;
331            }
332    
333            public static String getEmailFromAddress(
334                            PortletPreferences preferences, long companyId)
335                    throws SystemException {
336    
337                    return PortalUtil.getEmailFromAddress(
338                            preferences, companyId, PropsValues.BOOKMARKS_EMAIL_FROM_ADDRESS);
339            }
340    
341            public static String getEmailFromName(
342                            PortletPreferences preferences, long companyId)
343                    throws SystemException {
344    
345                    return PortalUtil.getEmailFromName(
346                            preferences, companyId, PropsValues.BOOKMARKS_EMAIL_FROM_NAME);
347            }
348    
349            public static List<Object> getEntries(Hits hits) {
350                    List<Object> entries = new ArrayList<Object>();
351    
352                    for (Document document : hits.getDocs()) {
353                            String entryClassName = document.get(Field.ENTRY_CLASS_NAME);
354                            long entryClassPK = GetterUtil.getLong(
355                                    document.get(Field.ENTRY_CLASS_PK));
356    
357                            try {
358                                    Object obj = null;
359    
360                                    if (entryClassName.equals(BookmarksEntry.class.getName())) {
361                                            obj = BookmarksEntryLocalServiceUtil.getEntry(entryClassPK);
362                                    }
363                                    else if (entryClassName.equals(
364                                                            BookmarksFolder.class.getName())) {
365    
366                                            obj = BookmarksFolderLocalServiceUtil.getFolder(
367                                                    entryClassPK);
368                                    }
369    
370                                    entries.add(obj);
371                            }
372                            catch (Exception e) {
373                                    if (_log.isWarnEnabled()) {
374                                            _log.warn(
375                                                    "Bookmarks search index is stale and contains entry " +
376                                                            entryClassPK);
377                                    }
378    
379                                    continue;
380                            }
381                    }
382    
383                    return entries;
384            }
385    
386            public static OrderByComparator getEntryOrderByComparator(
387                    String orderByCol, String orderByType) {
388    
389                    boolean orderByAsc = false;
390    
391                    if (orderByType.equals("asc")) {
392                            orderByAsc = true;
393                    }
394    
395                    OrderByComparator orderByComparator = null;
396    
397                    if (orderByCol.equals("create-date")) {
398                            orderByComparator = new EntryCreateDateComparator(orderByAsc);
399                    }
400                    else if (orderByCol.equals("modified-date")) {
401                            orderByComparator = new EntryModifiedDateComparator(orderByAsc);
402                    }
403                    else if (orderByCol.equals("name")) {
404                            orderByComparator = new EntryNameComparator(orderByAsc);
405                    }
406                    else if (orderByCol.equals("priority")) {
407                            orderByComparator = new EntryPriorityComparator(orderByAsc);
408                    }
409                    else if (orderByCol.equals("url")) {
410                            orderByComparator = new EntryURLComparator(orderByAsc);
411                    }
412                    else if (orderByCol.equals("visits")) {
413                            orderByComparator = new EntryVisitsComparator(orderByAsc);
414                    }
415    
416                    return orderByComparator;
417            }
418    
419            private static Log _log = LogFactoryUtil.getLog(BookmarksUtil.class);
420    
421    }