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.io.StringWriter;
32
import java.util.ArrayList;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Set;
36

    
37
import javax.xml.parsers.ParserConfigurationException;
38

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

    
54
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseTransformer;
55

    
56

    
57
/**
58
 *The query service of the embedded solr server.
59
 * @author tao
60
 *
61
 */
62
public class EmbeddedSolrQueryService extends SolrQueryService {
63
    private EmbeddedSolrServer solrServer = null;
64
    private CoreContainer coreContainer = null;
65
    private String collectionName = null;
66
    private SolrCore solrCore = null;
67
    
68
  
69
    /**
70
     * Constructor.
71
     * @param solrServer
72
     * @param solrCore
73
     * @throws NotFound 
74
     */
75
    public EmbeddedSolrQueryService(EmbeddedSolrServer solrServer, CoreContainer coreContainer, String collectionName) throws NotFound {
76
        if(solrServer == null) {
77
            throw new NullPointerException("EmbeddedSolrQueryService.constructor - the EmbeddedSolrServer parameter can't be null.");
78
        }
79
        if(coreContainer == null) {
80
            throw new NullPointerException("EmbeddedSolrQueryService.constructor - the CoreContainer parameter can't be null.");
81
        }
82
        if(collectionName == null || collectionName.trim().equals("")) {
83
            throw new NullPointerException("EmbeddedSolrQueryService.constructor - the name of Collection parameter can't be null or empty.");
84
        }
85
        this.solrServer = solrServer;
86
        this.coreContainer = coreContainer;
87
        this.collectionName = collectionName;
88
        this.solrCore = this.coreContainer.getCore(collectionName);
89
        if(solrCore == null) {
90
            throw new NotFound("0000","EmbeddedSolrQueryService.constructor - There is no SolrCore named "+collectionName+".");
91
        }
92
        schema = solrCore.getSchema();
93
        fieldMap = schema.getFields();
94
    }
95
    /**
96
     * Query the Solr server with specified query and user's identity. If the Subjects
97
     * is null, there will be no access rules for the query. This is the for the http solr server.
98
     * @param query the query string
99
     * @param subjects the user's identity which sent the query
100
     * @return the response
101
     * @throws Exception
102
     */
103
    /*public InputStream query(String query, Set<Subject>subjects) throws Exception {
104
        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");
105
    }*/
106
    
107
    /**
108
     * Query the Solr server with specified query and user's identity. If the Subjects
109
     * is null, there will be no access rules for the query. This is for the embedded solr server.
110
     * @param query the query params. 
111
     * @param subjects the user's identity which sent the query
112
     * @return the response
113
     * @throws SAXException 
114
     * @throws IOException 
115
     * @throws ParserConfigurationException 
116
     * @throws SolrServerException 
117
     * @throws UnsupportedType 
118
     * @throws Exception
119
     */
120
    public  InputStream query(SolrParams query, Set<Subject>subjects) throws ParserConfigurationException, IOException, SAXException, SolrServerException, UnsupportedType {
121
        InputStream inputStream = null;
122
        String wt = query.get(WT);
123
        query = appendAccessFilterParams(query, subjects);
124
        SolrQueryResponseTransformer solrTransformer = new SolrQueryResponseTransformer(solrCore);
125
        // handle normal and skin-based queries
126
        if (isSupportedWT(wt)) {
127
            // just handle as normal solr query
128
            //reload the core before query. Only after reloading the core, the query result can reflect the change made in metacat-index module.
129
            coreContainer.reload(collectionName);
130
            QueryResponse response = solrServer.query(query);
131
            inputStream = solrTransformer.transformResults(query, response, wt);
132
        } else {
133
            throw new UnsupportedType("0000","EmbeddSolrQueryService.query - the wt type "+wt+" in the solr query is not supported");
134
        }
135
        return inputStream;
136
    }
137
    
138
    
139
    
140
    
141
    /**
142
     * Get the fields map of the index schema
143
     * @return the fields map (the field name is the key and the SchemaField is the value).
144
     */
145
    public  Map<String, SchemaField> getIndexSchemaFields() {
146
        return fieldMap;
147
    }
148
    
149
    /**
150
     * Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
151
     * @return
152
     */
153
    public List<String> getValidSchemaField() {
154
        return super.getValidSchemaFields();
155
    }
156
    
157
    /**
158
     * Get the version of the solr server.
159
     * @return
160
     */
161
    public String getSolrServerVersion() {
162
        if( solrSpecVersion !=null ) {
163
            return solrSpecVersion;
164
        } else {
165
            Package p = SolrCore.class.getPackage();
166
            StringWriter tmp = new StringWriter();
167
            solrSpecVersion = p.getSpecificationVersion();
168
            if (null != solrSpecVersion) {
169
                try {
170
                    XML.escapeCharData(solrSpecVersion, tmp);
171
                    solrSpecVersion = tmp.toString();
172
                } catch (IOException e) {
173
                    e.printStackTrace();
174
                }
175
            }
176
            if (solrSpecVersion == null || solrSpecVersion.trim().equals("")) {
177
                solrSpecVersion = UNKNOWN;
178
            } 
179
            return solrSpecVersion;
180
        }
181
    }
182
}
(1-1/7)