Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class that gets Accession Number, check for uniqueness
4
 *             and register it into db
5
 *  Copyright: 2000 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Jivka Bojilova, Matt Jones
8
 *
9
 *   '$Author: tao $'
10
 *     '$Date: 2013-04-19 17:47:14 -0700 (Fri, 19 Apr 2013) $'
11
 * '$Revision: 7595 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27
package edu.ucsb.nceas.metacat.common.query;
28

    
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.Set;
34

    
35
import javax.xml.parsers.ParserConfigurationException;
36

    
37
import org.apache.solr.client.solrj.SolrServerException;
38
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
39
import org.apache.solr.client.solrj.response.QueryResponse;
40
import org.apache.solr.common.params.SolrParams;
41
import org.apache.solr.core.CoreContainer;
42
import org.apache.solr.core.SolrCore;
43
import org.apache.solr.schema.SchemaField;
44
import org.dataone.service.exceptions.NotFound;
45
import org.dataone.service.exceptions.NotImplemented;
46
import org.dataone.service.exceptions.UnsupportedType;
47
import org.dataone.service.types.v1.Subject;
48
import org.xml.sax.SAXException;
49

    
50
import edu.ucsb.nceas.metacat.common.SolrQueryResponseTransformer;
51

    
52

    
53
/**
54
 *The query service of the embedded solr server.
55
 * @author tao
56
 *
57
 */
58
public class EmbeddedSolrQueryService extends SolrQueryService {
59
    private EmbeddedSolrServer solrServer = null;
60
    private CoreContainer coreContainer = null;
61
    private String collectionName = null;
62
    private SolrCore solrCore = null;
63
    /**
64
     * Constructor.
65
     * @param solrServer
66
     * @param solrCore
67
     * @throws NotFound 
68
     */
69
    public EmbeddedSolrQueryService(EmbeddedSolrServer solrServer, CoreContainer coreContainer, String collectionName) throws NotFound {
70
        if(solrServer == null) {
71
            throw new NullPointerException("EmbeddedSolrQueryService.constructor - the EmbeddedSolrServer parameter can't be null.");
72
        }
73
        if(coreContainer == null) {
74
            throw new NullPointerException("EmbeddedSolrQueryService.constructor - the CoreContainer parameter can't be null.");
75
        }
76
        if(collectionName == null || collectionName.trim().equals("")) {
77
            throw new NullPointerException("EmbeddedSolrQueryService.constructor - the name of Collection parameter can't be null or empty.");
78
        }
79
        this.solrServer = solrServer;
80
        this.coreContainer = coreContainer;
81
        this.collectionName = collectionName;
82
        this.solrCore = this.coreContainer.getCore(collectionName);
83
        if(solrCore == null) {
84
            throw new NotFound("0000","EmbeddedSolrQueryService.constructor - There is no SolrCore named "+collectionName+".");
85
        }
86
    }
87
    /**
88
     * Query the Solr server with specified query and user's identity. If the Subjects
89
     * is null, there will be no access rules for the query. This is the for the http solr server.
90
     * @param query the query string
91
     * @param subjects the user's identity which sent the query
92
     * @return the response
93
     * @throws Exception
94
     */
95
    /*public InputStream query(String query, Set<Subject>subjects) throws Exception {
96
        throw new NotImplemented("0000", "EmbeddSolrQueryService - the method of  query(String query, Set<Subject>subjects) is not for the EmbeddedSolrServer. We donot need to implemente it");
97
    }*/
98
    
99
    /**
100
     * Query the Solr server with specified query and user's identity. If the Subjects
101
     * is null, there will be no access rules for the query. This is for the embedded solr server.
102
     * @param query the query params. 
103
     * @param subjects the user's identity which sent the query
104
     * @return the response
105
     * @throws SAXException 
106
     * @throws IOException 
107
     * @throws ParserConfigurationException 
108
     * @throws SolrServerException 
109
     * @throws UnsupportedType 
110
     * @throws Exception
111
     */
112
    public  InputStream query(SolrParams query, Set<Subject>subjects) throws ParserConfigurationException, IOException, SAXException, SolrServerException, UnsupportedType {
113
        InputStream inputStream = null;
114
        String wt = query.get(WT);
115
        query = appendAccessFilterParams(query, subjects);
116
        SolrQueryResponseTransformer solrTransformer = new SolrQueryResponseTransformer(solrCore);
117
        // handle normal and skin-based queries
118
        if (isSupportedWT(wt)) {
119
            // just handle as normal solr query
120
            //reload the core before query. Only after reloading the core, the query result can reflect the change made in metacat-index module.
121
            coreContainer.reload(collectionName);
122
            QueryResponse response = solrServer.query(query);
123
            inputStream = solrTransformer.transformResults(query, response, wt);
124
        } else {
125
            throw new UnsupportedType("0000","EmbeddSolrQueryService.query - the wt type "+wt+" in the solr query is not supported");
126
        }
127
        return inputStream;
128
    }
129
    
130
    
131
    
132
    
133
    /**
134
     * Get the fields list of the index schema
135
     * @return
136
     * @throws Exception
137
     */
138
    public  Map<String, SchemaField> getIndexSchemaFields() throws Exception {
139
        return null;
140
    }
141
    
142
    /**
143
     * Get the version of the solr server.
144
     * @return
145
     */
146
    public String getSolrServerVersion() {
147
        return null;
148
    }
149
}
(1-1/4)