Search in sources :

Example 1 with BibliographicEntity

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;
}
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) 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) LibraryType(dk.dbc.search.solrdocstore.jpa.LibraryType) HoldingsItemEntity(dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity) 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) WorkRetrieveResponse(dk.dbc.search.solrdocstore.response.WorkRetrieveResponse) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity) 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 BibliographicEntity

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();
}
Also used : SQLException(java.sql.SQLException) LibraryType(dk.dbc.search.solrdocstore.jpa.LibraryType) AgencyItemFieldKey(dk.dbc.search.solrdocstore.jpa.AgencyItemFieldKey) BibliographicResourceEntity(dk.dbc.search.solrdocstore.jpa.BibliographicResourceEntity) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity) OpenAgencyEntity(dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity) StatusResponse(dk.dbc.search.solrdocstore.response.StatusResponse) EJBException(javax.ejb.EJBException) EnqueueCollector(dk.dbc.search.solrdocstore.enqueue.EnqueueCollector)

Example 3 with BibliographicEntity

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();
}
Also used : Instant(java.time.Instant) HoldingsToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity) BibliographicToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicToBibliographicEntity) HoldingsToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity) EnqueueCollector(dk.dbc.search.solrdocstore.enqueue.EnqueueCollector)

Example 4 with BibliographicEntity

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);
}
Also used : HoldingsToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity) BibliographicToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicToBibliographicEntity) HoldingsToBibliographicEntity(dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity)

Example 5 with BibliographicEntity

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();
    }
}
Also used : LogWith(dk.dbc.log.LogWith) SQLException(java.sql.SQLException) BibliographicEntity(dk.dbc.search.solrdocstore.jpa.BibliographicEntity) StatusResponse(dk.dbc.search.solrdocstore.response.StatusResponse) EnqueueCollector(dk.dbc.search.solrdocstore.enqueue.EnqueueCollector) Path(javax.ws.rs.Path) Parameters(org.eclipse.microprofile.openapi.annotations.parameters.Parameters) Produces(javax.ws.rs.Produces) Timed(org.eclipse.microprofile.metrics.annotation.Timed) GET(javax.ws.rs.GET) APIResponses(org.eclipse.microprofile.openapi.annotations.responses.APIResponses) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Aggregations

BibliographicEntity (dk.dbc.search.solrdocstore.jpa.BibliographicEntity)44 Test (org.junit.Test)24 HoldingsToBibliographicEntity (dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity)18 IndexKeys (dk.dbc.search.solrdocstore.jpa.IndexKeys)15 Response (javax.ws.rs.core.Response)14 BibliographicToBibliographicEntity (dk.dbc.search.solrdocstore.jpa.BibliographicToBibliographicEntity)11 OpenAgencyEntity (dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity)9 BibliographicResourceEntity (dk.dbc.search.solrdocstore.jpa.BibliographicResourceEntity)8 HoldingsItemEntity (dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity)8 AgencyClassifierItemKey (dk.dbc.search.solrdocstore.jpa.AgencyClassifierItemKey)7 EntityManager (javax.persistence.EntityManager)6 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 Timed (org.eclipse.microprofile.metrics.annotation.Timed)6 LogWith (dk.dbc.log.LogWith)5 EnqueueCollector (dk.dbc.search.solrdocstore.enqueue.EnqueueCollector)4 AgencyItemFieldKey (dk.dbc.search.solrdocstore.jpa.AgencyItemFieldKey)4 BibliographicFrontendResponse (dk.dbc.search.solrdocstore.response.BibliographicFrontendResponse)4 LogWith.track (dk.dbc.log.LogWith.track)3