grails - 在Grails中克隆域实例

标签 grails gorm clone

我想知道如何将克隆功能添加到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/

相关文章:

multithreading - Grails promise 未调用回调

hibernate - 非空属性引用一个 transient 值-必须在当前操作之前保存 transient 实例

jquery - jqGrid 拖放行的副本,而不是移动行?

gitolite-admin 克隆问题

jquery - 如何多次克隆包含多个输入字段的 div?

grails - 子类对象不能删除

regex - grails的任何BDD框架(spock/geb/easyb/other)是否支持人类可读描述的正则表达式解析?

list - grails:使用 findAllBy 时保持列表顺序

grails - Grails-自定义Flash.message渲染

mongodb - 指向无效属性-MongoDB&Grails 3.3+