2017-09-13 67 views
0

我有一個複雜的scala模型,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 
)) 
}) 
} 
+0

'processSteps'的類型是什麼? '選項[']'?那個表達式在哪裏被使用? – pedromss

+3

你應該使用更短的方法名稱... – pedromss

+0

processSteps是一個未來[Seq [ProcesStepsModel]] – Felix

回答

1

看起來是什麼step.prerequisites是選項[序列[models.ProcessStepPrerequisitesModel]和你想分配未來這一點,processSteps是未來[序號[ProcesStepsModel]。

你似乎想要做什麼沿應當注意的

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] 

線是混合期貨和突變是充滿了危險,並帶回了很多的問題與併發性期貨排序的治癒。我將採取認真的看的東西代替分配如(假設ProcessStepsModel是一個案例類):

processSteps.map { psteps: Seq[ProcessStepsModel] => 
    Future.sequence(psteps.map { 
    step => { 
     val prerequisiteFuture = processStepPrerequisitesDTO.getProcessStepPrerequisiteIdsByProcessTemplateIdSeq(step.id) 
     prerequisiteFuture.map(prereqs => step.copy(prerequisites = Some(prereqs))) 
    } 
    }) 
} // Has type Future[Seq[models.ProcessStepPrerequisitesModel]] 

對象的創作似乎昂貴,但他們很可能不會在實踐中爲貴因爲防禦性的等待引起的阻擋(以及與之並行的超時)或突變引入的海森堡。

+0

感謝,我得到這個錯誤: '實測值:序號[models.ProcessStepTemplatesModel] => scala.concurrent.Future [序號[models.ProcessStepTemplatesModel] 需要:models.ProcessStepTemplatesModel =>? ' – Felix

+0

你也許有提示嗎? – Felix

+0

我認爲它非常接近目標,如果你有幾分鐘的時間再看一次,那將是驚人的。謝謝 :) – Felix