001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.bookmarks.util;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.portlet.LiferayWindowState;
025: import com.liferay.portal.kernel.util.OrderByComparator;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portlet.bookmarks.model.BookmarksEntry;
028: import com.liferay.portlet.bookmarks.model.BookmarksFolder;
029: import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
030: import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
031: import com.liferay.portlet.bookmarks.util.comparator.EntryCreateDateComparator;
032: import com.liferay.portlet.bookmarks.util.comparator.EntryModifiedDateComparator;
033: import com.liferay.portlet.bookmarks.util.comparator.EntryNameComparator;
034: import com.liferay.portlet.bookmarks.util.comparator.EntryPriorityComparator;
035: import com.liferay.portlet.bookmarks.util.comparator.EntryURLComparator;
036: import com.liferay.portlet.bookmarks.util.comparator.EntryVisitsComparator;
037:
038: import javax.portlet.PortletURL;
039: import javax.portlet.RenderRequest;
040: import javax.portlet.RenderResponse;
041: import javax.portlet.WindowState;
042:
043: import javax.servlet.jsp.PageContext;
044:
045: /**
046: * <a href="BookmarksUtil.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class BookmarksUtil {
052:
053: public static String getBreadcrumbs(long folderId, long entryId,
054: PageContext pageContext, RenderRequest req,
055: RenderResponse res) throws Exception {
056:
057: if (entryId > 0) {
058: BookmarksEntry entry = BookmarksEntryLocalServiceUtil
059: .getEntry(entryId);
060:
061: return getBreadcrumbs(entry.getFolder(), entry,
062: pageContext, req, res);
063: } else {
064: BookmarksFolder folder = null;
065:
066: try {
067: folder = BookmarksFolderLocalServiceUtil
068: .getFolder(folderId);
069: } catch (Exception e) {
070: }
071:
072: return getBreadcrumbs(folder, null, pageContext, req, res);
073: }
074: }
075:
076: public static String getBreadcrumbs(BookmarksFolder folder,
077: BookmarksEntry entry, PageContext pageContext,
078: RenderRequest req, RenderResponse res) throws Exception {
079:
080: if ((entry != null) && (folder == null)) {
081: folder = entry.getFolder();
082: }
083:
084: PortletURL foldersURL = res.createRenderURL();
085:
086: WindowState windowState = req.getWindowState();
087:
088: if (windowState.equals(LiferayWindowState.POP_UP)) {
089: foldersURL.setWindowState(LiferayWindowState.POP_UP);
090:
091: foldersURL.setParameter("struts_action",
092: "/bookmarks/select_folder");
093: } else {
094: foldersURL.setWindowState(WindowState.MAXIMIZED);
095:
096: foldersURL.setParameter("struts_action", "/bookmarks/view");
097: }
098:
099: String foldersLink = "<a href=\"" + foldersURL.toString()
100: + "\">" + LanguageUtil.get(pageContext, "folders")
101: + "</a>";
102:
103: if (folder == null) {
104: return foldersLink;
105: }
106:
107: String breadcrumbs = StringPool.BLANK;
108:
109: if (folder != null) {
110: for (int i = 0;; i++) {
111: folder = folder.toEscapedModel();
112:
113: PortletURL portletURL = res.createRenderURL();
114:
115: if (windowState.equals(LiferayWindowState.POP_UP)) {
116: portletURL
117: .setWindowState(LiferayWindowState.POP_UP);
118:
119: portletURL.setParameter("struts_action",
120: "/bookmarks/select_folder");
121: portletURL.setParameter("folderId", String
122: .valueOf(folder.getFolderId()));
123: } else {
124: portletURL.setWindowState(WindowState.MAXIMIZED);
125:
126: portletURL.setParameter("struts_action",
127: "/bookmarks/view");
128: portletURL.setParameter("folderId", String
129: .valueOf(folder.getFolderId()));
130: }
131:
132: String folderLink = "<a href=\""
133: + portletURL.toString() + "\">"
134: + folder.getName() + "</a>";
135:
136: if (i == 0) {
137: breadcrumbs = folderLink;
138: } else {
139: breadcrumbs = folderLink + " » "
140: + breadcrumbs;
141: }
142:
143: if (folder.isRoot()) {
144: break;
145: }
146:
147: folder = BookmarksFolderLocalServiceUtil
148: .getFolder(folder.getParentFolderId());
149: }
150: }
151:
152: breadcrumbs = foldersLink + " » " + breadcrumbs;
153:
154: if (entry != null) {
155: entry = entry.toEscapedModel();
156:
157: PortletURL entryURL = res.createRenderURL();
158:
159: entryURL.setWindowState(WindowState.MAXIMIZED);
160:
161: entryURL.setParameter("struts_action",
162: "/bookmarks/edit_entry");
163: entryURL.setParameter("entryId", String.valueOf(entry
164: .getEntryId()));
165:
166: String entryLink = "<a href=\"" + entryURL.toString()
167: + "\">" + entry.getEntryId() + "</a>";
168:
169: breadcrumbs = breadcrumbs + " » " + entryLink;
170: }
171:
172: return breadcrumbs;
173: }
174:
175: public static OrderByComparator getEntriesOrderByComparator(
176: String orderByCol, String orderByType) {
177:
178: boolean orderByAsc = false;
179:
180: if (orderByType.equals("asc")) {
181: orderByAsc = true;
182: }
183:
184: OrderByComparator orderByComparator = null;
185:
186: if (orderByCol.equals("create-date")) {
187: orderByComparator = new EntryCreateDateComparator(
188: orderByAsc);
189: } else if (orderByCol.equals("modified-date")) {
190: orderByComparator = new EntryModifiedDateComparator(
191: orderByAsc);
192: } else if (orderByCol.equals("name")) {
193: orderByComparator = new EntryNameComparator(orderByAsc);
194: } else if (orderByCol.equals("priority")) {
195: orderByComparator = new EntryPriorityComparator(orderByAsc);
196: } else if (orderByCol.equals("url")) {
197: orderByComparator = new EntryURLComparator(orderByAsc);
198: } else if (orderByCol.equals("visits")) {
199: orderByComparator = new EntryVisitsComparator(orderByAsc);
200: }
201:
202: return orderByComparator;
203: }
204:
205: }
|