scala操作Model对象值

时间:2017-09-13 13:36:36

标签: scala playframework

我有一个复杂的scala模型,

我希望更改模型的值。

我试过了:

  processSteps.map{
    step => {
      step.prerequisites = processStepPrerequisitesDTO.getProcessStepPrerequisiteIdsByProcessTemplateIdSeq(step.id)
    }
  }

但我收到了这个错误:

found   : scala.concurrent.Future[Seq[models.ProcessStepPrerequisitesModel]]
required: Option[Seq[models.ProcessStepPrerequisitesModel]]

如何解决此错误?

其他信息

def getProcessStepPrerequisiteIdsByProcessTemplateIdSeq(processStepTemplateId: Int): Future[Seq[ProcessStepPrerequisitesModel]] = {
    db.run(processStepsPrerequisites.filter(p => p.processsteptemplate === processStepTemplateId).result)
}

更新

也尝试了这个:

 processSteps.map{
    step => {
      processStepPrerequisitesDTO.getProcessStepPrerequisiteIdsByProcessTemplateIdSeq(step.id.get).map(a => {
        step.prerequisites ++ a
      })
    }
  }

但返回一个空数组

更新

我在地图中使用它

   def getEditProcessTemplateData(processTemplateId: Int): Action[AnyContent] = 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.map({ case (allApprovedProcessTemplates, processTemplate, prerequisites, postConditions, processSteps, processStepsPrerequisites,
processStepsPrerequisiteProcessTemplate, processTemplatesForStepPrerequisites, processStepsPostConditions, processStepPostConditionProcessTemplate, processTemplatesForStepPostConditions, derivedProcesses,
processTemplatesForStepDerivedProcesses, processStepsTemplates_ProcessTemplates, processTemplatesForProcessTemplatesToProcessStep, approvedProcessTemplate, trainedProcessTemplate, responsible, accountable, consulted, informed) =>

  processSteps.map{
    step => {
      processStepPrerequisitesDTO.getProcessStepPrerequisiteIdsByProcessTemplateIdSeq(step.id.get).map(a => {
        step.prerequisites ++ a
      })
    }
  }

  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" -> processSteps,
    "responsible" -> responsible,
    "accountable" -> accountable,
    "consulted" -> consulted,
    "informed" -> informed
  ))
})
}

1 个答案:

答案 0 :(得分:1)

它的外观是step.prerequisites是Option [Seq [models.ProcessStepPrerequisitesModel]],你正在尝试将未来分配给它,而processSteps是Future [Seq [ProcesStepsModel]]。 / p>

你似乎想要做的是

string[] file_names1 = Directory.GetFileSystemEntries(dir1, "*",
                       SearchOption.AllDirectories);

值得注意的是,混合期货和突变充满了危险,并带来了很多并发问题,即期货有所痊愈。我会认真考虑用类似的东西替换赋值(假设ProcessStepsModel是一个case类):

processSteps.flatMap { psteps: Seq[ProcessStepTemplatesModel] =>
    val updateFutures: Seq[Future[Unit]] = psteps.map {
      step => {
        val prerequisiteFuture = processStepPrerequisitesDTO.getProcessStepPrerequisiteIdsByProcessTemplateIdSeq(step.id.get)
        prerequisiteFuture.foreach(prereqs => step.prerequisites = Some(prereqs))
      }
    }
    Future.sequence(updateFutures)
} // Has type Future[Unit]

对象创作可能看起来很昂贵,但它们在实践中可能不会像防御性Awaits引入的阻塞(以及手头的超时)或者变异引入的heisenbug那样昂贵。