Search in sources :

Example 1 with VipCoreLibraryRule

use of dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule in project solr-document-store by DBCDK.

the class DocProducerIT method setUp.

@Before
public void setUp() throws Exception {
    try {
        pg.clearTables("bibliographicSolrKeys", "bibliographictobibliographic", "holdingsitemssolrkeys", "holdingstobibliographic");
        try (Connection connection = dataSource.getConnection();
            Statement stmt = connection.createStatement()) {
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300000, 'FBSSchool', False, False, False, NOW(), True);");
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300010, 'FBSSchool', False, False, False, NOW(), True);");
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300101, 'FBSSchool', False, False, False, NOW(), True);");
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300102, 'FBSSchool', False, False, False, NOW(), True);");
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300103, 'FBSSchool', False, False, False, NOW(), True);");
        }
    } catch (SQLException ex) {
        log.trace("Exception: {}", ex.getMessage());
    }
    for (SolrCollection collection : config.getSolrCollections()) {
        SolrClient solrClient = collection.getSolrClient();
        solrClient.deleteByQuery("*:*");
        solrClient.commit();
    }
    docProducer = new DocProducer();
    docProducer.config = config;
    docProducer.libraryRuleProvider = new LibraryRuleProviderBean() {

        @Override
        public VipCoreLibraryRule libraryRulesFor(String agencyId) {
            switch(agencyId) {
                case "300000":
                case "300101":
                case "300102":
                case "300103":
                case "300104":
                case "777777":
                    return new VipCoreLibraryRuleMockResponse(true, true, false, true, false, true);
                default:
                    throw new IllegalArgumentException("Don't know library rules for: " + agencyId);
            }
        }
    };
    docProducer.persistentWorkIdProvider = new PersistentWorkIdProviderBean() {

        @Override
        public String persistentWorkIdFor(String corepoWorkId) {
            return "xxx";
        }
    };
    ProfileProviderBean profileProviderBean = new ProfileProviderBean();
    profileProviderBean.config = config;
    profileProviderBean.vipCoreHttpClient = new VipCoreHttpClient();
    docProducer.profileProvider = profileProviderBean;
    docProducer.init();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) VipCoreLibraryRule(dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule) SolrClient(org.apache.solr.client.solrj.SolrClient) VipCoreHttpClient(dk.dbc.openagency.http.VipCoreHttpClient) Before(org.junit.Before)

Example 2 with VipCoreLibraryRule

use of dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule in project solr-document-store by DBCDK.

the class DocProducerTest method test.

@Test
public void test() throws Exception {
    System.out.println("test");
    Config config = new Config("solrUrl=Not-Relevant", "zookeeperUrl=Not-Relevant", "profileServiceUrl=Not-Relevant", "solrDocStoreUrl=Not-Relevant", "solrAppId=Not-Relevant", "queues=Not-Relevant", "openAgencyUrl=Not-Relevant", "vipCoreEndpoint=Not-Relevant", "workPresentationUrl=not-relevant", "scanProfiles=102030-magic,123456-basic", "scanDefaultFields=abc,def") {

        @Override
        protected Set<SolrCollection> makeSolrCollections(Client client) throws IllegalArgumentException {
            return Collections.EMPTY_SET;
        }

        @Override
        public Client getClient() {
            return CLIENT;
        }
    };
    DocProducer docProducer = new DocProducer() {

        @Override
        public SolrDocStoreResponse fetchSourceDoc(QueueJob job) throws IOException {
            String file = "DocProducerTest/" + job.getAgencyId() + "-" + job.getBibliographicRecordId() + ".json";
            System.out.println("file = " + file);
            try (InputStream stream = DocProducerTest.class.getClassLoader().getResourceAsStream(file)) {
                return OBJECT_MAPPER.readValue(stream, SolrDocStoreResponse.class);
            }
        }
    };
    docProducer.config = config;
    docProducer.libraryRuleProvider = new LibraryRuleProviderBean() {

        @Override
        public VipCoreLibraryRule libraryRulesFor(String agencyId) {
            return new VipCoreLibraryRuleMockResponse(true, true, true, true, false, true);
        }
    };
    docProducer.persistentWorkIdProvider = new PersistentWorkIdProviderBean() {

        @Override
        public String persistentWorkIdFor(String corepoWorkId) {
            return "persistent-for-" + corepoWorkId;
        }
    };
    docProducer.profileProvider = new ProfileProviderBean() {

        @Override
        public VipCoreProfile profileFor(String agencyId, String profile) {
            switch(agencyId + "-" + profile) {
                case "102030-magic":
                    return new VipCoreProfile(true, "220000-katalog");
                case "123456-basic":
                    return new VipCoreProfile(false, "220000-katalog");
                default:
                    throw new AssertionError();
            }
        }
    };
    SolrDocStoreResponse node = docProducer.fetchSourceDoc(QueueJob.manifestation(300101, "clazzifier", "23645564"));
    System.out.println("node = " + node);
    SolrCollection solrCollection = new SolrCollection() {

        @Override
        public FeatureSwitch getFeatures() {
            return new FeatureSwitch("all");
        }

        @Override
        public KnownSolrFields getSolrFields() {
            try (InputStream is = getClass().getClassLoader().getResourceAsStream("schema.xml")) {
                return new KnownSolrFields(is);
            } catch (IOException | SAXException ex) {
                throw new RuntimeException(ex);
            }
        }
    };
    SolrInputDocument document = docProducer.createSolrDocument(node, solrCollection);
    System.out.println("document = " + document);
    assertTrue(document.containsKey("dkcclterm.po"));
    assertFalse(document.containsKey("unknown.field"));
    assertEquals(3, document.get("rec.holdingsAgencyId").getValues().size());
    assertTrue(document.get("rec.holdingsAgencyId").getValues().contains("300101"));
    assertTrue(document.get("rec.holdingsAgencyId").getValues().contains("300102"));
    assertTrue(document.get("rec.holdingsAgencyId").getValues().contains("300104"));
    assertTrue(document.get("rec.repositoryId").getValues().contains("870970-basis:23645564"));
    assertTrue(document.containsKey("dkcclterm.ln"));
    assertTrue(document.get("dkcclterm.ln").getValues().contains("777777"));
    System.out.println("OK");
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) VipCoreLibraryRule(dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule) VipCoreProfile(dk.dbc.solrdocstore.updater.businesslogic.VipCoreProfile) SAXException(org.xml.sax.SAXException) FeatureSwitch(dk.dbc.solrdocstore.updater.businesslogic.FeatureSwitch) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocStoreResponse(dk.dbc.solrdocstore.updater.businesslogic.SolrDocStoreResponse) KnownSolrFields(dk.dbc.solrdocstore.updater.businesslogic.KnownSolrFields) Client(javax.ws.rs.client.Client) QueueJob(dk.dbc.search.solrdocstore.queue.QueueJob) Test(org.junit.Test)

Example 3 with VipCoreLibraryRule

use of dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule in project solr-document-store by DBCDK.

the class WorkerIT method setUp.

@Before
public void setUp() throws Exception {
    try {
        pg.clearTables("bibliographicSolrKeys", "bibliographictobibliographic", "holdingsitemssolrkeys", "holdingstobibliographic", "openagencycache");
        try (Connection connection = dataSource.getConnection();
            Statement stmt = connection.createStatement()) {
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300000, 'FBSSchool', False, False, False, NOW(), True);");
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300010, 'FBSSchool', False, False, False, NOW(), True);");
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300101, 'FBSSchool', False, False, False, NOW(), True);");
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300102, 'FBSSchool', False, False, False, NOW(), True);");
            stmt.executeUpdate("INSERT INTO openagencycache (agencyid, librarytype, partofbibdk, partofdanbib, authcreatecommonrecord, fetched, valid) VALUES(300103, 'FBSSchool', False, False, False, NOW(), True);");
        }
    } catch (SQLException ex) {
        log.trace("Exception: {}", ex.getMessage());
    }
    for (SolrCollection solrCollection : config.getSolrCollections()) {
        SolrClient solrClient = solrCollection.getSolrClient();
        solrClient.deleteByQuery("*:*");
        solrClient.commit();
    }
    worker = new Worker();
    worker.config = config;
    String solrDocStoreUrl1 = config.getSolrDocStoreUrl();
    System.out.println("solrDocStoreUrl1 = " + solrDocStoreUrl1);
    worker.dataSource = dataSource;
    worker.es = Executors.newCachedThreadPool();
    DocProducer docProducer = worker.docProducer = new DocProducer();
    docProducer.config = config;
    docProducer.libraryRuleProvider = new LibraryRuleProviderBean() {

        @Override
        public VipCoreLibraryRule libraryRulesFor(String agencyId) {
            switch(agencyId) {
                case "300000":
                case "300101":
                case "300102":
                case "300103":
                case "300104":
                case "777777":
                    return new VipCoreLibraryRuleMockResponse(true, true, false, true, false, true);
                default:
                    throw new IllegalArgumentException("Don't know library rules for: " + agencyId);
            }
        }
    };
    docProducer.persistentWorkIdProvider = new PersistentWorkIdProviderBean() {

        @Override
        public String persistentWorkIdFor(String corepoWorkId) {
            return "xxx";
        }
    };
    ProfileProviderBean profileProviderBean = new ProfileProviderBean();
    profileProviderBean.config = config;
    profileProviderBean.vipCoreHttpClient = new VipCoreHttpClient();
    docProducer.profileProvider = profileProviderBean;
    docProducer.init();
    worker.init();
    try (Connection connection = dataSource.getConnection();
        Statement stmt = connection.createStatement()) {
        stmt.executeUpdate("TRUNCATE queue");
        stmt.executeUpdate("TRUNCATE queue_error");
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) VipCoreLibraryRule(dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule) SolrClient(org.apache.solr.client.solrj.SolrClient) VipCoreHttpClient(dk.dbc.openagency.http.VipCoreHttpClient) Before(org.junit.Before)

Example 4 with VipCoreLibraryRule

use of dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule in project solr-document-store by DBCDK.

the class LibraryRuleProviderBean method libraryRulesFor.

@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@CacheResult(cacheName = "vipLibraryRules", exceptionCacheName = "vipLibraryRulesError", cachedExceptions = { IllegalStateException.class, ClientErrorException.class, ServerErrorException.class })
public VipCoreLibraryRule libraryRulesFor(String agencyId) {
    try {
        String vipCoreResponse = vipCoreHttpClient.getFromVipCore(config.getVipCoreEndpoint(), VipCoreHttpClient.LIBRARY_RULES_PATH + "/" + agencyId);
        if (vipCoreResponse.startsWith("{")) {
            log.debug("vipCoreResponse was: {}", vipCoreResponse);
            LibraryRulesResponse libraryRulesResponse = O.readValue(vipCoreResponse, LibraryRulesResponse.class);
            if (libraryRulesResponse.getError() != null)
                throw new IllegalStateException("library rules: " + libraryRulesResponse.getError().value());
            return new VipCoreLibraryRule(libraryRulesResponse);
        } else {
            throw new ClientErrorException("Vipcore said: " + vipCoreResponse.substring(Integer.max(0, vipCoreResponse.length() - 50)), Response.Status.BAD_REQUEST);
        }
    } catch (OpenAgencyException ex) {
        log.error("Error when fetching libraryrules for agencyId {}", agencyId);
        throw new IllegalStateException("library rules: " + ex.getMessage());
    } catch (JsonProcessingException ex) {
        log.error("Error when processing response from VipCore libraryrules for agency {}: {}", agencyId, ex.getMessage());
        log.debug("Error when processing response from VipCore libraryrules for agency {}: {}", agencyId, ex);
        throw new IllegalStateException("library rules: " + ex.getMessage());
    }
}
Also used : ClientErrorException(javax.ws.rs.ClientErrorException) LibraryRulesResponse(dk.dbc.vipcore.marshallers.LibraryRulesResponse) OpenAgencyException(dk.dbc.openagency.http.OpenAgencyException) VipCoreLibraryRule(dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TransactionAttribute(javax.ejb.TransactionAttribute) CacheResult(javax.cache.annotation.CacheResult)

Aggregations

VipCoreLibraryRule (dk.dbc.solrdocstore.updater.businesslogic.VipCoreLibraryRule)4 VipCoreHttpClient (dk.dbc.openagency.http.VipCoreHttpClient)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 SolrClient (org.apache.solr.client.solrj.SolrClient)2 Before (org.junit.Before)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 OpenAgencyException (dk.dbc.openagency.http.OpenAgencyException)1 QueueJob (dk.dbc.search.solrdocstore.queue.QueueJob)1 FeatureSwitch (dk.dbc.solrdocstore.updater.businesslogic.FeatureSwitch)1 KnownSolrFields (dk.dbc.solrdocstore.updater.businesslogic.KnownSolrFields)1 SolrDocStoreResponse (dk.dbc.solrdocstore.updater.businesslogic.SolrDocStoreResponse)1 VipCoreProfile (dk.dbc.solrdocstore.updater.businesslogic.VipCoreProfile)1 LibraryRulesResponse (dk.dbc.vipcore.marshallers.LibraryRulesResponse)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CacheResult (javax.cache.annotation.CacheResult)1 TransactionAttribute (javax.ejb.TransactionAttribute)1 ClientErrorException (javax.ws.rs.ClientErrorException)1