gpt4 book ai didi

grails - 在Grails中克隆域实例

转载 作者:行者123 更新时间:2023-12-02 14:23:27 27 4
gpt4 key购买 nike

我想知道如何将克隆功能添加到grails应用程序中。我在下面附加了一张图片,该图片解释了我的域类如何关联。一个模板有很多步骤,而这些步骤每个都有很多输入和/或输出。

目前,我可以在index.gsp页面上查看我的模板,但是我希望能够克隆整个模板以及它们所包含的步骤/输入/输出。

这有可能吗?

最佳答案

这是深度克隆的一个版本。尽管它是为满足特定需求而定制的,但它非常通用。我很确定上述情况已经很好地涵盖了。

 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"])

第一个参数是要克隆的原始对象
第二个参数是不能克隆的列的列表
第三个参数是必须按原样引用的属性列表。模板可能属于某个父对象,在克隆期间必须保持相同。

要保存克隆的对象,请创建另一个满足您自定义要求的方法。以上代码也可以在其他情况下使用。

关于grails - 在Grails中克隆域实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27217445/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com