Skip to content

Commit

Permalink
Issue #1065
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-stastny committed May 27, 2024
1 parent 857f88e commit 6e20772
Show file tree
Hide file tree
Showing 64 changed files with 222 additions and 5,691 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import cz.incad.kramerius.processes.starter.ProcessStarter;
import cz.incad.kramerius.resourceindex.ProcessingIndexFeeder;
import cz.incad.kramerius.resourceindex.ResourceIndexModule;
import cz.incad.kramerius.service.FOXMLAppendLicenseService;
import cz.incad.kramerius.service.SortingService;
import cz.incad.kramerius.solr.SolrModule;
import cz.incad.kramerius.statistics.NullStatisticsModule;
import cz.incad.kramerius.utils.IOUtils;
import cz.incad.kramerius.utils.XMLUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;
import cz.incad.kramerius.utils.pid.LexerException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.input.BOMInputStream;
Expand All @@ -37,8 +39,12 @@
import org.xml.sax.helpers.XMLReaderFactory;

import javax.xml.bind.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.xpath.XPathExpressionException;

import java.io.*;
import java.util.logging.Level;


/**
Expand Down Expand Up @@ -78,7 +84,7 @@ public static void main(String[] args) throws InterruptedException, JAXBExceptio
boolean policyPublic = Boolean.parseBoolean(args[argsIndex++]);
String importRoot = args.length > argsIndex ? args[argsIndex++] : KConfiguration.getInstance().getConfiguration().getString("convert.directory");
String exportRoot = args.length > argsIndex ? args[argsIndex++] : KConfiguration.getInstance().getConfiguration().getString("convert.target.directory");
new MetsConvertor().run(importRoot, exportRoot, policyPublic, false, null);
new MetsConvertor().run(importRoot, exportRoot, policyPublic, false, null, null);
} else { // as a process
if (args.length < 2) {
throw new RuntimeException("Not enough arguments.");
Expand All @@ -92,7 +98,6 @@ public static void main(String[] args) throws InterruptedException, JAXBExceptio
String importRoot = args.length > argsIndex ? args[argsIndex++] : KConfiguration.getInstance().getConfiguration().getString("convert.directory");
String exportRoot = args.length > argsIndex ? args[argsIndex++] : KConfiguration.getInstance().getConfiguration().getString("convert.target.directory");
boolean startIndexer = Boolean.valueOf(args.length > argsIndex ? args[argsIndex++] : KConfiguration.getInstance().getConfiguration().getString("ingest.startIndexer", "true"));


if (args.length > argsIndex) {
String arg = args[argsIndex++] ;
Expand All @@ -101,12 +106,15 @@ public static void main(String[] args) throws InterruptedException, JAXBExceptio
log.info(String.format("convert.useImageServer %s", arg));
}

String license = null;
if (startIndexer) { license = args.length > argsIndex ? args[argsIndex++] : null; }

try {
ProcessStarter.updateName(String.format("Import NDK METS z %s ", importRoot));
} catch (Exception e) {
log.error(e.getMessage());
}
new MetsConvertor().run(importRoot, exportRoot, policyPublic, startIndexer, authToken);
new MetsConvertor().run(importRoot, exportRoot, policyPublic, startIndexer, authToken, license);
}
}

Expand All @@ -129,7 +137,7 @@ private MetsConvertor() {
}
}

private void run(String importRoot, String exportRoot, boolean policyPublic, boolean startIndexer, String authToken) throws JAXBException, IOException, InterruptedException, SAXException, SolrServerException {
private void run(String importRoot, String exportRoot, boolean policyPublic, boolean startIndexer, String authToken, String license) throws JAXBException, IOException, InterruptedException, SAXException, SolrServerException {
checkAndConvertDirectory(importRoot, exportRoot, policyPublic);
if (!foundvalidPSP) {
throw new RuntimeException("No valid PSP found.");
Expand All @@ -138,7 +146,19 @@ private void run(String importRoot, String exportRoot, boolean policyPublic, boo
FedoraAccess fa = injector.getInstance(Key.get(FedoraAccess.class, Names.named("rawFedoraAccess")));
SortingService sortingServiceLocal = injector.getInstance(SortingService.class);
ProcessingIndexFeeder feeder = injector.getInstance(ProcessingIndexFeeder.class);
FOXMLAppendLicenseService foxmlService = injector.getInstance(FOXMLAppendLicenseService.class);


if (license != null && startIndexer) {
log.info(String.format("Applying license to %s", license));
try {
foxmlService.appendLicense(exportRoot, license);
} catch (XPathExpressionException | ParserConfigurationException | SAXException | IOException
| LexerException e) {
log.error(e.getMessage(), e);
}
}

Import.run(fa, feeder, sortingServiceLocal,
KConfiguration.getInstance().getProperty("ingest.url"),
KConfiguration.getInstance().getProperty("ingest.user"),
Expand Down
62 changes: 56 additions & 6 deletions processes/import/src/main/java/org/kramerius/Import.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import cz.incad.kramerius.processes.starter.ProcessStarter;
import cz.incad.kramerius.resourceindex.ProcessingIndexFeeder;
import cz.incad.kramerius.resourceindex.ResourceIndexModule;
import cz.incad.kramerius.service.FOXMLAppendLicenseService;
import cz.incad.kramerius.service.SortingService;
import cz.incad.kramerius.solr.SolrModule;
import cz.incad.kramerius.statistics.NullStatisticsModule;
Expand All @@ -27,15 +28,19 @@
import cz.incad.kramerius.utils.conf.KConfiguration;
import cz.incad.kramerius.utils.pid.LexerException;
import cz.incad.kramerius.utils.pid.PIDParser;

import org.apache.commons.io.FileUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.fcrepo.common.rdf.FedoraNamespace;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
Expand All @@ -45,6 +50,8 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathExpressionException;

import java.io.*;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
Expand Down Expand Up @@ -110,31 +117,74 @@ public static void main(String[] args) throws IOException, RepositoryException,
//process params
String importDirFromArgs = args.length > argsIndex ? args[argsIndex++] : null;
Boolean startIndexerFromArgs = args.length > argsIndex ? Boolean.valueOf(args[argsIndex++]) : null;


String license = null;
if (startIndexerFromArgs) { license = args.length > argsIndex ? args[argsIndex++] : null; }


//Boolean startIndexerFromArgs = args.length > argsIndex ? Boolean.valueOf(args[argsIndex++]) : null;


Injector injector = Guice.createInjector(new SolrModule(), new ResourceIndexModule(), new RepoModule(), new NullStatisticsModule(), new ImportModule());
FedoraAccess fa = injector.getInstance(Key.get(FedoraAccess.class, Names.named("rawFedoraAccess")));
SortingService sortingServiceLocal = injector.getInstance(SortingService.class);

FOXMLAppendLicenseService foxmlService = injector.getInstance(FOXMLAppendLicenseService.class);


//priority: 1. args, 2. System property, 3. KConfiguration, 4. explicit defalut value
String importDirectory = KConfiguration.getInstance().getProperty("import.directory");
if (importDirFromArgs != null) {
importDirectory = importDirFromArgs;
} else if (System.getProperties().containsKey("import.directory")) {
importDirectory = System.getProperty("import.directory");
}

Boolean startIndexer = Boolean.valueOf(KConfiguration.getInstance().getConfiguration().getString("ingest.startIndexer", "true"));
if (startIndexerFromArgs != null) {
startIndexer = startIndexerFromArgs;
} else if (System.getProperties().containsKey("ingest.startIndexer")) {
startIndexer = Boolean.valueOf(System.getProperty("ingest.startIndexer"));
}

ProcessStarter.updateName(String.format("Import FOXML z %s ", importDirectory));
log.info("import dir: " + importDirectory);
log.info("start indexer: " + startIndexer);

ProcessingIndexFeeder feeder = injector.getInstance(ProcessingIndexFeeder.class);
Import.run(fa, feeder, sortingServiceLocal, KConfiguration.getInstance().getProperty("ingest.url"), KConfiguration.getInstance().getProperty("ingest.user"), KConfiguration.getInstance().getProperty("ingest.password"), importDirectory, startIndexer, authToken);

if (license != null) {

File importFolder = new File(importDirectory);
File importParentFolder = importFolder.getParentFile();
File licensesImportFile = new File(importParentFolder, importFolder.getName()+"-"+license);
licensesImportFile.mkdirs();
log.info(String.format("Copy data from %s to %s", importFolder.getAbsolutePath(), licensesImportFile.getAbsolutePath()));
FileUtils.copyDirectory(importFolder, licensesImportFile);


log.info(String.format("Applying license %s", license));
try {
foxmlService.appendLicense(licensesImportFile.getAbsolutePath(), license);
} catch (XPathExpressionException | ParserConfigurationException | SAXException | IOException | LexerException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
}

ProcessStarter.updateName(String.format("Import FOXML z %s ", importDirectory));
log.info("import dir: " + licensesImportFile);
log.info("start indexer: " + startIndexer);
log.info("license : " + license);

Import.run(fa, feeder, sortingServiceLocal, KConfiguration.getInstance().getProperty("ingest.url"), KConfiguration.getInstance().getProperty("ingest.user"), KConfiguration.getInstance().getProperty("ingest.password"), importDirectory, startIndexer, authToken);

log.info( String.format("Deleting import folder %s", licensesImportFile));
FileUtils.deleteDirectory(licensesImportFile);

} else {

ProcessStarter.updateName(String.format("Import FOXML z %s ", importDirectory));
log.info("import dir: " + importDirectory);
log.info("start indexer: " + startIndexer);

Import.run(fa, feeder, sortingServiceLocal, KConfiguration.getInstance().getProperty("ingest.url"), KConfiguration.getInstance().getProperty("ingest.user"), KConfiguration.getInstance().getProperty("ingest.password"), importDirectory, startIndexer, authToken);

}
}

public static void run(FedoraAccess fa, ProcessingIndexFeeder feeder, SortingService sortingServiceParam, final String url, final String user, final String pwd, String importRoot) throws IOException, SolrServerException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import cz.incad.kramerius.imaging.lp.guice.GenerateDeepZoomCacheModule;
import cz.incad.kramerius.relation.RelationService;
import cz.incad.kramerius.relation.impl.RelationServiceImpl;
import cz.incad.kramerius.service.FOXMLAppendLicenseService;
import cz.incad.kramerius.service.SortingService;
import cz.incad.kramerius.service.impl.FOXMLAppendLicenseImpl;
import cz.incad.kramerius.service.impl.SortingServiceImpl;
import cz.incad.kramerius.statistics.StatisticsAccessLog;
import cz.incad.kramerius.statistics.accesslogs.AggregatedAccessLogs;
Expand All @@ -18,7 +20,7 @@ public class ImportModule extends AbstractModule {
protected void configure() {
bind(RelationService.class).to(RelationServiceImpl.class).in(Scopes.SINGLETON);
bind(SortingService.class).to(SortingServiceImpl.class).in(Scopes.SINGLETON);

bind(FOXMLAppendLicenseService.class).to(FOXMLAppendLicenseImpl.class).in(Scopes.SINGLETON);

//bind(FedoraAccess.class).annotatedWith(Names.named("rawFedoraAccess")).to(FedoraAccessImpl.class).in(Scopes.SINGLETON);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,15 @@ private List<String> paramsToList(String id, JSONObject params, Consumer<Boolean
File inputDataDir = extractMandatoryParamFileContainedInADir(params, "inputDataDir", new File(KConfiguration.getInstance().getProperty("import.directory")));
Boolean startIndexer = extractMandatoryParamBoolean(params, "startIndexer");

String license = extractOptionalParamString(params, "license", null);

List<String> result = new ArrayList<>();
result.add(inputDataDir.getPath());
result.add(startIndexer.toString());
if (license != null) {
result.add(license.toString());
}

consumer.accept(false);
return result;
}
Expand All @@ -1115,12 +1121,20 @@ private List<String> paramsToList(String id, JSONObject params, Consumer<Boolean
Boolean startIndexer = extractMandatoryParamBoolean(params, "startIndexer");
Boolean useIIPServer = extractMandatoryParamBoolean(params, "useIIPServer");

String license = extractOptionalParamString(params, "license", null);

List<String> result = new ArrayList<>();
result.add(policy);
result.add(inputDataDir.getPath());
result.add(convertedDataDir.getPath());
result.add(startIndexer.toString());
result.add(useIIPServer.toString());

if (license != null) {
result.add(license.toString());

}

consumer.accept(false);
return result;
}
Expand Down

This file was deleted.

Loading

0 comments on commit 6e20772

Please sign in to comment.