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.service.types.v1.Subject;
42
import org.dataone.service.util.Constants;
43

    
44
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
45

    
46

    
47
/**
48
 * A query interface for the solr server
49
 * @author tao
50
 *
51
 */
52
public abstract class SolrQueryService {
53
    
54
    public static final String WT = "wt";//the property name to specify the return type
55
    
56
    protected static final String FILTERQUERY = "fq";
57
    protected static final String UNKNOWN = "Unknown";
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
    
68
    protected Map<String, SchemaField> fieldMap = null;
69
    protected List<String> validSolrFieldNames = null;
70
    protected String solrSpecVersion = null;
71
    
72
    static {
73
        supportedWriterTypes = new ArrayList<String>();
74
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.CSV);
75
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.JSON);
76
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHP);
77
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHPS);
78
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.RUBY);
79
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.VELOCITY);
80
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PYTHON);
81
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.XML);
82
    }
83
    /**
84
     * Query the Solr server with specified query and user's identity. If the Subjects
85
     * is null, there will be no access rules for the query. This is the for the http solr server.
86
     * @param query the query string
87
     * @param subjects the user's identity which sent the query
88
     * @return the response
89
     * @throws Exception
90
     */
91
    //public abstract InputStream query(String query, Set<Subject>subjects) throws Exception;
92
    
93
    /**
94
     * Query the Solr server with specified query and user's identity. If the Subjects
95
     * is null, there will be no access rules for the query. This is for the embedded solr server.
96
     * @param query the query params. 
97
     * @param subjects the user's identity which sent the query
98
     * @return the response
99
     * @throws Exception
100
     */
101
    public abstract InputStream query(SolrParams query, Set<Subject>subjects) throws Exception;
102
    
103
    
104
  
105
    
106
    /**
107
     * Get the fields list of the index schema
108
     * @return
109
     * @throws Exception
110
     */
111
    public abstract Map<String, SchemaField> getIndexSchemaFields() throws Exception;
112
    
113
    /**
114
     * Get the version of the solr server.
115
     * @return
116
     */
117
    public abstract String getSolrServerVersion();
118
    
119
    /**
120
     * Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
121
     * @return
122
     */
123
    public abstract List<String> getValidSchemaField();
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
}
(5-5/6)