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.common.params.AppendedSolrParams;
38
import org.apache.solr.common.params.SolrParams;
39
import org.apache.solr.common.util.NamedList;
40
import org.apache.solr.schema.SchemaField;
41
import org.dataone.cn.indexer.solrhttp.SolrDoc;
42
import org.dataone.service.types.v1.Subject;
43
import org.dataone.service.util.Constants;
44

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

    
47

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