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.fields;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProperty;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.file.CmsResource;
034import org.opencms.search.I_CmsSearchDocument;
035import org.opencms.search.extractors.I_CmsExtractionResult;
036import org.opencms.util.CmsStringUtil;
037
038import java.util.List;
039
040import org.apache.lucene.document.Document;
041import org.apache.lucene.document.Field;
042import org.apache.lucene.document.StringField;
043
044/**
045 * Describes a field configuration using the old (pre 8.0) logic for categories that depend on properties.<p>
046 *
047 * Configure this class for a search index field configuration in case you want the old behavior.<p>
048 *
049 * @since 8.0.0
050 */
051public class CmsSearchFieldConfigurationOldCategories extends CmsLuceneFieldConfiguration {
052
053    /** The serial version id. */
054    private static final long serialVersionUID = -8513437470793639802L;
055
056    /**
057     * Default constructor.<p>
058     */
059    public CmsSearchFieldConfigurationOldCategories() {
060
061        // nothing special to to here
062    }
063
064    /**
065     * Extends the given document by resource category information based on properties.<p>
066     *
067     * @param document the document to extend
068     * @param cms the OpenCms context used for building the search index
069     * @param resource the resource that is indexed
070     * @param extractionResult the plain text extraction result from the resource
071     * @param properties the list of all properties directly attached to the resource (not searched)
072     * @param propertiesSearched the list of all searched properties of the resource
073     *
074     * @return the document extended by resource category information
075     *
076     * @see org.opencms.search.fields.CmsSearchFieldConfiguration#appendCategories(org.opencms.search.I_CmsSearchDocument, org.opencms.file.CmsObject, org.opencms.file.CmsResource, org.opencms.search.extractors.I_CmsExtractionResult, java.util.List, java.util.List)
077     */
078    @Override
079    protected I_CmsSearchDocument appendCategories(
080        I_CmsSearchDocument document,
081        CmsObject cms,
082        CmsResource resource,
083        I_CmsExtractionResult extractionResult,
084        List<CmsProperty> properties,
085        List<CmsProperty> propertiesSearched) {
086
087        Document doc = (Document)document.getDocument();
088
089        // add the category of the file (this is searched so the value can also be attached on a folder)
090        String value = CmsProperty.get(CmsPropertyDefinition.PROPERTY_SEARCH_CATEGORY, propertiesSearched).getValue();
091        if (CmsStringUtil.isNotEmpty(value)) {
092            // all categories are internally stored lower case
093            value = value.trim().toLowerCase();
094            if (value.length() > 0) {
095                Field field = new StringField(CmsSearchField.FIELD_CATEGORY, value, Field.Store.YES);
096                // field.setBoost(0);
097                doc.add(field);
098            }
099        }
100        return document;
101    }
102}