/**
   * Update Push Application
   *
   * @param pushApplicationID id of {@link PushApplication}
   * @param updatedPushApp new info of {@link PushApplication}
   * @statuscode 204 The PushApplication updated successfully
   * @statuscode 400 The format of the client request was incorrect
   * @statuscode 404 The requested PushApplication resource does not exist
   */
  @PUT
  @Path("/{pushAppID}")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @ReturnType("java.lang.Void")
  public Response updatePushApplication(
      @PathParam("pushAppID") String pushApplicationID, PushApplication updatedPushApp) {

    PushApplication pushApp = getSearch().findByPushApplicationIDForDeveloper(pushApplicationID);

    if (pushApp != null) {

      // some validation
      try {
        validateModelClass(updatedPushApp);
      } catch (ConstraintViolationException cve) {

        // Build and return the 400 (Bad Request) response
        ResponseBuilder builder = createBadRequestResponse(cve.getConstraintViolations());

        return builder.build();
      }

      // update name/desc:
      pushApp.setDescription(updatedPushApp.getDescription());
      pushApp.setName(updatedPushApp.getName());
      pushAppService.updatePushApplication(pushApp);

      return Response.noContent().build();
    }

    return Response.status(Status.NOT_FOUND)
        .entity("Could not find requested PushApplicationEntity")
        .build();
  }
  /**
   * Reset MasterSecret for Push Application
   *
   * @param pushApplicationID id of {@link PushApplication}
   * @return updated {@link PushApplication}
   * @statuscode 204 The MasterSecret for Push Application reset successfully
   * @statuscode 404 The requested PushApplication resource does not exist
   */
  @PUT
  @Path("/{pushAppID}/reset")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @ReturnType("org.jboss.aerogear.unifiedpush.api.PushApplication")
  public Response resetMasterSecret(@PathParam("pushAppID") String pushApplicationID) {

    // PushApplication pushApp =
    // pushAppService.findByPushApplicationIDForDeveloper(pushApplicationID,
    // extractUsername(request));
    PushApplication pushApp = getSearch().findByPushApplicationIDForDeveloper(pushApplicationID);

    if (pushApp != null) {
      // generate the new 'masterSecret' and apply it:
      String newMasterSecret = UUID.randomUUID().toString();
      pushApp.setMasterSecret(newMasterSecret);
      pushAppService.updatePushApplication(pushApp);

      return Response.ok(pushApp).build();
    }

    return Response.status(Status.NOT_FOUND)
        .entity("Could not find requested PushApplicationEntity")
        .build();
  }
 private void putDeviceCountIntoResponseHeaders(PushApplication app, ResponseBuilder response) {
   long appCount = 0;
   for (Variant variant : app.getVariants()) {
     long variantCount = installationDao.getNumberOfDevicesForVariantID(variant.getVariantID());
     appCount += variantCount;
     response.header("deviceCount_variant_" + variant.getVariantID(), variantCount);
   }
   response.header("deviceCount_app_" + app.getPushApplicationID(), appCount);
 }
 private void putActivityIntoResponseHeaders(PushApplication app, ResponseBuilder response) {
   response.header(
       "activity_app_" + app.getPushApplicationID(),
       metricsService.countMessagesForPushApplication(app.getPushApplicationID()));
   for (Variant variant : app.getVariants()) {
     response.header(
         "activity_variant_" + variant.getVariantID(),
         metricsService.countMessagesForVariant(variant.getVariantID()));
   }
 }
 private List<ApplicationVariant> wrapApplicationVariant(List<PushApplication> applications) {
   final List<ApplicationVariant> applicationVariants =
       new ArrayList<ApplicationVariant>(applications.size());
   for (PushApplication application : applications) {
     for (Variant variant : application.getVariants()) {
       final ApplicationVariant applicationVariant = new ApplicationVariant(application, variant);
       applicationVariants.add(applicationVariant);
     }
   }
   return applicationVariants;
 }
  /**
   * Create Push Application
   *
   * @param pushApp new {@link PushApplication}
   * @return created {@link PushApplication}
   * @statuscode 201 The PushApplication Variant created successfully
   * @statuscode 400 The format of the client request was incorrect
   */
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @ReturnType("org.jboss.aerogear.unifiedpush.api.PushApplication")
  public Response registerPushApplication(PushApplication pushApp) {

    // some validation
    try {
      validateModelClass(pushApp);
    } catch (ConstraintViolationException cve) {

      // Build and return the 400 (Bad Request) response
      ResponseBuilder builder = createBadRequestResponse(cve.getConstraintViolations());

      return builder.build();
    }

    pushAppService.addPushApplication(pushApp);

    return Response.created(
            UriBuilder.fromResource(PushApplicationEndpoint.class)
                .path(String.valueOf(pushApp.getPushApplicationID()))
                .build())
        .entity(pushApp)
        .build();
  }
  @Test
  public void updatePushApplication() {
    final String pushApplicationID1 = "888";

    final PushApplication pushApplication1 =
        pushApplicationDao.findByPushApplicationID(pushApplicationID1);
    assertThat(pushApplication1).isNotNull();
    assertThat(pushApplication1.getName()).isEqualTo("Push App 1");

    pushApplication1.setName("Cool Push App 1");
    pushApplicationDao.update(pushApplication1);

    assertThat(pushApplicationDao.findByPushApplicationID(pushApplicationID1)).isNotNull();
    assertThat(pushApplicationDao.findByPushApplicationID(pushApplicationID1).getName())
        .isEqualTo("Cool Push App 1");
  }
  @Test
  public void shouldFindPushApplicationNameAndIDBasedOnVariantID() {

    // when
    final List<PushApplication> applications =
        pushApplicationDao.findByVariantIds(Arrays.asList("1"));

    // then
    assertThat(applications).isNotEmpty();
    assertThat(applications.size()).isEqualTo(1);

    final PushApplication application = applications.iterator().next();
    assertThat(application.getName()).isEqualTo("Push App 1");
    assertThat(application.getVariants()).isNotEmpty();
    assertThat(application.getVariants().size()).isEqualTo(1);
    assertThat(application.getVariants().iterator().next().getId()).isEqualTo("1");
  }
  @Test
  public void primaryKeyUnmodifiedAfterUpdate() {
    final String id = "1";
    PushApplication pa = pushApplicationDao.find(id);

    assertThat(pa.getId()).isEqualTo(id);

    final PushApplication pushApplication1 =
        pushApplicationDao.findByPushApplicationID(pa.getPushApplicationID());
    pushApplication1.setName("Cool Push App 1");
    pushApplicationDao.update(pushApplication1);

    entityManager.flush();
    entityManager.clear();

    pa = pushApplicationDao.find(id);

    assertThat(pa.getName()).isEqualTo("Cool Push App 1");
  }