use of dk.dbc.search.solrdocstore.jpa.BibliographicEntity 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.BibliographicEntity in project solr-document-store by DBCDK.
the class ResourceBean method storeResource.
private Response storeResource(BibliographicResourceEntity resource) {
// Verify agency exists, throws exception if not exist
LibraryType libraryType;
try {
OpenAgencyEntity oaEntity = openAgency.lookup(resource.getAgencyId());
libraryType = oaEntity.getLibraryType();
} catch (EJBException ex) {
return Response.ok().entity(new StatusResponse("Unknown agency")).build();
}
if (resource.getValue()) {
entityManager.merge(resource);
} else {
BibliographicResourceEntity entity = entityManager.find(BibliographicResourceEntity.class, new AgencyItemFieldKey(resource.getAgencyId(), resource.getBibliographicRecordId(), resource.getField()));
if (entity != null)
entityManager.remove(entity);
}
// Enqueue all related bib items
List<BibliographicEntity> bibliographicEntities;
if (LibraryType.COMMON_AGENCY == resource.getAgencyId() || LibraryType.SCHOOL_COMMON_AGENCY == resource.getAgencyId() || libraryType == LibraryType.FBS || libraryType == LibraryType.FBSSchool) {
bibliographicEntities = commonRelatedBibEntities(resource);
} else {
bibliographicEntities = nonFBSBibEntries(resource);
}
try {
EnqueueCollector enqueue = enqueueSupplier.getEnqueueCollector();
bibliographicEntities.forEach(e -> {
if (!e.isDeleted()) {
enqueue.add(e, QueueType.RESOURCE, QueueType.WORKRESOURCE);
}
});
enqueue.commit();
} catch (SQLException ex) {
log.error("Unable to commit queue entries: {}", ex.getMessage());
log.debug("Unable to commit queue entries: ", ex);
return Response.status(Response.Status.BAD_REQUEST).entity(new StatusResponse("Unable to commit queue entries")).build();
}
return Response.ok().entity(new StatusResponse()).build();
}
use of dk.dbc.search.solrdocstore.jpa.BibliographicEntity in project solr-document-store by DBCDK.
the class BibliographicBean method addBibliographicKeys.
void addBibliographicKeys(BibliographicEntity bibliographicEntity, List<String> supersedes, boolean queueAll) throws SQLException {
EnqueueCollector enqueue = queueAll ? enqueueSupplier.getEnqueueCollector() : EnqueueCollector.VOID;
if (bibliographicEntity.getClassifier() == null) {
throw new IllegalStateException("classifier is not set");
}
log.info("AddBibliographicKeys called {}-{}:{}", bibliographicEntity.getAgencyId(), bibliographicEntity.getClassifier(), bibliographicEntity.getBibliographicRecordId());
BibliographicEntity dbbe = entityManager.find(BibliographicEntity.class, bibliographicEntity.asAgencyClassifierItemKey(), LockModeType.PESSIMISTIC_WRITE);
if (dbbe == null) {
if (!queueAll && !bibliographicEntity.isDeleted()) {
// A new record, should not respect the skipQueue flag
enqueue = enqueueSupplier.getEnqueueCollector();
}
// Going from non existing to deleted shouldn't result in queue jobs
if (!bibliographicEntity.isDeleted()) {
enqueue.add(bibliographicEntity, QueueType.MANIFESTATION, QueueType.WORK);
}
entityManager.merge(bibliographicEntity.asBibliographicEntity());
if (!bibliographicEntity.isDeleted()) {
// Only update holdings for this, if manifestation isn't deleted
updateHoldingsToBibliographic(bibliographicEntity.getAgencyId(), bibliographicEntity.getBibliographicRecordId(), enqueue);
}
// Record creates queue even id said not to
} else {
log.info("AddBibliographicKeys - Updating existing entity");
Instant dbTime = extractFedoraStreamDate(dbbe);
Instant reqTime = extractFedoraStreamDate(bibliographicEntity);
if (reqTime != null && dbTime != null && dbTime.isAfter(reqTime)) {
if (dbbe.isDeleted() && dbTime.isBefore(Instant.now().minusMillis(config.getReviveOlderWhenDeletedForAtleast()))) {
log.warn("Updating to an older stream date: pid = {}-{}:{}, request.repositoryId = {}, database.repositoryId = {}, database.time = {}, request.time = {}", bibliographicEntity.getAgencyId(), bibliographicEntity.getClassifier(), bibliographicEntity.getBibliographicRecordId(), bibliographicEntity.getRepositoryId(), dbbe.getRepositoryId(), dbTime, reqTime);
} else {
log.warn("Cannot update to an older stream date: pid = {}-{}:{}, request.repositoryId = {}, database.repositoryId = {}, database.time = {}, request.time = {}", bibliographicEntity.getAgencyId(), bibliographicEntity.getClassifier(), bibliographicEntity.getBibliographicRecordId(), bibliographicEntity.getRepositoryId(), dbbe.getRepositoryId(), dbTime, reqTime);
throw new IntermittentErrorException("Cannot update to an older stream date");
}
}
// If we delete or re-create, related holdings must be moved appropriately
if (bibliographicEntity.isDeleted() != dbbe.isDeleted()) {
if (!queueAll) {
// The record, changed delete status - should not respect the skipQueue flag
enqueue = enqueueSupplier.getEnqueueCollector();
}
// bib entity
if (bibliographicEntity.isDeleted()) {
if (bibliographicEntity.getAgencyId() == LibraryType.COMMON_AGENCY) {
deleteSuperseded(bibliographicEntity.getBibliographicRecordId());
}
enqueue.add(dbbe, QueueType.MANIFESTATION_DELETED, QueueType.WORK);
} else {
enqueue.add(bibliographicEntity, QueueType.MANIFESTATION, QueueType.WORK);
updateHoldingsToBibliographic(bibliographicEntity.getAgencyId(), bibliographicEntity.getBibliographicRecordId(), enqueue);
}
log.info("AddBibliographicKeys - Delete or recreate, going from {} -> {}", dbbe.isDeleted(), bibliographicEntity.isDeleted());
// We must flush since the tryAttach looks at the deleted field
entityManager.merge(bibliographicEntity.asBibliographicEntity());
entityManager.flush();
List<HoldingsToBibliographicEntity> relatedHoldings = bibliographicEntity.isDeleted() ? h2bBean.getRelatedHoldingsToBibliographic(dbbe.getAgencyId(), dbbe.getBibliographicRecordId()) : h2bBean.findRecalcCandidates(dbbe.getBibliographicRecordId());
for (HoldingsToBibliographicEntity relatedHolding : relatedHoldings) {
h2bBean.tryToAttachToBibliographicRecord(relatedHolding.getHoldingsAgencyId(), relatedHolding.getHoldingsBibliographicRecordId(), enqueue, QueueType.MANIFESTATION);
}
} else {
// Going from deleted to deleted shouldn't result in queue jobs
if (!bibliographicEntity.isDeleted()) {
enqueue.add(bibliographicEntity, QueueType.MANIFESTATION, QueueType.WORK);
}
// Simple update
entityManager.merge(bibliographicEntity.asBibliographicEntity());
}
}
if (bibliographicEntity.getAgencyId() == LibraryType.COMMON_AGENCY && !bibliographicEntity.isDeleted()) {
Set<String> supersededRecordIds = updateSuperseded(bibliographicEntity.getBibliographicRecordId(), supersedes);
if (supersededRecordIds.size() > 0) {
h2bBean.recalcAttachments(bibliographicEntity.getBibliographicRecordId(), supersededRecordIds, enqueue, QueueType.MANIFESTATION);
}
}
enqueue.commit();
}
use of dk.dbc.search.solrdocstore.jpa.BibliographicEntity in project solr-document-store by DBCDK.
the class HoldingsToBibliographicBean method attachToAgency.
public void attachToAgency(HoldingsToBibliographicEntity expectedState, EnqueueCollector enqueue, QueueType[] enqueueSources) {
HoldingsToBibliographicEntity foundEntity = entityManager.find(HoldingsToBibliographicEntity.class, expectedState.asKey());
if (foundEntity != null && !foundEntity.equals(expectedState)) {
List<BibliographicEntity> entitiesAffected = brBean.getBibliographicEntities(foundEntity.getBibliographicAgencyId(), foundEntity.getBibliographicRecordId());
entitiesAffected.forEach(e -> enqueue.add(e, enqueueSources));
}
List<BibliographicEntity> entitiesAffected = brBean.getBibliographicEntities(expectedState.getBibliographicAgencyId(), expectedState.getBibliographicRecordId());
entitiesAffected.forEach(e -> enqueue.add(e, enqueueSources));
entityManager.merge(expectedState);
}
use of dk.dbc.search.solrdocstore.jpa.BibliographicEntity in project solr-document-store by DBCDK.
the class QueueBean method queueWork.
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("work/{ workId : work:\\d+ }")
@Timed
@Operation(summary = "Queue a manifestation", description = "This operation puts a work and its manifestations on queue.")
@APIResponses({ @APIResponse(name = "Success", responseCode = "200", description = "The work was found, and put onto the queue", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(ref = StatusResponse.NAME))), @APIResponse(name = "Not Found", responseCode = "404", description = "There's no such work", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(ref = StatusResponse.NAME))) })
@Parameters({ @Parameter(name = "workId", description = "The (corepo-)id of the work", required = true), @Parameter(name = "trackingId", description = "For tracking the request", required = false) })
public Response queueWork(@PathParam("workId") String workId, @QueryParam("trackingId") String trackingId) {
if (trackingId == null || trackingId.isEmpty())
trackingId = UUID.randomUUID().toString();
try (LogWith logWith = LogWith.track(trackingId).pid(workId)) {
List<BibliographicEntity> biblEntitys = BibliographicEntity.fetchByWork(entityManager, workId);
if (biblEntitys.isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).entity(new StatusResponse("No such work")).build();
}
EnqueueCollector enqueueCollector = enqueueSupplier.getEnqueueCollector();
biblEntitys.forEach(biblEntity -> enqueueCollector.add(biblEntity, QueueType.ENDPOINT, QueueType.WORKENDPOINT));
enqueueCollector.commit();
return Response.ok(new StatusResponse()).build();
} catch (SQLException ex) {
log.error("Error queueing: {}: {}", workId, ex.getMessage());
log.debug("Error queueing: {}: ", workId, ex);
return Response.serverError().entity(new StatusResponse(ex.getMessage())).build();
}
}
Aggregations