Search in sources :

Example 1 with HoldingsItemEntity

use of dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity in project solr-document-store by DBCDK.

the class DocumentRetrieveBean method getDocumentsForUnit.

public List<DocumentRetrieveResponse> getDocumentsForUnit(String unitId, boolean includeHoldingsItemsIndexKeys) throws Exception {
    List<DocumentRetrieveResponse> res = new ArrayList<>();
    List<BibliographicEntity> bibliographicEntities = BibliographicEntity.fetchByUnit(entityManager, unitId);
    List<HoldingsInfo> holdingsObjs = entityManager.createQuery(SELECT_HOLDINGS_ITEMS_FOR_UNIT_JPA, HoldingsInfo.class).setParameter("unitId", unitId).getResultList();
    for (BibliographicEntity b : bibliographicEntities) {
        List<HoldingsItemEntity> holdingsItemEntityList = holdingsObjs.stream().filter(ho -> ho.holdingsToBibliographicEntity.getBibliographicAgencyId() == b.getAgencyId() && ho.holdingsToBibliographicEntity.getBibliographicRecordId().equals(b.getBibliographicRecordId())).map(h -> h.holdingsItemEntity).map(h -> includeHoldingsItemsIndexKeys ? h : h.copyForLightweightPresentation()).collect(Collectors.toList());
        List<Integer> partOfDanbib = b.getAgencyId() == LibraryType.COMMON_AGENCY ? getPartOfDanbibCommon(b.getBibliographicRecordId()) : Collections.EMPTY_LIST;
        LibraryType lt = oaBean.lookup(b.getAgencyId()).getLibraryType();
        List<BibliographicResourceEntity> resources = agencyLibTypeCommon(b.getAgencyId(), lt) ? brrBean.getResourcesForCommon(b.getBibliographicRecordId()) : brrBean.getResourcesFor(b.getAgencyId(), b.getBibliographicRecordId());
        Map<String, Map<Integer, Boolean>> attachedResources = mapResources(resources);
        DocumentRetrieveResponse r = new DocumentRetrieveResponse(b, holdingsItemEntityList, partOfDanbib, attachedResources);
        res.add(r);
    }
    return res;
}
Also used : DocumentRetrieveResponse(dk.dbc.search.solrdocstore.response.DocumentRetrieveResponse) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) BibliographicResourceEntity(dk.dbc.search.solrdocstore.jpa.BibliographicResourceEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) HoldingsInfo(dk.dbc.search.solrdocstore.response.HoldingsInfo) QueryParam(javax.ws.rs.QueryParam) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) Timed(org.eclipse.microprofile.metrics.annotation.Timed) UnitRetrieveResponse(dk.dbc.search.solrdocstore.response.UnitRetrieveResponse) LogWith.track(dk.dbc.log.LogWith.track) OpenAgencyEntity(dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity) DocumentRetrieveResponse(dk.dbc.search.solrdocstore.response.DocumentRetrieveResponse) LogWith(dk.dbc.log.LogWith) Stateless(javax.ejb.Stateless) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) HoldingsItemEntity(dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity) LibraryType(dk.dbc.search.solrdocstore.jpa.LibraryType) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) Collectors(java.util.stream.Collectors) AgencyClassifierItemKey(dk.dbc.search.solrdocstore.jpa.AgencyClassifierItemKey) List(java.util.List) Response(javax.ws.rs.core.Response) UriInfo(javax.ws.rs.core.UriInfo) Collections(java.util.Collections) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity) WorkRetrieveResponse(dk.dbc.search.solrdocstore.response.WorkRetrieveResponse) HoldingsInfo(dk.dbc.search.solrdocstore.response.HoldingsInfo) LibraryType(dk.dbc.search.solrdocstore.jpa.LibraryType) ArrayList(java.util.ArrayList) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity) BibliographicResourceEntity(dk.dbc.search.solrdocstore.jpa.BibliographicResourceEntity) HoldingsItemEntity(dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with HoldingsItemEntity

use of dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity in project solr-document-store by DBCDK.

the class DocumentRetrieveBean method getDocumentsForWork.

public List<DocumentRetrieveResponse> getDocumentsForWork(String workId, boolean includeHoldingsItemsIndexKeys) throws Exception {
    List<DocumentRetrieveResponse> res = new ArrayList<>();
    List<BibliographicEntity> bibliographicEntities = BibliographicEntity.fetchByWork(entityManager, workId);
    List<HoldingsInfo> holdingsObjs = entityManager.createQuery(SELECT_HOLDINGS_ITEMS_FOR_WORK_JPA, HoldingsInfo.class).setParameter("workId", workId).getResultList();
    for (BibliographicEntity b : bibliographicEntities) {
        List<HoldingsItemEntity> holdingsItemEntityList = holdingsObjs.stream().filter(ho -> ho.holdingsToBibliographicEntity.getBibliographicAgencyId() == b.getAgencyId() && ho.holdingsToBibliographicEntity.getBibliographicRecordId().equals(b.getBibliographicRecordId())).map(h -> h.holdingsItemEntity).map(h -> includeHoldingsItemsIndexKeys ? h : h.copyForLightweightPresentation()).collect(Collectors.toList());
        List<Integer> partOfDanbib = b.getAgencyId() == LibraryType.COMMON_AGENCY ? getPartOfDanbibCommon(b.getBibliographicRecordId()) : Collections.EMPTY_LIST;
        LibraryType lt = oaBean.lookup(b.getAgencyId()).getLibraryType();
        List<BibliographicResourceEntity> resources = agencyLibTypeCommon(b.getAgencyId(), lt) ? brrBean.getResourcesForCommon(b.getBibliographicRecordId()) : brrBean.getResourcesFor(b.getAgencyId(), b.getBibliographicRecordId());
        Map<String, Map<Integer, Boolean>> attachedResources = mapResources(resources);
        DocumentRetrieveResponse r = new DocumentRetrieveResponse(b, holdingsItemEntityList, partOfDanbib, attachedResources);
        res.add(r);
    }
    return res;
}
Also used : DocumentRetrieveResponse(dk.dbc.search.solrdocstore.response.DocumentRetrieveResponse) PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) BibliographicResourceEntity(dk.dbc.search.solrdocstore.jpa.BibliographicResourceEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) HoldingsInfo(dk.dbc.search.solrdocstore.response.HoldingsInfo) QueryParam(javax.ws.rs.QueryParam) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) Timed(org.eclipse.microprofile.metrics.annotation.Timed) UnitRetrieveResponse(dk.dbc.search.solrdocstore.response.UnitRetrieveResponse) LogWith.track(dk.dbc.log.LogWith.track) OpenAgencyEntity(dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity) DocumentRetrieveResponse(dk.dbc.search.solrdocstore.response.DocumentRetrieveResponse) LogWith(dk.dbc.log.LogWith) Stateless(javax.ejb.Stateless) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) HoldingsItemEntity(dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity) LibraryType(dk.dbc.search.solrdocstore.jpa.LibraryType) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) Collectors(java.util.stream.Collectors) AgencyClassifierItemKey(dk.dbc.search.solrdocstore.jpa.AgencyClassifierItemKey) List(java.util.List) Response(javax.ws.rs.core.Response) UriInfo(javax.ws.rs.core.UriInfo) Collections(java.util.Collections) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity) WorkRetrieveResponse(dk.dbc.search.solrdocstore.response.WorkRetrieveResponse) HoldingsInfo(dk.dbc.search.solrdocstore.response.HoldingsInfo) LibraryType(dk.dbc.search.solrdocstore.jpa.LibraryType) ArrayList(java.util.ArrayList) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity) BibliographicResourceEntity(dk.dbc.search.solrdocstore.jpa.BibliographicResourceEntity) HoldingsItemEntity(dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with HoldingsItemEntity

use of dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity in project solr-document-store by DBCDK.

the class HoldingsItemBean method setHoldingsKeys.

public void setHoldingsKeys(HoldingsItemEntity hi) throws SQLException {
    EnqueueCollector enqueue = enqueueSupplier.getEnqueueCollector();
    List<HoldingsItemEntity> his = entityManager.createQuery("SELECT h FROM HoldingsItemEntity h WHERE h.bibliographicRecordId = :bibId and h.agencyId = :agency", HoldingsItemEntity.class).setParameter("agency", hi.getAgencyId()).setParameter("bibId", hi.getBibliographicRecordId()).getResultList();
    boolean hadLiveHoldings = !his.isEmpty();
    boolean hasLiveHoldings = !hi.getIndexKeys().isEmpty();
    Set<String> oldLocations = his.isEmpty() ? EMPTY_SET : his.get(0).getLocations();
    log.info("Updating holdings for {}:{}", hi.getAgencyId(), hi.getBibliographicRecordId());
    if (!hadLiveHoldings && !hasLiveHoldings) {
        // No holdings before or now
        log.debug("No holdings before or now");
    } else if (hadLiveHoldings != hasLiveHoldings && hasLiveHoldings) {
        // holdings existence change -> exists
        entityManager.merge(hi);
        h2bBean.tryToAttachToBibliographicRecord(hi.getAgencyId(), hi.getBibliographicRecordId(), enqueue, QueueType.HOLDING, QueueType.UNIT, QueueType.WORK, QueueType.MAJORHOLDING, QueueType.UNITMAJORHOLDING, QueueType.FIRSTLASTHOLDING, QueueType.UNITFIRSTLASTHOLDING, QueueType.MAJORHOLDING, QueueType.WORKMAJORHOLDING, QueueType.FIRSTLASTHOLDING, QueueType.WORKFIRSTLASTHOLDING);
    } else if (hadLiveHoldings != hasLiveHoldings && hadLiveHoldings) {
        // holdings existence change -> none
        // Remove existing
        entityManager.remove(his.get(0));
        HoldingsToBibliographicKey key = new HoldingsToBibliographicKey(hi.getAgencyId(), hi.getBibliographicRecordId());
        HoldingsToBibliographicEntity binding = entityManager.find(HoldingsToBibliographicEntity.class, key);
        if (binding != null) {
            entityManager.remove(binding);
            queueRelatedBibliographic(binding, enqueue, QueueType.HOLDING, QueueType.UNIT, QueueType.WORK, QueueType.MAJORHOLDING, QueueType.UNITMAJORHOLDING, QueueType.FIRSTLASTHOLDING, QueueType.UNITFIRSTLASTHOLDING, QueueType.MAJORHOLDING, QueueType.WORKMAJORHOLDING, QueueType.FIRSTLASTHOLDING, QueueType.WORKFIRSTLASTHOLDING);
        }
    } else if (!oldLocations.equals(hi.getLocations())) {
        // holdings accessibility change
        entityManager.merge(hi);
        queueRelatedBibliographic(hi, enqueue, QueueType.HOLDING, QueueType.UNIT, QueueType.WORK, QueueType.MAJORHOLDING, QueueType.UNITMAJORHOLDING, QueueType.MAJORHOLDING, QueueType.WORKMAJORHOLDING);
    } else {
        entityManager.merge(hi);
        queueRelatedBibliographic(hi, enqueue, QueueType.HOLDING, QueueType.UNIT, QueueType.WORK);
    }
    enqueue.commit();
}
Also used : HoldingsToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity) HoldingsItemEntity(dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity) EnqueueCollector(dk.dbc.search.solrdocstore.enqueue.EnqueueCollector) HoldingsToBibliographicKey(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicKey)

Example 4 with HoldingsItemEntity

use of dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity in project solr-document-store by DBCDK.

the class HoldingsItemBeanIT method testFirstLast.

@Test(timeout = 2_000L)
public void testFirstLast() throws Exception {
    System.out.println("testFirstLast");
    JpaTestEnvironment env = env();
    EntityManager em = env.getEntityManager();
    BibliographicBean bib = createBibliographicBean(env, null);
    HoldingsItemBean hol = holdingsItemBeanWithRules(env, new QueueRuleEntity("a", QueueType.FIRSTLASTHOLDING, 0), new QueueRuleEntity("b", QueueType.UNITFIRSTLASTHOLDING, 0), new QueueRuleEntity("c", QueueType.WORKFIRSTLASTHOLDING, 0));
    env().getPersistenceContext().run(() -> {
        bib.addBibliographicKeys(true, jsonRequestBibl("870970-25912233", Instant.now()));
    });
    queueContentAndClear();
    // From non existing to live holdings
    env().getPersistenceContext().run(() -> {
        hol.setHoldingsKeys(jsonRequestHold("710100-25912233-a"));
    });
    assertThat(queueContentAndClear(), containsInAnyOrder("a,870970-basis:25912233", "b,unit:1", "c,work:1"));
    HoldingsToBibliographicEntity htobA = env().getPersistenceContext().run(() -> {
        HoldingsToBibliographicKey key = new HoldingsToBibliographicKey(710100, "25912233");
        return em.find(HoldingsToBibliographicEntity.class, key);
    });
    assertThat(htobA, notNullValue());
    // From live holdings to live holdings
    env().getPersistenceContext().run(() -> {
        hol.setHoldingsKeys(jsonRequestHold("710100-25912233-a"));
    });
    assertThat(queueContentAndClear(), empty());
    // From live holdings to no holdings
    env().getPersistenceContext().run(() -> {
        hol.setHoldingsKeys(jsonRequestHold("710100-25912233-d"));
    });
    assertThat(queueContentAndClear(), containsInAnyOrder("a,870970-basis:25912233", "b,unit:1", "c,work:1"));
    // Check that no h2b relation exists when no live holdings are present
    HoldingsToBibliographicEntity htobD = env().getPersistenceContext().run(() -> {
        HoldingsToBibliographicKey key = new HoldingsToBibliographicKey(710100, "25912233");
        return em.find(HoldingsToBibliographicEntity.class, key);
    });
    assertThat(htobD, nullValue());
    HoldingsItemEntity hi = env().getPersistenceContext().run(() -> {
        AgencyItemKey key = new AgencyItemKey(710100, "25912233");
        return em.find(HoldingsItemEntity.class, key);
    });
    assertThat(hi, nullValue());
}
Also used : JpaTestEnvironment(dk.dbc.commons.persistence.JpaTestEnvironment) AgencyItemKey(dk.dbc.search.solrdocstore.jpa.AgencyItemKey) EntityManager(javax.persistence.EntityManager) HoldingsToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity) QueueRuleEntity(dk.dbc.search.solrdocstore.jpa.QueueRuleEntity) HoldingsItemEntity(dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity) HoldingsToBibliographicKey(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicKey) Test(org.junit.Test)

Example 5 with HoldingsItemEntity

use of dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity in project solr-document-store by DBCDK.

the class HoldingsItemBeanIT method testFirstLastNoneToNone.

@Test(timeout = 2_000L)
public void testFirstLastNoneToNone() throws Exception {
    System.out.println("testFirstLastNoneToNone");
    JpaTestEnvironment env = env();
    EntityManager em = env.getEntityManager();
    BibliographicBean bib = createBibliographicBean(env, null);
    HoldingsItemBean hol = holdingsItemBeanWithRules(env, new QueueRuleEntity("a", QueueType.FIRSTLASTHOLDING, 0), new QueueRuleEntity("b", QueueType.UNITFIRSTLASTHOLDING, 0), new QueueRuleEntity("c", QueueType.WORKFIRSTLASTHOLDING, 0));
    env().getPersistenceContext().run(() -> {
        bib.addBibliographicKeys(true, jsonRequestBibl("870970-25912233", Instant.now()));
    });
    queueContentAndClear();
    // From non existing to no holdings
    env().getPersistenceContext().run(() -> {
        hol.setHoldingsKeys(jsonRequestHold("710100-25912233-d"));
    });
    assertThat(queueContentAndClear(), empty());
    // Check that no h2b relation exists when no live holdings are present
    HoldingsToBibliographicEntity htob = env().getPersistenceContext().run(() -> {
        HoldingsToBibliographicKey key = new HoldingsToBibliographicKey(710100, "25912233");
        return em.find(HoldingsToBibliographicEntity.class, key);
    });
    assertThat(htob, nullValue());
    // and no holdings
    HoldingsItemEntity hi = env().getPersistenceContext().run(() -> {
        AgencyItemKey key = new AgencyItemKey(710100, "25912233");
        return em.find(HoldingsItemEntity.class, key);
    });
    assertThat(hi, nullValue());
    // From no holdings to no holdings
    env().getPersistenceContext().run(() -> {
        hol.setHoldingsKeys(jsonRequestHold("710100-25912233-d"));
    });
    assertThat(queueContentAndClear(), empty());
}
Also used : JpaTestEnvironment(dk.dbc.commons.persistence.JpaTestEnvironment) AgencyItemKey(dk.dbc.search.solrdocstore.jpa.AgencyItemKey) EntityManager(javax.persistence.EntityManager) HoldingsToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity) QueueRuleEntity(dk.dbc.search.solrdocstore.jpa.QueueRuleEntity) HoldingsItemEntity(dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity) HoldingsToBibliographicKey(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicKey) Test(org.junit.Test)

Aggregations

HoldingsItemEntity (dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity)22 Test (org.junit.Test)14 HoldingsToBibliographicEntity (dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity)8 BibliographicEntity (dk.dbc.search.solrdocstore.jpa.BibliographicEntity)7 EntityManager (javax.persistence.EntityManager)6 AgencyItemKey (dk.dbc.search.solrdocstore.jpa.AgencyItemKey)5 HoldingsToBibliographicKey (dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicKey)5 DocumentRetrieveResponse (dk.dbc.search.solrdocstore.response.DocumentRetrieveResponse)5 Response (javax.ws.rs.core.Response)5 IndexKeys (dk.dbc.search.solrdocstore.jpa.IndexKeys)4 OpenAgencyEntity (dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 Timed (org.eclipse.microprofile.metrics.annotation.Timed)4 LogWith (dk.dbc.log.LogWith)3 AgencyClassifierItemKey (dk.dbc.search.solrdocstore.jpa.AgencyClassifierItemKey)3 BibliographicResourceEntity (dk.dbc.search.solrdocstore.jpa.BibliographicResourceEntity)3 LibraryType (dk.dbc.search.solrdocstore.jpa.LibraryType)3 HashMap (java.util.HashMap)3