override def delete()

in app/controllers/ProjectTemplateController.scala [82:122]


  override def delete(requestedId: Int) = IsAdminAsync { uid=> { request =>
    implicit val db:slick.jdbc.PostgresProfile#Backend#Database=dbConfig.db

    if(requestedId<0)
      Future(Conflict(Json.obj("status"->"error","detail"->"This object is still referred to by sub-objects")))
    else {
      /* step one - get the file for the template object that we want to delete */
      val fileFuture = selectid(requestedId).flatMap({
        case Success(templatesList) =>
          val template = templatesList.head
          template.file
        case Failure(error) =>
          logger.error(error.toString)
          throw error; //this will result in the future failing and can be picked up as a Try later on
      })

      /* step two - actually try to delete the file from disk */
      val fileDeleteFuture = fileFuture.flatMap(fileEntryDAO.deleteFromDisk)

      /* step three - delete the file object representing it */
      val fileObjectDeleteFuture = fileDeleteFuture.map({
        case Left(errormsg) => Left(errormsg)
        case Right(didDelete) =>
          val fileEntry = fileFuture.value.get.get //we know that fileFuture completed successfully or fileDeleteFuture won't succeed
          if (didDelete) fileEntry.deleteSelf
      })

      /*step four - now delete the template object that was using it */
      val templateDeleteFuture = fileDeleteFuture.flatMap({
        case Left(errorString) =>
          logger.error(s"Not able to delete the underlying file: $errorString")
          Future(InternalServerError(Json.obj("status" -> "error", "detail" -> s"Not able to delete the underlying file: $errorString")))
        case Right(managedDelete) =>
            /*now run the normal delete process for the project template object, even if the file could not be deleted (maybe it doesn't exist
          any more). Use a custom implentation of deleteAction to warn the frontend in this case*/
            deleteAction(requestedId, managedDelete)
      })

      templateDeleteFuture
    }
  }}