OSDN Git Service

Many many refactoring.
[open-pdm-light/PartList.git] / PartsList / PartsList / app / controllers / NotifyController.scala
1 package controllers
2
3 import play.api._
4 import play.api.i18n._
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 beans._
13 import controllers.services._
14 import org.squeryl._
15 import org.squeryl.PrimitiveTypeMode._
16 import scala.collection.mutable.ArrayBuffer
17
18 object NotifyController extends Controller{
19
20   var notifies :org.squeryl.Query[Notify] = null
21
22   val notifyRegistForm = Form(
23       mapping(
24           "message" -> nonEmptyText,
25           "users" -> mapping(
26                           "name" -> nonEmptyText
27           )(UserForm.apply)(UserForm.unapply),
28           "state" -> text,
29           "atach" -> mapping(
30               "grpName" -> text
31            )(AtachForm.apply)(AtachForm.unapply)
32       )(NotifyForm.apply)(NotifyForm.unapply)
33   )
34   
35   def createNotify(partId: Long, designChangeId: Long, notifyType: Long) = Action{ implicit request =>
36         Ok(views.html.createNotifyForm(notifyRegistForm, partId, designChangeId, notifyType))
37   }
38   
39   def notifyRegistration(partId: Long, designChangeId: Long, notifyType: Long) = Action(parse.multipartFormData) { implicit request =>
40     notifyRegistForm.bindFromRequest.fold(
41         formWithErrors => BadRequest(views.html.createNotifyForm(formWithErrors, partId, designChangeId, notifyType)),
42 //        formWithErrors => BadRequest(views.html.error(formWithErrors)),
43         notify => {
44           inTransaction {
45             val announceUser = UserManager().getByName(notify.users.name)
46             val paramState = Integer.decode(notify.state)
47             val newNotify = NotifyManager().insert(notify, notifyType, partId, designChangeId, paramState)
48             request.body.file("atach").map { atach =>
49                 AtachManager().uploadAtach(atach, notify.atach.grpName, 0, newNotify.id, 0)
50               }
51             var usersBuffer = ArrayBuffer[User]()
52               // 部品通知の場合
53             if(partId != 0) {
54               val targetPart = newNotify.part.head
55                 val users = targetPart.project.head.users
56                 val sendMail = SendMail(notifyType, announceUser.email, targetPart.id, 0, notifyType, paramState.longValue())
57                 for(user <- users) {
58                         sendMail.sendMail(user.email)
59                 }
60               // 設計変更通知の場合
61             } else {
62                  //設計変更対象リレーションの抽出
63                val targetRelations = DesignChangeManager().getTargetRelationByDesignChangeId(designChangeId)
64                  //各リレーションの上位品番と子品番のプロジェクトメンバー抽出
65                for(targetRelation <- targetRelations) {
66                  var users = targetRelation.parent.head.project.head.users
67                  users.copyToBuffer(usersBuffer)
68                  users = targetRelation.child.head.project.head.users
69                  users.copyToBuffer(usersBuffer)
70                  }
71                 //前述処理で抽出した全メンバーにメイル送信
72               val sendMail = SendMail(notifyType, announceUser.email, 0, designChangeId, notifyType, paramState.longValue())
73               for(user <- usersBuffer.distinct) {
74                 sendMail.sendMail(user.email)
75                 }
76             }
77           Ok(views.html.issueresult(notifyType, notifyType))
78           }
79         }
80     )
81   }
82
83   def showNotify(id: Long, partId: Long, designChangeId: Long, notifyType: Long, state: Long) = Action { implicit request =>
84     inTransaction {
85       val notify = NotifyManager().getById(id)
86       val notifyForm = NotifyForm(notify.message, UserForm(notify.user.assign(notify.user.head).name), Messages("notify.state" + notify.state), null)
87       Ok(views.html.showNotifyForm(notifyRegistForm.fill(notifyForm), id, partId, designChangeId, notifyType, state, notify))
88     }
89   }
90
91   def updateState(id: Long, partId: Long, designChangeId: Long, notifyType: Long, state: Long) = Action { implicit request =>
92     inTransaction {
93       val notify = NotifyManager().getById(id)
94       notify.state = 1
95       NotifyManager().update(notify)
96       val notifyForm = NotifyForm(notify.message, UserForm(notify.user.assign(notify.user.head).name), Messages("notify.state" + notify.state), null)
97       Ok(views.html.showNotifyForm(notifyRegistForm.fill(notifyForm),id, partId, designChangeId: Long, notifyType, state, notify))
98     }
99   }
100   
101   def appendAtach(id: Long, partId: Long, designChangeId: Long, notifyType: Long, state: Long) = Action(parse.multipartFormData) { implicit request =>
102     notifyRegistForm.bindFromRequest.fold(
103         formWithErrors => BadRequest(views.html.showNotifyForm(formWithErrors, id, partId, designChangeId, notifyType, state, null)), 
104         notify => {
105           inTransaction {
106             request.body.file("atach").map{ atach =>
107                 AtachManager().uploadAtach(atach, notify.atach.grpName, 0, id, 0)
108               }
109             Redirect(routes.NotifyController.showNotify(id, partId, designChangeId, notifyType, state))
110           }
111         }
112     )
113   }
114   
115   def listRecursion(page: Int, partId: Long, notifyType: Long, state: Long) = Action { implicit request =>
116     val notifyBuffer = ArrayBuffer[NotifyBean]()
117     inTransaction {
118       val row = Integer.decode(Messages("list.row"))
119       val notifyPart = PartManager().getById(partId)
120       if(state==9) {
121           notifies = notifyPart.notifies.where(n => n.notifyType === notifyType)
122        } else {
123          notifies = notifyPart.notifies.where(n => n.notifyType === notifyType and n.state === state)
124        }
125       for(notify <- notifies) {
126           var i:Int =0
127           notifyBuffer += NotifyBean(i, notify.message, notify.user.head.name, notify.id, Messages("notify.state" + notify.state), notify.atachs)
128           for(reply <- notify.replies) {
129 //          ReplyRecursion().down(reply, notifyBuffer, i)
130             for(returnNotifyBuffer <- ReplyRecursion().down(reply, i)) {
131               notifyBuffer += returnNotifyBuffer
132             }
133           }
134        }
135       Ok(views.html.notifylist(notifyBuffer.drop(page*row).take(row), notifyBuffer.size, partId, 0, notifyPart.name, page, notifyType, state))
136     }
137   }
138   
139   
140   def designChangeListRecursion(page: Int, designChangeId: Long, notifyType: Long, state: Long) = Action { implicit request =>
141     val notifyBuffer = ArrayBuffer[NotifyBean]()
142     inTransaction {
143       val row = Integer.decode(Messages("list.row"))
144       val designChange = DesignChangeManager().getById(designChangeId)
145       if(state==9) {
146           notifies = designChange.notifies.where(n => n.notifyType === notifyType)
147       } else {
148          notifies = designChange.notifies.where(n => n.notifyType === notifyType and n.state === state)
149        }
150       for(notify <- notifies) {
151         var i:Int =0
152         notifyBuffer += NotifyBean(i, notify.message, notify.user.assign(notify.user.head).name, notify.id, Messages("notify.state" + notify.state), notify.atachs)
153         for(reply <- notify.replies) {
154          for(returnNotifyBuffer <- ReplyRecursion().down(reply, i)) {
155             notifyBuffer += returnNotifyBuffer 
156           }
157         }
158       }
159       Ok(views.html.notifylist(notifyBuffer.drop(page*row).take(row), notifyBuffer.size, 0, designChange.id, designChange.dcNo, page, notifyType, state))
160     }
161   }
162   
163 }