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.stream;
20  
21  import java.io.IOException;
22  import java.nio.file.FileVisitResult;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.SimpleFileVisitor;
26  import java.nio.file.attribute.BasicFileAttributes;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.concurrent.TimeUnit;
30  
31  import org.apache.logging.log4j.LogManager;
32  import org.apache.logging.log4j.Logger;
33  import org.apache.solr.client.solrj.SolrClient;
34  import org.apache.solr.client.solrj.SolrServerException;
35  import org.apache.solr.common.SolrInputDocument;
36  import org.mycore.datamodel.metadata.MCRMetadataManager;
37  import org.mycore.datamodel.metadata.MCRObjectID;
38  import org.mycore.datamodel.niofs.MCRPath;
39  import org.mycore.solr.index.MCRSolrIndexHandler;
40  import org.mycore.solr.index.file.MCRSolrPathDocumentFactory;
41  import org.mycore.solr.index.handlers.MCRSolrAbstractIndexHandler;
42  import org.mycore.solr.index.handlers.MCRSolrIndexHandlerFactory;
43  import org.mycore.solr.index.handlers.document.MCRSolrInputDocumentsHandler;
44  import org.mycore.solr.index.statistic.MCRSolrIndexStatistic;
45  
46  /**
47   * Commits <code>MCRFile</code> objects to solr, be aware that the files are
48   * not indexed directly, but added to a list of sub index handlers.
49   * 
50   * @author Matthias Eichner
51   */
52  public class MCRSolrFilesIndexHandler extends MCRSolrAbstractIndexHandler {
53  
54      private static final Logger LOGGER = LogManager.getLogger(MCRSolrFilesIndexHandler.class);
55  
56      protected String mcrID;
57  
58      protected List<MCRSolrIndexHandler> subHandlerList;
59  
60      /**
61       * Creates a new solr file index handler.
62       * 
63       * @param mcrID id of the derivate or mcrobject, if you put a mcrobject id here
64       * all files of each derivate are indexed
65       * @param solrClient where to index
66       */
67      public MCRSolrFilesIndexHandler(String mcrID, SolrClient solrClient) {
68          super(solrClient);
69          this.mcrID = mcrID;
70          this.subHandlerList = new ArrayList<>();
71      }
72  
73      @Override
74      public void index() throws IOException, SolrServerException {
75          MCRObjectID mcrID = MCRObjectID.getInstance(getID());
76          if (!MCRMetadataManager.exists(mcrID)) {
77              LOGGER.warn("Unable to index '{}' cause it doesn't exists anymore!", mcrID);
78              return;
79          }
80          if (mcrID.getTypeId().equals("derivate")) {
81              indexDerivate(mcrID);
82          } else {
83              indexObject(mcrID);
84          }
85      }
86  
87      protected void indexDerivate(MCRObjectID derivateID) throws IOException {
88          MCRPath rootPath = MCRPath.getPath(derivateID.toString(), "/");
89          final MCRSolrIndexHandlerFactory ihf = MCRSolrIndexHandlerFactory.getInstance();
90          final List<MCRSolrIndexHandler> subHandlerList = this.subHandlerList;
91          final List<SolrInputDocument> docs = new ArrayList<>();
92          final SolrClient solrClient = this.solrClient;
93          Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
94  
95              @Override
96              public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
97                  boolean sendContent = ihf.checkFile(file, attrs);
98                  try {
99                      if (sendContent) {
100                         subHandlerList.add(ihf.getIndexHandler(file, attrs, solrClient, true));
101                     } else {
102                         SolrInputDocument fileDoc = MCRSolrPathDocumentFactory.getInstance().getDocument(file, attrs);
103                         docs.add(fileDoc);
104                     }
105                 } catch (Exception ex) {
106                     LOGGER.error("Error creating transfer thread", ex);
107                 }
108                 return super.visitFile(file, attrs);
109             }
110 
111         });
112         int fileCount = subHandlerList.size() + docs.size();
113         LOGGER.info("Sending {} file(s) for derivate \"{}\"", fileCount, derivateID);
114         if (!docs.isEmpty()) {
115             MCRSolrInputDocumentsHandler subHandler = new MCRSolrInputDocumentsHandler(docs, solrClient);
116             subHandler.setCommitWithin(getCommitWithin());
117             this.subHandlerList.add(subHandler);
118         }
119     }
120 
121     protected void indexObject(MCRObjectID objectID) throws IOException {
122         List<MCRObjectID> derivateIds = MCRMetadataManager.getDerivateIds(objectID, 0, TimeUnit.MILLISECONDS);
123         for (MCRObjectID derivateID : derivateIds) {
124             indexDerivate(derivateID);
125         }
126     }
127 
128     @Override
129     public List<MCRSolrIndexHandler> getSubHandlers() {
130         return this.subHandlerList;
131     }
132 
133     public String getID() {
134         return mcrID;
135     }
136 
137     @Override
138     public MCRSolrIndexStatistic getStatistic() {
139         return new MCRSolrIndexStatistic("no index operation");
140     }
141 
142     @Override
143     public int getDocuments() {
144         return 0;
145     }
146 
147     @Override
148     public String toString() {
149         return "index files of " + this.mcrID;
150     }
151 
152 }