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.IndexSchema;
41
import org.apache.solr.schema.SchemaField;
42
import org.dataone.service.types.v1.Subject;
43
import org.dataone.service.util.Constants;
44

    
45
import edu.ucsb.nceas.metacat.common.query.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
    protected static final String UNKNOWN = "Unknown";
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
    
69
    protected IndexSchema schema = null;
70
    protected Map<String, SchemaField> fieldMap = null;
71
    protected List<String> validSolrFieldNames = null;
72
    protected String solrSpecVersion = null;
73
    
74
    static {
75
        supportedWriterTypes = new ArrayList<String>();
76
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.CSV);
77
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.JSON);
78
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHP);
79
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHPS);
80
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.RUBY);
81
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.VELOCITY);
82
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PYTHON);
83
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.XML);
84
    }
85
    /**
86
     * Query the Solr server with specified query and user's identity. If the Subjects
87
     * is null, there will be no access rules for the query. This is the for the http solr server.
88
     * @param query the query string
89
     * @param subjects the user's identity which sent the query
90
     * @return the response
91
     * @throws Exception
92
     */
93
    //public abstract InputStream query(String query, Set<Subject>subjects) throws Exception;
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 for the embedded solr server.
98
     * @param query the query params. 
99
     * @param subjects the user's identity which sent the query
100
     * @return the response
101
     * @throws Exception
102
     */
103
    public abstract InputStream query(SolrParams query, Set<Subject>subjects) throws Exception;
104
    
105
    
106
  
107
    
108
    /**
109
     * Get the fields list of the index schema
110
     * @return
111
     * @throws Exception
112
     */
113
    public abstract Map<String, SchemaField> getIndexSchemaFields() throws Exception;
114
    
115
    /**
116
     * Get the version of the solr server.
117
     * @return
118
     */
119
    public abstract String getSolrServerVersion();
120
    
121
    /**
122
     * Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
123
     * @return
124
     */
125
    protected List<String> getValidSchemaFields() {
126
        if (validSolrFieldNames != null && !validSolrFieldNames.isEmpty()) {
127
            //System.out.println("the valid file name is\n"+validSolrFieldNames);
128
            return validSolrFieldNames;
129
        } else {
130
            validSolrFieldNames = new ArrayList<String>();
131
            if(fieldMap != null) {
132
                Set<String> fieldNames = fieldMap.keySet();
133
                for(String fieldName : fieldNames) {
134
                    SchemaField field = fieldMap.get(fieldName);
135
                    //remove the field which is the target field of a CopyField.
136
                    if(field != null && !schema.isCopyFieldTarget(field)) {
137
                         validSolrFieldNames.add(fieldName);
138
                    }
139
                }
140
            }
141
            //System.out.println("the valid file name is\n"+validSolrFieldNames);
142
            return validSolrFieldNames;
143
        }
144
    }
145
    
146
    /**
147
     * If the solr server supports the specified wt.
148
     * @param wt
149
     * @return true if it supports; otherwise false.
150
     */
151
    public static boolean isSupportedWT(String wt) {
152
        if (wt == null ||supportedWriterTypes.contains(wt)) {
153
            return true;
154
        } else {
155
            return false;
156
        }
157
    }
158
    
159
    /*
160
     * Append the access filter query to the params
161
     */
162
    protected SolrParams appendAccessFilterParams(SolrParams solrParams, Set<Subject>subjects) {
163
        SolrParams append = null;
164
        if(solrParams != null) {
165
            StringBuffer query = generateAccessFilterParamsString(subjects);      
166
            if(query != null && query.length() != 0) {
167
                log.info("=================== fq query is "+query.toString());
168
                NamedList fq = new NamedList();
169
                fq.add(FILTERQUERY, query.toString());
170
                SolrParams fqParam = SolrParams.toSolrParams(fq);
171
                append = new AppendedSolrParams(solrParams, fqParam);
172
            } else {
173
                append = solrParams;
174
            }
175
        }
176
        return append;
177
    }
178
    
179
    protected StringBuffer generateAccessFilterParamsString(Set<Subject>subjects) {
180
        StringBuffer query = new StringBuffer();
181
        boolean first = true;
182
        if(subjects != null) {
183
            for(Subject subject : subjects) {
184
                if(subject != null) {
185
                    String subjectName = subject.getValue();
186
                    if(subjectName != null && !subjectName.trim().equals("")) {
187
                        if(first) {
188
                            first = false;
189
                            query.append(OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
190
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
191
                                query.append(OR+OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
192
                            }
193
                        } else {
194
                            query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
195
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
196
                                query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
197
                            }
198
                        }
199
                    }
200
                   
201
                }
202
               
203
            }
204
        }
205
        return query;
206
    }
207
}
(6-6/7)