package eu.dnetlib.functionality.index.solr; import java.io.File; import java.io.IOException; import java.io.StringReader; import javax.xml.transform.TransformerException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Required; import eu.dnetlib.functionality.index.solr.utils.IndexSchemaFactory; public class OfflineIndexSolrClient extends AbstractIndexSolrClient { private ThreadLocal domFactory = new ThreadLocal() { @Override protected SAXReader initialValue() { return new SAXReader(); }}; private IndexSchemaFactory schemaFactory; @Override public void updateSchema(final String inputFilePath, final String layout) { try { final Document doc = parse(readFile(inputFilePath)); final String mdFormat = doc.valueOf("//CONFIGURATION/NAME"); if (mdFormat != null && !mdFormat.isEmpty()) { // gets the index name based on the default interpretation Node fieldNode = doc.selectSingleNode("//STATUS/LAYOUTS/LAYOUT[@name='"+layout+"']/FIELDS"); if (fieldNode == null || fieldNode.asXML().isEmpty()) throw new IllegalArgumentException("cannot find layout: " + layout); final Document fields = parse(fieldNode.asXML()); System.out.println(IOUtils.toString(schemaFactory.getSchema(fields))); } } catch (DocumentException e) { throw new IllegalArgumentException(inputFilePath, e); } catch (TransformerException e) { throw new IllegalArgumentException("cannot generate schema from given file"); } catch (IOException e) { throw new IllegalArgumentException("cannot read given file: " + inputFilePath); } } /* private List getIndexDataDir(final String solrDataDir, final String mdFormat, final String layout) { return Lists.newArrayList(new File(solrDataDir).list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.startsWith(mdFormat + "-" + layout); } })); } */ private String readFile(String profilePath) throws IOException { if (profilePath == null) throw new IOException("cannot read given file "); return FileUtils.readFileToString(new File(profilePath)); } private Document parse(final String source) throws DocumentException { return domFactory.get().read(new StringReader(source)); } /** * Method writes the content's output to the specified directory with the given fileName * * @param indexDir * @param config * @param fileName * @throws IOException private void write(final String directory, final Reader content, final String fileName) throws IOException { final File fConfig = new File(directory, fileName); try { FileOutputStream output = new FileOutputStream(fConfig); IOUtils.copy(content, output); output.flush(); output.close(); } catch (FileNotFoundException e) { System.out.println("SKIPPING ######################"); } } */ ////////////////////////////////////////////////////////// @Required public void setSchemaFactory(IndexSchemaFactory schemaFactory) { this.schemaFactory = schemaFactory; } public IndexSchemaFactory getSchemaFactory() { return schemaFactory; } }