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.InputStream;
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.Map;
33
import java.util.Set;
34

    
35
import org.apache.commons.logging.Log;
36
import org.apache.commons.logging.LogFactory;
37
import org.apache.solr.client.solrj.response.QueryResponse;
38
import org.apache.solr.common.params.AppendedSolrParams;
39
import org.apache.solr.common.params.SolrParams;
40
import org.apache.solr.common.util.NamedList;
41
import org.apache.solr.schema.SchemaField;
42
import org.dataone.cn.indexer.solrhttp.SolrDoc;
43
import org.dataone.service.types.v1.Subject;
44
import org.dataone.service.util.Constants;
45

    
46
import edu.ucsb.nceas.metacat.common.SolrQueryResponseWriterFactory;
47

    
48

    
49
/**
50
 * A query interface for the solr server
51
 * @author tao
52
 *
53
 */
54
public abstract class SolrQueryService {
55
    
56
    public static final String WT = "wt";//the property name to specify the return type
57
    
58
    protected static final String FILTERQUERY = "fq";
59
    private static final String READPERMISSION = "readPermission";
60
    private static final String RIGHTSHOLDER = "rightsHolder";
61
    private static final String OPENPARENTHESE = "(";
62
    private static final String CLOSEPARENTHESE = ")";
63
    private static final String COLON = ":";
64
    private static final String OR = "OR";
65
    
66
    private static Log log = LogFactory.getLog(SolrQueryService.class);
67
    private static List<String> supportedWriterTypes = null;
68
    static {
69
        supportedWriterTypes = new ArrayList<String>();
70
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.CSV);
71
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.JSON);
72
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHP);
73
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHPS);
74
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.RUBY);
75
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.VELOCITY);
76
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PYTHON);
77
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.XML);
78
    }
79
    /**
80
     * Query the Solr server with specified query and user's identity. If the Subjects
81
     * is null, there will be no access rules for the query. This is the for the http solr server.
82
     * @param query the query string
83
     * @param subjects the user's identity which sent the query
84
     * @return the response
85
     * @throws Exception
86
     */
87
    public abstract InputStream query(String query, Set<Subject>subjects) throws Exception;
88
    
89
    /**
90
     * Query the Solr server with specified query and user's identity. If the Subjects
91
     * is null, there will be no access rules for the query. This is for the embedded solr server.
92
     * @param query the query params. 
93
     * @param subjects the user's identity which sent the query
94
     * @return the response
95
     * @throws Exception
96
     */
97
    public abstract InputStream query(SolrParams query, Set<Subject>subjects) throws Exception;
98
    
99
    
100
    /**
101
     * Get the list of SolrDocs for the specified ids from the solr server.
102
     *  * Note: each id only can have one SolrDoc or null SolrDoc. If it is null, it
103
     * wouldn't be added to the list.
104
     * @param ids the specified ids
105
     * @return the SolrDocs of the ids.
106
     * @throws Exception
107
     */
108
    public abstract List<SolrDoc> query(List<String> ids) throws Exception;
109
    
110
    
111
    /**
112
     * Get the fields list of the index schema
113
     * @return
114
     * @throws Exception
115
     */
116
    public abstract Map<String, SchemaField> getIndexSchemaFields() throws Exception;
117
    
118
    /**
119
     * Get the version of the solr server.
120
     * @return
121
     */
122
    public abstract String getSolrServerVersion();
123
    
124
    
125
    
126
    /**
127
     * If the solr server supports the specified wt.
128
     * @param wt
129
     * @return true if it supports; otherwise false.
130
     */
131
    public static boolean isSupportedWT(String wt) {
132
        if (wt == null ||supportedWriterTypes.contains(wt)) {
133
            return true;
134
        } else {
135
            return false;
136
        }
137
    }
138
    
139
    /*
140
     * Append the access filter query to the params
141
     */
142
    protected SolrParams appendAccessFilterParams(SolrParams solrParams, Set<Subject>subjects) {
143
        SolrParams append = null;
144
        if(solrParams != null) {
145
            StringBuffer query = generateAccessFilterParamsString(subjects);      
146
            if(query != null && query.length() != 0) {
147
                log.info("=================== fq query is "+query.toString());
148
                NamedList fq = new NamedList();
149
                fq.add(FILTERQUERY, query.toString());
150
                SolrParams fqParam = SolrParams.toSolrParams(fq);
151
                append = new AppendedSolrParams(solrParams, fqParam);
152
            } else {
153
                append = solrParams;
154
            }
155
        }
156
        return append;
157
    }
158
    
159
    protected StringBuffer generateAccessFilterParamsString(Set<Subject>subjects) {
160
        StringBuffer query = new StringBuffer();
161
        boolean first = true;
162
        if(subjects != null) {
163
            for(Subject subject : subjects) {
164
                if(subject != null) {
165
                    String subjectName = subject.getValue();
166
                    if(subjectName != null && !subjectName.trim().equals("")) {
167
                        if(first) {
168
                            first = false;
169
                            query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
170
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
171
                                query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
172
                            }
173
                        } else {
174
                            query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
175
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
176
                                query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
177
                            }
178
                        }
179
                    }
180
                   
181
                }
182
               
183
            }
184
        }
185
        return query;
186
    }
187
}
(3-3/4)