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.portlet.LiferayWindowState;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portal.util.PortalUtil;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
024    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
025    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
026    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
027    import com.liferay.portlet.bookmarks.util.comparator.EntryCreateDateComparator;
028    import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
029    import com.liferay.portlet.bookmarks.util.comparator.EntryNameComparator;
030    import com.liferay.portlet.bookmarks.util.comparator.EntryPriorityComparator;
031    import com.liferay.portlet.bookmarks.util.comparator.EntryURLComparator;
032    import com.liferay.portlet.bookmarks.util.comparator.EntryVisitsComparator;
033    
034    import java.util.Collections;
035    import java.util.List;
036    
037    import javax.portlet.PortletURL;
038    import javax.portlet.RenderResponse;
039    
040    import javax.servlet.http.HttpServletRequest;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class BookmarksUtil {
046    
047            public static void addPortletBreadcrumbEntries(
048                            BookmarksEntry entry, HttpServletRequest request,
049                            RenderResponse renderResponse)
050                    throws Exception {
051    
052                    BookmarksFolder folder = entry.getFolder();
053    
054                    if (folder.getFolderId() !=
055                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
056    
057                            addPortletBreadcrumbEntries(folder, request, renderResponse);
058                    }
059    
060                    PortletURL portletURL = renderResponse.createRenderURL();
061    
062                    portletURL.setParameter("struts_action", "/bookmarks/view_entry");
063                    portletURL.setParameter("entryId", String.valueOf(entry.getEntryId()));
064    
065                    PortalUtil.addPortletBreadcrumbEntry(
066                            request, entry.getName(), portletURL.toString());
067            }
068    
069            public static void addPortletBreadcrumbEntries(
070                            BookmarksFolder folder, HttpServletRequest request,
071                            RenderResponse renderResponse)
072                    throws Exception {
073    
074                    String strutsAction = ParamUtil.getString(request, "struts_action");
075    
076                    PortletURL portletURL = renderResponse.createRenderURL();
077    
078                    if (strutsAction.equals("/bookmarks/select_folder")) {
079                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
080                                    WebKeys.THEME_DISPLAY);
081    
082                            portletURL.setParameter(
083                                    "struts_action", "/bookmarks/select_folder");
084                            portletURL.setWindowState(LiferayWindowState.POP_UP);
085    
086                            PortalUtil.addPortletBreadcrumbEntry(
087                                    request, themeDisplay.translate("home"), portletURL.toString());
088                    }
089                    else {
090                            portletURL.setParameter("struts_action", "/bookmarks/view");
091                    }
092    
093                    List<BookmarksFolder> ancestorFolders = folder.getAncestors();
094    
095                    Collections.reverse(ancestorFolders);
096    
097                    for (BookmarksFolder ancestorFolder : ancestorFolders) {
098                            portletURL.setParameter(
099                                    "folderId", String.valueOf(ancestorFolder.getFolderId()));
100    
101                            PortalUtil.addPortletBreadcrumbEntry(
102                                    request, ancestorFolder.getName(), portletURL.toString());
103                    }
104    
105                    portletURL.setParameter(
106                            "folderId", String.valueOf(folder.getFolderId()));
107    
108                    if (folder.getFolderId() !=
109                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
110    
111                            PortalUtil.addPortletBreadcrumbEntry(
112                                    request, folder.getName(), portletURL.toString());
113                    }
114            }
115    
116            public static void addPortletBreadcrumbEntries(
117                            long folderId, HttpServletRequest request,
118                            RenderResponse renderResponse)
119                    throws Exception {
120    
121                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
122                            return;
123                    }
124    
125                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
126                            folderId);
127    
128                    addPortletBreadcrumbEntries(folder, request, renderResponse);
129            }
130    
131            public static OrderByComparator getEntriesOrderByComparator(
132                    String orderByCol, String orderByType) {
133    
134                    boolean orderByAsc = false;
135    
136                    if (orderByType.equals("asc")) {
137                            orderByAsc = true;
138                    }
139    
140                    OrderByComparator orderByComparator = null;
141    
142                    if (orderByCol.equals("create-date")) {
143                            orderByComparator = new EntryCreateDateComparator(orderByAsc);
144                    }
145                    else if (orderByCol.equals("modified-date")) {
146                            orderByComparator = new EntryModifiedDateComparator(orderByAsc);
147                    }
148                    else if (orderByCol.equals("name")) {
149                            orderByComparator = new EntryNameComparator(orderByAsc);
150                    }
151                    else if (orderByCol.equals("priority")) {
152                            orderByComparator = new EntryPriorityComparator(orderByAsc);
153                    }
154                    else if (orderByCol.equals("url")) {
155                            orderByComparator = new EntryURLComparator(orderByAsc);
156                    }
157                    else if (orderByCol.equals("visits")) {
158                            orderByComparator = new EntryVisitsComparator(orderByAsc);
159                    }
160    
161                    return orderByComparator;
162            }
163    
164    }