Search in sources :

Example 1 with DownloadImportConfiguration

use of cz.mzk.recordmanager.server.model.DownloadImportConfiguration in project RecordManager2 by moravianlibrary.

the class ImportRecordsFileReader method initializeDownloadReader.

protected void initializeDownloadReader() throws IOException {
    DownloadImportConfiguration config = dicDao.get(confId);
    if (config == null) {
        throw new IllegalArgumentException(String.format("Configuration with id=%s not found.", confId));
    }
    String url = config.getUrl();
    if (url == null || url.isEmpty()) {
        throw new IllegalArgumentException(String.format("Missing url in DownloadImportConfiguration with id=%s.", confId));
    }
    this.format = IOFormat.stringToExportFormat(config.getFormat());
    this.reader = getMarcReader(httpClient.executeGet(url));
}
Also used : DownloadImportConfiguration(cz.mzk.recordmanager.server.model.DownloadImportConfiguration)

Example 2 with DownloadImportConfiguration

use of cz.mzk.recordmanager.server.model.DownloadImportConfiguration in project RecordManager2 by moravianlibrary.

the class AntikvariatyRecordsReader method initializeReader.

protected void initializeReader() throws Exception {
    DownloadImportConfiguration config = configDao.get(configId);
    if (config == null) {
        throw new IllegalArgumentException(String.format("Configuration with id=%s not found.", configId));
    }
    String url = config.getUrl();
    if (url == null || url.isEmpty()) {
        throw new IllegalArgumentException(String.format("Missing url in DownloadImportConfiguration with id=%s.", configId));
    }
    try {
        reader = new StaxEventItemReader<AntikvariatyRecord>();
        reader.setResource(new InputStreamResource(httpClient.executeGet(url)));
        reader.setFragmentRootElementName("record");
        Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
        unmarshaller.setClassesToBeBound(AntikvariatyRecord.class);
        unmarshaller.afterPropertiesSet();
        reader.setUnmarshaller(unmarshaller);
        reader.setSaveState(false);
        reader.open(null);
        reader.afterPropertiesSet();
    } catch (Exception ex) {
        throw new RuntimeException("StaxEventItemReader can not be created", ex);
    }
}
Also used : Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) DownloadImportConfiguration(cz.mzk.recordmanager.server.model.DownloadImportConfiguration) AntikvariatyRecord(cz.mzk.recordmanager.server.model.AntikvariatyRecord) InputStreamResource(org.springframework.core.io.InputStreamResource)

Example 3 with DownloadImportConfiguration

use of cz.mzk.recordmanager.server.model.DownloadImportConfiguration in project RecordManager2 by moravianlibrary.

the class OAIItemProcessor method beforeStep.

@Override
public void beforeStep(StepExecution stepExecution) {
    try (SessionBinder session = sync.register()) {
        Long confId = stepExecution.getJobParameters().getLong("configurationId");
        OAIHarvestConfiguration hc = configDao.get(confId);
        if (hc != null) {
            format = formatResolver.resolve(hc.getMetadataPrefix());
            String regex = MoreObjects.firstNonNull(hc.getRegex(), DEFAULT_EXTRACT_ID_PATTERN);
            configuration = hc;
            idExtractor = new RegexpExtractor(regex);
        } else {
            DownloadImportConfiguration dic = downloadImportConfDao.get(confId);
            if (dic != null) {
                format = formatResolver.resolve(Constants.METADATA_FORMAT_XML_MARC);
                String regex = MoreObjects.firstNonNull(dic.getRegex(), DEFAULT_EXTRACT_ID_PATTERN);
                configuration = dic;
                idExtractor = new RegexpExtractor(regex);
            }
        }
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException tce) {
            throw new RuntimeException(tce);
        }
    }
}
Also used : RegexpExtractor(cz.mzk.recordmanager.server.util.RegexpExtractor) OAIHarvestConfiguration(cz.mzk.recordmanager.server.model.OAIHarvestConfiguration) TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SessionBinder(cz.mzk.recordmanager.server.util.HibernateSessionSynchronizer.SessionBinder) DownloadImportConfiguration(cz.mzk.recordmanager.server.model.DownloadImportConfiguration)

Example 4 with DownloadImportConfiguration

use of cz.mzk.recordmanager.server.model.DownloadImportConfiguration in project RecordManager2 by moravianlibrary.

the class ImportAntikvariatyTest method simpleTest.

@Test
public void simpleTest() throws Exception {
    final Long confId = 500L;
    DownloadImportConfiguration config = configDao.get(confId);
    Assert.assertNotNull(config);
    Assert.assertNotNull(config.getUrl());
    reset(httpClient);
    InputStream response0 = this.getClass().getResourceAsStream("/antikvariaty/antikvariaty-base.xml");
    expect(httpClient.executeGet("http://local.antikvariaty.mzk/file.xml")).andReturn(response0);
    replay(httpClient);
    Job job = jobRegistry.getJob(Constants.JOB_ID_IMPORT_ANTIKVARIATY);
    Map<String, JobParameter> params = new HashMap<String, JobParameter>();
    params.put(Constants.JOB_PARAM_CONF_ID, new JobParameter(confId));
    JobParameters jobParams = new JobParameters(params);
    jobLauncher.run(job, jobParams);
    AntikvariatyRecord toBeChangedRecord = antikDao.get(1L);
    Assert.assertNotNull(toBeChangedRecord);
    Assert.assertTrue(toBeChangedRecord.getCatalogueIds().size() == 1);
    Assert.assertEquals(toBeChangedRecord.getCatalogueIds().get(0), "zb00026");
    Assert.assertNotNull(toBeChangedRecord.getUpdated());
    Assert.assertNotNull(toBeChangedRecord.getTitle());
    Assert.assertNotNull(toBeChangedRecord.getUrl());
    Assert.assertNotNull(toBeChangedRecord.getPublicationYear());
    reset(httpClient);
    response0 = this.getClass().getResourceAsStream("/antikvariaty/antikvariaty-update.xml");
    expect(httpClient.executeGet("http://local.antikvariaty.mzk/file.xml")).andReturn(response0);
    replay(httpClient);
    job = jobRegistry.getJob(Constants.JOB_ID_IMPORT_ANTIKVARIATY);
    params = new HashMap<String, JobParameter>();
    params.put(Constants.JOB_PARAM_CONF_ID, new JobParameter(confId));
    params.put(Constants.JOB_PARAM_REPEAT, new JobParameter(1L));
    jobParams = new JobParameters(params);
    jobLauncher.run(job, jobParams);
    AntikvariatyRecord updatedRecord = antikDao.get(1L);
    Assert.assertNotNull(updatedRecord);
    Assert.assertTrue(updatedRecord.getCatalogueIds().size() == 1);
    Assert.assertEquals(updatedRecord.getCatalogueIds().get(0), "xxxxxxxxxxxxxxx");
    Assert.assertTrue(updatedRecord.getUpdated().compareTo(toBeChangedRecord.getUpdated()) > 0);
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) DownloadImportConfiguration(cz.mzk.recordmanager.server.model.DownloadImportConfiguration) JobParameters(org.springframework.batch.core.JobParameters) Job(org.springframework.batch.core.Job) JobParameter(org.springframework.batch.core.JobParameter) AntikvariatyRecord(cz.mzk.recordmanager.server.model.AntikvariatyRecord) Test(org.testng.annotations.Test) AbstractTest(cz.mzk.recordmanager.server.AbstractTest)

Example 5 with DownloadImportConfiguration

use of cz.mzk.recordmanager.server.model.DownloadImportConfiguration in project RecordManager2 by moravianlibrary.

the class ImportRecordsWriter method beforeStep.

@Override
public void beforeStep(StepExecution stepExecution) {
    try (SessionBinder session = sync.register()) {
        OAIHarvestConfiguration hc = oaiHarvestConfigurationDao.get(configurationId);
        String regex = null;
        if (hc != null) {
            regex = hc.getRegex();
            harvestConfiguration = hc;
        } else {
            DownloadImportConfiguration dic = downloadImportConfigurationDao.get(configurationId);
            if (dic != null) {
                regex = dic.getRegex();
                harvestConfiguration = dic;
            }
        }
        if (regex != null) {
            regexpExtractor = new RegexpExtractor(regex);
        }
    }
}
Also used : RegexpExtractor(cz.mzk.recordmanager.server.util.RegexpExtractor) OAIHarvestConfiguration(cz.mzk.recordmanager.server.model.OAIHarvestConfiguration) SessionBinder(cz.mzk.recordmanager.server.util.HibernateSessionSynchronizer.SessionBinder) DownloadImportConfiguration(cz.mzk.recordmanager.server.model.DownloadImportConfiguration)

Aggregations

DownloadImportConfiguration (cz.mzk.recordmanager.server.model.DownloadImportConfiguration)5 AntikvariatyRecord (cz.mzk.recordmanager.server.model.AntikvariatyRecord)2 OAIHarvestConfiguration (cz.mzk.recordmanager.server.model.OAIHarvestConfiguration)2 SessionBinder (cz.mzk.recordmanager.server.util.HibernateSessionSynchronizer.SessionBinder)2 RegexpExtractor (cz.mzk.recordmanager.server.util.RegexpExtractor)2 AbstractTest (cz.mzk.recordmanager.server.AbstractTest)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 Job (org.springframework.batch.core.Job)1 JobParameter (org.springframework.batch.core.JobParameter)1 JobParameters (org.springframework.batch.core.JobParameters)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1 Jaxb2Marshaller (org.springframework.oxm.jaxb.Jaxb2Marshaller)1 Test (org.testng.annotations.Test)1