OSDN Git Service

Appendix Atach list at the Notify list , show Notify and show Reply
[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 org.squeryl._
10 import org.squeryl.PrimitiveTypeMode._
11 object ReplyController extends Controller{
12   val replyRegistForm = Form(
13       mapping(
14           "message" -> nonEmptyText,
15           "users" -> mapping (
16               "name" -> nonEmptyText
17             )(UserForm.apply)(UserForm.unapply),
18             "atach" -> mapping {
19                 "grpName" -> text
20               }(AtachForm.apply)(AtachForm.unapply)
21         )(ReplyForm.apply)(ReplyForm.unapply)
22    )
23    
24   def createReply(notifyId: Long, replyId: Long, notifyType: Long, replyType: Long, partId: Long, state: Long) = Action { implicit request =>
25     Ok(views.html.createReplyForm(replyRegistForm, notifyId, replyId, replyType, partId, notifyType, state))
26   }
27   
28   def replyRegistration(notifyId: Long, replyId: Long, partId: Long, notifyType: Long, state: Long) = Action(parse.multipartFormData) { implicit request =>
29     replyRegistForm.bindFromRequest.fold(
30         formWithErrors => BadRequest(views.html.createReplyForm(formWithErrors, notifyId, replyId, 0, partId, notifyType, state)),
31         reply => {
32           inTransaction {
33             val replyUser = PartsListDb.users.where(u => u.name like reply.users.name + "%").head
34             val newReply = PartsListDb.replies.insert(Reply(reply.message, replyUser.id, notifyId, replyId, reply.atach))
35             val uploadAtach = UploadAtach()
36             request.body.file("atach").map { atach =>
37                 uploadAtach.uploadAtach(atach, newReply.atach.grpName, 0, 0, newReply.id)
38               }
39             var notify: Notify = null
40             if(notifyId!=0) {
41                 // 通知直下のreplyの場合、notifyより対象Partを割り出し、プロジェクトユーザー抽出。メイル通知。
42                 notify = PartsListDb.notifies.where(n => n.id === notifyId).head
43             } else {
44               // reply配下のreplyの場合、再帰処理によりnotify抽出後、対象Partを割り出し、プロジェクトユーザー抽出。メイル通知。
45               val targetReply = PartsListDb.replies.where(tr => tr.id === replyId).head
46                 val topReply = upRecursion(targetReply)
47                 notify = PartsListDb.notifies.where(n => n.id === topReply.notifyId).head              
48               } 
49             val targetPart = notify.part.assign(notify.part.head)
50             val users = targetPart.project.assign(targetPart.project.head).users
51             val sendMail = SendMail(2, replyUser.email, targetPart.id, notifyType, state)
52             for(user <- users) {
53                sendMail.sendMail(user.email)
54               }
55            }
56           Ok(views.html.issueresult(2))
57          }
58      )
59   }
60   
61   def showReply(id: Long, partId: Long, notifyType: Long, replyType: Long, state: Long) = Action { implicit request =>
62     inTransaction {
63       val reply = PartsListDb.replies.where(r => r.id === id).head
64       val replyForm = ReplyForm(reply.message, UserForm(reply.user.assign(reply.user.head).name), null)
65       Ok(views.html.showReplyForm(replyRegistForm.fill(replyForm), id, partId, notifyType, replyType, state, reply))
66     }
67   }
68   
69   def upRecursion(reply: Reply):Reply = {
70     if(reply.parentReply.size == 0) {
71       return reply
72     } else {
73       println("Messages " + reply.message)
74       upRecursion(reply.parentReply.head)
75     }
76   }
77 }