1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
48
49
50
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
62
63
64
65
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 }