Project

General

Profile

1
/**
2
 *  Copyright: 2013 Regents of the University of California and the
3
 *             National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.common.query;
20

    
21
import java.io.InputStream;
22
import java.util.ArrayList;
23
import java.util.List;
24
import java.util.Map;
25
import java.util.Set;
26

    
27
import org.apache.commons.logging.Log;
28
import org.apache.commons.logging.LogFactory;
29
import org.apache.solr.common.params.AppendedSolrParams;
30
import org.apache.solr.common.params.SolrParams;
31
import org.apache.solr.common.util.NamedList;
32
import org.apache.solr.schema.IndexSchema;
33
import org.apache.solr.schema.SchemaField;
34
import org.dataone.service.types.v1.Subject;
35
import org.dataone.service.util.Constants;
36

    
37
import edu.ucsb.nceas.metacat.common.query.SolrQueryResponseWriterFactory;
38

    
39

    
40
/**
41
 * An abstract query service class for the solr server
42
 * @author tao
43
 *
44
 */
45
public abstract class SolrQueryService {
46
    
47
    public static final String WT = "wt";//the property name to specify the return type
48
    
49
    protected static final String FILTERQUERY = "fq";
50
    protected static final String UNKNOWN = "Unknown";
51
    private static final String READPERMISSION = "readPermission";
52
    private static final String RIGHTSHOLDER = "rightsHolder";
53
    private static final String OPENPARENTHESE = "(";
54
    private static final String CLOSEPARENTHESE = ")";
55
    private static final String COLON = ":";
56
    private static final String OR = "OR";
57
    
58
    private static Log log = LogFactory.getLog(SolrQueryService.class);
59
    private static List<String> supportedWriterTypes = null;
60
    
61
    protected IndexSchema schema = null;
62
    protected Map<String, SchemaField> fieldMap = null;
63
    protected List<String> validSolrFieldNames = null;
64
    protected String solrSpecVersion = null;
65
    
66
    static {
67
        supportedWriterTypes = new ArrayList<String>();
68
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.CSV);
69
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.JSON);
70
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHP);
71
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PHPS);
72
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.RUBY);
73
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.VELOCITY);
74
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.PYTHON);
75
        supportedWriterTypes.add(SolrQueryResponseWriterFactory.XML);
76
    }
77
  
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 for the embedded solr server.
82
     * @param query the query params. 
83
     * @param subjects the user's identity which sent the query
84
     * @return the response
85
     * @throws Exception
86
     */
87
    public abstract InputStream query(SolrParams query, Set<Subject>subjects) throws Exception;
88
    
89
    
90
  
91
    
92
    /**
93
     * Get the fields list of the index schema
94
     * @return
95
     * @throws Exception
96
     */
97
    public abstract Map<String, SchemaField> getIndexSchemaFields() throws Exception;
98
    
99
    /**
100
     * Get the version of the solr server.
101
     * @return
102
     */
103
    public abstract String getSolrServerVersion();
104
    
105
    /**
106
     * Get the list of the valid field name (moved the fields names of the CopyFieldTarget).
107
     * @return
108
     */
109
    protected List<String> getValidSchemaFields() {
110
        if (validSolrFieldNames != null && !validSolrFieldNames.isEmpty()) {
111
            //System.out.println("the valid file name is\n"+validSolrFieldNames);
112
            return validSolrFieldNames;
113
        } else {
114
            validSolrFieldNames = new ArrayList<String>();
115
            if(fieldMap != null) {
116
                Set<String> fieldNames = fieldMap.keySet();
117
                for(String fieldName : fieldNames) {
118
                    SchemaField field = fieldMap.get(fieldName);
119
                    //remove the field which is the target field of a CopyField.
120
                    if(field != null && !schema.isCopyFieldTarget(field)) {
121
                         validSolrFieldNames.add(fieldName);
122
                    }
123
                }
124
            }
125
            //System.out.println("the valid file name is\n"+validSolrFieldNames);
126
            return validSolrFieldNames;
127
        }
128
    }
129
    
130
    /**
131
     * If the solr server supports the specified wt.
132
     * @param wt
133
     * @return true if it supports; otherwise false.
134
     */
135
    public static boolean isSupportedWT(String wt) {
136
        if (wt == null ||supportedWriterTypes.contains(wt)) {
137
            return true;
138
        } else {
139
            return false;
140
        }
141
    }
142
    
143
    /*
144
     * Append the access filter query to the params
145
     */
146
    protected SolrParams appendAccessFilterParams(SolrParams solrParams, Set<Subject>subjects) {
147
        SolrParams append = null;
148
        if(solrParams != null) {
149
            StringBuffer query = generateAccessFilterParamsString(subjects);      
150
            if(query != null && query.length() != 0) {
151
                log.info("=================== fq query is "+query.toString());
152
                NamedList fq = new NamedList();
153
                fq.add(FILTERQUERY, query.toString());
154
                SolrParams fqParam = SolrParams.toSolrParams(fq);
155
                append = new AppendedSolrParams(solrParams, fqParam);
156
            } else {
157
                append = solrParams;
158
            }
159
        }
160
        return append;
161
    }
162
    
163
    protected StringBuffer generateAccessFilterParamsString(Set<Subject>subjects) {
164
        StringBuffer query = new StringBuffer();
165
        boolean first = true;
166
        if(subjects != null) {
167
            for(Subject subject : subjects) {
168
                if(subject != null) {
169
                    String subjectName = subject.getValue();
170
                    if(subjectName != null && !subjectName.trim().equals("")) {
171
                        if(first) {
172
                            first = false;
173
                            query.append(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
                        } else {
178
                            query.append(OR + OPENPARENTHESE+READPERMISSION+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
179
                            if(!subjectName.equals(Constants.SUBJECT_PUBLIC) && !subjectName.equals(Constants.SUBJECT_AUTHENTICATED_USER)) {
180
                                query.append(OR + OPENPARENTHESE+RIGHTSHOLDER+COLON+"\""+subjectName+"\""+CLOSEPARENTHESE);
181
                            }
182
                        }
183
                    }
184
                   
185
                }
186
               
187
            }
188
        }
189
        return query;
190
    }
191
}
(6-6/7)