OSDN Git Service

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