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;
}
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;
}
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();
}
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());
}
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());
}
Aggregations