【问题标题】:Cloning an instance of domain in Grails在 Grails 中克隆域的实例
【发布时间】:2014-11-30 19:47:56
【问题描述】:

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

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

这可能吗?如果可以,怎么办?

【问题讨论】:

  • “克隆”是什么意思?您的问题没有解释这意味着什么以及您期望的结果。
  • 我不完全确定如何措辞,但我希望能够获取模板(域类)的实例并能够复制其所有值以及所有值在与之相关的实例中。对不起,如果这不是更清楚。
  • 不,现在很清楚了。您可能想查看这些似乎满足您需求的其他帖子:stackoverflow.com/questions/20220711/…stackoverflow.com/questions/17614791/…
  • 谢谢!你知道如何处理这个 ID 吗?
  • 这完全取决于您所说的“处理 ID”。您应该能够修改给出的示例以做任何您需要的事情。

标签: grails grails-orm clone


【解决方案1】:

这里是深度克隆的一个版本。虽然它有点定制以满足特定需求,但它非常通用。而且我很确定上述场景已经很好地涵盖了这一点。

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

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

要保存克隆的对象,请创建另一个满足您的自定义要求的方法。以上代码也适用于其他场景。

【讨论】:

  • 要使上述内容作为服务工作,请创建一个 CloneService.groovy 并对其进行编译,之后您将需要这两个 def:class CloneService { def grailsApplication def databaseUtilityService 并且您将顶部需要这两个导入:import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClassProperty import org.codehaus.groovy.grails.commons.GrailsClass
猜你喜欢
  • 2021-07-23
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2014-11-22
相关资源
最近更新 更多