001    /**
002     * Copyright (c) 2000-2010 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                            long folderId, HttpServletRequest request,
071                            RenderResponse renderResponse)
072                    throws Exception {
073    
074                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
075                            return;
076                    }
077    
078                    BookmarksFolder folder = BookmarksFolderLocalServiceUtil.getFolder(
079                            folderId);
080    
081                    addPortletBreadcrumbEntries(folder, request, renderResponse);
082            }
083    
084            public static void addPortletBreadcrumbEntries(
085                            BookmarksFolder folder, HttpServletRequest request,
086                            RenderResponse renderResponse)
087                    throws Exception {
088    
089                    String strutsAction = ParamUtil.getString(
090                            request, "struts_action");
091    
092                    boolean selectFolder = strutsAction.equals("/bookmarks/select_folder");
093    
094                    PortletURL portletURL = renderResponse.createRenderURL();
095    
096                    if (selectFolder) {
097                            ThemeDisplay themeDisplay =     (ThemeDisplay)request.getAttribute(
098                                    WebKeys.THEME_DISPLAY);
099    
100                            portletURL.setWindowState(LiferayWindowState.POP_UP);
101    
102                            portletURL.setParameter(
103                                    "struts_action", "/bookmarks/select_folder");
104    
105                            PortalUtil.addPortletBreadcrumbEntry(
106                                    request, themeDisplay.translate("bookmarks-home"),
107                                    portletURL.toString());
108                    }
109                    else {
110                            portletURL.setParameter("struts_action", "/bookmarks/view");
111                    }
112    
113                    List<BookmarksFolder> ancestorFolders = folder.getAncestors();
114    
115                    Collections.reverse(ancestorFolders);
116    
117                    for (BookmarksFolder ancestorFolder : ancestorFolders) {
118                            portletURL.setParameter(
119                                    "folderId", String.valueOf(ancestorFolder.getFolderId()));
120    
121                            PortalUtil.addPortletBreadcrumbEntry(
122                                    request, ancestorFolder.getName(), portletURL.toString());
123                    }
124    
125                    portletURL.setParameter(
126                            "folderId", String.valueOf(folder.getFolderId()));
127    
128                    if (folder.getFolderId() !=
129                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
130    
131                            PortalUtil.addPortletBreadcrumbEntry(
132                                    request, folder.getName(), portletURL.toString());
133                    }
134            }
135    
136            public static OrderByComparator getEntriesOrderByComparator(
137                    String orderByCol, String orderByType) {
138    
139                    boolean orderByAsc = false;
140    
141                    if (orderByType.equals("asc")) {
142                            orderByAsc = true;
143                    }
144    
145                    OrderByComparator orderByComparator = null;
146    
147                    if (orderByCol.equals("create-date")) {
148                            orderByComparator = new EntryCreateDateComparator(orderByAsc);
149                    }
150                    else if (orderByCol.equals("modified-date")) {
151                            orderByComparator = new EntryModifiedDateComparator(orderByAsc);
152                    }
153                    else if (orderByCol.equals("name")) {
154                            orderByComparator = new EntryNameComparator(orderByAsc);
155                    }
156                    else if (orderByCol.equals("priority")) {
157                            orderByComparator = new EntryPriorityComparator(orderByAsc);
158                    }
159                    else if (orderByCol.equals("url")) {
160                            orderByComparator = new EntryURLComparator(orderByAsc);
161                    }
162                    else if (orderByCol.equals("visits")) {
163                            orderByComparator = new EntryVisitsComparator(orderByAsc);
164                    }
165    
166                    return orderByComparator;
167            }
168    
169    }