OSDN Git Service

Refactoring to PartsMasterController
[open-pdm-light/PartList.git] / PartsList / PartsList / app / controllers / ReplyController.scala
1 package controllers
2 import play.api._
3 import play.api.mvc._
4 import play.api.data._
5 import play.api.data.Forms._
6 import forms._
7 import models._
8 import utils._
9 import services._
10 import org.squeryl._
11 import org.squeryl.PrimitiveTypeMode._
12 import scala.collection.mutable.ArrayBuffer
13 object ReplyController extends Controller{
14   val replyRegistForm = Form(
15       mapping(
16           "message" -> nonEmptyText,
17           "users" -> mapping (
18               "name" -> nonEmptyText
19             )(UserForm.apply)(UserForm.unapply),
20             "atach" -> mapping {
21                 "grpName" -> text
22               }(AtachForm.apply)(AtachForm.unapply)
23         )(ReplyForm.apply)(ReplyForm.unapply)
24    )
25    
26   def createReply(notifyId: Long, replyId: Long, notifyType: Long, replyType: Long, partId: Long, state: Long) = Action { implicit request =>
27     Ok(views.html.createReplyForm(replyRegistForm, notifyId, replyId, replyType, partId, notifyType, state))
28   }
29   
30   def replyRegistration(notifyId: Long, replyId: Long, partId: Long, notifyType: Long, state: Long) = Action(parse.multipartFormData) { implicit request =>
31     replyRegistForm.bindFromRequest.fold(
32         formWithErrors => BadRequest(views.html.createReplyForm(formWithErrors, notifyId, replyId, 0, partId, notifyType, state)),
33         reply => {
34           inTransaction {
35             val replyUser = PartsListDb.users.where(u => u.name like reply.users.name + "%").head
36             val newReply = PartsListDb.replies.insert(Reply(reply.message, replyUser.id, notifyId, replyId, reply.atach))
37             var usersBuffer = ArrayBuffer[User]()
38             request.body.file("atach").map { atach =>
39                 AtachManager().uploadAtach(atach, newReply.atach.grpName, 0, 0, newReply.id)
40               }
41             var notify: Notify = null
42             if(notifyId!=0) {
43                 // 通知直下のreplyの場合、notifyより対象Partを割り出し、プロジェクトユーザー抽出。メイル通知。
44                 notify = PartsListDb.notifies.where(n => n.id === notifyId).head
45             } else {
46               // reply配下のreplyの場合、再帰処理によりnotify抽出後、対象Partを割り出し、プロジェクトユーザー抽出。メイル通知。
47               val targetReply = PartsListDb.replies.where(tr => tr.id === replyId).head
48                 val topReply = upRecursion(targetReply)
49                 notify = PartsListDb.notifies.where(n => n.id === topReply.notifyId).head              
50               }
51               // 部品表通知/依頼の回答であれば、Notifyが紐付くPartのプロジェクトメンバにメイル通知。
52             if(notifyType == 0 || notifyType == 1) {
53                 val targetPart = notify.part.assign(notify.part.head)
54                 val users = targetPart.project.assign(targetPart.project.head).users
55                 val sendMail = SendMail(2, replyUser.email, targetPart.id, 0, notifyType, state)
56                 for(user <- users) {
57                         sendMail.sendMail(user.email)
58                 }
59               }
60               // 設計変更通知/依頼の回答であれば、Notifyが紐付くDesin ChangeのdelRelation、addRelationの全ての上品番、
61               // 子品番のプロジェクトメンバにメイル通知。
62             if(notifyType == 3 || notifyType == 4) {
63                 //設計変更対象リレーションの抽出
64               val targetRelations = notify.designCgange.assign(notify.designCgange.head).delPartRelation.++:(notify.designCgange.assign(notify.designCgange.head).addPartRelation)
65                 //各リレーションの上位品番と子品番のプロジェクトメンバー抽出
66               for(targetRelation <- targetRelations) {
67                 var users = targetRelation.parent.head.project.assign(targetRelation.parent.head.project.head).users
68                 users.copyToBuffer(usersBuffer)
69                 users = targetRelation.child.head.project.assign(targetRelation.child.head.project.head).users
70                 users.copyToBuffer(usersBuffer)
71                 }
72               val sendMail = SendMail(2, replyUser.email, 0, notify.designCgange.head.id, notifyType, state)
73               for(user <- usersBuffer.distinct) {
74                 sendMail.sendMail(user.email)
75                 }
76               }
77            }
78           Ok(views.html.issueresult(2, notifyType))
79          }
80      )
81   }
82   
83   def showReply(id: Long, partId: Long, notifyType: Long, replyType: Long, state: Long) = Action { implicit request =>
84     inTransaction {
85       val reply = PartsListDb.replies.where(r => r.id === id).head
86       val replyForm = ReplyForm(reply.message, UserForm(reply.user.assign(reply.user.head).name), null)
87       Ok(views.html.showReplyForm(replyRegistForm.fill(replyForm), id, partId, notifyType, replyType, state, reply))
88     }
89   }
90   
91   def upRecursion(reply: Reply):Reply = {
92     if(reply.parentReply.size == 0) {
93       return reply
94     } else {
95       println("Messages " + reply.message)
96       upRecursion(reply.parentReply.head)
97     }
98   }
99   
100   def appendAtach(id: Long, partId: Long, notifyType: Long, replyType: Long, state: Long) = Action(parse.multipartFormData) { implicit request =>
101     replyRegistForm.bindFromRequest.fold(
102         formWithErrors => BadRequest(views.html.showReplyForm(formWithErrors, id, partId, notifyType, replyType, state, null)),
103 //        formWithErrors => BadRequest(views.html.error(formWithErrors)),
104         reply => {
105           inTransaction {
106             request.body.file("atach").map { atach =>
107                 AtachManager().uploadAtach(atach, reply.atach.grpName, 0, 0, id)
108               }
109             Redirect(routes.ReplyController.showReply(id, partId, notifyType, replyType, state))
110             }
111           }
112      )
113   }
114 }