/RapidCMDB/devDocs/test/manualTesting/ModelOperations/RapidServer/RapidSuite/grails-app/controllers/FictionController.groovy

https://github.com/TouK/RapidOSS3TouK · Groovy · 245 lines · 202 code · 24 blank · 19 comment · 39 complexity · 3ab2015b989153c9e33f07fa71488ab2 MD5 · raw file

  1. /*
  2. * All content copyright (C) 2004-2008 iFountain, LLC., except as may otherwise be
  3. * noted in a separate copyright notice. All rights reserved.
  4. * This file is part of RapidCMDB.
  5. *
  6. * RapidCMDB is free software; you can redistribute it and/or modify
  7. * it under the terms version 2 of the GNU General Public License as
  8. * published by the Free Software Foundation. This program is distributed
  9. * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  10. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  11. * PARTICULAR PURPOSE. See the GNU General Public License for more
  12. * details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA.
  18. */
  19. import com.ifountain.rcmdb.domain.util.ControllerUtils
  20. import com.ifountain.rcmdb.domain.util.DomainClassUtils;
  21. class FictionController {
  22. def index = {redirect(action: list, params: params)}
  23. // the delete, save and update actions only accept POST requests
  24. def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']
  25. def list = {
  26. if (!params.max) params.max = 10
  27. [fictionList: Fiction.search("alias:*", params).results]
  28. }
  29. def show = {
  30. def fiction = Fiction.get([id: params.id])
  31. if (!fiction) {
  32. flash.message = "Fiction not found with id ${params.id}"
  33. redirect(action: list)
  34. }
  35. else {
  36. if (fiction.class != Fiction)
  37. {
  38. def controllerName = fiction.class.simpleName;
  39. if (controllerName.length() == 1)
  40. {
  41. controllerName = controllerName.toLowerCase();
  42. }
  43. else
  44. {
  45. controllerName = controllerName.substring(0, 1).toLowerCase() + controllerName.substring(1);
  46. }
  47. redirect(action: show, controller: controllerName, id: params.id)
  48. }
  49. else
  50. {
  51. return [fiction: fiction]
  52. }
  53. }
  54. }
  55. def delete = {
  56. def fiction = Fiction.get([id: params.id])
  57. if (fiction) {
  58. fiction.remove()
  59. flash.message = "Fiction ${params.id} deleted"
  60. redirect(action: list)
  61. }
  62. else {
  63. flash.message = "Fiction not found with id ${params.id}"
  64. redirect(action: list)
  65. }
  66. }
  67. def edit = {
  68. def fiction = Fiction.get([id: params.id])
  69. if (!fiction) {
  70. flash.message = "Fiction not found with id ${params.id}"
  71. redirect(action: list)
  72. }
  73. else {
  74. return [fiction: fiction]
  75. }
  76. }
  77. def update = {
  78. def fiction = Fiction.get([id: params.id])
  79. if (fiction) {
  80. fiction.update(ControllerUtils.getClassProperties(params, Fiction));
  81. if (!fiction.hasErrors()) {
  82. flash.message = "Fiction ${params.id} updated"
  83. redirect(action: show, id: fiction.id)
  84. }
  85. else {
  86. render(view: 'edit', model: [fiction: fiction])
  87. }
  88. }
  89. else {
  90. flash.message = "Fiction not found with id ${params.id}"
  91. redirect(action: edit, id: params.id)
  92. }
  93. }
  94. def create = {
  95. def fiction = new Fiction()
  96. fiction.properties = params
  97. return ['fiction': fiction]
  98. }
  99. def save = {
  100. def fiction = Fiction.add(ControllerUtils.getClassProperties(params, Fiction))
  101. if (!fiction.hasErrors()) {
  102. flash.message = "Fiction ${fiction.id} created"
  103. redirect(action: show, id: fiction.id)
  104. }
  105. else {
  106. render(view: 'create', model: [fiction: fiction])
  107. }
  108. }
  109. def addTo = {
  110. def fiction = Fiction.get([id: params.id])
  111. if (!fiction) {
  112. flash.message = "Fiction not found with id ${params.id}"
  113. redirect(action: list)
  114. }
  115. else {
  116. def relationName = params.relationName;
  117. if (relationName) {
  118. def otherClass = DomainClassUtils.getStaticMapVariable(Fiction, "relations")[relationName].type;
  119. def relatedObjectList = [];
  120. if (otherClass) {
  121. relatedObjectList = otherClass.metaClass.invokeStaticMethod(otherClass, "list");
  122. }
  123. return [fiction: fiction, relationName: relationName, relatedObjectList: relatedObjectList]
  124. }
  125. else {
  126. flash.message = "No relation name specified for add relation action"
  127. redirect(action: edit, id: fiction.id)
  128. }
  129. }
  130. }
  131. def addRelation = {
  132. def fiction = Fiction.get([id: params.id])
  133. if (!fiction) {
  134. flash.message = "Fiction not found with id ${params.id}"
  135. redirect(action: list)
  136. }
  137. else {
  138. def relationName = params.relationName;
  139. def otherClass = DomainClassUtils.getStaticMapVariable(Fiction, "relations")[relationName].type;
  140. if (otherClass) {
  141. def res = otherClass.metaClass.invokeStaticMethod(otherClass, "get", params.relatedObjectId.toLong());
  142. if (res) {
  143. def relationMap = [:];
  144. relationMap[relationName] = res;
  145. fiction.addRelation(relationMap);
  146. if (fiction.hasErrors()) {
  147. def relatedObjectList = otherClass.metaClass.invokeStaticMethod(otherClass, "list");
  148. render(view: 'addTo', model: [fiction: fiction, relationName: relationName, relatedObjectList: relatedObjectList])
  149. }
  150. else {
  151. flash.message = "Fiction ${params.id} updated"
  152. redirect(action: edit, id: fiction.id)
  153. }
  154. }
  155. else {
  156. flash.message = otherClass.getName() + " not found with id ${params.relatedObjectId}"
  157. redirect(action: addTo, id: params.id, relationName: relationName)
  158. }
  159. }
  160. else {
  161. flash.message = "No relation exist with name ${relationName}"
  162. redirect(action: addTo, id: params.id, relationName: relationName)
  163. }
  164. }
  165. }
  166. def removeRelation = {
  167. def fiction = Fiction.get([id: params.id])
  168. if (!fiction) {
  169. flash.message = "Fiction not found with id ${params.id}"
  170. redirect(action: list)
  171. }
  172. else {
  173. def relationName = params.relationName;
  174. def otherClass = com.ifountain.rcmdb.domain.util.DomainClassUtils.getStaticMapVariable(Fiction, "relations")[relationName].type;
  175. if (otherClass) {
  176. def res = otherClass.metaClass.invokeStaticMethod(otherClass, "get", params.relatedObjectId.toLong());
  177. if (res) {
  178. def relationMap = [:];
  179. relationMap[relationName] = res;
  180. fiction.removeRelation(relationMap);
  181. if (fiction.hasErrors()) {
  182. render(view: 'edit', model: [fiction: fiction])
  183. }
  184. else {
  185. flash.message = "Fiction ${params.id} updated"
  186. redirect(action: edit, id: fiction.id)
  187. }
  188. }
  189. else {
  190. flash.message = otherClass.getName() + " not found with id ${params.relatedObjectId}"
  191. redirect(action: edit, id: fiction.id)
  192. }
  193. }
  194. else {
  195. flash.message = "No relation exist with name ${relationName}"
  196. redirect(action: edit, id: fiction.id)
  197. }
  198. }
  199. }
  200. def reloadOperations = {
  201. def modelClass = grailsApplication.getClassForName("Fiction")
  202. if (modelClass)
  203. {
  204. try
  205. {
  206. modelClass.metaClass.invokeStaticMethod(modelClass, "reloadOperations", [] as Object[]);
  207. flash.message = "Model operations reloaded"
  208. redirect(action: list)
  209. } catch (t)
  210. {
  211. flash.message = "Exception occurred while reloading model operations Reason:${t.toString()}"
  212. redirect(action: list)
  213. }
  214. }
  215. else
  216. {
  217. flash.message = "Model currently not loaded by application. You should reload application."
  218. redirect(action: list)
  219. }
  220. }
  221. }