Project

General

Profile

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

    
25
import java.io.File;
26
import java.io.FileInputStream;
27
import java.io.IOException;
28
import java.io.StringWriter;
29
import java.util.Collections;
30
import java.util.Comparator;
31
import java.util.HashMap;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.Properties;
35
import java.util.Set;
36

    
37
import javax.xml.parsers.ParserConfigurationException;
38

    
39

    
40

    
41
import org.apache.commons.io.FileUtils;
42
import org.apache.commons.logging.Log;
43
import org.apache.commons.logging.LogFactory;
44
import org.apache.solr.common.util.XML;
45
import org.apache.solr.core.CoreContainer;
46
import org.apache.solr.core.SolrCore;
47
import org.apache.solr.schema.IndexSchema;
48
import org.apache.solr.schema.SchemaField;
49
import org.dataone.service.exceptions.NotFound;
50
import org.dataone.service.exceptions.UnsupportedType;
51
import org.dataone.service.types.v1_1.QueryEngineDescription;
52
import org.dataone.service.types.v1_1.QueryField;
53
import org.xml.sax.SAXException;
54

    
55
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
56
import edu.ucsb.nceas.metacat.common.query.SolrQueryServiceController;
57
import edu.ucsb.nceas.metacat.properties.PropertyService;
58
import edu.ucsb.nceas.utilities.FileUtil;
59

    
60

    
61
/**
62
 * This class handles the request for getting the solr engine description.
63
 * @author tao
64
 *
65
 */
66
public class MetacatSolrEngineDescriptionHandler {
67
    private static final String UNKNOWN = "Unknown";
68
    private static final String DESCRIPTIONFILENAME= "solrQueryFieldDescriptions.properties";
69
    private static final String HTTPSOLRSERVERSCHEMAURLPATH="/admin/file/?contentType=text/xml;charset=utf-8&file=schema.xml";
70
    
71
    private static MetacatSolrEngineDescriptionHandler handler = null;
72
    private static Log logger = LogFactory.getLog(MetacatSolrEngineDescriptionHandler.class);
73
    private QueryEngineDescription qed = null;
74
    
75
    /**
76
     * Get an instance of the class.
77
     * @return
78
     */
79
    public static MetacatSolrEngineDescriptionHandler getInstance() throws Exception {
80
        if(handler == null) {
81
            handler = new MetacatSolrEngineDescriptionHandler();
82
        }
83
        return handler;
84
    }
85
    
86
    /**
87
     * Get the QueryEngineDescription
88
     * @return
89
     */
90
    public QueryEngineDescription getQueryEngineDescritpion() {
91
        return qed;
92
    }
93
    
94
    /*
95
     * Constructor
96
     */
97
    private MetacatSolrEngineDescriptionHandler() throws Exception {
98
       /*CoreContainer container = SolrServerFactory.getCoreContainer();
99
       if(container == null) {
100
           throw new Exception("MetacatSolrEngineDescriptionHandler - The Solr Server is not configured as an EmbeddedSolrServer and the EmbeddedSolrServer is the only SolrServer that the Metacat can provide the Query Engine Description.");
101
       }
102
       String coreName = SolrServerFactory.getCollectionName();
103
       if(container == null) {
104
           throw new Exception("MetacatSolrEngineDescriptionHandler - The collection name should not be null. Please check the value of the property \"solr.collectionName\" in the metacat.properties file.");
105
       }
106
       SolrCore core = container.getCore(coreName);
107
       if(core == null) {
108
           throw new Exception("MetacatSolrEngineDescriptionHandler - Metacat can't find the SolrCore for the given name - "+SolrServerFactory.getCollectionName());
109
       }*/
110
       init();
111
    }
112
    
113
   
114
    
115
    private void init() throws IOException, UnsupportedType, NotFound, ParserConfigurationException, SAXException {
116
        Map<String, String>fieldDescriptions = loadSchemaFieldDescriptions();
117
        qed = new QueryEngineDescription();
118
        qed.setName(MetacatSolrIndex.SOLRQUERY);
119
        //setSchemaVersionFromPropertiesFile(qed);
120
        setSolrVersion();
121

    
122
        //IndexSchema schema = core.getSchema();
123
        Map<String, SchemaField> fieldMap = SolrQueryServiceController.getInstance().getIndexSchemaFields();
124
        for (SchemaField schemaField : fieldMap.values()) {
125
            qed.addQueryField(createQueryFieldFromSchemaField(schemaField, fieldDescriptions));
126
        }
127
        Collections.sort(qed.getQueryFieldList(), new QueryFieldAlphaComparator());
128
    }
129
    
130
    /**
131
     * Based on org.apache.solr.handler.admin.SystemInfoHandler.getLuceneInfo()
132
     * @throws SAXException 
133
     * @throws IOException 
134
     * @throws ParserConfigurationException 
135
     * @throws NotFound 
136
     * @throws UnsupportedType 
137
     */
138
    private void setSolrVersion() throws UnsupportedType, NotFound, ParserConfigurationException, IOException, SAXException {
139
        qed.setQueryEngineVersion(SolrQueryServiceController.getInstance().getSolrSpecVersion());
140
    }
141
    
142

    
143
   
144
    
145
    private Map<String, String> loadSchemaFieldDescriptions() throws IOException {
146
        Map<String, String>fieldDescriptions = new HashMap<String, String>();
147
        Properties descriptionProperties = new Properties();
148
        String propertyFilePath = PropertyService.CONFIG_FILE_DIR + FileUtil.getFS() + DESCRIPTIONFILENAME;
149
        //System.out.println("the input strema is ================= "+propertyFilePath);
150
        descriptionProperties.load(new FileInputStream(new File(propertyFilePath)));
151
        Set<Object> names = descriptionProperties.keySet();
152
        for(Object nameObj : names) {
153
            String name = (String) nameObj;
154
            String description = (String) descriptionProperties.get(name);
155
            fieldDescriptions.put(name, description);
156
        }
157
        return fieldDescriptions;
158
         
159
    }
160
    
161
    private QueryField createQueryFieldFromSchemaField(SchemaField field,
162
                    Map<String, String> fieldDescriptions) {
163
                QueryField queryField = new QueryField();
164
                queryField.setName(field.getName());
165
                queryField.setType(field.getType().getTypeName());
166
                String description = fieldDescriptions.get(field.getName());
167
                if ( description != null && !description.trim().equals("")) {
168
                    queryField.addDescription(description);
169
                }
170
                queryField.setSearchable(field.indexed());
171
                queryField.setReturnable(field.stored());
172
                queryField.setMultivalued(field.multiValued());
173
                queryField.setSortable(isSortable(field));
174
                return queryField;
175
            }
176

    
177
    private boolean isSortable(SchemaField field) {
178
                String type = field.getType().getTypeName();
179
                if ("int".equals(type) || "long".equals(type) || "float".equals(type)
180
                        || "double".equals(type)) {
181
                    return false;
182
                } else {
183
                    return true;
184
                }
185
    }
186
    
187
    private class QueryFieldAlphaComparator implements Comparator<QueryField> {
188
        public int compare(QueryField arg0, QueryField arg1) {
189
            String field1Name = arg0.getName();
190
            String field2Name = arg1.getName();
191
            return field1Name.compareToIgnoreCase(field2Name);
192
        }
193
    }
194
}
(1-1/2)