View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe 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 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe 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 MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.solr.index.handlers.document;
20  
21  import java.io.IOException;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.apache.solr.client.solrj.SolrClient;
26  import org.apache.solr.client.solrj.SolrServerException;
27  import org.apache.solr.client.solrj.request.UpdateRequest;
28  import org.apache.solr.common.SolrInputDocument;
29  import org.mycore.solr.MCRSolrConstants;
30  import org.mycore.solr.MCRSolrUtils;
31  import org.mycore.solr.index.MCRSolrIndexer;
32  import org.mycore.solr.index.handlers.MCRSolrAbstractIndexHandler;
33  import org.mycore.solr.index.statistic.MCRSolrIndexStatistic;
34  import org.mycore.solr.index.statistic.MCRSolrIndexStatisticCollector;
35  
36  /**
37   * @author Thomas Scheffler (yagee)
38   *
39   */
40  public class MCRSolrInputDocumentHandler extends MCRSolrAbstractIndexHandler {
41  
42      private static Logger LOGGER = LogManager.getLogger(MCRSolrInputDocumentHandler.class);
43  
44      SolrInputDocument document;
45  
46      public MCRSolrInputDocumentHandler(SolrInputDocument document) {
47          super();
48          this.document = document;
49      }
50  
51      public MCRSolrInputDocumentHandler(SolrInputDocument document, SolrClient solrClient) {
52          super(solrClient);
53          this.document = document;
54      }
55  
56      /* (non-Javadoc)
57       * @see org.mycore.solr.index.MCRSolrIndexHandler#index()
58       */
59      @Override
60      public void index() throws IOException, SolrServerException {
61          String id = String.valueOf(document.getFieldValue("id"));
62          SolrClient solrClient = getSolrClient();
63          LOGGER.info("Sending {} to SOLR...", id);
64          if (MCRSolrUtils.useNestedDocuments()) {
65              MCRSolrIndexer.deleteById(solrClient, id);
66          }
67          UpdateRequest updateRequest = getUpdateRequest(MCRSolrConstants.SOLR_UPDATE_PATH);
68          updateRequest.add(document);
69          updateRequest.process(solrClient);
70      }
71  
72      /* (non-Javadoc)
73       * @see org.mycore.solr.index.MCRSolrIndexHandler#getStatistic()
74       */
75      @Override
76      public MCRSolrIndexStatistic getStatistic() {
77          return MCRSolrIndexStatisticCollector.DOCUMENTS;
78      }
79  
80      @Override
81      public String toString() {
82          return "index " + document.getFieldValue("id");
83      }
84  
85  }