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.net.MalformedURLException;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.Set;
35

    
36
import javax.xml.parsers.ParserConfigurationException;
37

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

    
51
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
52

    
53
/**
54
 * A class to handle the solr query. It will call the SolrServerFactory to generate a SolrServer and
55
 * determine the type - the embedded or http solr server.
56
 * @author tao
57
 *
58
 */
59
public class SolrQueryServiceController {
60
    private static SolrQueryServiceController controller = null;
61
    private boolean isEmbeddedSolrServer = false;
62
    private EmbeddedSolrQueryService embeddedQueryService = null;
63
    private HttpSolrQueryService httpQueryService = null;
64
    
65
    /*
66
     * Private consctructor
67
     */
68
    private SolrQueryServiceController() throws UnsupportedType, ParserConfigurationException, IOException, SAXException, NotFound {
69
        SolrServer solrServer = SolrServerFactory.createSolrServer();
70
        if(solrServer instanceof EmbeddedSolrServer) {
71
            isEmbeddedSolrServer = true;
72
            EmbeddedSolrServer embeddedServer = (EmbeddedSolrServer) solrServer;
73
            CoreContainer coreContainer = SolrServerFactory.getCoreContainer();
74
            String collectionName = SolrServerFactory.getCollectionName();
75
            embeddedQueryService = new EmbeddedSolrQueryService(embeddedServer, coreContainer, collectionName);
76
        } else {
77
            isEmbeddedSolrServer = false;
78
            CommonsHttpSolrServer httpServer = (CommonsHttpSolrServer)solrServer;
79
            httpQueryService = new HttpSolrQueryService(httpServer);
80
        }
81
    }
82
    
83
    /**
84
     * Get the controller
85
     * @return
86
     * @throws UnsupportedType
87
     * @throws NotFound
88
     * @throws ParserConfigurationException
89
     * @throws IOException
90
     * @throws SAXException
91
     */
92
    public static SolrQueryServiceController getInstance() throws UnsupportedType, NotFound, ParserConfigurationException, IOException, SAXException {
93
        if(controller == null) {
94
            controller = new SolrQueryServiceController();
95
        }
96
        return controller;
97
    }
98
    
99
   
100
    
101
    /**
102
     * Query the Solr server with specified query string or SolrParams and the user's identity. 
103
     * @param query  query string. It is for the HttpSolrQueryService.
104
     * @param params the SolrParam. It is for the EmbeddedSolrQueryService.
105
     * @param subjects
106
     * @return
107
     * @throws NotImplemented
108
     * @throws NotFound
109
     * @throws IOException
110
     * @throws SolrServerException 
111
     * @throws SAXException 
112
     * @throws ParserConfigurationException 
113
     * @throws UnsupportedType 
114
     */
115
    public InputStream query(SolrParams params,Set<Subject>subjects) throws NotImplemented, NotFound, IOException, UnsupportedType, ParserConfigurationException, SAXException, SolrServerException  {
116
        if(isEmbeddedSolrServer) {
117
            return embeddedQueryService.query(params, subjects);
118
        } else {
119
            return httpQueryService.query(params, subjects);
120
        }
121
      
122
    }
123
    
124
    /**
125
     * Get the spec version of the solr server
126
     * @return
127
     */
128
    public String getSolrSpecVersion() {
129
        if(isEmbeddedSolrServer) {
130
            return embeddedQueryService.getSolrServerVersion();
131
        } else {
132
            return httpQueryService.getSolrServerVersion();
133
        }
134
    }
135
    
136
    /**
137
     * Get the fields list of the index schema
138
     * @return
139
     * @throws SAXException 
140
     * @throws IOException 
141
     * @throws ParserConfigurationException 
142
     * @throws MalformedURLException 
143
     * @throws Exception
144
     */
145
    public  Map<String, SchemaField> getIndexSchemaFields() throws MalformedURLException, ParserConfigurationException, IOException, SAXException  {
146
        if(isEmbeddedSolrServer) {
147
            return embeddedQueryService.getIndexSchemaFields();
148
        } else {
149
            return httpQueryService.getIndexSchemaFields();
150
        }
151
    }
152
    
153
   
154
    
155
    /**
156
     * Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
157
     * @return
158
     * @throws SAXException 
159
     * @throws IOException 
160
     * @throws ParserConfigurationException 
161
     * @throws MalformedURLException 
162
     */
163
    public List<String> getValidSchemaFields() throws MalformedURLException, ParserConfigurationException, IOException, SAXException {
164
        if(isEmbeddedSolrServer) {
165
            return embeddedQueryService.getValidSchemaFields();
166
        } else{
167
            return httpQueryService.getValidSchemaField();
168
        }
169
       
170
    }
171

    
172
    
173
}
(7-7/7)