/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
- /*
- * All content copyright (C) 2004-2008 iFountain, LLC., except as may otherwise be
- * noted in a separate copyright notice. All rights reserved.
- * This file is part of RapidCMDB.
- *
- * RapidCMDB is free software; you can redistribute it and/or modify
- * it under the terms version 2 of the GNU General Public License as
- * published by the Free Software Foundation. This program is distributed
- * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
- * even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA.
- */
- import com.ifountain.rcmdb.domain.util.ControllerUtils
- import com.ifountain.rcmdb.domain.util.DomainClassUtils;
- class FictionController {
- def index = {redirect(action: list, params: params)}
- // the delete, save and update actions only accept POST requests
- def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']
- def list = {
- if (!params.max) params.max = 10
- [fictionList: Fiction.search("alias:*", params).results]
- }
- def show = {
- def fiction = Fiction.get([id: params.id])
- if (!fiction) {
- flash.message = "Fiction not found with id ${params.id}"
- redirect(action: list)
- }
- else {
- if (fiction.class != Fiction)
- {
- def controllerName = fiction.class.simpleName;
- if (controllerName.length() == 1)
- {
- controllerName = controllerName.toLowerCase();
- }
- else
- {
- controllerName = controllerName.substring(0, 1).toLowerCase() + controllerName.substring(1);
- }
- redirect(action: show, controller: controllerName, id: params.id)
- }
- else
- {
- return [fiction: fiction]
- }
- }
- }
- def delete = {
- def fiction = Fiction.get([id: params.id])
- if (fiction) {
- fiction.remove()
- flash.message = "Fiction ${params.id} deleted"
- redirect(action: list)
- }
- else {
- flash.message = "Fiction not found with id ${params.id}"
- redirect(action: list)
- }
- }
- def edit = {
- def fiction = Fiction.get([id: params.id])
- if (!fiction) {
- flash.message = "Fiction not found with id ${params.id}"
- redirect(action: list)
- }
- else {
- return [fiction: fiction]
- }
- }
- def update = {
- def fiction = Fiction.get([id: params.id])
- if (fiction) {
- fiction.update(ControllerUtils.getClassProperties(params, Fiction));
- if (!fiction.hasErrors()) {
- flash.message = "Fiction ${params.id} updated"
- redirect(action: show, id: fiction.id)
- }
- else {
- render(view: 'edit', model: [fiction: fiction])
- }
- }
- else {
- flash.message = "Fiction not found with id ${params.id}"
- redirect(action: edit, id: params.id)
- }
- }
- def create = {
- def fiction = new Fiction()
- fiction.properties = params
- return ['fiction': fiction]
- }
- def save = {
- def fiction = Fiction.add(ControllerUtils.getClassProperties(params, Fiction))
- if (!fiction.hasErrors()) {
- flash.message = "Fiction ${fiction.id} created"
- redirect(action: show, id: fiction.id)
- }
- else {
- render(view: 'create', model: [fiction: fiction])
- }
- }
- def addTo = {
- def fiction = Fiction.get([id: params.id])
- if (!fiction) {
- flash.message = "Fiction not found with id ${params.id}"
- redirect(action: list)
- }
- else {
- def relationName = params.relationName;
- if (relationName) {
- def otherClass = DomainClassUtils.getStaticMapVariable(Fiction, "relations")[relationName].type;
- def relatedObjectList = [];
- if (otherClass) {
- relatedObjectList = otherClass.metaClass.invokeStaticMethod(otherClass, "list");
- }
- return [fiction: fiction, relationName: relationName, relatedObjectList: relatedObjectList]
- }
- else {
- flash.message = "No relation name specified for add relation action"
- redirect(action: edit, id: fiction.id)
- }
- }
- }
- def addRelation = {
- def fiction = Fiction.get([id: params.id])
- if (!fiction) {
- flash.message = "Fiction not found with id ${params.id}"
- redirect(action: list)
- }
- else {
- def relationName = params.relationName;
- def otherClass = DomainClassUtils.getStaticMapVariable(Fiction, "relations")[relationName].type;
- if (otherClass) {
- def res = otherClass.metaClass.invokeStaticMethod(otherClass, "get", params.relatedObjectId.toLong());
- if (res) {
- def relationMap = [:];
- relationMap[relationName] = res;
- fiction.addRelation(relationMap);
- if (fiction.hasErrors()) {
- def relatedObjectList = otherClass.metaClass.invokeStaticMethod(otherClass, "list");
- render(view: 'addTo', model: [fiction: fiction, relationName: relationName, relatedObjectList: relatedObjectList])
- }
- else {
- flash.message = "Fiction ${params.id} updated"
- redirect(action: edit, id: fiction.id)
- }
- }
- else {
- flash.message = otherClass.getName() + " not found with id ${params.relatedObjectId}"
- redirect(action: addTo, id: params.id, relationName: relationName)
- }
- }
- else {
- flash.message = "No relation exist with name ${relationName}"
- redirect(action: addTo, id: params.id, relationName: relationName)
- }
- }
- }
- def removeRelation = {
- def fiction = Fiction.get([id: params.id])
- if (!fiction) {
- flash.message = "Fiction not found with id ${params.id}"
- redirect(action: list)
- }
- else {
- def relationName = params.relationName;
- def otherClass = com.ifountain.rcmdb.domain.util.DomainClassUtils.getStaticMapVariable(Fiction, "relations")[relationName].type;
- if (otherClass) {
- def res = otherClass.metaClass.invokeStaticMethod(otherClass, "get", params.relatedObjectId.toLong());
- if (res) {
- def relationMap = [:];
- relationMap[relationName] = res;
- fiction.removeRelation(relationMap);
- if (fiction.hasErrors()) {
- render(view: 'edit', model: [fiction: fiction])
- }
- else {
- flash.message = "Fiction ${params.id} updated"
- redirect(action: edit, id: fiction.id)
- }
- }
- else {
- flash.message = otherClass.getName() + " not found with id ${params.relatedObjectId}"
- redirect(action: edit, id: fiction.id)
- }
- }
- else {
- flash.message = "No relation exist with name ${relationName}"
- redirect(action: edit, id: fiction.id)
- }
- }
- }
- def reloadOperations = {
- def modelClass = grailsApplication.getClassForName("Fiction")
- if (modelClass)
- {
- try
- {
- modelClass.metaClass.invokeStaticMethod(modelClass, "reloadOperations", [] as Object[]);
- flash.message = "Model operations reloaded"
- redirect(action: list)
- } catch (t)
- {
- flash.message = "Exception occurred while reloading model operations Reason:${t.toString()}"
- redirect(action: list)
- }
- }
- else
- {
- flash.message = "Model currently not loaded by application. You should reload application."
- redirect(action: list)
- }
- }
- }