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.content;
20  
21  import java.io.IOException;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.solr.client.solrj.SolrClient;
26  import org.apache.solr.client.solrj.SolrServerException;
27  import org.apache.solr.common.SolrInputDocument;
28  import org.mycore.common.content.MCRContent;
29  import org.mycore.datamodel.metadata.MCRObjectID;
30  import org.mycore.solr.MCRSolrClientFactory;
31  import org.mycore.solr.index.MCRSolrIndexHandler;
32  import org.mycore.solr.index.document.MCRSolrInputDocumentFactory;
33  import org.mycore.solr.index.handlers.MCRSolrAbstractIndexHandler;
34  import org.mycore.solr.index.handlers.document.MCRSolrInputDocumentHandler;
35  import org.mycore.solr.index.statistic.MCRSolrIndexStatistic;
36  import org.mycore.solr.index.statistic.MCRSolrIndexStatisticCollector;
37  import org.xml.sax.SAXException;
38  
39  /**
40   * @author Thomas Scheffler (yagee)
41   *
42   */
43  public class MCRSolrMCRContentIndexHandler extends MCRSolrAbstractIndexHandler {
44  
45      MCRObjectID id;
46  
47      MCRContent content;
48  
49      private SolrInputDocument document;
50  
51      public MCRSolrMCRContentIndexHandler(MCRObjectID id, MCRContent content) {
52          this(id, content, MCRSolrClientFactory.getMainSolrClient());
53      }
54  
55      public MCRSolrMCRContentIndexHandler(MCRObjectID id, MCRContent content, SolrClient solrClient) {
56          super(solrClient);
57          this.id = id;
58          this.content = content;
59      }
60  
61      @Override
62      public MCRSolrIndexStatistic getStatistic() {
63          return MCRSolrIndexStatisticCollector.DOCUMENTS;
64      }
65  
66      /* (non-Javadoc)
67       * @see org.mycore.solr.index.handlers.MCRSolrAbstractIndexHandler#index()
68       */
69      @Override
70      public void index() throws IOException, SolrServerException {
71          try {
72              this.document = MCRSolrInputDocumentFactory.getInstance().getDocument(id, content);
73          } catch (SAXException e) {
74              throw new IOException(e);
75          }
76      }
77  
78      @Override
79      public List<MCRSolrIndexHandler> getSubHandlers() {
80          MCRSolrIndexHandler mcrSolrIndexHandler = new MCRSolrInputDocumentHandler(document, getSolrClient());
81          mcrSolrIndexHandler.setCommitWithin(getCommitWithin());
82          return Collections.singletonList(mcrSolrIndexHandler);
83      }
84  
85      @Override
86      public int getDocuments() {
87          return 0;
88      }
89  
90      @Override
91      public String toString() {
92          return "index " + id;
93      }
94  
95  }