Project

General

Profile

« Previous | Next » 

Revision 7696

Added by Jing Tao over 11 years ago

Modify the subprocessor to use SolrServer rather than solr service url.

View differences:

metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/resourcemap/ResourceMapSubprocessor.java
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: leinfelder $'
10
 *     '$Date: 2011-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
11
 * '$Revision: 6595 $'
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.index.resourcemap;
28

  
29
import java.io.IOException;
30
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.List;
33
import java.util.Map;
34
import java.util.Set;
35

  
36
import javax.xml.parsers.ParserConfigurationException;
37
import javax.xml.xpath.XPathExpressionException;
38

  
39
import org.apache.commons.codec.EncoderException;
40
import org.apache.commons.logging.Log;
41
import org.apache.commons.logging.LogFactory;
42
import org.apache.solr.client.solrj.SolrServer;
43
import org.apache.solr.client.solrj.SolrServerException;
44
import org.apache.solr.client.solrj.response.QueryResponse;
45
import org.apache.solr.common.SolrDocument;
46
import org.apache.solr.common.SolrDocumentList;
47
import org.apache.solr.common.params.SolrParams;
48
import org.apache.solr.servlet.SolrRequestParsers;
49
import org.dataone.cn.indexer.parser.AbstractDocumentSubprocessor;
50
import org.dataone.cn.indexer.parser.IDocumentSubprocessor;
51
import org.dataone.cn.indexer.resourcemap.ResourceMap;
52
import org.dataone.cn.indexer.solrhttp.HTTPService;
53
import org.dataone.cn.indexer.solrhttp.SolrDoc;
54
import org.dataone.cn.indexer.solrhttp.SolrElementField;
55
import org.w3c.dom.Document;
56
import org.xml.sax.SAXException;
57

  
58
import edu.ucsb.nceas.metacat.common.SolrServerFactory;
59
import edu.ucsb.nceas.metacat.index.SolrIndex;
60

  
61
/**
62
 * A solr index parser for the ResourceMap file.
63
 * The solr doc of the ResourceMap self only has the system metadata information.
64
 * The solr docs of the science metadata doc and data file have the resource map package information.
65
 */
66
public class ResourceMapSubprocessor extends AbstractDocumentSubprocessor implements IDocumentSubprocessor {
67

  
68
    private static final String QUERY ="q=id:";
69
    private static Log log = LogFactory.getLog(SolrIndex.class);
70
    private static SolrServer solrServer =  null;
71
    static {
72
        try {
73
            solrServer = SolrServerFactory.createSolrServer();
74
        } catch (Exception e) {
75
            log.error("ResourceMapSubprocessor - can't generate the SolrServer since - "+e.getMessage());
76
        }
77
    }
78
          
79
    @Override
80
    public Map<String, SolrDoc> processDocument(String identifier, Map<String, SolrDoc> docs,
81
    Document doc) throws IOException, EncoderException, SAXException,
82
    XPathExpressionException, ParserConfigurationException, SolrServerException {
83
        SolrDoc resourceMapDoc = docs.get(identifier);
84
        List<SolrDoc> processedDocs = processResourceMap(resourceMapDoc, doc);
85
        Map<String, SolrDoc> processedDocsMap = new HashMap<String, SolrDoc>();
86
        for (SolrDoc processedDoc : processedDocs) {
87
            processedDocsMap.put(processedDoc.getIdentifier(), processedDoc);
88
        }
89
        return processedDocsMap;
90
    }
91

  
92
    private List<SolrDoc> processResourceMap(SolrDoc indexDocument, Document resourceMapDocument)
93
                    throws XPathExpressionException, IOException, SAXException, ParserConfigurationException, EncoderException, SolrServerException{
94
        ResourceMap resourceMap = new ResourceMap(resourceMapDocument);
95
        List<String> documentIds = resourceMap.getAllDocumentIDs();
96
        //List<SolrDoc> updateDocuments = getHttpService().getDocuments(getSolrQueryUri(), documentIds);
97
        List<SolrDoc> updateDocuments = getSolrDocs(documentIds);
98
        List<SolrDoc> mergedDocuments = resourceMap.mergeIndexedDocuments(updateDocuments);
99
        mergedDocuments.add(indexDocument);
100
        return mergedDocuments;
101
    }
102
    
103
    /*
104
     * Get the SolrDoc list for the list of the ids.
105
     */
106
    private List<SolrDoc> getSolrDocs(List<String> ids) throws SolrServerException {
107
        List<SolrDoc> list = new ArrayList<SolrDoc>();
108
        if(ids != null) {
109
            for(String id : ids) {
110
                SolrDoc doc = getSolrDoc(id);
111
                if(doc != null) {
112
                    list.add(doc);
113
                }
114
            }
115
        }
116
        return list;
117
    }
118
    
119
    /*
120
     * Get the SolrDoc for the specified id 
121
     */
122
    private SolrDoc getSolrDoc(String id) throws SolrServerException {
123
        SolrDoc solrDoc = null;
124
        if(solrServer != null) {
125
           String query = QUERY+"\""+id+"\"";
126
           SolrParams solrParams = SolrRequestParsers.parseQueryString(query);
127
           QueryResponse response = solrServer.query(solrParams);
128
           solrDoc = transformQueryResponseToSolrDoc(response);
129
        }
130
        return solrDoc;
131
    }
132
    
133
    /**
134
     * Transform a Solr QueryReponse to a SolrDoc. The QueryReponse contains a list of
135
     * SolrDocuments. This method will transform the first SolrDocuments (Solr lib) to
136
     * the SolrDoc (d1_cn_index_processor lib).
137
     * @param reponse
138
     * @return
139
     */
140
    public static SolrDoc transformQueryResponseToSolrDoc(QueryResponse reponse) {
141
        SolrDoc solrDoc = new SolrDoc();
142
        if(reponse != null) {
143
            SolrDocumentList list = reponse.getResults();
144
            if(list != null && !list.isEmpty()) {
145
                SolrDocument document = list.get(0);
146
                if(document != null) {
147
                    List<SolrElementField> elementFieldList = new ArrayList<SolrElementField>();
148
                    Set<String> keys = document.keySet();
149
                    for(String key :keys) {
150
                        Object value = document.get(key);
151
                        SolrElementField solrElement = new SolrElementField(key, value.toString());
152
                        elementFieldList.add(solrElement);
153
                    }
154
                    solrDoc.setFieldList(elementFieldList);
155
                }
156
                
157
            }
158
        }
159
        return solrDoc;
160
    }
161

  
162
}

Also available in: Unified diff