diff --git a/grails-app/conf/import/yana.xsd b/grails-app/conf/import/yana.xsd index 0f26b12..4580525 100644 --- a/grails-app/conf/import/yana.xsd +++ b/grails-app/conf/import/yana.xsd @@ -109,7 +109,6 @@ - diff --git a/grails-app/controllers/com/dtolabs/ChildNodeController.groovy b/grails-app/controllers/com/dtolabs/ChildNodeController.groovy index 1dd6ead..76054f9 100644 --- a/grails-app/controllers/com/dtolabs/ChildNodeController.groovy +++ b/grails-app/controllers/com/dtolabs/ChildNodeController.groovy @@ -6,201 +6,200 @@ import com.dtolabs.NodeTypeRelationship import org.springframework.dao.DataIntegrityViolationException import grails.plugins.springsecurity.Secured -@Secured(['ROLE_YANA_ADMIN','ROLE_YANA_ARCHITECT','ROLE_YANA_SUPERUSER']) +@Secured(['ROLE_YANA_ADMIN', 'ROLE_YANA_ARCHITECT', 'ROLE_YANA_SUPERUSER']) class ChildNodeController { - def iconService - def xmlService - def jsonService + def iconService + def xmlService + def jsonService - //static allowedMethods = [save: "POST", update: "POST", delete: "POST"] - def api(){ - switch(request.method){ - case "POST": - def json = request.JSON - this.save() - break - case "GET": - def json = request.JSON - this.show() - break - case "PUT": - def json = request.JSON - this.update() - break - case "DELETE": - def json = request.JSON - if(params.id){ - def cnode = ChildNode.get(params.id) - if(cnode){ - try{ - cnode.delete(flush:true) - response.status = 200 - render "Successfully Deleted." - }catch(org.springframework.dao.DataIntegrityViolationException e) { - ChildNode.withSession { session -> - session.clear() - } + def api() { + switch (request.method) { + case "POST": + def json = request.JSON + this.save() + break + case "GET": + def json = request.JSON + this.show() + break + case "PUT": + def json = request.JSON + this.update() + break + case "DELETE": + def json = request.JSON + if (params.id) { + def cnode = ChildNode.get(params.id) + if (cnode) { + try { + cnode.delete(flush: true) + response.status = 200 + render "Successfully Deleted." + } catch (org.springframework.dao.DataIntegrityViolationException e) { + ChildNode.withSession { session -> + session.clear() + } + + response.status = 400 //Bad Request + render "Referential Integrity Violation: Please remove/reassign all Node relationships first." + } + } else { + response.status = 404 //Not Found + render "${params.id} not found." + } + } else { + response.status = 400 //Bad Request + render """DELETE request must include the id""" + } + break + } + return + } + + def listapi() { + switch (request.method) { + case "POST": + def json = request.JSON + this.list() + break + } + return + } - response.status = 400 //Bad Request - render "Referential Integrity Violation: Please remove/reassign all Node relationships first." - } - }else{ - response.status = 404 //Not Found - render "${params.id} not found." - } - }else{ - response.status = 400 //Bad Request - render """DELETE request must include the id""" - } - break - } - return - } - - def listapi(){ - switch(request.method){ - case "POST": - def json = request.JSON - this.list() - break - } - return - } - def index() { redirect(action: "list", params: params) } def list() { - if(params.format && params.format!='none'){ - ArrayList cnodes = ChildNode.list() - switch(params.format.toLowerCase()){ - case 'xml': - def xml = xmlService.formatChildNodes(cnodes) - render(text: xml, contentType: "text/xml") - break; - case 'json': - def json = jsonService.formatChildNodes(cnodes) - render(text:json, contentType: "text/json") - break; - } - }else{ - params.max = Math.min(params.max ? params.int('max') : 10, 100) - [childNodeInstanceList: ChildNode.list(params), childNodeInstanceTotal: ChildNode.count()] - } + if (params.format && params.format != 'none') { + ArrayList cnodes = ChildNode.list() + switch (params.format.toLowerCase()) { + case 'xml': + def xml = xmlService.formatChildNodes(cnodes) + render(text: xml, contentType: "text/xml") + break; + case 'json': + def json = jsonService.formatChildNodes(cnodes) + render(text: json, contentType: "text/json") + break; + } + } else { + params.max = Math.min(params.max ? params.int('max') : 10, 100) + [childNodeInstanceList: ChildNode.list(params), childNodeInstanceTotal: ChildNode.count()] + } } def create() { [childNodeInstance: new ChildNode(params)] } - + def save() { - Node parent = Node.get(params.parent) - Node child = Node.get(params.child) - - def ntparents = NodeTypeRelationship.findByChild(child.nodetype) - def nparents = [] - ntparents.each{ntp -> - nparents += ntp.parent - } - def ntchildren = NodeTypeRelationship.findByParent(parent.nodetype) - def nchildren = [] - ntchildren.each{ntc -> - nchildren += ntc.child - } - - def exists = ChildNode.findByParentAndChild(parent,child) - def childNodeInstance - if(!exists){ - if(nparents.contains(parent.nodetype) && nchildren.contains(child.nodetype)){ - childNodeInstance= new ChildNode(relationshipName:"${params.relationshipName}",parent:parent,child:child) - if (!childNodeInstance.save(flush: true)) { - if(params.action=='api'){ - response.status = 400 //Bad Request - render "ChildNode Creation Failed" - return - }else{ - render(view: "create", model: [childNodeInstance: childNodeInstance]) - return - } - }else{ - if(params.action=='api'){ - if(params.format && params.format!='none'){ - ArrayList cnodes = [childNodeInstance] - switch(params.format.toLowerCase()){ - case 'xml': - def xml = xmlService.formatChildNodes(cnodes) - render(text: xml, contentType: "text/xml") - break; - case 'json': - def json = jsonService.formatChildNodes(cnodes) - render(text:json, contentType: "text/json") - break; - } - }else{ - response.status = 200 - render "Successfully created." - return - } - }else{ - flash.message = message(code: 'default.created.message', args: [message(code: 'childNode.label', default: 'ChildNode'), childNodeInstance.id]) - redirect(action: "show", id: childNodeInstance.id) - } - } - }else{ - if(params.action=='api'){ - response.status = 400 - render "Parent/Child relationship is not available. Please Create in NodeTypeRelationship." - return - }else{ - flash.message = "Parent/Child relationship is not available. Please Create in NodeTypeRelationship" - render(view: "create", model: [childNodeInstance: childNodeInstance]) - } - } - }else{ - if(params.action=='api'){ - response.status = 404 //Not Found - render "Existing relationship for that Parent and child node already exists. Please try again." - return - }else{ - flash.message = "Existing relationship for that Parent and child node already exists. Please try again." - render(view: "create", model: [childNodeInstance: childNodeInstance]) - return - } - } + Node parent = Node.get(params.parent) + Node child = Node.get(params.child) + + def ntparents = NodeTypeRelationship.findByChild(child.nodetype) + def nparents = [] + ntparents.each {ntp -> + nparents += ntp.parent + } + def ntchildren = NodeTypeRelationship.findByParent(parent.nodetype) + def nchildren = [] + ntchildren.each {ntc -> + nchildren += ntc.child + } + + def exists = ChildNode.findByParentAndChild(parent, child) + def childNodeInstance + if (!exists) { + if (nparents.contains(parent.nodetype) && nchildren.contains(child.nodetype)) { + childNodeInstance = new ChildNode(parent: parent, child: child) + if (!childNodeInstance.save(flush: true)) { + if (params.action == 'api') { + response.status = 400 //Bad Request + render "ChildNode Creation Failed" + return + } else { + render(view: "create", model: [childNodeInstance: childNodeInstance]) + return + } + } else { + if (params.action == 'api') { + if (params.format && params.format != 'none') { + ArrayList cnodes = [childNodeInstance] + switch (params.format.toLowerCase()) { + case 'xml': + def xml = xmlService.formatChildNodes(cnodes) + render(text: xml, contentType: "text/xml") + break; + case 'json': + def json = jsonService.formatChildNodes(cnodes) + render(text: json, contentType: "text/json") + break; + } + } else { + response.status = 200 + render "Successfully created." + return + } + } else { + flash.message = message(code: 'default.created.message', args: [message(code: 'childNode.label', default: 'ChildNode'), childNodeInstance.id]) + redirect(action: "show", id: childNodeInstance.id) + } + } + } else { + if (params.action == 'api') { + response.status = 400 + render "Parent/Child relationship is not available. Please Create in NodeTypeRelationship." + return + } else { + flash.message = "Parent/Child relationship is not available. Please Create in NodeTypeRelationship" + render(view: "create", model: [childNodeInstance: childNodeInstance]) + } + } + } else { + if (params.action == 'api') { + response.status = 404 //Not Found + render "Existing relationship for that Parent and child node already exists. Please try again." + return + } else { + flash.message = "Existing relationship for that Parent and child node already exists. Please try again." + render(view: "create", model: [childNodeInstance: childNodeInstance]) + return + } + } } def show() { - String path = iconService.getSmallIconPath() + String path = iconService.getSmallIconPath() def childNodeInstance = ChildNode.get(params.id) if (!childNodeInstance) { - flash.message = message(code: 'default.not.found.message', args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) + flash.message = message(code: 'default.not.found.message', args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) redirect(action: "list") return - }else{ - if(params.format && params.format!='none'){ - ArrayList cnodes = [childNodeInstance] - if(childNodeInstance){ - switch(params.format.toLowerCase()){ - case 'xml': - def xml = xmlService.formatChildNodes(cnodes) - render(text: xml, contentType: "text/xml") - break; - case 'json': - def json = jsonService.formatChildNodes(cnodes) - render(text:json, contentType: "text/json") - break; - } - }else{ - response.status = 404 //Not Found - render "${params.id} not found." - } - }else{ - [childNodeInstance: childNodeInstance,path:path] - } + } else { + if (params.format && params.format != 'none') { + ArrayList cnodes = [childNodeInstance] + if (childNodeInstance) { + switch (params.format.toLowerCase()) { + case 'xml': + def xml = xmlService.formatChildNodes(cnodes) + render(text: xml, contentType: "text/xml") + break; + case 'json': + def json = jsonService.formatChildNodes(cnodes) + render(text: json, contentType: "text/json") + break; + } + } else { + response.status = 404 //Not Found + render "${params.id} not found." + } + } else { + [childNodeInstance: childNodeInstance, path: path] + } } } @@ -216,63 +215,60 @@ class ChildNodeController { } def update() { - def childNodeInstance = ChildNode.get(params.id) - if(!params.relationshipName){ - if(params.format){ - response.status = 404 //Not Found - render "${params.id} not found." - }else{ - flash.message = "Relationshipname is a required field" - render(view: "edit", model: [childNodeInstance: childNodeInstance]) - } - }else{ - if (!childNodeInstance) { - flash.message = message(code: 'default.not.found.message', args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) - redirect(action: "list") - return - } - - if (params.version) { - def version = params.version.toLong() - if (childNodeInstance.version > version) { - childNodeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", - [message(code: 'childNode.label', default: 'ChildNode')] as Object[], - "Another user has updated this ChildNode while you were editing") - render(view: "edit", model: [childNodeInstance: childNodeInstance]) - return - } - } - - childNodeInstance.properties = params - - if (!childNodeInstance.save(flush: true)) { - render(view: "edit", model: [childNodeInstance: childNodeInstance]) - return - } - if(params.format){ - response.status = 200 //Not Found - render "Successfully edited." - }else{ - flash.message = message(code: 'default.updated.message', args: [message(code: 'childNode.label', default: 'ChildNode'), childNodeInstance.id]) - redirect(action: "show", id: childNodeInstance.id) - } - } + def childNodeInstance = ChildNode.get(params.id) + + if (!childNodeInstance) { + flash.message = message(code: 'default.not.found.message', + args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) + redirect(action: "list") + return + } + + if (params.version) { + def version = params.version.toLong() + if (childNodeInstance.version > version) { + childNodeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", + [message(code: 'childNode.label', default: 'ChildNode')] as Object[], + "Another user has updated this ChildNode while you were editing") + render(view: "edit", model: [childNodeInstance: childNodeInstance]) + return + } + } + + childNodeInstance.properties = params + + if (!childNodeInstance.save(flush: true)) { + render(view: "edit", model: [childNodeInstance: childNodeInstance]) + return + } + if (params.format) { + response.status = 200 //Not Found + render "Successfully edited." + } else { + flash.message = message(code: 'default.updated.message', + args: [message(code: 'childNode.label', default: 'ChildNode'), childNodeInstance.id]) + redirect(action: "show", id: childNodeInstance.id) + } + } def delete() { def childNodeInstance = ChildNode.get(params.id) if (!childNodeInstance) { - flash.message = message(code: 'default.not.found.message', args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) + flash.message = message(code: 'default.not.found.message', + args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) redirect(action: "list") return } try { childNodeInstance.delete(flush: true) - flash.message = message(code: 'default.deleted.message', args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) + flash.message = message(code: 'default.deleted.message', + args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) redirect(action: "list") - }catch (DataIntegrityViolationException e) { - flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) + } catch (DataIntegrityViolationException e) { + flash.message = message(code: 'default.not.deleted.message', + args: [message(code: 'childNode.label', default: 'ChildNode'), params.id]) redirect(action: "show", id: params.id) } } diff --git a/grails-app/domain/com/dtolabs/ChildNode.groovy b/grails-app/domain/com/dtolabs/ChildNode.groovy index 20c3d4a..7693ab1 100644 --- a/grails-app/domain/com/dtolabs/ChildNode.groovy +++ b/grails-app/domain/com/dtolabs/ChildNode.groovy @@ -1,22 +1,19 @@ package com.dtolabs class ChildNode { - static belongsTo = [ Node ] + static belongsTo = Node - String relationshipName Node parent Node child static constraints = { - relationshipName(nullable:false) parent(nullable:false) child(nullable:false) } Map toMap() { def map = [ - relationship: this.relationshipName, - child: [name: child.name, type: child.nodetype.name, id: child.id,], + child: [name: child.name, type: child.nodetype.name, id: child.id], parent: [name: parent.name, type: parent.nodetype.name, id: parent.id] ] return map diff --git a/grails-app/services/com/dtolabs/ImportService.groovy b/grails-app/services/com/dtolabs/ImportService.groovy index 56edb2f..6edc10c 100644 --- a/grails-app/services/com/dtolabs/ImportService.groovy +++ b/grails-app/services/com/dtolabs/ImportService.groovy @@ -179,7 +179,6 @@ class ImportService { if (!childNode && relationship) { childNode = new ChildNode(child: child, parent: parent) - childNode.relationshipName = nodechild.@relationship.toString() childNode.save(flush: true, failOnError: true) } diff --git a/grails-app/services/com/dtolabs/JsonService.groovy b/grails-app/services/com/dtolabs/JsonService.groovy index a83e892..3f7bd3a 100644 --- a/grails-app/services/com/dtolabs/JsonService.groovy +++ b/grails-app/services/com/dtolabs/JsonService.groovy @@ -46,7 +46,7 @@ class JsonService { String formatChildNodes(ArrayList data){ ArrayList result = [:] data.each(){ val1 -> - result += [childNode:[id:val1.id,parentNodeId:val1.parent.id,parentName:val1.parent.name,parentNodeType:val1.parent.nodetype.name,childNodeId:val1.child.id,childName:val1.child.name,childNodeType:val1.child.nodetype.name,relationshipName:val1.relationshipName]] + result += [childNode:[id:val1.id,parentNodeId:val1.parent.id,parentName:val1.parent.name,parentNodeType:val1.parent.nodetype.name,childNodeId:val1.child.id,childName:val1.child.name,childNodeType:val1.child.nodetype.name]] } return result as JSON } diff --git a/grails-app/services/com/dtolabs/NodeService.groovy b/grails-app/services/com/dtolabs/NodeService.groovy index a1cd510..ecbffc6 100644 --- a/grails-app/services/com/dtolabs/NodeService.groovy +++ b/grails-app/services/com/dtolabs/NodeService.groovy @@ -195,7 +195,6 @@ class NodeService { ChildNode childNode = ChildNode.findByParentAndChild(parent, child) if (!childNode) { childNode = new ChildNode() - childNode.relationshipName = name childNode.parent = parent childNode.child = child childNode.save(flush: true) @@ -211,4 +210,4 @@ class NodeService { return name } -} +} diff --git a/grails-app/services/com/dtolabs/XmlService.groovy b/grails-app/services/com/dtolabs/XmlService.groovy index 4ccf109..9b6106f 100644 --- a/grails-app/services/com/dtolabs/XmlService.groovy +++ b/grails-app/services/com/dtolabs/XmlService.groovy @@ -30,14 +30,14 @@ class XmlService { parents(){ def rents = ChildNode.findAllByChild(Node.get(val1.id.toLong())); rents.each{ parent -> - node(childnodeId:parent.id,id:parent.parent.id,name:parent.parent.name,nodetypeId:parent.parent.nodetype.id,type:parent.parent.nodetype.name,tags:parent.parent.tags,relationshipName:parent.relationshipName,rolename:NodeTypeRelationship.findByParentAndChild(parent.parent.nodetype,parent.child.nodetype).roleName) + node(childnodeId:parent.id,id:parent.parent.id,name:parent.parent.name,nodetypeId:parent.parent.nodetype.id,type:parent.parent.nodetype.name,tags:parent.parent.tags,rolename:NodeTypeRelationship.findByParentAndChild(parent.parent.nodetype,parent.child.nodetype).roleName) } } children(){ def kinder = ChildNode.findAllByParent(Node.get(val1.id.toLong())); kinder.each{ child -> - node(childnodeId:child.id,id:child.child.id,name:child.child.name,nodetypeId:child.child.nodetype.id,type:child.child.nodetype.name,tags:child.child.tags,relationshipName:child.relationshipName,rolename:NodeTypeRelationship.findByParentAndChild(child.parent.nodetype,child.child.nodetype).roleName) + node(childnodeId:child.id,id:child.child.id,name:child.child.name,nodetypeId:child.child.nodetype.id,type:child.child.nodetype.name,tags:child.child.tags,rolename:NodeTypeRelationship.findByParentAndChild(child.parent.nodetype,child.child.nodetype).roleName) } } } @@ -52,7 +52,7 @@ class XmlService { xml.childNodes() { cnodes.each(){ val1 -> - childNode(id:val1.id,parentNodeId:val1.parent.id,parentName:val1.parent.name,parentNodeType:val1.parent.nodetype.name,childNodeId:val1.child.id,childName:val1.child.name,childNodeType:val1.child.nodetype.name,relationshipName:val1.relationshipName) + childNode(id:val1.id,parentNodeId:val1.parent.id,parentName:val1.parent.name,parentNodeType:val1.parent.nodetype.name,childNodeId:val1.child.id,childName:val1.child.name,childNodeType:val1.child.nodetype.name) } } return writer.toString() diff --git a/grails-app/views/node/show.gsp b/grails-app/views/node/show.gsp index 0e31ce5..d4be960 100644 --- a/grails-app/views/node/show.gsp +++ b/grails-app/views/node/show.gsp @@ -99,7 +99,7 @@
  • -${it.relationshipName}:${it?.parent?.name?.encodeAsHTML()}
  • +${it?.parent.encodeAsHTML()}
    @@ -112,7 +112,7 @@
  • - ${it.relationshipName}:${it?.child?.name?.encodeAsHTML()}
  • + ${it?.child.encodeAsHTML()}
    diff --git a/src/main/rerun/rerun-tests/MakeRerunTests.groovy b/src/main/rerun/rerun-tests/MakeRerunTests.groovy index ea2d173..0f706d7 100644 --- a/src/main/rerun/rerun-tests/MakeRerunTests.groovy +++ b/src/main/rerun/rerun-tests/MakeRerunTests.groovy @@ -150,9 +150,8 @@ def deleteNodeRelationship(String key) { nodeTypeRelationshipMap.remove(key) } -def createChildNode(String relationshipName, Node parent, Node child) { +def createChildNode(Node parent, Node child) { ChildNode childNode = new ChildNode() - childNode.relationshipName = relationshipName childNode.child = child childNode.parent = parent childNodeMap.put(child.name + "::" + parent.name, childNode) @@ -285,8 +284,8 @@ def parseXML() { NodeTypeRelationship nodeTypeRelationship = nodeTypeRelationshipMap.get(child.nodetype.name + "::" + parent.nodetype.name) if (!childNode && nodeTypeRelationship) { - childNode = createChildNode(nodeRelationshipsXml.@relationshipname.toString(), - parent, child) + childNode = createChildNode( + parent, child) } else { //Error } @@ -563,7 +562,6 @@ def emitRerunNodeChildRelationships() { def (childNode, childNodeIndex) = findNodeByName(childNodeMapValue.child.name) println(childNodeIndex - + ":" + childNodeMapValue.relationshipName + ":" + parentNodeIndex + ":" + childNodeMapValue.child.name + ":" + childNodeMapValue.child.nodetype.name) @@ -585,7 +583,6 @@ def emitRerunNodeParentRelationships() { def (childNode, childNodeIndex) = findNodeByName(childNodeMapValue.child.name) println(parentNodeIndex - + ":" + childNodeMapValue.relationshipName + ":" + childNodeIndex + ":" + childNodeMapValue.parent.name + ":" + childNodeMapValue.parent.nodetype.name) diff --git a/test/integration/com/dtolabs/ProjectServiceDeleteTests.groovy b/test/integration/com/dtolabs/ProjectServiceDeleteTests.groovy index f141298..91be4ed 100644 --- a/test/integration/com/dtolabs/ProjectServiceDeleteTests.groovy +++ b/test/integration/com/dtolabs/ProjectServiceDeleteTests.groovy @@ -163,7 +163,7 @@ class ProjectServiceDeleteTests extends GroovyTestCase { assert null != n2.save() //child node - def cn1 = new ChildNode(relationshipName: 'blah', parent: n1, child: n1b) + def cn1 = new ChildNode(parent: n1, child: n1b) assert null!=cn1.save() diff --git a/test/unit/com/dtolabs/ChildNodeControllerTests.groovy b/test/unit/com/dtolabs/ChildNodeControllerTests.groovy index 0a524c4..f70cd1d 100644 --- a/test/unit/com/dtolabs/ChildNodeControllerTests.groovy +++ b/test/unit/com/dtolabs/ChildNodeControllerTests.groovy @@ -24,7 +24,6 @@ class ChildNodeControllerTests { params["id"] = 1 params["version"] = 1 - params["relationshipName"] = "test.server.com_Jetty [softwareInstallation]" params["parent"] = serverNode params["child"] = softwareNode }