Created
July 31, 2012 22:19
-
-
Save ahonor/3221154 to your computer and use it in GitHub Desktop.
Export service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.dtolabs | |
import groovy.xml.MarkupBuilder | |
import com.dtolabs.groovy.util.BuilderUtil | |
/** | |
* Provides a model export service that takes a project | |
* and dumps it out as XML-based model content. | |
* | |
* Export format must conform to the /import/yana.xsd file. | |
*/ | |
class ExportService { | |
/** | |
* Dump the model | |
* @param project The project model to export | |
*/ | |
def export(final Project project) { | |
this.exportXml(project) | |
} | |
def exportXml(final Project project) { | |
def writer = new StringWriter() | |
def mb = new MarkupBuilder(writer) | |
BuilderUtil bu = new BuilderUtil() | |
def root = [yana:[ | |
attributes: [], types: [], nodes: [], | |
relationships: [], children: [child: []] ] | |
] | |
/** | |
* Attributes | |
*/ | |
Attribute.findAllByProject(project).each { Attribute attr -> | |
Map attrMap = attr.toMap() | |
BuilderUtil.makeAttribute(attrMap, "name") | |
BuilderUtil.makeAttribute(attrMap, "filter") | |
BuilderUtil.makeAttribute(attrMap, "id") | |
BuilderUtil.makeAttribute(attrMap, "description") | |
root.yana.attributes << attrMap | |
} | |
BuilderUtil.makePlural(root.yana, "attributes") | |
/** | |
* NodeType | |
*/ | |
def nodeTypeRelationships = [] | |
NodeType.findAllByProject(project).each { NodeType type -> | |
type.parents.each { | |
if (! nodeTypeRelationships.contains(it)) nodeTypeRelationships << it | |
} | |
Map typeMap = type.toMap() | |
BuilderUtil.makeAttribute(typeMap, "name") | |
BuilderUtil.makeAttribute(typeMap, "id") | |
typeMap.remove("parents") // don't include relationship info | |
typeMap.attributes.each { Map m -> | |
BuilderUtil.makeAttribute(m, "required") | |
BuilderUtil.makeAttribute(m, "name") | |
m.remove("id") // NodeAttributes are an internal object | |
} | |
BuilderUtil.makePlural(typeMap, "attributes") | |
root.yana.types << typeMap | |
} | |
BuilderUtil.makePlural(root.yana, "types") | |
/** | |
* NodetypeRelationships | |
*/ | |
nodeTypeRelationships.each { NodeTypeRelationship rel -> | |
Map relMap = rel.toMap() | |
relMap.remove("id") | |
BuilderUtil.makeAttribute(relMap, "parent") | |
BuilderUtil.makeAttribute(relMap, "child") | |
BuilderUtil.makeAttribute(relMap, "name") | |
root.yana.relationships << relMap | |
} | |
BuilderUtil.makePlural(root.yana, "relationships") | |
/** | |
* Node | |
*/ | |
def childNodes = [] | |
def attrKeys = ["name", "id","type", "tags"] | |
Node.findAllByProject(project).each { Node nd -> | |
nd.parents.each { | |
if (! childNodes.contains(it)) childNodes << it | |
} | |
Map ndMap = nd.toMap() | |
ndMap.remove("parents") | |
attrKeys.each { key -> | |
BuilderUtil.makeAttribute(ndMap, key) | |
} | |
ndMap.remove("typeId") | |
ndMap.attributes.each { Map m -> | |
BuilderUtil.makeAttribute(m, "value") | |
BuilderUtil.makeAttribute(m, "name") | |
m.remove("id") | |
m.remove("required") | |
} | |
BuilderUtil.makePlural(ndMap, "attributes") | |
root.yana.nodes << ndMap | |
} | |
BuilderUtil.makePlural(root.yana, "nodes") | |
/** | |
* ChildNodes | |
*/ | |
childNodes.each { ChildNode child -> | |
Map childMap = child.toMap() | |
if (childMap.relationship) { | |
BuilderUtil.makeAttribute(childMap, "relationship") | |
} else { | |
childMap.remove("relationship") | |
} | |
childMap.remove("id") | |
childMap.putAll(childMap.child) | |
childMap.remove("child") | |
BuilderUtil.makeAttribute(childMap, "name") | |
BuilderUtil.makeAttribute(childMap, "type") | |
BuilderUtil.makeAttribute(childMap, "id") | |
BuilderUtil.makeAttribute(childMap.parent, "name") | |
BuilderUtil.makeAttribute(childMap.parent, "type") | |
BuilderUtil.makeAttribute(childMap.parent, "id") | |
root.yana.children.child << childMap | |
} | |
/** | |
* Dump and return the data | |
*/ | |
bu.mapToDom(root, mb) | |
return writer.toString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment