001    /**
002     * Copyright (c) 2000-2012 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.setWindowState(LiferayWindowState.POP_UP);
110    
111                            portletURL.setParameter(
112                                    "struts_action", "/bookmarks/select_folder");
113    
114                            PortalUtil.addPortletBreadcrumbEntry(
115                                    request, themeDisplay.translate("home"), portletURL.toString());
116                    }
117                    else {
118                            portletURL.setParameter("struts_action", "/bookmarks/view");
119                    }
120    
121                    List<BookmarksFolder> ancestorFolders = folder.getAncestors();
122    
123                    Collections.reverse(ancestorFolders);
124    
125                    for (BookmarksFolder ancestorFolder : ancestorFolders) {
126                            portletURL.setParameter(
127                                    "folderId", String.valueOf(ancestorFolder.getFolderId()));
128    
129                            PortalUtil.addPortletBreadcrumbEntry(
130                                    request, ancestorFolder.getName(), portletURL.toString());
131                    }
132    
133                    portletURL.setParameter(
134                            "folderId", String.valueOf(folder.getFolderId()));
135    
136                    if (folder.getFolderId() !=
137                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
138    
139                            BookmarksFolder unescapedFolder = folder.toUnescapedModel();
140    
141                            PortalUtil.addPortletBreadcrumbEntry(
142                                    request, unescapedFolder.getName(), portletURL.toString());
143                    }
144            }
145    
146            public static void addPortletBreadcrumbEntries(
147                            long folderId, HttpServletRequest request,
148                            RenderResponse renderResponse)
149                    throws Exception {
150    
151                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
152                            return;
153                    }
154    
155                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
156                            folderId);
157    
158                    addPortletBreadcrumbEntries(folder, request, renderResponse);
159            }
160    
161            public static String getAbsolutePath(
162                            PortletRequest portletRequest, long folderId)
163                    throws PortalException, SystemException {
164    
165                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
166                            WebKeys.THEME_DISPLAY);
167    
168                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
169                            return themeDisplay.translate("home");
170                    }
171    
172                    BookmarksFolder folder =
173                            BookmarksFolderLocalServiceUtil.fetchBookmarksFolder(folderId);
174    
175                    List<BookmarksFolder> folders = folder.getAncestors();
176    
177                    StringBundler sb = new StringBundler((folders.size() * 3) + 5);
178    
179                    sb.append(themeDisplay.translate("home"));
180                    sb.append(StringPool.SPACE);
181    
182                    Collections.reverse(folders);
183    
184                    for (BookmarksFolder curFolder : folders) {
185                            sb.append(StringPool.RAQUO);
186                            sb.append(StringPool.SPACE);
187                            sb.append(curFolder.getName());
188                    }
189    
190                    sb.append(StringPool.RAQUO);
191                    sb.append(StringPool.SPACE);
192                    sb.append(folder.getName());
193    
194                    return sb.toString();
195            }
196    
197            public static String getControlPanelLink(
198                            PortletRequest portletRequest, long folderId)
199                    throws PortalException, SystemException {
200    
201                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
202                            WebKeys.THEME_DISPLAY);
203    
204                    PortletURL portletURL = PortletURLFactoryUtil.create(
205                            portletRequest, PortletKeys.BOOKMARKS,
206                            PortalUtil.getControlPanelPlid(themeDisplay.getCompanyId()),
207                            PortletRequest.RENDER_PHASE);
208    
209                    portletURL.setParameter("struts_action", "/bookmarks/view");
210                    portletURL.setParameter("folderId", String.valueOf(folderId));
211    
212                    return portletURL.toString();
213            }
214    
215            public static Map<Locale, String> getEmailEntryAddedBodyMap(
216                    PortletPreferences preferences) {
217    
218                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
219                            preferences, "emailEntryAddedBody");
220    
221                    Locale defaultLocale = LocaleUtil.getDefault();
222    
223                    String defaultValue = map.get(defaultLocale);
224    
225                    if (Validator.isNotNull(defaultValue)) {
226                            return map;
227                    }
228    
229                    map.put(
230                            defaultLocale,
231                            ContentUtil.get(
232                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_BODY)));
233    
234                    return map;
235            }
236    
237            public static boolean getEmailEntryAddedEnabled(
238                    PortletPreferences preferences) {
239    
240                    String emailEntryAddedEnabled = preferences.getValue(
241                            "emailEntryAddedEnabled", StringPool.BLANK);
242    
243                    if (Validator.isNotNull(emailEntryAddedEnabled)) {
244                            return GetterUtil.getBoolean(emailEntryAddedEnabled);
245                    }
246                    else {
247                            return GetterUtil.getBoolean(
248                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_ENABLED));
249                    }
250            }
251    
252            public static Map<Locale, String> getEmailEntryAddedSubjectMap(
253                    PortletPreferences preferences) {
254    
255                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
256                            preferences, "emailEntryAddedSubject");
257    
258                    Locale defaultLocale = LocaleUtil.getDefault();
259    
260                    String defaultValue = map.get(defaultLocale);
261    
262                    if (Validator.isNotNull(defaultValue)) {
263                            return map;
264                    }
265    
266                    map.put(
267                            defaultLocale,
268                            ContentUtil.get(
269                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_SUBJECT)));
270    
271                    return map;
272            }
273    
274            public static Map<Locale, String> getEmailEntryUpdatedBodyMap(
275                    PortletPreferences preferences) {
276    
277                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
278                            preferences, "emailEntryUpdatedBody");
279    
280                    Locale defaultLocale = LocaleUtil.getDefault();
281    
282                    String defaultValue = map.get(defaultLocale);
283    
284                    if (Validator.isNotNull(defaultValue)) {
285                            return map;
286                    }
287    
288                    map.put(
289                            defaultLocale,
290                            ContentUtil.get(
291                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_BODY)));
292    
293                    return map;
294            }
295    
296            public static boolean getEmailEntryUpdatedEnabled(
297                    PortletPreferences preferences) {
298    
299                    String emailEntryUpdatedEnabled = preferences.getValue(
300                            "emailEntryUpdatedEnabled", StringPool.BLANK);
301    
302                    if (Validator.isNotNull(emailEntryUpdatedEnabled)) {
303                            return GetterUtil.getBoolean(emailEntryUpdatedEnabled);
304                    }
305                    else {
306                            return GetterUtil.getBoolean(
307                                    PropsUtil.get(PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_ENABLED));
308                    }
309            }
310    
311            public static Map<Locale, String> getEmailEntryUpdatedSubjectMap(
312                    PortletPreferences preferences) {
313    
314                    Map<Locale, String> map = LocalizationUtil.getLocalizationMap(
315                            preferences, "emailEntryUpdatedSubject");
316    
317                    Locale defaultLocale = LocaleUtil.getDefault();
318    
319                    String defaultValue = map.get(defaultLocale);
320    
321                    if (Validator.isNotNull(defaultValue)) {
322                            return map;
323                    }
324    
325                    map.put(
326                            defaultLocale,
327                            ContentUtil.get(
328                                    PropsUtil.get(
329                                            PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_SUBJECT)));
330    
331                    return map;
332            }
333    
334            public static String getEmailFromAddress(
335                            PortletPreferences preferences, long companyId)
336                    throws SystemException {
337    
338                    return PortalUtil.getEmailFromAddress(
339                            preferences, companyId, PropsValues.BOOKMARKS_EMAIL_FROM_ADDRESS);
340            }
341    
342            public static String getEmailFromName(
343                            PortletPreferences preferences, long companyId)
344                    throws SystemException {
345    
346                    return PortalUtil.getEmailFromName(
347                            preferences, companyId, PropsValues.BOOKMARKS_EMAIL_FROM_NAME);
348            }
349    
350            public static List<Object> getEntries(Hits hits) {
351                    List<Object> entries = new ArrayList<Object>();
352    
353                    for (Document document : hits.getDocs()) {
354                            String entryClassName = document.get(Field.ENTRY_CLASS_NAME);
355                            long entryClassPK = GetterUtil.getLong(
356                                    document.get(Field.ENTRY_CLASS_PK));
357    
358                            try {
359                                    Object obj = null;
360    
361                                    if (entryClassName.equals(BookmarksEntry.class.getName())) {
362                                            obj = BookmarksEntryLocalServiceUtil.getEntry(entryClassPK);
363                                    }
364                                    else if (entryClassName.equals(
365                                                            BookmarksFolder.class.getName())) {
366    
367                                            obj = BookmarksFolderLocalServiceUtil.getFolder(
368                                                    entryClassPK);
369                                    }
370    
371                                    entries.add(obj);
372                            }
373                            catch (Exception e) {
374                                    if (_log.isWarnEnabled()) {
375                                            _log.warn(
376                                                    "Bookmarks search index is stale and contains entry " +
377                                                            entryClassPK);
378                                    }
379    
380                                    continue;
381                            }
382                    }
383    
384                    return entries;
385            }
386    
387            public static OrderByComparator getEntryOrderByComparator(
388                    String orderByCol, String orderByType) {
389    
390                    boolean orderByAsc = false;
391    
392                    if (orderByType.equals("asc")) {
393                            orderByAsc = true;
394                    }
395    
396                    OrderByComparator orderByComparator = null;
397    
398                    if (orderByCol.equals("create-date")) {
399                            orderByComparator = new EntryCreateDateComparator(orderByAsc);
400                    }
401                    else if (orderByCol.equals("modified-date")) {
402                            orderByComparator = new EntryModifiedDateComparator(orderByAsc);
403                    }
404                    else if (orderByCol.equals("name")) {
405                            orderByComparator = new EntryNameComparator(orderByAsc);
406                    }
407                    else if (orderByCol.equals("priority")) {
408                            orderByComparator = new EntryPriorityComparator(orderByAsc);
409                    }
410                    else if (orderByCol.equals("url")) {
411                            orderByComparator = new EntryURLComparator(orderByAsc);
412                    }
413                    else if (orderByCol.equals("visits")) {
414                            orderByComparator = new EntryVisitsComparator(orderByAsc);
415                    }
416    
417                    return orderByComparator;
418            }
419    
420            private static Log _log = LogFactoryUtil.getLog(BookmarksUtil.class);
421    
422    }