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.imagegallery.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.imagegallery.model.IGFolder;
29  import com.liferay.portlet.imagegallery.model.IGImage;
30  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
31  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
32  
33  import javax.portlet.PortletURL;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  import javax.portlet.WindowState;
37  
38  import javax.servlet.jsp.PageContext;
39  
40  /**
41   * <a href="IGUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class IGUtil {
47  
48      public static String getBreadcrumbs(
49              long folderId, long imageId, PageContext pageContext,
50              RenderRequest req, RenderResponse res)
51          throws Exception {
52  
53          if (imageId > 0) {
54              IGImage image = IGImageLocalServiceUtil.getImage(imageId);
55  
56              return getBreadcrumbs(
57                  image.getFolder(), image, pageContext, req, res);
58          }
59          else {
60              IGFolder folder = null;
61  
62              try {
63                  folder = IGFolderLocalServiceUtil.getFolder(folderId);
64              }
65              catch (Exception e) {
66              }
67  
68              return getBreadcrumbs(folder, null, pageContext, req, res);
69          }
70      }
71  
72      public static String getBreadcrumbs(
73              IGFolder folder, IGImage image, PageContext pageContext,
74              RenderRequest req, RenderResponse res)
75          throws Exception {
76  
77          if ((image != null) && (folder == null)) {
78              folder = image.getFolder();
79          }
80  
81          PortletURL foldersURL = res.createRenderURL();
82  
83          WindowState windowState = req.getWindowState();
84  
85          if (windowState.equals(LiferayWindowState.POP_UP)) {
86              foldersURL.setWindowState(LiferayWindowState.POP_UP);
87  
88              foldersURL.setParameter(
89                  "struts_action", "/image_gallery/select_folder");
90          }
91          else {
92              foldersURL.setWindowState(WindowState.MAXIMIZED);
93  
94              foldersURL.setParameter("struts_action", "/image_gallery/view");
95          }
96  
97          String foldersLink =
98              "<a href=\"" + foldersURL.toString() + "\">" +
99                  LanguageUtil.get(pageContext, "folders") + "</a>";
100 
101         if (folder == null) {
102             return foldersLink;
103         }
104 
105         String breadcrumbs = StringPool.BLANK;
106 
107         if (folder != null) {
108             for (int i = 0;; i++) {
109                 PortletURL portletURL = res.createRenderURL();
110 
111                 if (windowState.equals(LiferayWindowState.POP_UP)) {
112                     portletURL.setWindowState(LiferayWindowState.POP_UP);
113 
114                     portletURL.setParameter(
115                         "struts_action", "/image_gallery/select_folder");
116                     portletURL.setParameter(
117                         "folderId", String.valueOf(folder.getFolderId()));
118                 }
119                 else {
120                     portletURL.setWindowState(WindowState.MAXIMIZED);
121 
122                     portletURL.setParameter(
123                         "struts_action", "/image_gallery/view");
124                     portletURL.setParameter(
125                         "folderId", String.valueOf(folder.getFolderId()));
126                 }
127 
128                 String folderLink =
129                     "<a href=\"" + portletURL.toString() + "\">" +
130                         folder.getName() + "</a>";
131 
132                 if (i == 0) {
133                     breadcrumbs = folderLink;
134                 }
135                 else {
136                     breadcrumbs = folderLink + " &raquo; " + breadcrumbs;
137                 }
138 
139                 if (folder.isRoot()) {
140                     break;
141                 }
142 
143                 folder = IGFolderLocalServiceUtil.getFolder(
144                     folder.getParentFolderId());
145             }
146         }
147 
148         breadcrumbs = foldersLink + " &raquo; " + breadcrumbs;
149 
150         if (image != null) {
151             PortletURL imageURL = res.createRenderURL();
152 
153             imageURL.setWindowState(WindowState.MAXIMIZED);
154 
155             imageURL.setParameter("struts_action", "/image_gallery/edit_image");
156             imageURL.setParameter(
157                 "imageId", String.valueOf(image.getImageId()));
158 
159             String imageLink =
160                 "<a href=\"" + imageURL.toString() + "\">" +
161                     image.getImageId() + "</a>";
162 
163             breadcrumbs = breadcrumbs + " &raquo; " + imageLink;
164         }
165 
166         return breadcrumbs;
167     }
168 
169 }