playframework scala Seq [Future [Model]]删除未来

时间:2017-09-19 09:03:16

标签: scala playframework playframework-2.0

我有以下问题,

我有一个Seq[Future[ProcessStepTemplatesModel]],我想删除内心的未来。

我想要得到的是Seq[ProcessStepTemplatesModel]

完整的代码如下所示

Future.sequence {
        processSteps.map { step => // This is a "for each" over a Seq[ProcessStepTemplatesModel]
            val prerequisiteFuture = processStepPrerequisitesDTO.getProcessStepPrerequisiteProcessTemplateIds(step.id.get)

            prerequisiteFuture.map(prereqTemplates => {

             processTemplateDTO.getProessTemplatesForStepPreqrequsites(prereqTemplates).map(pres => {
                 step.stepPrerequisites ++Some(pres)
                 step
                })

            })
        }
      }

由于

更新

def getEditProcessTemplateData(processTemplateId: Int) = Action.async {
    //Get all steps of this process templates
    val stepIds: Future[Seq[Int]] = processTemplateDTO.getProcessStepTemplateIds(processTemplateId)

    val process = for {
      allApprovedProcessTemplates <- processTemplateDTO.getApprovedProcessTemplates //Get all approved process templates
      processTemplate <- processTemplateDTO.getProcessTemplate(processTemplateId) // Get the Process Template
      prerequisites <- getProcessTemplateForEdit(processPrerequisitesDTO.getProcessPrerequisiteProcessTemplateIdsByProcessTemplateId(processTemplateId))
      postConditions <- getProcessTemplateForEdit(processPostConditionsDTO.getProcessPostConditionProcessTemplateIdsByProcessTemplateId(processTemplateId))
      approvedProcessTemplate <- processTemplateDTO.getProcessTemplate(processTemplate.get.approveprocess)
      trainedProcessTemplate <- processTemplateDTO.getProcessTemplate(processTemplate.get.trainingsprocess)
      processSteps <- processTemplateDTO.getProcessStepTemplates(processTemplateId)
      // Step prerequisites
      processStepsPrerequisites <- getProcessStepsPrerequisites(stepIds)
      processStepsPrerequisiteProcessTemplate <- getProcessStepsPrerequisiteProcessTemplate(stepIds)
      processTemplatesForStepPrerequisites <- getProcessTemplateForStepPrerequisite(stepIds)
      // Step post conditions
      processStepsPostConditions <- getProcessStepsPostConditions(stepIds)
      processStepPostConditionProcessTemplate <- getProcessStepPostConditionProcessTemplate(stepIds)
      processTemplatesForStepPostConditions <- getProcessTemplateForStepPostCondition(stepIds)
      // Derived processes
      derivedProcesses <- getDerivedProcesses(stepIds)
      processTemplatesForStepDerivedProcesses <- getProcessStepsDerivedProcesses(stepIds)
      // Process to process step
      processStepsTemplates_ProcessTemplates <- getProcessStepsTemplates_ProcessTemplates(stepIds)
      processTemplatesForProcessTemplatesToProcessStep <- getProcessTemplateToProcessStepId(stepIds)
      responsible <- raciProcessTemplateDTO.getResponsibleProcessTemplates(processTemplateId) // get all responsibles for this process template
      accountable <- raciProcessTemplateDTO.getAccountableProcessTemplates(processTemplateId) // get all accountables for this process template
      consulted <- raciProcessTemplateDTO.getConsultedProcessTemplates(processTemplateId) // get all consulted for this process template
      informed <- raciProcessTemplateDTO.getInformedProcessTemplates(processTemplateId) // get all consulted for this process template
    } yield (allApprovedProcessTemplates, processTemplate, prerequisites, postConditions, processSteps, processStepsPrerequisites,
      processStepsPrerequisiteProcessTemplate, processTemplatesForStepPrerequisites, processStepsPostConditions, processStepPostConditionProcessTemplate, processTemplatesForStepPostConditions, derivedProcesses,
      processTemplatesForStepDerivedProcesses, processStepsTemplates_ProcessTemplates, processTemplatesForProcessTemplatesToProcessStep, approvedProcessTemplate, trainedProcessTemplate, responsible, accountable, consulted, informed)

    process.flatMap({ case (allApprovedProcessTemplates, processTemplate, prerequisites, postConditions, processSteps, processStepsPrerequisites,
    processStepsPrerequisiteProcessTemplate, processTemplatesForStepPrerequisites, processStepsPostConditions, processStepPostConditionProcessTemplate, processTemplatesForStepPostConditions, derivedProcesses,
    processTemplatesForStepDerivedProcesses, processStepsTemplates_ProcessTemplates, processTemplatesForProcessTemplatesToProcessStep, approvedProcessTemplate, trainedProcessTemplate, responsible, accountable, consulted, informed) =>

      Future.sequence {
        processSteps.map {
          step =>
            val prerequisiteFuture = processStepPrerequisitesDTO.getProcessStepPrerequisiteProcessTemplateIds(step.id.get)

            prerequisiteFuture.map(prereqTemplates => {

              processTemplateDTO.getProessTemplatesForStepPreqrequsites(prereqTemplates).map(pres => {
                  println("test", pres) // they are printed
                  step.stepPrerequisites ++Some(pres)
                  println("test2", step)
                 step
                })

            })
        }
      }.map {
        stepTemplates =>
          Ok(Json.obj(
            "allApprovedProcessTemplates" -> allApprovedProcessTemplates,
            "processTemplate" -> processTemplate,
            "prerequisites" -> prerequisites,
            "postConditions" -> postConditions,
            "approvedProcessTemplate" -> approvedProcessTemplate,
            "trainedProcessTemplate" -> trainedProcessTemplate,
            // Step prerequisites
            "processStepsPrerequisites" -> processStepsPrerequisites,
            "processStepsPrerequisiteProcessTemplate" -> processStepsPrerequisiteProcessTemplate,
            "processTemplatesForStepPrerequisites" -> processTemplatesForStepPrerequisites,
            // Step post conditions
            "processStepsPostConditions" -> processStepsPostConditions,
            "processStepPostConditionProcessTemplate" -> processStepPostConditionProcessTemplate,
            "processTemplatesForStepPostConditions" -> processTemplatesForStepPostConditions,
            // Derived processes
            "derivedProcesses" -> derivedProcesses,
            "processTemplatesForStepDerivedProcesses" -> processTemplatesForStepDerivedProcesses,
            // Process to process step
            "processStepsTemplates_ProcessTemplates" -> processStepsTemplates_ProcessTemplates,
            "processTemplatesForProcessTemplatesToProcessStep" -> processTemplatesForProcessTemplatesToProcessStep,
            "steps" -> "stepTemplates",
            "responsible" -> responsible,
            "accountable" -> accountable,
            "consulted" -> consulted,
            "informed" -> informed
          ))
      }
    })
  }

enter image description here

2 个答案:

答案 0 :(得分:6)

Future.sequenceSeq[Future[ProcessStepTemplatesModel]]转换为Future[Seq[ProcessStepTemplatesModel]]。因此,如果您只需要Seq[ProcessStepTemplatesModel],您可以等到将来到来

Await.result({
  Future.sequence {
    processSteps.map ...
  }
}, 1 minute)

但是在你的情况下,阻止是否是一个好主意,只有你可以知道。

例如,这里有一个控制器,List[Future[Int]]并应用Future.sequence打印整数:

/app/controllers/HomeController.scala

package controllers

import javax.inject._

import play.api._
import play.api.mvc._

import scala.concurrent.Future

import scala.concurrent.ExecutionContext.Implicits.global

@Singleton
class HomeController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {

  def index() =
    Action.async {
      Future.sequence {
        List(Future(1), Future(2))
      }.map { nums =>
          Ok(views.html.index(nums))
      }
    }
}

/app/views/index.scala.html

@(nums: List[Int])

@main("Welcome to Play") {
  <h1>Welcome to Play!</h1>
    <ul>
    @for(num <- nums) {
        <li>@num</li>
    }
    </ul>
}

好的,这个与你的相似,并打印11,12:

  def index() =
    Action.async {
      Future.sequence {
        List(1, 2).map(num =>
          Future{
            Thread.sleep(5000)
            10
          }.map(n =>
            num + n
          )
        )
      }.map {
        nums =>
          Ok(views.html.index(nums))
      }
    }

下面

             step.stepPrerequisites ++Some(pres)
             step

我无法看到您如何使用step.stepPrerequisites ++Some(pres)的值,而是返回原始step。有副作用吗?

答案 1 :(得分:1)

工作解决方案是:

alert(data.id)