• R/O
  • HTTP
  • SSH
  • HTTPS

PartList: Commit

Product Data Management


Commit MetaInfo

Revision7ca458f2471430f95ce9dd433abd45351979f678 (tree)
Time2014-12-01 17:49:40
Authoruyaji <yuichiro.uyama@gmai...>
Commiteruyaji

Log Message

The implementation the Future and the Lambda expression.

Change Summary

Incremental Difference

--- a/PartsList/PartsList/app/controllers/PartsListController.scala
+++ b/PartsList/PartsList/app/controllers/PartsListController.scala
@@ -21,10 +21,11 @@ import com.google.inject._
2121 import modules._
2222 import scala.io.Source
2323 import java.io.FileInputStream
24-import scala.concurrent.Future
24+
2525 import play.api.libs.concurrent.Execution.Implicits._
26+import scala.concurrent.Future
2627
27-object PartsListController extends Controller{
28+object PartsListController extends Controller{
2829 val inject = Guice.createInjector(new ServiceModules)
2930 val projectManager = inject.getInstance(classOf[ProjectManager])
3031 val Home = Redirect(routes.PartsListController.index(0,0,"",""))
@@ -42,8 +43,8 @@ object PartsListController extends Controller{
4243 "grpName" -> text
4344 )(CsvForm.apply)(CsvForm.unapply)
4445 )
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 {
4748 Action { implicit request =>
4849 if(request == null) {
4950 Ok(views.html.errors.errorNotAuthentication())
@@ -66,17 +67,23 @@ object PartsListController extends Controller{
6667 if(request == null) {
6768 Ok(views.html.errors.errorNotAuthentication())
6869 } 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+ }
7274 }
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+ }
7886 }
79- }
8087 }
8188 }
8289 }
@@ -249,4 +256,4 @@ object PartsListController extends Controller{
249256 }
250257 }
251258
252-}
\ No newline at end of file
259+}
--- a/PartsList/PartsList/app/controllers/services/PartRecursion.scala
+++ b/PartsList/PartsList/app/controllers/services/PartRecursion.scala
@@ -2,58 +2,54 @@ package controllers.services
22 import models._
33 import beans._
44 import scala.collection.immutable.Seq
5+
56 case class PartRecursion(){
67
78 def down(parentPart: Part, parentQuantity: Long, opt: Int, key: String):Seq[PartsListShowBean] = {
9+ println("*********** Check2 **********")
810 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)
1113 if(pr.delDcId==0) {
1214 var quantity:Long = 0
1315 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")) {
1921 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))
2123 } else {
2224 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+ })
3534 return partBuffer
3635 }
3736
3837 def up(childPart: Part, opt: Int): Seq[PartsListBean] = {
3938 var partBuffer = Seq[PartsListBean]()
4039 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
5754 }
58-
5955 }
\ No newline at end of file
--- a/PartsList/PartsList/app/controllers/services/PartsList.scala
+++ b/PartsList/PartsList/app/controllers/services/PartsList.scala
@@ -1,57 +1,96 @@
11 package controllers.services
22 import scala.collection.immutable.Map
33 import scala.collection.immutable.Seq
4-import org.squeryl.PrimitiveTypeMode._
54 import beans._
65 import models._
76 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+
815 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)))
1928
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
5282 }
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+ }
5695
5796 }
\ No newline at end of file
Show on old repository browser