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.imagegallerydisplay.util;
016    
017    import com.liferay.portal.kernel.portlet.LiferayWindowState;
018    import com.liferay.portal.kernel.repository.model.Folder;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portal.util.WebKeys;
024    import com.liferay.portlet.PortletPreferencesFactoryUtil;
025    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
026    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
027    
028    import java.util.Collections;
029    import java.util.List;
030    
031    import javax.portlet.PortletPreferences;
032    import javax.portlet.PortletURL;
033    import javax.portlet.RenderResponse;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class IGUtil {
041    
042            public static void addPortletBreadcrumbEntries(
043                            Folder folder, HttpServletRequest request,
044                            RenderResponse renderResponse)
045                    throws Exception {
046    
047                    String strutsAction = ParamUtil.getString(request, "struts_action");
048    
049                    PortletURL portletURL = renderResponse.createRenderURL();
050    
051                    if (strutsAction.equals("/image_gallery_display/select_folder")) {
052                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
053                                    WebKeys.THEME_DISPLAY);
054    
055                            portletURL.setParameter("struts_action", strutsAction);
056                            portletURL.setWindowState(LiferayWindowState.POP_UP);
057    
058                            PortalUtil.addPortletBreadcrumbEntry(
059                                    request, themeDisplay.translate("home"), portletURL.toString());
060                    }
061                    else {
062                            portletURL.setParameter(
063                                    "struts_action", "/image_gallery_display/view");
064                    }
065    
066                    long defaultFolderId = getDefaultFolderId(request);
067    
068                    List<Folder> ancestorFolders = Collections.emptyList();
069    
070                    if ((folder != null) && (folder.getFolderId() != defaultFolderId)) {
071                            ancestorFolders = folder.getAncestors();
072    
073                            int indexOfRootFolder = -1;
074    
075                            for (int i = 0; i < ancestorFolders.size(); i++) {
076                                    Folder ancestorFolder = ancestorFolders.get(i);
077    
078                                    if (defaultFolderId == ancestorFolder.getFolderId()) {
079                                            indexOfRootFolder = i;
080                                    }
081                            }
082    
083                            if (indexOfRootFolder > -1) {
084                                    ancestorFolders = ancestorFolders.subList(0, indexOfRootFolder);
085                            }
086                    }
087    
088                    Collections.reverse(ancestorFolders);
089    
090                    for (Folder ancestorFolder : ancestorFolders) {
091                            portletURL.setParameter(
092                                    "folderId", String.valueOf(ancestorFolder.getFolderId()));
093    
094                            PortalUtil.addPortletBreadcrumbEntry(
095                                    request, ancestorFolder.getName(), portletURL.toString());
096                    }
097    
098                    portletURL.setParameter(
099                            "folderId", String.valueOf(folder.getFolderId()));
100    
101                    if (strutsAction.equals("/journal/select_image_gallery")) {
102                            portletURL.setParameter(
103                                    "groupId", String.valueOf(folder.getGroupId()));
104                    }
105    
106                    PortalUtil.addPortletBreadcrumbEntry(
107                            request, folder.getName(), portletURL.toString());
108            }
109    
110            public static void addPortletBreadcrumbEntries(
111                            long folderId, HttpServletRequest request,
112                            RenderResponse renderResponse)
113                    throws Exception {
114    
115                    if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
116                            return;
117                    }
118    
119                    Folder folder = DLAppLocalServiceUtil.getFolder(folderId);
120    
121                    addPortletBreadcrumbEntries(folder, request, renderResponse);
122            }
123    
124            protected static long getDefaultFolderId(HttpServletRequest request)
125                    throws Exception {
126    
127                    PortletPreferences portletPreferences =
128                            PortletPreferencesFactoryUtil.getPortletPreferences(
129                                    request, PortalUtil.getPortletId(request));
130    
131                    return GetterUtil.getLong(
132                            portletPreferences.getValue(
133                                    "rootFolderId",
134                                    String.valueOf(DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)));
135            }
136    
137    }