Search in sources :

Example 1 with CreateHarvestingClientCommand

use of edu.harvard.iq.dataverse.engine.command.impl.CreateHarvestingClientCommand in project dataverse by IQSS.

the class HarvestingClients method createHarvestingClient.

@POST
@Path("{nickName}")
public Response createHarvestingClient(String jsonBody, @PathParam("nickName") String nickName, @QueryParam("key") String apiKey) throws IOException, JsonParseException {
    try (StringReader rdr = new StringReader(jsonBody)) {
        JsonObject json = Json.createReader(rdr).readObject();
        HarvestingClient harvestingClient = new HarvestingClient();
        // TODO: check that it doesn't exist yet...
        harvestingClient.setName(nickName);
        String dataverseAlias = jsonParser().parseHarvestingClient(json, harvestingClient);
        Dataverse ownerDataverse = dataverseService.findByAlias(dataverseAlias);
        if (ownerDataverse == null) {
            return error(Response.Status.BAD_REQUEST, "No such dataverse: " + dataverseAlias);
        }
        harvestingClient.setDataverse(ownerDataverse);
        if (ownerDataverse.getHarvestingClientConfigs() == null) {
            ownerDataverse.setHarvestingClientConfigs(new ArrayList<>());
        }
        ownerDataverse.getHarvestingClientConfigs().add(harvestingClient);
        DataverseRequest req = createDataverseRequest(findUserOrDie());
        HarvestingClient managedHarvestingClient = execCommand(new CreateHarvestingClientCommand(req, harvestingClient));
        return created("/harvest/clients/" + nickName, harvestingConfigAsJson(managedHarvestingClient));
    } catch (JsonParseException ex) {
        return error(Response.Status.BAD_REQUEST, "Error parsing harvesting client: " + ex.getMessage());
    } catch (WrappedResponse ex) {
        return ex.getResponse();
    }
}
Also used : DataverseRequest(edu.harvard.iq.dataverse.engine.command.DataverseRequest) CreateHarvestingClientCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateHarvestingClientCommand) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient) JsonParseException(edu.harvard.iq.dataverse.util.json.JsonParseException) Dataverse(edu.harvard.iq.dataverse.Dataverse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with CreateHarvestingClientCommand

use of edu.harvard.iq.dataverse.engine.command.impl.CreateHarvestingClientCommand in project dataverse by IQSS.

the class HarvestingClientsPage method createClient.

public void createClient(ActionEvent ae) {
    // will be set as type OAI by default
    HarvestingClient newHarvestingClient = new HarvestingClient();
    newHarvestingClient.setName(newNickname);
    if (getSelectedDestinationDataverse() == null) {
        JsfHelper.JH.addMessage(FacesMessage.SEVERITY_ERROR, "Failed to create a new Harvesting Client configuration: no destination dataverse selected.");
    }
    newHarvestingClient.setDataverse(getSelectedDestinationDataverse());
    if (getSelectedDestinationDataverse().getHarvestingClientConfigs() == null) {
        getSelectedDestinationDataverse().setHarvestingClientConfigs(new ArrayList<>());
    }
    getSelectedDestinationDataverse().getHarvestingClientConfigs().add(newHarvestingClient);
    newHarvestingClient.setHarvestingUrl(newHarvestingUrl);
    if (!StringUtils.isEmpty(newOaiSet)) {
        newHarvestingClient.setHarvestingSet(newOaiSet);
    }
    newHarvestingClient.setMetadataPrefix(newMetadataFormat);
    newHarvestingClient.setHarvestStyle(newHarvestingStyle);
    if (isNewHarvestingScheduled()) {
        newHarvestingClient.setScheduled(true);
        if (isNewHarvestingScheduledWeekly()) {
            newHarvestingClient.setSchedulePeriod(HarvestingClient.SCHEDULE_PERIOD_WEEKLY);
            if (getWeekDayNumber() == null) {
            // create a "week day is required..." error message, etc.
            // but we may be better off not even giving them an opportunity
            // to leave the field blank - ?
            }
            newHarvestingClient.setScheduleDayOfWeek(getWeekDayNumber());
        } else {
            newHarvestingClient.setSchedulePeriod(HarvestingClient.SCHEDULE_PERIOD_DAILY);
        }
        if (getHourOfDay() == null) {
        // see the comment above, about the day of week. same here.
        }
        newHarvestingClient.setScheduleHourOfDay(getHourOfDay());
    }
    // make default archive url (used to generate links pointing back to the
    // archival sources, when harvested datasets are displayed in search results),
    // from the harvesting url:
    newHarvestingClient.setArchiveUrl(makeDefaultArchiveUrl());
    // set default description - they can customize it as they see fit:
    newHarvestingClient.setArchiveDescription(JH.localize("harvestclients.viewEditDialog.archiveDescription.default.generic"));
    try {
        newHarvestingClient = engineService.submit(new CreateHarvestingClientCommand(dvRequestService.getDataverseRequest(), newHarvestingClient));
        configuredHarvestingClients = harvestingClientService.getAllHarvestingClients();
        // NO, we no longer create timers here. It is the job of the Mother Timer!
        // dataverseTimerService.createHarvestTimer(newHarvestingClient);
        String successMessage = JH.localize("harvestclients.newClientDialog.success");
        successMessage = successMessage.replace("{0}", newHarvestingClient.getName());
        JsfHelper.addSuccessMessage(successMessage);
    }/* TODO: (?) add a dedicated "NameAlreadyExists" exception for the 
             create client command? 
          catch ( CreateHarvestingClientCommand.NicknameAlreadyExistsException naee ) {
            FacesContext.getCurrentInstance().addMessage(newHarvestingClient.getName(),
                           new FacesMessage( FacesMessage.SEVERITY_ERROR, naee.getMessage(), null));

        }*/
     catch (CommandException ex) {
        logger.log(Level.WARNING, "Harvesting client creation command failed", ex);
        JsfHelper.JH.addMessage(FacesMessage.SEVERITY_ERROR, "Harvesting Client creation command failed.", ex.getMessage());
    } catch (Exception ex) {
        JH.addMessage(FacesMessage.SEVERITY_FATAL, "Harvesting client creation failed (reason unknown).");
        logger.log(Level.SEVERE, "Harvesting client creation failed (reason unknown)." + ex.getMessage(), ex);
    }
    setPageMode(PageMode.VIEW);
}
Also used : CreateHarvestingClientCommand(edu.harvard.iq.dataverse.engine.command.impl.CreateHarvestingClientCommand) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) HarvestingClient(edu.harvard.iq.dataverse.harvest.client.HarvestingClient) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException)

Aggregations

CreateHarvestingClientCommand (edu.harvard.iq.dataverse.engine.command.impl.CreateHarvestingClientCommand)2 HarvestingClient (edu.harvard.iq.dataverse.harvest.client.HarvestingClient)2 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 DataverseRequest (edu.harvard.iq.dataverse.engine.command.DataverseRequest)1 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)1 JsonParseException (edu.harvard.iq.dataverse.util.json.JsonParseException)1 StringReader (java.io.StringReader)1 JsonObject (javax.json.JsonObject)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1