我想知道如何将克隆功能添加到我的grails应用程序中。我在下面附上了一张图片,说明了我的域类如何关联。一个模板有许多步骤,每个步骤都有许多输入和/或输出。
目前我可以在index.gsp页面上查看我的模板,但我希望能够克隆整个模板及其包含的步骤/输入/输出。
这是可能的,如果是这样的话怎么样?
答案 0 :(得分:3)
这是深度克隆的一个版本。虽然它有点定制以满足特定需求,但它非常通用。而且我很确定上面说的情况很好地涵盖了这个。
Object deepClone(def domainInstanceToClone, def notCloneable) {
return deepClone(domainInstanceToClone, notCloneable, null)
}
Object deepClone(def domainInstanceToClone) {
return deepClone(domainInstanceToClone, null, null)
}
Object deepClone(def domainInstanceToClone, def notCloneable, def bindOriginal) {
if (domainInstanceToClone.getClass().name.contains("_javassist"))
return null
//Our target instance for the instance we want to clone
def newDomainInstance = domainInstanceToClone?.getClass()?.newInstance()
//Returns a DefaultGrailsDomainClass (as interface GrailsDomainClass) for inspecting properties
GrailsClass domainClass = domainInstanceToClone.domainClass.grailsApplication.getDomainClass(newDomainInstance.getClass().name)
for (DefaultGrailsDomainClassProperty prop in domainClass?.getPersistentProperties()) {
if (notCloneable && prop.name in notCloneable) {
continue
}
if (bindOriginal && prop.name in bindOriginal) {
newDomainInstance."${prop.name}" = domainInstanceToClone."${prop.name}"
continue
}
if (prop.association) {
if (prop.owningSide) {
//we have to deep clone owned associations
if (prop.oneToOne) {
def newAssociationInstance = deepClone(domainInstanceToClone?."${prop.name}", notCloneable, bindOriginal)
newDomainInstance."${prop.name}" = newAssociationInstance
} else {
domainInstanceToClone."${prop.name}".each { associationInstance ->
def newAssociationInstance = deepClone(associationInstance, notCloneable, bindOriginal)
if (prop.oneToMany) {
if (newAssociationInstance) {
newDomainInstance."addTo${prop.name.capitalize()}"(newAssociationInstance)
}
} else {
newDomainInstance."${prop.name}" = newAssociationInstance
}
}
}
} else {
if (!prop.bidirectional) {
//If the association isn't owned or the owner, then we can just do a shallow copy of the reference.
newDomainInstance."${prop.name}" = domainInstanceToClone."${prop.name}"
}
// @@JR
// Yes bidirectional and not owning. E.g. clone Report, belongsTo Organisation which hasMany
// manyToOne. Just add to the owning objects collection.
else {
//println "${prop.owningSide} - ${prop.name} - ${prop.oneToMany}"
//return
if (prop.manyToOne) {
newDomainInstance."${prop.name}" = domainInstanceToClone."${prop.name}"
def owningInstance = domainInstanceToClone."${prop.name}"
// Need to find the collection.
String otherSide = prop.otherSide.name.capitalize()
//println otherSide
//owningInstance."addTo${otherSide}"(newDomainInstance)
} else if (prop.manyToMany) {
//newDomainInstance."${prop.name}" = [] as Set
domainInstanceToClone."${prop.name}".each {
//newDomainInstance."${prop.name}".add(it)
}
} else if (prop.oneToMany) {
domainInstanceToClone."${prop.name}".each { associationInstance ->
def newAssociationInstance = deepClone(associationInstance, notCloneable, bindOriginal)
newDomainInstance."addTo${prop.name.capitalize()}"(newAssociationInstance)
}
}
}
}
} else {
//If the property isn't an association then simply copy the value
newDomainInstance."${prop.name}" = domainInstanceToClone."${prop.name}"
if (prop.name == "activationDate") {
newDomainInstance."${prop.name}" = new Date()
}
}
}
return newDomainInstance
}
示例用法是: -
Template cloneTemplate = cloneService.deepClone(originalTemplate,["id","name"],["parent"])
第一个参数是要克隆的原始对象 第二个参数是不能克隆的列的列表 第三个参数是必须引用的属性列表,因为它是。模板可能属于某个父项,在克隆过程中必须保持相同。
要保存克隆对象,请创建另一个满足自定义要求的方法。以上代码也适用于其他方案。