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.file.CmsResourceFilter;
038import org.opencms.main.CmsException;
039import org.opencms.main.OpenCms;
040import org.opencms.search.CmsSearchManager;
041import org.opencms.search.solr.CmsSolrIndex;
042import org.opencms.search.solr.CmsSolrQuery;
043import org.opencms.util.CmsRequestUtil;
044import org.opencms.util.CmsStringUtil;
045
046import java.util.ArrayList;
047import java.util.Arrays;
048import java.util.Collections;
049import java.util.HashMap;
050import java.util.List;
051import java.util.Map;
052
053/**
054 * A Solr collector.<p>
055 *
056 * @since 8.5.0
057 */
058public class CmsSolrCollector extends A_CmsResourceCollector {
059
060    /** Constant array of the collectors implemented by this class. */
061    private static final String[] COLLECTORS = {"byQuery", "byContext"};
062
063    /** Array list for fast collector name lookup. */
064    private static final List<String> COLLECTORS_LIST = Collections.unmodifiableList(Arrays.asList(COLLECTORS));
065
066    /** The folder path to create the "create link" for. */
067    private static final String PARAM_CREATE_PATH = "createPath";
068
069    /** Option, specifying if the Solr Query is URL-encoded or not */
070    private static final String PARAM_DECODE_URL = "decodeUrl";
071
072    /** A constant for a key. */
073    private static final String SOLR_PART = "solrPart";
074
075    /**
076     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCollectorNames()
077     */
078    public List<String> getCollectorNames() {
079
080        return COLLECTORS_LIST;
081    }
082
083    /**
084     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateLink(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
085     */
086    public String getCreateLink(CmsObject cms, String collectorName, String param) throws CmsException {
087
088        collectorName = collectorName == null ? COLLECTORS[1] : collectorName;
089        switch (COLLECTORS_LIST.indexOf(collectorName)) {
090            case 0: // byQuery
091            case 1: // byContext
092                Map<String, String> paramsAsMap = getParamsAsMap(param);
093                CmsSolrQuery q = new CmsSolrQuery(
094                    null,
095                    CmsRequestUtil.createParameterMap(
096                        paramsAsMap.get(SOLR_PART),
097                        Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(),
098                        cms.getRequestContext().getEncoding()));
099                String type = CmsSolrQuery.getResourceType(q.getFilterQueries());
100                String path = paramsAsMap.get(PARAM_CREATE_PATH);
101                if ((type != null) && (path != null)) {
102                    return OpenCms.getResourceManager().getNameGenerator().getNewFileName(cms, path, 4);
103                }
104                return null;
105            default:
106                throw new CmsDataAccessException(
107                    Messages.get().container(Messages.ERR_COLLECTOR_NAME_INVALID_1, collectorName));
108        }
109    }
110
111    /**
112     * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateParam(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
113     */
114    public String getCreateParam(CmsObject cms, String collectorName, String param) throws CmsDataAccessException {
115
116        collectorName = collectorName == null ? COLLECTORS[1] : collectorName;
117        switch (COLLECTORS_LIST.indexOf(collectorName)) {
118            case 0: // byQuery
119            case 1: // byContext
120                // check if the param supports resource creation
121                Map<String, String> paramsAsMap = getParamsAsMap(param);
122                CmsSolrQuery q = new CmsSolrQuery(
123                    null,
124                    CmsRequestUtil.createParameterMap(
125                        paramsAsMap.get(SOLR_PART),
126                        Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(),
127                        cms.getRequestContext().getEncoding()));
128                String type = CmsSolrQuery.getResourceType(q.getFilterQueries());
129                String path = paramsAsMap.get(PARAM_CREATE_PATH);
130                if ((type != null) && (path != null)) {
131                    return param;
132                }
133                return null;
134            default:
135                throw new CmsDataAccessException(
136                    Messages.get().container(Messages.ERR_COLLECTOR_NAME_INVALID_1, collectorName));
137        }
138    }
139
140    /**
141     * @see org.opencms.file.collectors.A_CmsResourceCollector#getCreateTypeId(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
142     */
143    @Override
144    public int getCreateTypeId(CmsObject cms, String collectorName, String param) throws CmsException {
145
146        int result = -1;
147        Map<String, String> paramsAsMap = getParamsAsMap(param);
148        if (paramsAsMap.get(PARAM_CREATE_PATH) != null) {
149            String solrParams = paramsAsMap.get(SOLR_PART);
150            CmsSolrQuery q = new CmsSolrQuery(
151                null,
152                CmsRequestUtil.createParameterMap(
153                    solrParams,
154                    Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(),
155                    cms.getRequestContext().getEncoding()));
156            String type = CmsSolrQuery.getResourceType(q.getFilterQueries());
157            if (type != null) {
158                result = OpenCms.getResourceManager().getResourceType(type).getTypeId();
159            }
160        }
161        return result;
162    }
163
164    /**
165     * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
166     */
167    public List<CmsResource> getResults(CmsObject cms, String collectorName, String param)
168    throws CmsDataAccessException, CmsException {
169
170        return getResults(cms, collectorName, param, -1);
171    }
172
173    /**
174     * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
175     */
176    public List<CmsResource> getResults(CmsObject cms, String name, String param, int numResults) throws CmsException {
177
178        name = name == null ? COLLECTORS[1] : name;
179        Map<String, String> paramsAsMap = getParamsAsMap(param);
180        Map<String, String[]> pm = CmsRequestUtil.createParameterMap(
181            paramsAsMap.get(SOLR_PART),
182            Boolean.valueOf(paramsAsMap.get(PARAM_DECODE_URL)).booleanValue(),
183            cms.getRequestContext().getEncoding());
184        CmsSolrQuery q = COLLECTORS_LIST.indexOf(name) == 0 ? new CmsSolrQuery(null, pm) : new CmsSolrQuery(cms, pm);
185        boolean excludeTimerange = !cms.getRequestContext().getCurrentProject().isOnlineProject()
186            && Boolean.valueOf(paramsAsMap.get(CmsCollectorData.PARAM_EXCLUDETIMERANGE)).booleanValue();
187        if (excludeTimerange) {
188            q.removeExpiration();
189        }
190        if (numResults > 0) {
191            q.setRows(Integer.valueOf(numResults));
192        }
193        // Do only return the minimal set of fields, since on can never access the solr result at all, but only the resource itself
194        q.setFields(CmsSolrQuery.MINIMUM_FIELDS);
195        CmsSolrIndex index = CmsSearchManager.getIndexSolr(cms, pm);
196        return new ArrayList<CmsResource>(
197            index.search(cms, q, true, excludeTimerange ? CmsResourceFilter.IGNORE_EXPIRATION : null));
198    }
199
200    /**
201     * Splits the given parameter String into the query part and the cms specific arguments.<p>
202     *
203     * @param param the parameter String to parse
204     *
205     * @return a map containing the arguments
206     */
207    private Map<String, String> getParamsAsMap(String param) {
208
209        Map<String, String> result = new HashMap<String, String>();
210        if (param != null) {
211            int in = (param.indexOf('|'));
212            if (in != -1) {
213                String solrPart = param.substring(0, in);
214                String cmsPart = param.substring(in);
215                result = CmsStringUtil.splitAsMap(cmsPart, "|", "=");
216                result.put(SOLR_PART, solrPart);
217
218            } else {
219                result.put(SOLR_PART, param);
220            }
221        }
222        return result;
223    }
224}