Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. package workbook
  2.  
  3. import grails.plugins.springsecurity.Secured
  4. import groovy.json.JsonBuilder
  5. import org.springframework.context.i18n.LocaleContextHolder
  6.  
  7. import java.text.DateFormat
  8. import java.text.SimpleDateFormat
  9.  
  10. import static org.springframework.http.HttpStatus.*
  11. import grails.transaction.Transactional
  12.  
  13. @Transactional(readOnly = true)
  14. @Secured(['IS_AUTHENTICATED_FULLY'])
  15. class WorkbookController {
  16. def testService
  17. def springSecurityService
  18. def workbooksSearchService
  19.  
  20. static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
  21.  
  22. def index() {
  23. if (!LocaleContextHolder.getLocaleContext().getLocale().toString()) {
  24. session['org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'] = new Locale("ru", "RU")
  25. }
  26. render(view: '../index')
  27. }
  28.  
  29. def list(Integer max) {
  30. params.max = Math.min(max ?: 10, 100)
  31. respond Workbook.list(params), model: [workbookInstanceCount: Workbook.count(), iTotalDisplayRecords: workbooksSearchService.getResults(params).iTotalDisplayRecords]
  32. }
  33.  
  34. def show() {
  35. def workbookInstance = Workbook.get(params.id)
  36. workbookInstance.isDocumentEditable = false
  37. return [workbookInstance: workbookInstance]
  38.  
  39. //respond workbookInstance
  40. }
  41.  
  42. def create() {
  43. //render(status: "404", text: "Failed to create workbook")
  44. //testService.serviceMethod()
  45. //println 'user logged in ' + springSecurityService?.principal?.username
  46. Workbook workbook = flash.workbook ?: new Workbook()
  47. workbook.isDocumentEditable = true
  48. if (workbook.workPlaces == null) {
  49. workbook.workPlaces = new ArrayList<WorkPlace>()
  50. }
  51. session.workbookInstance = workbook
  52. return [workbookInstance: workbook]
  53. //respond workbook
  54. }
  55.  
  56. def save() {
  57. Workbook workbookInstance = session.workbookInstance
  58. bindData(workbookInstance, params)
  59. println 'fffffffffffffff ' + workbookInstance.firstName
  60. if (workbookInstance == null) {
  61. notFound()
  62. return
  63. }
  64. workbookInstance.validate()
  65. if (workbookInstance.hasErrors()) {
  66. println 'eeeeeeeeeeeeeeeeeeeeee'
  67. workbookInstance.discard()
  68. flash.workbook = workbookInstance
  69. redirect(action: 'create')
  70. //respond workbookInstance.errors, view: 'create'
  71. return
  72. }
  73.  
  74. workbookInstance.save flush: true
  75.  
  76. redirect(action: "show", id: workbookInstance.id)
  77. //redirect workbookInstance
  78. /*request.withFormat {
  79. form multipartForm {
  80. flash.message = message(code: 'default.created.message', args: [message(code: 'workbookInstance.label', default: 'Workbook'), workbookInstance.id])
  81. redirect workbookInstance
  82. }
  83. '*' { respond workbookInstance, [status: CREATED] }
  84. }*/
  85. }
  86.  
  87. def edit() {
  88. def workbookInstance = Workbook.get(params.id)
  89. workbookInstance.isDocumentEditable = true
  90. println 'workbook ' + workbookInstance
  91. session.workbookInstance = workbookInstance
  92. return [workbookInstance: workbookInstance, errors: workbookInstance.hasErrors()]
  93. //respond workbookInstance
  94. }
  95.  
  96. @Transactional
  97. def update() {
  98. bindData()
  99. Workbook workbookInstance = session.workbookInstance
  100. bindData(workbookInstance, params)
  101.  
  102. if (workbookInstance == null) {
  103. notFound()
  104. return
  105. }
  106.  
  107. workbookInstance.validate()
  108.  
  109. if (workbookInstance.hasErrors()) {
  110. respond workbookInstance.errors, view: 'edit'
  111. return
  112. }
  113.  
  114. workbookInstance.save flush: true
  115.  
  116. redirect(action: "show", id: workbookInstance.id)
  117.  
  118. /*request.withFormat {
  119. form multipartForm {
  120. flash.message = message(code: 'default.updated.message', args: [message(code: 'Workbook.label', default: 'Workbook'), workbookInstance.id])
  121. redirect workbookInstance
  122. }
  123. '*' { respond workbookInstance, [status: OK] }
  124. }*/
  125. }
  126.  
  127. @Transactional
  128. def delete(Workbook workbookInstance) {
  129. if (workbookInstance == null) {
  130. notFound()
  131. return
  132. }
  133.  
  134. workbookInstance.delete flush: true
  135.  
  136. request.withFormat {
  137. form multipartForm {
  138. flash.message = message(code: 'default.deleted.message', args: [message(code: 'Workbook.label', default: 'Workbook'), workbookInstance.id])
  139. redirect action: "list", method: "GET"
  140. }
  141. '*' { render status: NO_CONTENT }
  142. }
  143. }
  144.  
  145. @Transactional
  146. def deleteDirect(Long id) {
  147. def workbookInstance = Workbook.get(id)
  148. delete(workbookInstance)
  149. }
  150.  
  151. protected void notFound() {
  152. request.withFormat {
  153. form multipartForm {
  154. flash.message = message(code: 'default.not.found.message', args: [message(code: 'workbookInstance.label', default: 'Workbook'), params.id])
  155. redirect action: "index", method: "GET"
  156. }
  157. '*' { render status: NOT_FOUND }
  158. }
  159. }
  160.  
  161. def addWorkPlace() {
  162. def workbookInstance = session.workbookInstance
  163. println 'workbook workbook workbook ' + workbookInstance
  164. DateFormat df = new SimpleDateFormat("dd/MM/yyyy")
  165. def company = params.company
  166. def position = params.position
  167. Date startDate = df.parse(params?.startDate as String)
  168. Date endDate = params?.endDate ? df.parse(params?.endDate as String) : null
  169.  
  170. def workPlace = new WorkPlace()
  171. println 'ddddddddddddddddddddddddd ' + workbookInstance.workPlaces
  172. workPlace.setWorkPlaceNmb(workbookInstance.workPlaces.size() + 1)
  173. workPlace.setCompany(company)
  174. workPlace.setPosition(position)
  175. workPlace.setWorkStartDate(startDate)
  176. workPlace.setWorkEndDate(endDate)
  177. workbookInstance.addToWorkPlaces(workPlace)
  178. session.workbookInstance = workbookInstance
  179. render(template: '/utils/workPlaceRow', model: [workPlace: workPlace, isDocumentEditable: true])
  180. }
  181.  
  182.  
  183. def deleteWorkPlace() {
  184. def workbookInstance = session.workbookInstance
  185. println 'Number of workplaces ' + workbookInstance.workPlaces.size()
  186. def wpNumber = Integer.parseInt(params.wpNumber)
  187. println 'wpNumber wpNumber ' + wpNumber
  188. workbookInstance.workPlaces.remove(wpNumber - 1)
  189. println 'Number of workplaces ' + workbookInstance.workPlaces.size()
  190.  
  191. for (def workPlace : workbookInstance.workPlaces) {
  192. if (workPlace.workPlaceNmb > wpNumber) {
  193. --workPlace.workPlaceNmb
  194. }
  195. }
  196.  
  197. for (def workPlace : workbookInstance.workPlaces) {
  198. println workPlace
  199. }
  200.  
  201. session.workbookInstance = workbookInstance
  202. render(template: '/utils/workPlacesList', model: [workPlacesToRender: workbookInstance.workPlaces, isDocumentEditable: true])
  203. }
  204.  
  205. def loadWorkbooksData() {
  206. def result = workbooksSearchService.getResults(params)
  207. println 'aaaaaaaaaaaaaaaa ' + result.iTotalRecords
  208. def builder = new JsonBuilder()
  209. def root = builder {
  210. "sEcho" params.sEcho
  211. "iTotalRecords" result.iTotalRecords
  212. "iTotalDisplayRecords" result.iTotalDisplayRecords
  213.  
  214. data result.domainsToDisplay.collect { Workbook wb ->
  215. [
  216. "0": wb.firstName,
  217. "1": wb.lastName,
  218. "2": wb.passportNumber,
  219. "3": wb.country,
  220. "4": wb.dateOfBirth]
  221. }
  222. }
  223. render builder.toString()
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement