001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.search.galleries;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035import org.opencms.search.I_CmsSearchDocument;
036import org.opencms.search.Messages;
037import org.opencms.search.fields.CmsSearchField;
038import org.opencms.search.solr.CmsSolrIndex;
039import org.opencms.util.CmsStringUtil;
040import org.opencms.util.CmsUUID;
041
042import java.util.Locale;
043
044/**
045 * Contains the functions for the gallery search.<p>
046 *
047 * @since 8.0.0
048 */
049public class CmsGallerySearch {
050
051    /** The OpenCms object used for the search. */
052    protected transient CmsObject m_cms;
053
054    /**
055     * The gallery search index used for the gallery search.<p>
056     *
057     * TODO: Replace with the Solr index, as well.
058     */
059    private CmsSolrIndex m_index;
060
061    /**
062     * Searches by structure id.<p>
063     *
064     * @param cms the OpenCms context to use for the search
065     * @param structureId the structure id of the document to search for
066     * @param locale the locale for which the search result should be returned
067     *
068     * @return the search result
069     *
070     * @throws CmsException if something goes wrong
071     */
072    public static CmsGallerySearchResult searchById(CmsObject cms, CmsUUID structureId, Locale locale)
073    throws CmsException {
074
075        CmsGallerySearch gallerySearch = new CmsGallerySearch();
076        gallerySearch.init(cms);
077        gallerySearch.setIndexForProject(cms);
078        return gallerySearch.searchById(structureId, locale);
079    }
080
081    /**
082     * Searches by structure id.<p>
083     *
084     * @param cms the OpenCms context to use for the search
085     * @param rootPath the resource root path
086     * @param locale the locale for which the search result should be returned
087     *
088     * @return the search result
089     *
090     * @throws CmsException if something goes wrong
091     */
092    public static CmsGallerySearchResult searchByPath(CmsObject cms, String rootPath, Locale locale)
093    throws CmsException {
094
095        CmsGallerySearch gallerySearch = new CmsGallerySearch();
096        gallerySearch.init(cms);
097        gallerySearch.setIndexForProject(cms);
098        return gallerySearch.searchByPath(rootPath, locale);
099    }
100
101    /**
102     * Returns the name of the current search index.<p>
103     *
104     * @return the name of the current search index
105     */
106    public String getIndex() {
107
108        return getSearchIndex().getName();
109    }
110
111    /**
112     * Returns the gallery search result list.<p>
113     *
114     * @param params the gallery search parameters
115     *
116     * @return the gallery search result list
117     *
118     * @throws CmsException if the search failed
119     */
120    public CmsGallerySearchResultList getResult(CmsGallerySearchParameters params) throws CmsException {
121
122        CmsGallerySearchResultList result = null;
123        if ((m_cms == null) || (m_index == null)) {
124            throw new CmsException(Messages.get().container(Messages.ERR_SEARCH_NOT_INITIALIZED_0));
125        }
126        result = m_index.gallerySearch(m_cms, params);
127
128        if (result.size() > 0) {
129
130            result.calculatePages(params.getResultPage(), params.getMatchesPerPage());
131
132        } else {
133            result = new CmsGallerySearchResultList();
134        }
135
136        return result;
137    }
138
139    /**
140     * Returns the current gallery search index.<p>
141     *
142     * @return the current gallery search index
143     */
144    public CmsSolrIndex getSearchIndex() {
145
146        return m_index;
147    }
148
149    /**
150     * Initializes the bean with the provided OpenCms context object.<p>
151     *
152     * @param cms the OpenCms context to use for the search
153     */
154    public void init(CmsObject cms) {
155
156        m_cms = cms;
157    }
158
159    /**
160     * Searches by structure id.<p>
161     *
162     * @param id the structure id of the document to search for
163     * @param locale the locale for which the search result should be returned
164     *
165     * @return the search result
166     *
167     * @throws CmsException if something goes wrong
168     */
169    public CmsGallerySearchResult searchById(CmsUUID id, Locale locale) throws CmsException {
170
171        I_CmsSearchDocument sDoc = m_index.getDocument(
172            CmsSearchField.FIELD_ID,
173            id.toString(),
174            CmsGallerySearchResult.getRequiredSolrFields());
175
176        CmsGallerySearchResult result = null;
177        if ((sDoc != null) && (sDoc.getDocument() != null)) {
178            result = new CmsGallerySearchResult(sDoc, m_cms, 100, locale);
179        } else {
180            CmsResource res = m_cms.readResource(id, CmsResourceFilter.ALL);
181            result = new CmsGallerySearchResult(m_cms, res, null);
182        }
183        return result;
184    }
185
186    /**
187     * Searches by structure id.<p>
188     *
189     * @param path the resource path
190     * @param locale the locale for which the search result should be returned
191     *
192     * @return the search result
193     *
194     * @throws CmsException if something goes wrong
195     */
196    public CmsGallerySearchResult searchByPath(String path, Locale locale) throws CmsException {
197
198        I_CmsSearchDocument sDoc = m_index.getDocument(CmsSearchField.FIELD_PATH, path);
199        CmsGallerySearchResult result = null;
200        if ((sDoc != null) && (sDoc.getDocument() != null)) {
201            result = new CmsGallerySearchResult(sDoc, m_cms, 100, locale);
202        } else {
203            CmsResource res = m_cms.readResource(path, CmsResourceFilter.IGNORE_EXPIRATION);
204            result = new CmsGallerySearchResult(m_cms, res, null);
205        }
206        return result;
207    }
208
209    /**
210     * Set the name of the index to search.<p>
211     *
212     * A former search result will be deleted.<p>
213     *
214     * @param indexName the name of the index
215     *
216     * @throws CmsException if the index was not found
217     */
218    public void setIndex(String indexName) throws CmsException {
219
220        if (CmsStringUtil.isEmpty(indexName)) {
221            throw new CmsException(Messages.get().container(Messages.ERR_INDEXSOURCE_CREATE_MISSING_NAME_0));
222        }
223        CmsSolrIndex index = OpenCms.getSearchManager().getIndexSolr(indexName);
224        if (index == null) {
225            throw new CmsException(Messages.get().container(Messages.ERR_INDEX_NOT_FOUND_1, indexName));
226        }
227
228        m_index = index;
229
230    }
231
232    /**
233     * Sets the index name according to the current project.<p>
234     *
235     * @param cms the cms context
236     *
237     * @throws CmsException in case setting the index fails
238     */
239    public void setIndexForProject(CmsObject cms) throws CmsException {
240
241        setIndex(
242            cms.getRequestContext().getCurrentProject().isOnlineProject()
243            ? CmsSolrIndex.DEFAULT_INDEX_NAME_ONLINE
244            : CmsSolrIndex.DEFAULT_INDEX_NAME_OFFLINE);
245    }
246}