001    /**
002     * Copyright (c) 2000-present 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.asset.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.search.Document;
020    import com.liferay.portal.kernel.search.Field;
021    import com.liferay.portal.kernel.search.Hits;
022    import com.liferay.portal.kernel.search.Indexer;
023    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
024    import com.liferay.portal.kernel.util.GetterUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.asset.model.AssetCategory;
029    import com.liferay.portlet.asset.model.AssetVocabulary;
030    import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
031    
032    import java.util.ArrayList;
033    import java.util.Collections;
034    import java.util.List;
035    
036    import javax.portlet.PortletURL;
037    import javax.portlet.RenderResponse;
038    
039    import javax.servlet.http.HttpServletRequest;
040    
041    /**
042     * @author Eudaldo Alonso
043     */
044    public class AssetCategoryUtil {
045    
046            public static void addPortletBreadcrumbEntry(
047                            AssetVocabulary vocabulary, AssetCategory category,
048                            HttpServletRequest request, RenderResponse renderResponse)
049                    throws PortalException {
050    
051                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
052                            WebKeys.THEME_DISPLAY);
053    
054                    PortletURL portletURL = renderResponse.createRenderURL();
055    
056                    portletURL.setParameter("struts_action", "/asset_category_admin/view");
057    
058                    PortalUtil.addPortletBreadcrumbEntry(
059                            request, LanguageUtil.get(request, "vocabularies"),
060                            portletURL.toString());
061    
062                    if (category == null) {
063                            PortalUtil.addPortletBreadcrumbEntry(
064                                    request, vocabulary.getTitle(themeDisplay.getLocale()), null);
065    
066                            return;
067                    }
068    
069                    portletURL.setParameter(
070                            "struts_action", "/asset_category_admin/view_categories");
071    
072                    portletURL.setParameter(
073                            "vocabularyId", String.valueOf(vocabulary.getVocabularyId()));
074    
075                    PortalUtil.addPortletBreadcrumbEntry(
076                            request, vocabulary.getTitle(themeDisplay.getLocale()),
077                            portletURL.toString());
078    
079                    List<AssetCategory> ancestorsCategories = category.getAncestors();
080    
081                    Collections.reverse(ancestorsCategories);
082    
083                    for (AssetCategory curCategory : ancestorsCategories) {
084                            portletURL.setParameter(
085                                    "categoryId", String.valueOf(curCategory.getCategoryId()));
086    
087                            PortalUtil.addPortletBreadcrumbEntry(
088                                    request, curCategory.getTitle(themeDisplay.getLocale()),
089                                    portletURL.toString());
090                    }
091    
092                    PortalUtil.addPortletBreadcrumbEntry(
093                            request, category.getTitle(themeDisplay.getLocale()), null);
094            }
095    
096            public static List<AssetCategory> getCategories(Hits hits)
097                    throws PortalException {
098    
099                    List<Document> documents = hits.toList();
100    
101                    List<AssetCategory> categories = new ArrayList<AssetCategory>(
102                            documents.size());
103    
104                    for (Document document : documents) {
105                            long categoryId = GetterUtil.getLong(
106                                    document.get(Field.ASSET_CATEGORY_ID));
107    
108                            AssetCategory category = AssetCategoryLocalServiceUtil.getCategory(
109                                    categoryId);
110    
111                            if (category == null) {
112                                    categories = null;
113    
114                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
115                                            AssetCategory.class);
116    
117                                    long companyId = GetterUtil.getLong(
118                                            document.get(Field.COMPANY_ID));
119    
120                                    indexer.delete(companyId, document.getUID());
121                            }
122                            else if (categories != null) {
123                                    categories.add(category);
124                            }
125                    }
126    
127                    return categories;
128            }
129    
130    }