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.search.Document;
019    import com.liferay.portal.kernel.search.Field;
020    import com.liferay.portal.kernel.search.Hits;
021    import com.liferay.portal.kernel.search.Indexer;
022    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portlet.asset.model.AssetVocabulary;
025    import com.liferay.portlet.asset.service.AssetVocabularyLocalServiceUtil;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    
030    /**
031     * @author Eudaldo Alonso
032     */
033    public class AssetVocabularyUtil {
034    
035            public static List<AssetVocabulary> getVocabularies(Hits hits)
036                    throws PortalException {
037    
038                    List<Document> documents = hits.toList();
039    
040                    List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>(
041                            documents.size());
042    
043                    for (Document document : documents) {
044                            long vocabularyId = GetterUtil.getLong(
045                                    document.get(Field.ASSET_VOCABULARY_ID));
046    
047                            AssetVocabulary vocabulary =
048                                    AssetVocabularyLocalServiceUtil.getVocabulary(vocabularyId);
049    
050                            if (vocabulary == null) {
051                                    vocabularies = null;
052    
053                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
054                                            AssetVocabulary.class);
055    
056                                    long companyId = GetterUtil.getLong(
057                                            document.get(Field.COMPANY_ID));
058    
059                                    indexer.delete(companyId, document.getUID());
060                            }
061                            else if (vocabularies != null) {
062                                    vocabularies.add(vocabulary);
063                            }
064                    }
065    
066                    return vocabularies;
067            }
068    
069    }