Project

General

Profile

« Previous | Next » 

Revision 7662

Added by Jing Tao over 11 years ago

Use a new class to handle the solr query engine description request.

View differences:

src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
105 105
import edu.ucsb.nceas.metacat.MetaCatServlet;
106 106
import edu.ucsb.nceas.metacat.MetacatHandler;
107 107
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
108
import edu.ucsb.nceas.metacat.index.MetacatSolrEngineDescriptionHandler;
108 109
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
109 110
import edu.ucsb.nceas.metacat.properties.PropertyService;
110 111
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
......
1432 1433
	        return qed;
1433 1434
	    } else if (engine != null && engine.equals(MetacatSolrIndex.SOLRQUERY)) {
1434 1435
	        try {
1435
	            QueryEngineDescription qed = MetacatSolrIndex.getInstance().getQueryEngineDescription();
1436
	            QueryEngineDescription qed = MetacatSolrEngineDescriptionHandler.getInstance().getQueryEngineDescritpion();
1436 1437
	            return qed;
1437 1438
	        } catch (Exception e) {
1438 1439
	            e.printStackTrace();
src/edu/ucsb/nceas/metacat/index/MetacatSolrEngineDescriptionHandler.java
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

  
38

  
39
import org.apache.commons.io.FileUtils;
40
import org.apache.commons.logging.Log;
41
import org.apache.commons.logging.LogFactory;
42
import org.apache.solr.common.util.XML;
43
import org.apache.solr.core.CoreContainer;
44
import org.apache.solr.core.SolrCore;
45
import org.apache.solr.schema.IndexSchema;
46
import org.apache.solr.schema.SchemaField;
47
import org.dataone.service.types.v1_1.QueryEngineDescription;
48
import org.dataone.service.types.v1_1.QueryField;
49

  
50
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
51
import edu.ucsb.nceas.metacat.properties.PropertyService;
52
import edu.ucsb.nceas.utilities.FileUtil;
53

  
54

  
55
/**
56
 * This class handles the request for getting the solr engine description.
57
 * @author tao
58
 *
59
 */
60
public class MetacatSolrEngineDescriptionHandler {
61
    private static final String UNKNOWN = "Unknown";
62
    private static final String DESCRIPTIONFILENAME= "solrQueryFieldDescriptions.properties";
63
    
64
    private static MetacatSolrEngineDescriptionHandler handler = null;
65
    private static Log logger = LogFactory.getLog(MetacatSolrEngineDescriptionHandler.class);
66
    private QueryEngineDescription qed = null;
67
    
68
    /**
69
     * Get an instance of the class.
70
     * @return
71
     */
72
    public static MetacatSolrEngineDescriptionHandler getInstance() throws Exception {
73
        if(handler == null) {
74
            handler = new MetacatSolrEngineDescriptionHandler();
75
        }
76
        return handler;
77
    }
78
    
79
    /**
80
     * Get the QueryEngineDescription
81
     * @return
82
     */
83
    public QueryEngineDescription getQueryEngineDescritpion() {
84
        return qed;
85
    }
86
    
87
    /*
88
     * Constructor
89
     */
90
    private MetacatSolrEngineDescriptionHandler() throws Exception {
91
       CoreContainer container = SolrServerFactory.getCoreContainer();
92
       if(container == null) {
93
           throw new Exception("MetacatSolrEngineDescriptionHandler - The Solr Server is not configured as an EmbeddedSolrServer since Metacat can't find the CoreContainer");
94
       }
95
       String coreName = SolrServerFactory.getCollectionName();
96
       if(container == null) {
97
           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.");
98
       }
99
       SolrCore core = container.getCore(coreName);
100
       if(core == null) {
101
           throw new Exception("MetacatSolrEngineDescriptionHandler - Metacat can't find the SolrCore for the given name - "+SolrServerFactory.getCollectionName());
102
       }
103
       init(core);
104
    }
105
    
106
   
107
    
108
    private void init(SolrCore core) throws IOException {
109
        Map<String, String>fieldDescriptions = loadSchemaFieldDescriptions();
110
        qed = new QueryEngineDescription();
111
        qed.setName(MetacatSolrIndex.SOLRQUERY);
112
        //setSchemaVersionFromPropertiesFile(qed);
113
        setSolrVersion();
114

  
115
        IndexSchema schema = core.getSchema();
116
        Map<String, SchemaField> fieldMap = schema.getFields();
117
        for (SchemaField schemaField : fieldMap.values()) {
118
            qed.addQueryField(createQueryFieldFromSchemaField(schemaField, fieldDescriptions));
119
        }
120
        Collections.sort(qed.getQueryFieldList(), new QueryFieldAlphaComparator());
121
    }
122
    
123
    /**
124
     * Based on org.apache.solr.handler.admin.SystemInfoHandler.getLuceneInfo()
125
     */
126
    private void setSolrVersion() {
127
        String solrSpecVersion = "";
128
        Package p = SolrCore.class.getPackage();
129
        StringWriter tmp = new StringWriter();
130
        solrSpecVersion = p.getSpecificationVersion();
131
        if (null != solrSpecVersion) {
132
            try {
133
                XML.escapeCharData(solrSpecVersion, tmp);
134
            } catch (IOException e) {
135
                e.printStackTrace();
136
            }
137
            solrSpecVersion = tmp.toString();
138
        }
139
        if (solrSpecVersion != null && !solrSpecVersion.trim().equals("")) {
140
            qed.setQueryEngineVersion(solrSpecVersion);
141
        } else {
142
            qed.setQueryEngineVersion(UNKNOWN);
143
        }
144
        
145
    }
146
    
147

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

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

Also available in: Unified diff