Search in sources :

Example 1 with EnqueueCollector

use of dk.dbc.search.solrdocstore.enqueue.EnqueueCollector 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 2 with EnqueueCollector

use of dk.dbc.search.solrdocstore.enqueue.EnqueueCollector 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.WORK, 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.WORK, 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.WORK, QueueType.MAJORHOLDING, QueueType.WORKMAJORHOLDING);
    } else {
        entityManager.merge(hi);
        queueRelatedBibliographic(hi, enqueue, QueueType.HOLDING, 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 3 with EnqueueCollector

use of dk.dbc.search.solrdocstore.enqueue.EnqueueCollector 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 EnqueueCollector

use of dk.dbc.search.solrdocstore.enqueue.EnqueueCollector 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)

Example 5 with EnqueueCollector

use of dk.dbc.search.solrdocstore.enqueue.EnqueueCollector in project solr-document-store by DBCDK.

the class OpenAgencyStatusBean method purgeAgency.

ArrayList<String> purgeAgency(int agencyId) throws SQLException {
    ArrayList<String> resp = new ArrayList<>();
    EnqueueCollector enqueue = supplier.getEnqueueCollector();
    // Queue related records
    entityManager.createQuery("SELECT b FROM BibliographicEntity b" + " JOIN HoldingsToBibliographicEntity h2b" + " JOIN HoldingsItemEntity hi" + " ON b.agencyId = h2b.bibliographicAgencyId AND b.bibliographicRecordId = h2b.bibliographicRecordId" + " AND hi.agencyId = h2b.holdingsAgencyId AND hi.bibliographicRecordId = h2b.holdingsBibliographicRecordId" + " WHERE h2b.holdingsAgencyId = :agencyId", BibliographicEntity.class).setParameter("agencyId", agencyId).getResultList().stream().forEach(entity -> enqueue.add(entity, QueueType.MANIFESTATION));
    enqueue.commit();
    // Purge holdings if no error occured
    if (resp.isEmpty()) {
        // Purge h2b
        entityManager.createQuery("SELECT hi FROM HoldingsItemEntity hi JOIN HoldingsToBibliographicEntity h2b" + " ON hi.agencyId = h2b.holdingsAgencyId AND hi.bibliographicRecordId = h2b.holdingsBibliographicRecordId" + " WHERE h2b.holdingsAgencyId = :agencyId", HoldingsItemEntity.class).setParameter("agencyId", agencyId).getResultList().stream().forEach(entityManager::remove);
        // Purge h
        entityManager.createQuery("SELECT h2b FROM HoldingsToBibliographicEntity h2b" + " WHERE h2b.holdingsAgencyId = :agencyId", HoldingsToBibliographicEntity.class).setParameter("agencyId", agencyId).getResultList().stream().forEach(entityManager::remove);
        // Purge oac
        OpenAgencyEntity oa = entityManager.find(OpenAgencyEntity.class, agencyId);
        if (oa != null) {
            entityManager.remove(oa);
        }
    }
    return resp;
}
Also used : ArrayList(java.util.ArrayList) OpenAgencyEntity(dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity) EnqueueCollector(dk.dbc.search.solrdocstore.enqueue.EnqueueCollector)

Aggregations

EnqueueCollector (dk.dbc.search.solrdocstore.enqueue.EnqueueCollector)7 BibliographicEntity (dk.dbc.search.solrdocstore.jpa.BibliographicEntity)4 StatusResponse (dk.dbc.search.solrdocstore.response.StatusResponse)3 SQLException (java.sql.SQLException)3 LogWith (dk.dbc.log.LogWith)2 HoldingsToBibliographicEntity (dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicEntity)2 OpenAgencyEntity (dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Timed (org.eclipse.microprofile.metrics.annotation.Timed)2 Operation (org.eclipse.microprofile.openapi.annotations.Operation)2 Parameters (org.eclipse.microprofile.openapi.annotations.parameters.Parameters)2 APIResponses (org.eclipse.microprofile.openapi.annotations.responses.APIResponses)2 AgencyClassifierItemKey (dk.dbc.search.solrdocstore.jpa.AgencyClassifierItemKey)1 AgencyItemFieldKey (dk.dbc.search.solrdocstore.jpa.AgencyItemFieldKey)1 BibliographicResourceEntity (dk.dbc.search.solrdocstore.jpa.BibliographicResourceEntity)1 BibliographicToBibliographicEntity (dk.dbc.search.solrdocstore.jpa.BibliographicToBibliographicEntity)1 HoldingsItemEntity (dk.dbc.search.solrdocstore.jpa.HoldingsItemEntity)1 HoldingsToBibliographicKey (dk.dbc.search.solrdocstore.jpa.HoldingsToBibliographicKey)1