Product Data Management
| Revision | 7ca458f2471430f95ce9dd433abd45351979f678 (tree) |
|---|---|
| Time | 2014-12-01 17:49:40 |
| Author | uyaji <yuichiro.uyama@gmai...> |
| Commiter | uyaji |
The implementation the Future and the Lambda expression.
| @@ -21,10 +21,11 @@ import com.google.inject._ | ||
| 21 | 21 | import modules._ |
| 22 | 22 | import scala.io.Source |
| 23 | 23 | import java.io.FileInputStream |
| 24 | -import scala.concurrent.Future | |
| 24 | + | |
| 25 | 25 | import play.api.libs.concurrent.Execution.Implicits._ |
| 26 | +import scala.concurrent.Future | |
| 26 | 27 | |
| 27 | -object PartsListController extends Controller{ | |
| 28 | +object PartsListController extends Controller{ | |
| 28 | 29 | val inject = Guice.createInjector(new ServiceModules) |
| 29 | 30 | val projectManager = inject.getInstance(classOf[ProjectManager]) |
| 30 | 31 | val Home = Redirect(routes.PartsListController.index(0,0,"","")) |
| @@ -42,8 +43,8 @@ object PartsListController extends Controller{ | ||
| 42 | 43 | "grpName" -> text |
| 43 | 44 | )(CsvForm.apply)(CsvForm.unapply) |
| 44 | 45 | ) |
| 45 | - | |
| 46 | - def index(opt:Int, page:Int, key:String, relationKey:String) = AuthAction { | |
| 46 | + | |
| 47 | + def index(opt:Int, page:Int, key:String, relationKey:String) = AuthAction { | |
| 47 | 48 | Action { implicit request => |
| 48 | 49 | if(request == null) { |
| 49 | 50 | Ok(views.html.errors.errorNotAuthentication()) |
| @@ -66,17 +67,23 @@ object PartsListController extends Controller{ | ||
| 66 | 67 | if(request == null) { |
| 67 | 68 | Ok(views.html.errors.errorNotAuthentication()) |
| 68 | 69 | } else { |
| 69 | - val futureOfPartsBuffer: Future[Seq[PartsListShowBean]] = Future { | |
| 70 | - inTransaction { | |
| 71 | - PartsList().getPartsBuffer(opt, page, key, relationKey) | |
| 70 | + val futureOfPartsBuffer: Future[Seq[PartsListShowBean]] = Future { | |
| 71 | + inTransaction { | |
| 72 | + PartsList().getPartsBuffer(opt, page, key, relationKey) | |
| 73 | + } | |
| 72 | 74 | } |
| 73 | - } | |
| 74 | - val attachment = "attachment; filename=" + key + "_download.csv" | |
| 75 | - Async { | |
| 76 | - futureOfPartsBuffer.map { fpb => | |
| 77 | - Ok(views.html.partsListsCsv(fpb)).withHeaders(CONTENT_DISPOSITION -> attachment).as("text/csv") | |
| 75 | + val attachment = "attachment; filename=" + key + "_download.csv" | |
| 76 | +// Ok(views.html.partsListsCsv(scala.concurrent.Await.result(futureOfPartsBuffer, scala.concurrent.duration.Duration.Inf))).withHeaders(CONTENT_DISPOSITION -> attachment).as("text/csv") | |
| 77 | + Async { | |
| 78 | + futureOfPartsBuffer.map { fpb => | |
| 79 | + var totalCost:Long =0 | |
| 80 | + fpb.map(pb => | |
| 81 | + totalCost += pb.cost | |
| 82 | + ) | |
| 83 | + println("Check2") | |
| 84 | + Ok(views.html.partsListsCsv(fpb)).withHeaders(CONTENT_DISPOSITION -> attachment).as("text/csv") | |
| 85 | + } | |
| 78 | 86 | } |
| 79 | - } | |
| 80 | 87 | } |
| 81 | 88 | } |
| 82 | 89 | } |
| @@ -249,4 +256,4 @@ object PartsListController extends Controller{ | ||
| 249 | 256 | } |
| 250 | 257 | } |
| 251 | 258 | |
| 252 | -} | |
| \ No newline at end of file | ||
| 259 | +} |
| @@ -2,58 +2,54 @@ package controllers.services | ||
| 2 | 2 | import models._ |
| 3 | 3 | import beans._ |
| 4 | 4 | import scala.collection.immutable.Seq |
| 5 | + | |
| 5 | 6 | case class PartRecursion(){ |
| 6 | 7 | |
| 7 | 8 | def down(parentPart: Part, parentQuantity: Long, opt: Int, key: String):Seq[PartsListShowBean] = { |
| 9 | + println("*********** Check2 **********") | |
| 8 | 10 | var partBuffer = Seq[PartsListShowBean]() |
| 9 | - for(child <-parentPart.parts) { | |
| 10 | - val pr = parentPart.partRelation(child.id) | |
| 11 | + parentPart.parts.map(ch => { | |
| 12 | + val pr = parentPart.partRelation(ch.id) | |
| 11 | 13 | if(pr.delDcId==0) { |
| 12 | 14 | var quantity:Long = 0 |
| 13 | 15 | if(opt==2) { |
| 14 | - quantity = parentQuantity * pr.quantity | |
| 15 | - } else { | |
| 16 | - quantity = pr.quantity | |
| 17 | - } | |
| 18 | - if(opt!=3 || opt==3 && child.name.startsWith("Unit")) { | |
| 16 | + quantity = parentQuantity * pr.quantity | |
| 17 | + } else { | |
| 18 | + quantity = pr.quantity | |
| 19 | + } | |
| 20 | + if(opt!=3 || opt==3 && ch.name.startsWith("Unit")) { | |
| 19 | 21 | if(key.isEmpty()) { |
| 20 | - partBuffer = partBuffer.:+(PartsListShowBean(parentPart.name, child, quantity, pr.relationKey, quantity * child.price)) | |
| 22 | + partBuffer = partBuffer.:+(PartsListShowBean(parentPart.name, ch, quantity, pr.relationKey, quantity * ch.price)) | |
| 21 | 23 | } else { |
| 22 | 24 | if(pr.relationKey.equals(key)) { |
| 23 | - partBuffer = partBuffer.:+(PartsListShowBean(parentPart.name, child, quantity, pr.relationKey, quantity * child.price)) | |
| 24 | - } | |
| 25 | - } | |
| 26 | - } | |
| 27 | - if(opt==1 || opt==2 && child.price.==(0) || opt==3) { | |
| 28 | - val partBuffers = PartRecursion().down(child, quantity, opt, key) | |
| 29 | - for (returnPartBuffer <- partBuffers) { | |
| 30 | - partBuffer = partBuffer.:+(returnPartBuffer) | |
| 31 | - } | |
| 32 | - } | |
| 33 | - } | |
| 34 | - } | |
| 25 | + partBuffer = partBuffer.:+(PartsListShowBean(parentPart.name, ch, quantity, pr.relationKey, quantity * ch.price)) | |
| 26 | + } | |
| 27 | + } | |
| 28 | + } | |
| 29 | + if(opt==1 || opt==2 && ch.price.==(0) || opt==3) { | |
| 30 | + down(ch, quantity, opt, key).par.map(rpb => partBuffer = partBuffer.:+(rpb)) | |
| 31 | + } | |
| 32 | + } | |
| 33 | + }) | |
| 35 | 34 | return partBuffer |
| 36 | 35 | } |
| 37 | 36 | |
| 38 | 37 | def up(childPart: Part, opt: Int): Seq[PartsListBean] = { |
| 39 | 38 | var partBuffer = Seq[PartsListBean]() |
| 40 | 39 | if(childPart.parentParts.size == 0 && opt == 2) { |
| 41 | - partBuffer = partBuffer.:+(PartsListBean("", childPart.name, 0, "")) | |
| 42 | - } | |
| 43 | - for(parent <- childPart.parentParts) { | |
| 44 | - if(parent.partRelation(childPart.id).delDcId == 0 ) { | |
| 45 | - if(opt != 2) { | |
| 46 | - val pr = parent.partRelation(childPart.id) | |
| 47 | - partBuffer = partBuffer.:+(PartsListBean(childPart.name, parent.name, pr.quantity, pr.relationKey)) | |
| 48 | - } | |
| 49 | - if(opt != 1) { | |
| 50 | - for(returnPartBuffer <- PartRecursion().up(parent, opt)) { | |
| 51 | - partBuffer = partBuffer.:+(returnPartBuffer) | |
| 52 | - } | |
| 53 | - } | |
| 54 | - } | |
| 55 | - } | |
| 56 | - return partBuffer | |
| 40 | + partBuffer = partBuffer.:+(PartsListBean("", childPart.name, 0, "")) | |
| 41 | + } | |
| 42 | + childPart.parentParts.map( prt => { | |
| 43 | + if(prt.partRelation(childPart.id).delDcId == 0 ) { | |
| 44 | + if(opt != 2) { | |
| 45 | + val pr = prt.partRelation(childPart.id) | |
| 46 | + partBuffer = partBuffer.:+(PartsListBean(childPart.name, prt.name, pr.quantity, pr.relationKey)) | |
| 47 | + } | |
| 48 | + if(opt != 1) { | |
| 49 | + up(prt, opt).par.map(rpb => partBuffer = partBuffer.:+(rpb)) | |
| 50 | + } | |
| 51 | + } | |
| 52 | + }) | |
| 53 | + return partBuffer | |
| 57 | 54 | } |
| 58 | - | |
| 59 | 55 | } |
| \ No newline at end of file |
| @@ -1,57 +1,96 @@ | ||
| 1 | 1 | package controllers.services |
| 2 | 2 | import scala.collection.immutable.Map |
| 3 | 3 | import scala.collection.immutable.Seq |
| 4 | -import org.squeryl.PrimitiveTypeMode._ | |
| 5 | 4 | import beans._ |
| 6 | 5 | import models._ |
| 7 | 6 | import models.services._ |
| 7 | +import scala.concurrent.Future | |
| 8 | +import scala.concurrent.ExecutionContext | |
| 9 | +import scala.concurrent.Await | |
| 10 | +import scala.concurrent.duration.Duration | |
| 11 | +import scala.util.Success | |
| 12 | +import scala.util.Failure | |
| 13 | +import org.squeryl.PrimitiveTypeMode._ | |
| 14 | + | |
| 8 | 15 | case class PartsList() { |
| 9 | - def getPartsBuffer(opt:Int, page:Int, key:String, relationKey:String):Seq[PartsListShowBean] = { | |
| 10 | - var partsBuffer = Seq[PartsListShowBean]() | |
| 11 | - val targetParts = PartManager().getByName(key) | |
| 12 | - if(targetParts.size != 0) { | |
| 13 | - for(returnPartBuffer <- PartRecursion().down(PartManager().getByName(key).head, 1, opt, relationKey)) { | |
| 14 | - partsBuffer = partsBuffer.:+(returnPartBuffer) | |
| 15 | - } | |
| 16 | - } | |
| 17 | - return partsBuffer | |
| 18 | - } | |
| 16 | + def getPartsBuffer(opt:Int, page:Int, key:String, relationKey:String):Seq[PartsListShowBean] = { | |
| 17 | + import ExecutionContext.Implicits.global | |
| 18 | + var partsBuffer = Seq[PartsListShowBean]() | |
| 19 | + val targetParts = PartManager().getByName(key) | |
| 20 | + if(targetParts.size != 0) { | |
| 21 | +// PartRecursion().down(PartManager().getByName(key).head, 1, opt, relationKey).par.map(rpb => partsBuffer = partsBuffer.:+(rpb)) | |
| 22 | + val futurePartBuffer:Future[Seq[PartsListShowBean]] = Future{ | |
| 23 | + inTransaction { | |
| 24 | + PartRecursion().down(PartManager().getByName(key).head, 1, opt, relationKey) | |
| 25 | + } | |
| 26 | + } | |
| 27 | +// futurePartBuffer.map(rpbs => rpbs.par.map(rpb => partsBuffer = partsBuffer.:+(rpb))) | |
| 19 | 28 | |
| 20 | - def getMatrixPartsList(partsListBeans: Seq[Seq[PartsListShowBean]], allChildren: Seq[Part]): Map[Part, Seq[MultiPartsListBean]] = { | |
| 21 | - var matrixPartsListBeans = Map[Part, Seq[MultiPartsListBean]]() | |
| 22 | - // 抽出した各モデル毎の部品表配列に対し、部品の有無をチェエク | |
| 23 | - for(partsListBean <- partsListBeans) { | |
| 24 | - var allChildrenList = Seq[MultiPartsListBean]() | |
| 25 | - // 全ての部品一覧作成 | |
| 26 | - for(child <- allChildren.distinct.sortBy(p => p.name)) { | |
| 27 | - allChildrenList = allChildrenList.:+(MultiPartsListBean(child, 1)) | |
| 28 | - } | |
| 29 | - // 比較対象モデルの部品一覧作成 | |
| 30 | - var targetChildren = Seq[MultiPartsListBean]() | |
| 31 | - for(targetChildlist <- partsListBean) { | |
| 32 | - targetChildren = targetChildren.:+(MultiPartsListBean(targetChildlist.child, 1)) | |
| 33 | - } | |
| 34 | - // 全ての部品一覧の各部品が、比較対象モデルの部品一覧に存在するかチェック。 | |
| 35 | - if(partsListBean.size !=0 ) { | |
| 36 | - for(compare <- allChildrenList) { | |
| 37 | - if(!targetChildren.contains(compare)) { | |
| 38 | - //存在しない場合、表示をOff | |
| 39 | - val position:Int = allChildrenList.indexOf(compare) | |
| 40 | - allChildrenList(position).showOn = 0 | |
| 41 | - } | |
| 42 | - } | |
| 43 | - // 表示制御済みの部品表配列を蓄積 | |
| 44 | - matrixPartsListBeans = matrixPartsListBeans.+((PartManager().getByName(partsListBean.head.parent).head, allChildrenList)) | |
| 45 | - // 構成部品なしの場合、全部品を表示Off | |
| 46 | - } else { | |
| 47 | - for(allChild <- allChildrenList) { | |
| 48 | - allChild.showOn = 0 | |
| 49 | - } | |
| 50 | - // 表示制御済みの部品表配列を蓄積 | |
| 51 | - matrixPartsListBeans =matrixPartsListBeans.+((null, allChildrenList)) | |
| 29 | +/* futurePartBuffer onComplete { | |
| 30 | + case Success(rpbs) => { | |
| 31 | + println("************** Check *********************** = " + rpbs.size) | |
| 32 | + rpbs.map( | |
| 33 | + rpb => { | |
| 34 | + partsBuffer = partsBuffer.:+(rpb) | |
| 35 | + println(rpb.parent + "/" + rpb.child.name ) | |
| 36 | + } | |
| 37 | + ) | |
| 38 | + } | |
| 39 | + case Failure(t) => println("エラーが発生しました。" + t.getMessage()) | |
| 40 | + }*/ | |
| 41 | + | |
| 42 | +/* futurePartBuffer map { | |
| 43 | + rpbs => { | |
| 44 | + println("************** Check *********************** = " + rpbs.size) | |
| 45 | + rpbs.map( | |
| 46 | + rpb => { | |
| 47 | + partsBuffer = partsBuffer.:+(rpb) | |
| 48 | + println(rpb.parent + "/" + rpb.child.name ) | |
| 49 | + } | |
| 50 | + ) | |
| 51 | + } | |
| 52 | + }*/ | |
| 53 | + | |
| 54 | + // 上の2つの処理は共にNonBlockingなので、部品表の展開をまたず、空のpartsBufferを返却し、画面表示(何も表示されない) | |
| 55 | + // 対して、以下の処理は明示的なブロッキング待機なので、部品表展開を待って、partsBufferを蓄積し、画面表示(部品表が表示される) | |
| 56 | + Await.result(futurePartBuffer, Duration.Inf).map(rpb => { | |
| 57 | + println(rpb.parent + "/" + rpb.child.name ) | |
| 58 | + partsBuffer = partsBuffer.:+(rpb) | |
| 59 | + }) | |
| 60 | + } | |
| 61 | + println("** Check3 **") | |
| 62 | + partsBuffer | |
| 63 | + } | |
| 64 | + | |
| 65 | + def getMatrixPartsList(partsListBeans: Seq[Seq[PartsListShowBean]], allChildren: Seq[Part]): Map[Part, Seq[MultiPartsListBean]] = { | |
| 66 | + var matrixPartsListBeans = Map[Part, Seq[MultiPartsListBean]]() | |
| 67 | + // 抽出した各モデル毎の部品表配列に対し、部品の有無をチェエク | |
| 68 | + partsListBeans.map(plb => { | |
| 69 | + var allChildrenList = Seq[MultiPartsListBean]() | |
| 70 | + // 全ての部品一覧作成 | |
| 71 | + allChildren.distinct.sortBy(p => p.name).map(ch => allChildrenList = allChildrenList.:+(MultiPartsListBean(ch, 1))) | |
| 72 | + // 比較対象モデルの部品一覧作成 | |
| 73 | + var targetChildren = Seq[MultiPartsListBean]() | |
| 74 | + plb.par.map(tch => targetChildren = targetChildren.:+(MultiPartsListBean(tch.child, 1))) | |
| 75 | + // 全ての部品一覧の各部品が、比較対象モデルの部品一覧に存在するかチェック。 | |
| 76 | + if(plb.size !=0 ) { | |
| 77 | + allChildrenList.par.map(comp => { | |
| 78 | + if(!targetChildren.contains(comp)) { | |
| 79 | + //存在しない場合、表示をOff | |
| 80 | + val position:Int = allChildrenList.indexOf(comp) | |
| 81 | + allChildrenList(position).showOn = 0 | |
| 52 | 82 | } |
| 53 | - } | |
| 54 | - return matrixPartsListBeans | |
| 55 | - } | |
| 83 | + }) | |
| 84 | + // 表示制御済みの部品表配列を蓄積 | |
| 85 | + matrixPartsListBeans = matrixPartsListBeans.+((PartManager().getByName(plb.head.parent).head, allChildrenList)) | |
| 86 | + // 構成部品なしの場合、全部品を表示Off | |
| 87 | + } else { | |
| 88 | + allChildrenList.par.map(ac => ac.showOn = 0) | |
| 89 | + // 表示制御済みの部品表配列を蓄積 | |
| 90 | + matrixPartsListBeans =matrixPartsListBeans.+((null, allChildrenList)) | |
| 91 | + } | |
| 92 | + }) | |
| 93 | + matrixPartsListBeans | |
| 94 | + } | |
| 56 | 95 | |
| 57 | 96 | } |
| \ No newline at end of file |