Search in sources :

Example 1 with LibraryRules

use of dk.dbc.vipcore.marshallers.LibraryRules in project dataio by DBCDK.

the class LHRRetriever method getLibraryRules.

private AddiMetaData.LibraryRules getLibraryRules(long agencyId) throws VipCoreException {
    if (libraryRulesCache.containsKey(agencyId)) {
        return libraryRulesCache.get(agencyId);
    }
    final LibraryRules libraryRules = vipCoreLibraryRulesConnector.getLibraryRulesByAgencyId(Long.toString(agencyId));
    final List<LibraryRule> libraryRuleList = libraryRules.getLibraryRule();
    final AddiMetaData.LibraryRules metadataRules = new AddiMetaData.LibraryRules();
    libraryRuleList.stream().filter(entry -> entry.getBool() != null).forEach(entry -> metadataRules.withLibraryRule(entry.getName(), entry.getBool()));
    metadataRules.withAgencyType(libraryRules.getAgencyType());
    libraryRulesCache.put(agencyId, metadataRules);
    return metadataRules;
}
Also used : VipCoreLibraryRulesConnector(dk.dbc.vipcore.libraryrules.VipCoreLibraryRulesConnector) BufferedInputStream(java.io.BufferedInputStream) MarcReaderException(dk.dbc.marc.reader.MarcReaderException) PGSimpleDataSource(org.postgresql.ds.PGSimpleDataSource) ArgParseException(dk.dbc.dataio.cli.lhrretriever.arguments.ArgParseException) Base64(org.apache.commons.codec.binary.Base64) Pid(dk.dbc.dataio.commons.types.Pid) ByteArrayInputStream(java.io.ByteArrayInputStream) JavaScript(dk.dbc.dataio.commons.types.JavaScript) Document(org.w3c.dom.Document) QueueException(dk.dbc.rawrepo.queue.QueueException) Map(java.util.Map) MarcXchangeV1Writer(dk.dbc.marc.writer.MarcXchangeV1Writer) HttpClient(dk.dbc.httpclient.HttpClient) Script(dk.dbc.dataio.jobprocessor.javascript.Script) StringUtil(dk.dbc.dataio.commons.utils.lang.StringUtil) VipCoreLibraryRulesConnectorFactory(dk.dbc.vipcore.libraryrules.VipCoreLibraryRulesConnectorFactory) AddiReader(dk.dbc.commons.addi.AddiReader) ConfigParseException(dk.dbc.dataio.cli.lhrretriever.config.ConfigParseException) FlowStoreServiceConnector(dk.dbc.dataio.common.utils.flowstore.FlowStoreServiceConnector) LibraryRule(dk.dbc.vipcore.marshallers.LibraryRule) JacksonFeature(org.glassfish.jersey.jackson.JacksonFeature) ConfigurationException(dk.dbc.rawrepo.queue.ConfigurationException) Collection(java.util.Collection) MarcRecord(dk.dbc.marc.binding.MarcRecord) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Stream(java.util.stream.Stream) SAXException(org.xml.sax.SAXException) RecordDTO(dk.dbc.rawrepo.dto.RecordDTO) JSONBException(dk.dbc.commons.jsonb.JSONBException) LibraryRules(dk.dbc.vipcore.marshallers.LibraryRules) ConfigJson(dk.dbc.dataio.cli.lhrretriever.config.ConfigJson) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) HashMap(java.util.HashMap) Arguments(dk.dbc.dataio.cli.lhrretriever.arguments.Arguments) RecordServiceConnectorFactory(dk.dbc.rawrepo.record.RecordServiceConnectorFactory) ArrayList(java.util.ArrayList) AddiRecord(dk.dbc.commons.addi.AddiRecord) AddiMetaData(dk.dbc.dataio.commons.types.AddiMetaData) SQLException(java.sql.SQLException) Charset(java.nio.charset.Charset) Flow(dk.dbc.dataio.commons.types.Flow) MarcXchangeV1Reader(dk.dbc.marc.reader.MarcXchangeV1Reader) FlowComponentContent(dk.dbc.dataio.commons.types.FlowComponentContent) StringSourceSchemeHandler(dk.dbc.dataio.jobprocessor.javascript.StringSourceSchemeHandler) FlowComponent(dk.dbc.dataio.commons.types.FlowComponent) DataSource(javax.sql.DataSource) RecordServiceConnector(dk.dbc.rawrepo.record.RecordServiceConnector) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) File(java.io.File) JSONBContext(dk.dbc.commons.jsonb.JSONBContext) RecordIdDTO(dk.dbc.rawrepo.dto.RecordIdDTO) JaxpUtil(dk.dbc.dataio.commons.utils.lang.JaxpUtil) RawRepoConnector(dk.dbc.dataio.harvester.utils.rawrepo.RawRepoConnector) BufferedReader(java.io.BufferedReader) VipCoreException(dk.dbc.vipcore.exception.VipCoreException) InputStream(java.io.InputStream) LibraryRules(dk.dbc.vipcore.marshallers.LibraryRules) LibraryRule(dk.dbc.vipcore.marshallers.LibraryRule) AddiMetaData(dk.dbc.dataio.commons.types.AddiMetaData)

Example 2 with LibraryRules

use of dk.dbc.vipcore.marshallers.LibraryRules in project dataio by DBCDK.

the class VipCoreConnectionTest method getLibraryRules_connectorReturnsNonEmptyResponse_returnsLibraryRules.

@Test
public void getLibraryRules_connectorReturnsNonEmptyResponse_returnsLibraryRules() throws VipCoreException {
    final LibraryRule firstLibraryRule = new LibraryRule();
    firstLibraryRule.setName("1st rule");
    firstLibraryRule.setBool(true);
    final LibraryRule secondLibraryRule = new LibraryRule();
    secondLibraryRule.setName("2nd rule");
    secondLibraryRule.setBool(false);
    final LibraryRule thirdLibraryRule = new LibraryRule();
    thirdLibraryRule.setName("3rd rule");
    thirdLibraryRule.setString("value");
    final LibraryRules libraryRules = new LibraryRules();
    libraryRules.setAgencyType("myType");
    libraryRules.setLibraryRule(Arrays.asList(firstLibraryRule, secondLibraryRule, thirdLibraryRule));
    final AddiMetaData.LibraryRules expectedLibraryRules = new AddiMetaData.LibraryRules().withAgencyType(libraryRules.getAgencyType()).withLibraryRule(firstLibraryRule.getName(), firstLibraryRule.getBool()).withLibraryRule(secondLibraryRule.getName(), secondLibraryRule.getBool()).withLibraryRule(thirdLibraryRule.getName(), thirdLibraryRule.getString());
    when(connector.getLibraryRulesByAgencyId(anyString(), anyString())).thenReturn(libraryRules);
    assertThat(newVipCoreConnection().getLibraryRules(123456, "test"), is(expectedLibraryRules));
}
Also used : LibraryRules(dk.dbc.vipcore.marshallers.LibraryRules) LibraryRule(dk.dbc.vipcore.marshallers.LibraryRule) AddiMetaData(dk.dbc.dataio.commons.types.AddiMetaData) Test(org.junit.Test)

Example 3 with LibraryRules

use of dk.dbc.vipcore.marshallers.LibraryRules in project dataio by DBCDK.

the class VipCoreConnectionTest method getLibraryRules_connectorReturnsNonEmptyResponse_cachesLibraryRules.

@Test
public void getLibraryRules_connectorReturnsNonEmptyResponse_cachesLibraryRules() throws VipCoreException {
    final LibraryRules emptyLibraryRules = new LibraryRules();
    emptyLibraryRules.setLibraryRule(new ArrayList<>());
    when(connector.getLibraryRulesByAgencyId("123456", null)).thenReturn(emptyLibraryRules);
    final VipCoreConnection vipCoreConnection = newVipCoreConnection();
    assertSame(vipCoreConnection.getLibraryRules(123456, null), vipCoreConnection.getLibraryRules(123456, null));
    verify(connector, times(1)).getLibraryRulesByAgencyId("123456", null);
}
Also used : LibraryRules(dk.dbc.vipcore.marshallers.LibraryRules) Test(org.junit.Test)

Example 4 with LibraryRules

use of dk.dbc.vipcore.marshallers.LibraryRules in project solr-document-store by DBCDK.

the class OpenAgencyProxyBean method loadOpenAgencyEntry.

@Timed
public OpenAgencyEntity loadOpenAgencyEntry(int agencyId) {
    try {
        LibraryRulesResponse libraryRulesResponse = getLibraryRuleResponse(agencyId);
        if (libraryRulesResponse.getError() != null) {
            return new OpenAgencyEntity(agencyId, LibraryType.Missing, false, false, false);
        }
        final List<LibraryRules> libraryRuleList = libraryRulesResponse.getLibraryRules();
        if (libraryRuleList == null || libraryRuleList.isEmpty()) {
            return null;
        } else {
            return new OpenAgencyEntity(libraryRuleList.get(0));
        }
    } catch (JsonProcessingException e) {
        log.error("Unable to unmarshall response from vipCore from agency {}, error: {}", agencyId, e.getMessage());
        log.debug("Unable to unmarshall response from vipCore from agency {}, error: {}", agencyId, e);
        throw new EJBException(e);
    } catch (RuntimeException | OpenAgencyException | IOException e) {
        log.error("Error happened while fetching vipCore library rules for agency {}: {}", agencyId, e.getMessage());
        throw new EJBException(e);
    }
}
Also used : LibraryRules(dk.dbc.vipcore.marshallers.LibraryRules) OpenAgencyEntity(dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity) LibraryRulesResponse(dk.dbc.vipcore.marshallers.LibraryRulesResponse) OpenAgencyException(dk.dbc.openagency.http.OpenAgencyException) IOException(java.io.IOException) EJBException(javax.ejb.EJBException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Timed(org.eclipse.microprofile.metrics.annotation.Timed)

Example 5 with LibraryRules

use of dk.dbc.vipcore.marshallers.LibraryRules in project dataio by DBCDK.

the class VipCoreConnection method getLibraryRules.

/**
 * Retrieves library rules for given agency ID.
 * <p>
 * All non-null rule sets are cached. All subsequent requests for the same agency ID
 * will return the result from the cache.
 * </p>
 *
 * @param agencyId   agency ID
 * @param trackingId tracking ID used in web-service call, can be null
 * @return library rules or null if no rules could be found.
 * @throws IllegalStateException on error communicating with the vipcore web-service
 */
public AddiMetaData.LibraryRules getLibraryRules(int agencyId, String trackingId) throws IllegalStateException {
    return libraryRulesCache.computeIfAbsent(agencyId, id -> {
        try {
            final LibraryRules libraryRules = connector.getLibraryRulesByAgencyId(Integer.toString(agencyId), trackingId);
            final AddiMetaData.LibraryRules addiMetaDatalibraryRules = new AddiMetaData.LibraryRules();
            libraryRules.getLibraryRule().forEach(entry -> addiMetaDatalibraryRules.withLibraryRule(entry.getName(), entry.getBool() != null ? entry.getBool() : entry.getString()));
            return addiMetaDatalibraryRules.withAgencyType(libraryRules.getAgencyType());
        } catch (AgencyNotFoundException e) {
            return null;
        } catch (VipCoreException e) {
            throw new IllegalStateException("Error while looking up library rules for agency ID " + agencyId, e);
        }
    });
}
Also used : AgencyNotFoundException(dk.dbc.vipcore.exception.AgencyNotFoundException) LibraryRules(dk.dbc.vipcore.marshallers.LibraryRules) VipCoreException(dk.dbc.vipcore.exception.VipCoreException) AddiMetaData(dk.dbc.dataio.commons.types.AddiMetaData)

Aggregations

LibraryRules (dk.dbc.vipcore.marshallers.LibraryRules)5 AddiMetaData (dk.dbc.dataio.commons.types.AddiMetaData)3 VipCoreException (dk.dbc.vipcore.exception.VipCoreException)2 LibraryRule (dk.dbc.vipcore.marshallers.LibraryRule)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 AddiReader (dk.dbc.commons.addi.AddiReader)1 AddiRecord (dk.dbc.commons.addi.AddiRecord)1 JSONBContext (dk.dbc.commons.jsonb.JSONBContext)1 JSONBException (dk.dbc.commons.jsonb.JSONBException)1 ArgParseException (dk.dbc.dataio.cli.lhrretriever.arguments.ArgParseException)1 Arguments (dk.dbc.dataio.cli.lhrretriever.arguments.Arguments)1 ConfigJson (dk.dbc.dataio.cli.lhrretriever.config.ConfigJson)1 ConfigParseException (dk.dbc.dataio.cli.lhrretriever.config.ConfigParseException)1 FlowStoreServiceConnector (dk.dbc.dataio.common.utils.flowstore.FlowStoreServiceConnector)1 Flow (dk.dbc.dataio.commons.types.Flow)1 FlowComponent (dk.dbc.dataio.commons.types.FlowComponent)1 FlowComponentContent (dk.dbc.dataio.commons.types.FlowComponentContent)1 JavaScript (dk.dbc.dataio.commons.types.JavaScript)1 Pid (dk.dbc.dataio.commons.types.Pid)1