Search in sources :

Example 11 with SchedulerTransaction

use of org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction in project opencast by opencast.

the class SchedulerRestService method addTransactionEvent.

@PUT
@Path("/transaction/{id}/add")
@RestQuery(name = "transactionaddevent", description = "Commits the scheduler transaction with specified id", returnDescription = "Successfully committed scheduler transaction", reponses = { @RestResponse(responseCode = SC_OK, description = "Successfully committed scheduler transaction"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Scheduler transaction with specified ID does not exist"), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "You do not have permission to commit the scheduler transaction. Maybe you need to authenticate.") }, pathParameters = { @RestParameter(name = "id", type = RestParameter.Type.STRING, isRequired = true, description = "ID of scheduler transaction") }, restParameters = { @RestParameter(name = "start", isRequired = true, type = Type.INTEGER, description = "The start date of the event"), @RestParameter(name = "end", isRequired = true, type = Type.INTEGER, description = "The end date of the event"), @RestParameter(name = "agent", isRequired = true, type = Type.STRING, description = "The agent of the event"), @RestParameter(name = "users", isRequired = false, type = Type.STRING, description = "Comma separated list of user ids (speakers/lecturers) for the event"), @RestParameter(name = "mediaPackage", isRequired = true, type = Type.TEXT, description = "The media package of the event"), @RestParameter(name = "wfproperties", isRequired = false, type = Type.TEXT, description = "The workflow properties for the event"), @RestParameter(name = "agentparameters", isRequired = false, type = Type.TEXT, description = "The capture agent properties for the event"), @RestParameter(name = "optOut", isRequired = false, type = Type.BOOLEAN, description = "The opt out status of the event") })
public Response addTransactionEvent(@PathParam("id") String transactionId, @FormParam("start") long startTime, @FormParam("end") long endTime, @FormParam("agent") String agentId, @FormParam("users") String users, @FormParam("mediaPackage") String mediaPackageXml, @FormParam("wfproperties") String workflowProperties, @FormParam("agentparameters") String agentParameters, @FormParam("optOut") Boolean optOut) throws UnauthorizedException, NotFoundException {
    if (startTime < 0 || endTime < 0) {
        logger.info("Cannot add event without proper start and/or end time");
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (StringUtils.isBlank(agentId)) {
        logger.info("Cannot add event without agent identifier");
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (StringUtils.isBlank(mediaPackageXml)) {
        logger.info("Cannot add event without media package");
        return Response.status(Status.BAD_REQUEST).build();
    }
    MediaPackage mediaPackage;
    try {
        mediaPackage = MediaPackageParser.getFromXml(mediaPackageXml);
    } catch (Exception e) {
        logger.info("Could not parse media package:", e);
        return Response.status(Status.BAD_REQUEST).build();
    }
    Map<String, String> caProperties = new HashMap<>();
    if (StringUtils.isNotBlank(agentParameters)) {
        try {
            Properties prop = parseProperties(agentParameters);
            caProperties.putAll((Map) prop);
        } catch (Exception e) {
            logger.info("Could not parse capture agent properties: {}", agentParameters);
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    Map<String, String> wfProperties = new HashMap<>();
    if (StringUtils.isNotBlank(workflowProperties)) {
        try {
            Properties prop = parseProperties(workflowProperties);
            wfProperties.putAll((Map) prop);
        } catch (IOException e) {
            logger.info("Could not parse workflow configuration properties: {}", workflowProperties);
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    Set<String> userIds = new HashSet<>();
    String[] ids = StringUtils.split(users, ",");
    if (ids != null)
        userIds.addAll(Arrays.asList(ids));
    DateTime startDate = new DateTime(startTime).toDateTime(DateTimeZone.UTC);
    DateTime endDate = new DateTime(endTime).toDateTime(DateTimeZone.UTC);
    try {
        SchedulerTransaction transaction = service.getTransaction(transactionId);
        transaction.addEvent(startDate.toDate(), endDate.toDate(), agentId, userIds, mediaPackage, wfProperties, caProperties, Opt.nul(optOut));
        return Response.ok().build();
    } catch (NotFoundException e) {
        throw e;
    } catch (UnauthorizedException e) {
        throw e;
    } catch (Exception e) {
        logger.error("Unable to commit scheduler transaction '{}': {}", transactionId, getStackTrace(e));
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) SchedulerTransaction(org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) ParseException(java.text.ParseException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) DateTime(org.joda.time.DateTime) MediaPackage(org.opencastproject.mediapackage.MediaPackage) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) RestQuery(org.opencastproject.util.doc.rest.RestQuery) PUT(javax.ws.rs.PUT)

Example 12 with SchedulerTransaction

use of org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction in project opencast by opencast.

the class SchedulerServiceImplTest method testTransactionConflicts.

@Test
public void testTransactionConflicts() throws Exception {
    Date start = new Date();
    Date end = new Date(System.currentTimeMillis() + 60000);
    String captureDeviceID = "demo";
    String seriesId = "series1";
    Set<String> userIds = new HashSet<>();
    userIds.add("user1");
    userIds.add("user2");
    MediaPackage mp = generateEvent(Opt.<String>none());
    mp.setSeries(seriesId);
    DublinCoreCatalog event = generateEvent(captureDeviceID, start, end);
    addDublinCore(Opt.<String>none(), mp, event);
    Map<String, String> caProperties = generateCaptureAgentMetadata("demo");
    try {
        schedSvc.addEvent(start, end, captureDeviceID, userIds, mp, wfPropertiesUpdated, caProperties, Opt.<Boolean>none(), Opt.some("new"), SchedulerService.ORIGIN);
    } catch (SchedulerTransactionLockException e) {
        fail("Transaction create lock not working!");
    }
    Assert.assertFalse(schedSvc.hasActiveTransaction(mp.getIdentifier().compact()));
    SchedulerTransaction trx = schedSvc.createTransaction("new");
    assertEquals("new", trx.getSource());
    SchedulerTransaction trx2 = schedSvc.createTransaction("new2");
    assertEquals("new2", trx2.getSource());
    try {
        schedSvc.createTransaction("new");
        fail("Duplicated transaction created!");
    } catch (SchedulerConflictException e) {
        assertNotNull(e);
    }
    try {
        schedSvc.updateEvent(mp.getIdentifier().compact(), Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), Opt.<Set<String>>none(), Opt.<MediaPackage>none(), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.some(Opt.some(false)), SchedulerService.ORIGIN);
        fail("Transaction update lock not working!");
    } catch (SchedulerTransactionLockException e) {
        assertNotNull(e);
    }
    try {
        schedSvc.removeEvent(mp.getIdentifier().compact());
        fail("Transaction delete lock not working!");
    } catch (SchedulerTransactionLockException e) {
        assertNotNull(e);
    }
    try {
        MediaPackage mp2 = generateEvent(Opt.<String>none());
        schedSvc.addEvent(start, end, captureDeviceID, userIds, mp2, wfPropertiesUpdated, caProperties, Opt.<Boolean>none(), Opt.some("new"), SchedulerService.ORIGIN);
        fail("Transaction create lock not working!");
    } catch (SchedulerTransactionLockException e) {
        assertNotNull(e);
    }
    SchedulerTransaction trx3 = schedSvc.getTransaction(trx.getId());
    assertEquals(trx, trx3);
    mp = generateEvent(Opt.<String>none());
    event = generateEvent(captureDeviceID, new Date(), end);
    addDublinCore(Opt.<String>none(), mp, event);
    trx.addEvent(start, end, captureDeviceID, userIds, mp, wfPropertiesUpdated, caProperties, Opt.<Boolean>none());
    Assert.assertTrue(schedSvc.hasActiveTransaction(mp.getIdentifier().compact()));
    trx.commit();
    Assert.assertFalse(schedSvc.hasActiveTransaction(mp.getIdentifier().compact()));
    try {
        MediaPackage mp2 = generateEvent(Opt.<String>none());
        Date startDate = new Date(System.currentTimeMillis() + 600000);
        Date endDate = new Date(System.currentTimeMillis() + 660000);
        schedSvc.addEvent(startDate, endDate, captureDeviceID, userIds, mp2, wfPropertiesUpdated, caProperties, Opt.<Boolean>none(), Opt.some("new"), SchedulerService.ORIGIN);
    } catch (SchedulerTransactionLockException e) {
        fail("Transaction create lock not working!");
    }
}
Also used : SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) SchedulerTransaction(org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

SchedulerTransaction (org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction)12 HashSet (java.util.HashSet)8 MediaPackage (org.opencastproject.mediapackage.MediaPackage)8 Date (java.util.Date)7 Test (org.junit.Test)7 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)7 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)7 NotFoundException (org.opencastproject.util.NotFoundException)7 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)6 AResult (org.opencastproject.assetmanager.api.query.AResult)6 RichAResult (org.opencastproject.assetmanager.api.query.RichAResult)6 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)5 ARecord (org.opencastproject.assetmanager.api.query.ARecord)4 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)4 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)4 Path (javax.ws.rs.Path)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 RestQuery (org.opencastproject.util.doc.rest.RestQuery)3 IOException (java.io.IOException)2 POST (javax.ws.rs.POST)2