001/*
002 * File   : $Source$
003 * Date   : $Date$
004 * Version: $Revision$
005 *
006 * This library is part of OpenCms -
007 * the Open Source Content Management System
008 *
009 * Copyright (C) 2002 - 2009 Alkacon Software (http://www.alkacon.com)
010 *
011 * This library is free software; you can redistribute it and/or
012 * modify it under the terms of the GNU Lesser General Public
013 * License as published by the Free Software Foundation; either
014 * version 2.1 of the License, or (at your option) any later version.
015 *
016 * This library is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 * Lesser General Public License for more details.
020 *
021 * For further information about Alkacon Software, please see the
022 * company website: http://www.alkacon.com
023 *
024 * For further information about OpenCms, please see the
025 * project website: http://www.opencms.org
026 *
027 * You should have received a copy of the GNU Lesser General Public
028 * License along with this library; if not, write to the Free Software
029 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030 */
031
032package org.opencms.file.collectors;
033
034import org.opencms.file.CmsDataAccessException;
035import org.opencms.file.CmsObject;
036import org.opencms.file.CmsResource;
037import org.opencms.main.CmsException;
038import org.opencms.main.OpenCms;
039import org.opencms.search.CmsSearchManager;
040import org.opencms.search.solr.CmsSolrIndex;
041import org.opencms.search.solr.CmsSolrQuery;
042import org.opencms.util.CmsRequestUtil;
043import org.opencms.util.CmsStringUtil;
044
045import java.util.ArrayList;
046import java.util.Arrays;
047import java.util.Collections;
048import java.util.HashMap;
049import java.util.List;
050import java.util.Map;
051
052/**
053 * A Solr collector.<p>
054 *
055 * @since 8.5.0
056 */
057public class CmsSolrCollector extends A_CmsResourceCollector {
058
059    /** Constant array of the collectors implemented by this class. */
060    private static final String[] COLLECTORS = {"byQuery", "byContext"};
061
062    /** Array list for fast collector name lookup. */
063    private static final List<String> COLLECTORS_LIST = Collections.unmodifiableList(Arrays.asList(COLLECTORS));
064
065    /** The folder path to create the "create link" for. */
066    private static final String PARAM_CREATE_PATH = "createPath";
067
068    /** Option, specifying if the Solr Query is URL-encoded or not */
069    private static final String PARAM_DECODE_URL = "decodeUrl";
070
071    /** A constant for a key. */
072    private static final String SOLR_PART = "solrPart";
073
074    /**
075     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames()
076     */
077    public List<String> getCollectorNames() {
078
079        return COLLECTORS_LIST;
080    }
081
082    /**
083     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateLink(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
084     */
085    public String getCreateLink(CmsObject cms, String collectorName, String param) throws CmsException {
086
087        collectorName = collectorName == null ? COLLECTORS[1] : collectorName;
088        switch (COLLECTORS_LIST.indexOf(collectorName)) {
089            case 0: // byQuery
090            case 1: // byContext
091                Map<String, String> paramsAsMap = getParamsAsMap(param);
092                CmsSolrQuery q = new CmsSolrQuery(
093                    null,
094                    CmsRequestUtil.createParameterMap(
095                        paramsAsMap.get(SOLR_PART),
096                        Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(),
097                        cms.getRequestContext().getEncoding()));
098                String type = CmsSolrQuery.getResourceType(q.getFilterQueries());
099                String path = paramsAsMap.get(PARAM_CREATE_PATH);
100                if ((type != null) && (path != null)) {
101                    return OpenCms.getResourceManager().getNameGenerator().getNewFileName(cms, path, 4);
102                }
103                return null;
104            default:
105                throw new CmsDataAccessException(
106                    Messages.get().container(Messages.ERR_COLLECTOR_NAME_INVALID_1, collectorName));
107        }
108    }
109
110    /**
111     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateParam(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
112     */
113    public String getCreateParam(CmsObject cms, String collectorName, String param) throws CmsDataAccessException {
114
115        collectorName = collectorName == null ? COLLECTORS[1] : collectorName;
116        switch (COLLECTORS_LIST.indexOf(collectorName)) {
117            case 0: // byQuery
118            case 1: // byContext
119                // check if the param supports resource creation
120                Map<String, String> paramsAsMap = getParamsAsMap(param);
121                CmsSolrQuery q = new CmsSolrQuery(
122                    null,
123                    CmsRequestUtil.createParameterMap(
124                        paramsAsMap.get(SOLR_PART),
125                        Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(),
126                        cms.getRequestContext().getEncoding()));
127                String type = CmsSolrQuery.getResourceType(q.getFilterQueries());
128                String path = paramsAsMap.get(PARAM_CREATE_PATH);
129                if ((type != null) && (path != null)) {
130                    return param;
131                }
132                return null;
133            default:
134                throw new CmsDataAccessException(
135                    Messages.get().container(Messages.ERR_COLLECTOR_NAME_INVALID_1, collectorName));
136        }
137    }
138
139    /**
140     * @see org.opencms.file.collectors.A_CmsResourceCollector#getCreateTypeId(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
141     */
142    @Override
143    public int getCreateTypeId(CmsObject cms, String collectorName, String param) throws CmsException {
144
145        int result = -1;
146        Map<String, String> paramsAsMap = getParamsAsMap(param);
147        if (paramsAsMap.get(PARAM_CREATE_PATH) != null) {
148            String solrParams = paramsAsMap.get(SOLR_PART);
149            CmsSolrQuery q = new CmsSolrQuery(
150                null,
151                CmsRequestUtil.createParameterMap(
152                    solrParams,
153                    Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(),
154                    cms.getRequestContext().getEncoding()));
155            String type = CmsSolrQuery.getResourceType(q.getFilterQueries());
156            if (type != null) {
157                result = OpenCms.getResourceManager().getResourceType(type).getTypeId();
158            }
159        }
160        return result;
161    }
162
163    /**
164     * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
165     */
166    public List<CmsResource> getResults(CmsObject cms, String collectorName, String param)
167    throws CmsDataAccessException, CmsException {
168
169        return getResults(cms, collectorName, param, -1);
170    }
171
172    /**
173     * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
174     */
175    public List<CmsResource> getResults(CmsObject cms, String name, String param, int numResults) throws CmsException {
176
177        name = name == null ? COLLECTORS[1] : name;
178        Map<String, String> paramsAsMap = getParamsAsMap(param);
179        Map<String, String[]> pm = CmsRequestUtil.createParameterMap(
180            paramsAsMap.get(SOLR_PART),
181            Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(),
182            cms.getRequestContext().getEncoding());
183        CmsSolrQuery q = COLLECTORS_LIST.indexOf(name) == 0 ? new CmsSolrQuery(null, pm) : new CmsSolrQuery(cms, pm);
184        boolean excludeTimerange = Boolean.valueOf(
185            paramsAsMap.get(CmsCollectorData.PARAM_EXCLUDETIMERANGE)).booleanValue();
186        if (excludeTimerange) {
187            q.removeExpiration();
188        }
189        if (numResults > 0) {
190            q.setRows(Integer.valueOf(numResults));
191        }
192        // Do only return the minimal set of fields, since on can never access the solr result at all, but only the resource itself
193        q.setFields(CmsSolrQuery.MINIMUM_FIELDS);
194        CmsSolrIndex index = CmsSearchManager.getIndexSolr(cms, pm);
195        return new ArrayList<CmsResource>(index.search(cms, q, true));
196    }
197
198    /**
199     * Splits the given parameter String into the query part and the cms specific arguments.<p>
200     *
201     * @param param the parameter String to parse
202     *
203     * @return a map containing the arguments
204     */
205    private Map<String, String> getParamsAsMap(String param) {
206
207        Map<String, String> result = new HashMap<String, String>();
208        if (param != null) {
209            int in = (param.indexOf('|'));
210            if (in != -1) {
211                String solrPart = param.substring(0, in);
212                String cmsPart = param.substring(in);
213                result = CmsStringUtil.splitAsMap(cmsPart, "|", "=");
214                result.put(SOLR_PART, solrPart);
215
216            } else {
217                result.put(SOLR_PART, param);
218            }
219        }
220        return result;
221    }
222}