OSDN Git Service

Refactoring
[open-pdm-light/PartList.git] / PartsList / PartsList / app / controllers / PartsMasterController.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.services._
10 import beans._
11 import services._
12 import org.squeryl._
13 import org.squeryl.PrimitiveTypeMode._
14 import scala.collection.mutable.ArrayBuffer
15
16 object PartsMasterController extends Controller{
17    
18    val Home = Redirect(routes.PartsMasterController.list(0,""))
19
20    val partRegistForm = Form(
21        mapping(
22            "name" -> nonEmptyText,
23            "atach" -> mapping(
24                "grpName" -> text
25              )(AtachForm.apply)(AtachForm.unapply),
26            "projectName" -> nonEmptyText,
27            "cost" -> longNumber
28         )(PartForm.apply)(PartForm.unapply)
29     )
30
31         def create = Action { implicit request =>
32           Ok(views.html.createPartForm(partRegistForm))
33         }
34
35         def partRegistration() = Action(parse.multipartFormData) { implicit request =>
36           partRegistForm.bindFromRequest.fold(
37               formWithErrors => BadRequest(views.html.createPartForm(formWithErrors)),
38 //            formWithErrors => BadRequest(views.html.error(formWithErrors)),
39               part => {
40                 inTransaction {
41                   val newPart = PartManager().insert(part)
42               request.body.file("atach").map { atach =>
43                 AtachManager().uploadAtach(atach, newPart.atach.grpName, newPart.id, 0, 0)
44                     }
45                   Home.flashing("success" -> "Part %s has been created".format(part.name))
46                 }  
47               }
48           )
49         }
50
51         def update(id:Long) = Action { implicit request =>
52           inTransaction {
53             val part = PartManager().getById(id)
54             val partForm = PartForm(part.name, null, part.project.assign(part.project.head).name, part.price)
55                 Ok(views.html.updatePartForm(partRegistForm.fill(partForm), id, part))
56           }
57         }
58
59         def partModification(id:Long) = Action(parse.multipartFormData) { implicit request =>
60           partRegistForm.bindFromRequest.fold(
61               formWithErrors => BadRequest(views.html.updatePartForm(formWithErrors, id , null)),
62               part => {
63                 inTransaction {
64                   val updPart = PartManager().update(part, id)
65                    request.body.file("atach").map { atach =>
66                      AtachManager().uploadAtach(atach, part.atach.grpName, updPart.id, 0, 0)
67                     }
68                   Home.flashing("success" -> "Part %s has been created".format(part.name))
69                 }  
70               }
71           )
72         }
73
74         def partDelete(id:Long) = Action {
75           inTransaction {
76             PartManager().delete(id)
77             Home.flashing("success" -> "Part %s has been created".format(id))
78           }
79         }
80         
81         def list(page:Int, key:String) = Action { implicit request =>
82           inTransaction {
83                   val row = Integer.decode(Messages("list.row"))
84                   val buff = PartManager().getByLikeName(key)
85                   Ok(views.html.partsmastershow(buff.page(page*row, row), buff.size, key, page))
86           }
87         }       
88
89 }