I'm confused as to how to implement this or if it's really even possible/appropriate. My colleague and I are building a web app for a client using Grails 3. He created the initial domains which I'm guessing where an almost one-to-one copy from the Realm models from the mobile apps. I've since modified them in an attempt to get some form of deep cloning to work as three domains have a one-to-many relationship.
The Problem
How would I go about creating a deep copy of a domain? I have tried suggested answers with little success:
Picking ideas from various places I've come to formulating a clone(Domain)
method shown below. It almost works (I think), but has issues with the collections throwing a HibernateException - Found shared references to a collection: Location.equipments
.
Called in a controller as:
def copy() {
Survey.clone(Survey.get(params.id))
redirect action: 'index'
}
Any ideas or guidance?
Currently the domains are the following:
class Survey {
int id
String name
String contactName
String contactEmail
String facilityAddress
String facilityCity
String facilityStateProvince
String facilityZip
String distributorName
String distributorEmail
String distributorPhoneNumber
static Survey clone(Survey self) {
Survey clone = new Survey()
String exclude = "locations"
clone.properties = self.properties.findAll {
it.key != exclude
}
self.locations.each {
Location copy = Location.clone it
clone.addToLocations copy
}
clone.save()
}
static transients = ['clone']
static belongsTo = User
static hasMany = [locations: Location]
}
class Location {
int id
String name
String[] hazardsPresent
HazardType[] hazardTypes
ExposureArea[] exposureArea
RiskLevel exposureLevel
String comments
byte[] picture
static Location clone(Location self) {
Location clone = new Location()
String[] excludes = ['equipment', 'products']
clone.properties = self.properties.findAll {
!(it.key in excludes)
}
self.equipments.each {
Equipment copy = Equipment.clone it
self.addToEquipments copy
}
self.products.each {
RecommendedProduct copy = new RecommendedProduct()
copy.properties = it.properties
copy.save()
clone.addToProducts copy
}
clone.save()
}
static transients = ['clone']
static belongsTo = Survey
static hasMany = [equipments: Equipment, products: RecommendedProduct]
static constraints = {
picture(maxSize: 1024 * 1024)
}
}
class Equipment {
int id
EquipmentType type
String name
Brand brand
// Redacted 26 boolean properties
// ...
static Equipment clone(Equipment self) {
Equipment clone = new Equipment()
String exclude = "extras"
clone.properties = self.properties.findAll {
it.key != exclude
}
self.extras.each {
EquipmentQuestionExtra copy = new EquipmentQuestionExtra()
copy.properties = it.properties
copy.save()
clone.addToExtras copy
}
clone.save()
}
static transients = ['clone']
static belongsTo = Location
static hasMany = [extras: EquipmentQuestionExtra]
}
class RecommendedProduct {
int productId
int quantityChosen
String comment
static belongsTo = Location
}
class EquipmentQuestionExtra {
int id
String questionText
String comment
byte[] picture
static belongsTo = Equipment
static constraints = {
picture(maxSize: 1024 * 1024)
}
}