/**
   * 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();
  }
  /**
   * 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();
  }
  /**
   * 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();
  }
  /**
   * Count Push Applications
   *
   * @param pushApplicationID id of {@link PushApplication}
   * @return count number for each {@link org.jboss.aerogear.unifiedpush.api.VariantType}
   */
  @GET
  @Path("/{pushAppID}/count")
  @Produces(MediaType.APPLICATION_JSON)
  @ReturnType("java.util.Map<java.lang.String, java.lang.Long>")
  public Response countInstallations(@PathParam("pushAppID") String pushApplicationID) {

    Map<String, Long> result = pushAppService.countInstallationsByType(pushApplicationID);

    return Response.ok(result).build();
  }
  /**
   * Delete Push Application
   *
   * @param pushApplicationID id of {@link PushApplication}
   * @statuscode 204 The PushApplication successfully deleted
   * @statuscode 404 The requested PushApplication resource does not exist
   */
  @DELETE
  @Path("/{pushAppID}")
  @Produces(MediaType.APPLICATION_JSON)
  @ReturnType("java.lang.Void")
  public Response deletePushApplication(@PathParam("pushAppID") String pushApplicationID) {

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

    if (pushApp != null) {
      pushAppService.removePushApplication(pushApp);
      return Response.noContent().build();
    }
    return Response.status(Status.NOT_FOUND)
        .entity("Could not find requested PushApplicationEntity")
        .build();
  }