1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.blogs.util;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.portlet.LiferayWindowState;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portlet.blogs.model.BlogsCategory;
29  import com.liferay.portlet.blogs.service.BlogsCategoryLocalServiceUtil;
30  
31  import javax.portlet.PortletURL;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  import javax.portlet.WindowState;
35  
36  import javax.servlet.jsp.PageContext;
37  
38  /**
39   * <a href="BlogsUtil.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class BlogsUtil {
45  
46      public static String getBreadcrumbs(
47              long categoryId, PageContext pageContext, RenderRequest req,
48              RenderResponse res)
49          throws Exception {
50  
51          BlogsCategory category = null;
52  
53          try {
54              category = BlogsCategoryLocalServiceUtil.getCategory(categoryId);
55          }
56          catch (Exception e) {
57          }
58  
59          return getBreadcrumbs(category, pageContext, req, res);
60      }
61  
62      public static String getBreadcrumbs(
63              BlogsCategory category, PageContext pageContext, RenderRequest req,
64              RenderResponse res)
65          throws Exception {
66  
67          PortletURL categoriesURL = res.createRenderURL();
68  
69          WindowState windowState = req.getWindowState();
70  
71          if (windowState.equals(LiferayWindowState.POP_UP)) {
72              categoriesURL.setWindowState(LiferayWindowState.POP_UP);
73  
74              categoriesURL.setParameter(
75                  "struts_action", "/blogs/select_category");
76          }
77          else {
78              categoriesURL.setWindowState(WindowState.MAXIMIZED);
79  
80              categoriesURL.setParameter("struts_action", "/blogs/view");
81              categoriesURL.setParameter("tabs1", "categories");
82          }
83  
84          String categoriesLink =
85              "<a href=\"" + categoriesURL.toString() + "\">" +
86                  LanguageUtil.get(pageContext, "categories") + "</a>";
87  
88          if (category == null) {
89              return categoriesLink;
90          }
91  
92          String breadcrumbs = StringPool.BLANK;
93  
94          if (category != null) {
95              for (int i = 0;; i++) {
96                  PortletURL portletURL = res.createRenderURL();
97  
98                  if (windowState.equals(LiferayWindowState.POP_UP)) {
99                      portletURL.setWindowState(LiferayWindowState.POP_UP);
100 
101                     portletURL.setParameter(
102                         "struts_action", "/blogs/select_category");
103                 }
104                 else {
105                     portletURL.setWindowState(WindowState.MAXIMIZED);
106 
107                     portletURL.setParameter("struts_action", "/blogs/view");
108                     portletURL.setParameter("tabs1", "categories");
109                     portletURL.setParameter(
110                         "categoryId", String.valueOf(category.getCategoryId()));
111                 }
112 
113                 String categoryLink =
114                     "<a href=\"" + portletURL.toString() + "\">" +
115                         category.getName() + "</a>";
116 
117                 if (i == 0) {
118                     breadcrumbs = categoryLink;
119                 }
120                 else {
121                     breadcrumbs = categoryLink + " &raquo; " + breadcrumbs;
122                 }
123 
124                 if (category.isRoot()) {
125                     break;
126                 }
127 
128                 category = BlogsCategoryLocalServiceUtil.getCategory(
129                     category.getParentCategoryId());
130             }
131         }
132 
133         breadcrumbs = categoriesLink + " &raquo; " + breadcrumbs;
134 
135         return breadcrumbs;
136     }
137 
138 }