Search in sources :

Example 1 with StatusResponse

use of dk.dbc.search.solrdocstore.response.StatusResponse 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 StatusResponse

use of dk.dbc.search.solrdocstore.response.StatusResponse 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 3 with StatusResponse

use of dk.dbc.search.solrdocstore.response.StatusResponse in project solr-document-store by DBCDK.

the class QueueBean method queueManifestation.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("manifestation/{ agencyId : \\d+}-{ classifier : \\w+ }:{ bibliographicRecordId : .+}")
@Operation(summary = "Queue a manifestation", description = "This operation puts a manifestation," + " and optionally (if it isn't deleted) its work on queue.")
@APIResponses({ @APIResponse(name = "Success", responseCode = "200", description = "The manifestation 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 manifestation", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(ref = StatusResponse.NAME))) })
@Parameters({ @Parameter(name = "agencyId", description = "The agency that owns the record", required = true), @Parameter(name = "classifier", description = "The classifier of the records (usually basis or katalog)", required = true), @Parameter(name = "bibliographicRecordId", description = "The id of the record", required = true), @Parameter(name = "trackingId", description = "For tracking the request", required = false) })
@Timed
public Response queueManifestation(@PathParam("agencyId") Integer agencyId, @PathParam("classifier") String classifier, @PathParam("bibliographicRecordId") String bibliographicRecordId, @QueryParam("trackingId") String trackingId) {
    if (trackingId == null || trackingId.isEmpty())
        trackingId = UUID.randomUUID().toString();
    String pid = agencyId + "-" + classifier + ":" + bibliographicRecordId;
    try (LogWith logWith = LogWith.track(trackingId).pid(pid)) {
        AgencyClassifierItemKey bibliographicKey = new AgencyClassifierItemKey(agencyId, classifier, bibliographicRecordId);
        BibliographicEntity biblEntity = entityManager.find(BibliographicEntity.class, bibliographicKey);
        if (biblEntity == null) {
            return Response.status(Response.Status.NOT_FOUND).entity(new StatusResponse("No such manifestation")).build();
        }
        EnqueueCollector enqueueCollector = enqueueSupplier.getEnqueueCollector();
        if (biblEntity.isDeleted()) {
            enqueueCollector.add(biblEntity, QueueType.ENDPOINT);
        } else {
            enqueueCollector.add(biblEntity, QueueType.ENDPOINT, QueueType.WORKENDPOINT);
        }
        enqueueCollector.commit();
        return Response.ok(new StatusResponse()).build();
    } catch (SQLException ex) {
        log.error("Error queueing: {}: {}", pid, ex.getMessage());
        log.debug("Error queueing: {}: ", pid, 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) AgencyClassifierItemKey(dk.dbc.search.solrdocstore.jpa.AgencyClassifierItemKey) 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 4 with StatusResponse

use of dk.dbc.search.solrdocstore.response.StatusResponse in project solr-document-store by DBCDK.

the class StatusBean method getStatus.

@GET
@Produces({ MediaType.APPLICATION_JSON })
@Timed
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE")
public Response getStatus() {
    log.trace("getStatus called ");
    try (Connection connection = getConnection();
        Statement stmt = connection.createStatement();
        ResultSet resultSet = stmt.executeQuery("SELECT NOW()")) {
        if (resultSet.next()) {
            resultSet.getTimestamp(1);
            log.trace("status - ok");
            return Response.ok().entity(new StatusResponse()).build();
        }
        return Response.serverError().entity(new StatusResponse("No rows when communicating with database")).build();
    } catch (SQLException ex) {
        log.error("Error getting connection for status: {}", ex.getMessage());
        log.debug("Error getting connection for status: ", ex);
        return Response.serverError().entity(new StatusResponse("SQL Exception: " + ex.getMessage())).build();
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) StatusResponse(dk.dbc.search.solrdocstore.response.StatusResponse) Produces(javax.ws.rs.Produces) Timed(org.eclipse.microprofile.metrics.annotation.Timed) GET(javax.ws.rs.GET) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

StatusResponse (dk.dbc.search.solrdocstore.response.StatusResponse)4 SQLException (java.sql.SQLException)4 EnqueueCollector (dk.dbc.search.solrdocstore.enqueue.EnqueueCollector)3 BibliographicEntity (dk.dbc.search.solrdocstore.jpa.BibliographicEntity)3 GET (javax.ws.rs.GET)3 Produces (javax.ws.rs.Produces)3 Timed (org.eclipse.microprofile.metrics.annotation.Timed)3 LogWith (dk.dbc.log.LogWith)2 Path (javax.ws.rs.Path)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 LibraryType (dk.dbc.search.solrdocstore.jpa.LibraryType)1 OpenAgencyEntity (dk.dbc.search.solrdocstore.jpa.OpenAgencyEntity)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1