how to prevent casade on updates - grails

I'm working in grails and I've noticed some strange behavior to which I'd like to fix but I cannot figure out what I'm missing.
I have a location which can have may buildings. Each building can have multiple suites.
In addition I have a tenant who has a tenant location. The tenant location is used more for historical reporting so that we can see if tenants have moved locations.
The classes look like this
class Location {
String name
String shortName
Country country
LocationTypeEnum locationType = LocationTypeEnum.STUD // as default
static hasMany = [building: Building]
Date dateCreated
Date lastUpdated
static constraints = {
name unique: true, maxSize: 30
shortName unique: true, maxSize: 3
country nullable: false
locationType nullable: false
}
static mapping = {
cache usage: 'nonstrict-read-write', include: 'non-lazy'
}
}
class Building {
String type
String name
Date dateCreated
Date lastUpdated
static belongsTo = [location: Location]
static hasMany = [suites: Suite]
static constraints = {
name unique: true, maxSize: 25
type inList: ["A", "B", "C"], nullable: true
}
String toString() {
name
}
}
class Suite {
int suiteNumber
Date dateCreated
static belongsTo = [building: Building]
static constraints = {
pen(validator: { return it > 0 && (it.toString().length()) <= 3 })
}
String toString() {
suite.toString()
}
}
class Tenant {
static hasMany = [tenantLocation: TenantLocation]
----
Suite suite
String comments
Date dateCreated
Date lastUpdated
boolean active = true
static constraints = {
--------
type
nullable: false
comments maxSize: 5000
}
}
class TenantLocation {
static belongsTo = [tenant: Tenant]
Tenant tenant
Suite suite
Date dateCreated
}
So the idea is that a tenant location is created when tenant is created and a new tenantLocation is created only if and when the current tenant suite changes.
However, what I'm seeing is not only is the tenantLocation being saved (which is what I want) the suite is also being updated (which is not what I want).
For example I have Building 1 and suites 1 - 20 and Building 2 with suites 1 - 25. I have a tenant that is in Building 1 suite 5, they move to Building 2 suite 23. Now all of a sudden Building 2 has two suites with a suite number of 5.
How do I keep my suites from moving from one building to another when I only want the suite the tenant has to change?
The Code that is doing the updating is in the Tenant Controller and looks like this:
def update() {
def tenantInstance = Tenant.get(params.id)
if (!tenantInstance) {
flash.message = message(code: 'default.not.found.message', args: [
message(code: 'tenant.label', default: 'Tenant'),
params.id
])
redirect action: "list"
return
}
if (params.version) {
def version = params.version.toLong()
if (tenantInstance.version > version) {
tenantInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[
message(code: 'tenantInstance.label', default: 'Tenant')] as Object[],
"Another user has updated this Tenant while you were editing")
render view: "edit", model: [tenantInstance: tenantInstance, BuidingListInstance: Building.list()]
return
}
}
tenantInstance.properties = params
if (!tenantInstance.save(flush: true)) {
render view: "edit", model: [tenantInstance: tenantInstance, BuildingListInstance: Building.list()]
return
}
def tenantLocation= TenantLocation.findAllByTenant(tenantInstance)
def locationCheck = tenantLocation.last()
//if tenant location is not null and the suite/building change create new Tenant Location entry
if (tenantLocation!=null)
{
if(locationCheck.pen.id !=tenantInstance.suite.id)
{
def location = new TenantLocation(
tenant: tenantInstance,
suite: tenantInstance.suite,
dateCreated: new Date()
).save(flush:true)
}
}
flash.message = message(code: 'default.updated.message', args: [
message(code: 'tenant.label', default: 'Tenant'),
tenantInstance.id
])
redirect action: "show", id: tenantInstance.id
}

Related

Convert Sql Query to Grails/Gorm query

How do I convert the following SQL query to Grails/Gorm? Can this be done with a basic query? I would like to avoid using Criteria, Projections and HQL to keep it consistent with the structure of the other queries in my code base (which are basic queries).
SELECT dealer_group_user_dealer_users_id, COUNT(user_id)
FROM db.dealer_group_user_user
GROUP BY dealer_group_user_dealer_users_id;
And is it possible to perform the query in the gsp page to display the result for a specific user_id as opposed to running the query in the controller?
To update from a comment made, below is my domain classes.
class DealerGroupUser extends User{
static hasMany = [dealerUsers: User]
static constraints = {
}
}
class User {
transient authService
Boolean active = true
String firstName
String lastName
String title
String username
String emailAddress
String passwordHash
Date lastLoginTime
Date dateCreated
Date lastUpdated
Retailer dealer
Client client
Date passwordUpdated
Long dealerUser
Boolean isReadOnlyClientManager = false
String regionClientManager
// Transient properties
String fullName
String password
static transients = ['fullName', 'password']
static mapping = {
//permissions fetch: 'join'
sort firstName: 'asc' // TODO: Sort on fullName
}
static hasMany = [roles: Role, permissions: String]
static constraints = {
// NOTE: If a username is not provided, the user's email address will be used
firstName maxSize: 30, blank: false
lastName maxSize: 30, blank: false
title maxSize: 50, blank: false, nullable: true
username blank: false, unique: true
emailAddress email: true, unique: false, blank: false
passwordHash blank: false
lastLoginTime nullable: true
active nullable: true
dealer nullable: true
client nullable: true
passwordUpdated nullable: true
dealerUser nullable: true
regionClientManager nullable: true
}
void setEmailAddress(String emailAddress) {
if (EmailValidator.instance.isValid(emailAddress)) {
this.emailAddress = emailAddress
if (!username) {
username = emailAddress
}
}
}
static namedQueries = {
dealerGroupUsers {
eq 'class', 'com.db.torque.DealerGroupUser'
}
}
Integer setPassword(String plainTextPassword) {
plainTextPassword = plainTextPassword?.trim()
if (plainTextPassword) {
if (!plainTextPassword.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+\$).{8,}\$")){
return -1
}
String previousPassword = this.passwordHash
String newPassword = authService.encrypt(plainTextPassword)
if (previousPassword != newPassword) {
this.passwordHash = newPassword
return 1
}
else {
return -2
}
}
return -1
}
#Transient
public static List<User> findAllByRolesContains(Role role) {
return User.executeQuery("""
SELECT u
FROM User as u
WHERE :role IN elements(u.roles)
""", [role: role])
}
String fullName() {
return "${firstName} ${lastName}"
}
String toString() {
return fullName()
}
}
SELECT dealer_group_user_dealer_users_id, COUNT(user_id)
FROM db.dealer_group_user_user
GROUP BY dealer_group_user_dealer_users_id;
In your user class you have :
Retailer dealer
So lets start from the beginning what is query doing, listing users from user then getting a count of how many times user appears on Retailer domain class ?
The best way to do this would
be
String query = """ select
new map(
u.id as userId,
(select count(d) from DealerGroupUser d left join d.dealerUsers du where du=u) as userCount
)
From user u order by u.id
"""
def results = User.executeQuery(query,[:],[readOnly:true]
This should do what you are doing I think.
And is it possible to perform the query in the gsp page to display the
result for a specific user_id as opposed to running the query in the
controller?
Views are presentation layer and hardcore work should be kept out of it - if needed to use a TagLib call.
Controllers although used in grails examples and rolled out as part of defaults to make things easier also not the best place. You should be doing that in a service which is injected in a controller and presented in view as the actual model of what is needed.
That is the proper way - GSP have a runtime size - keep it short, keep it sweet

Error in creating objects

Hello I have these two classes
class Company {
String company_name
static constraints = {
company_name size: 3..20, unique: true, nullable: false
}
String toString() { return company_name}
}
class Job {
String job_name
Company company
static constraints = {
job_name size: 3..20, unique: false, nullable: false
company nullable: false
}
String toString() { return job_name}
}
and this method :
def register(String company_name,String job_name)
{
def job = new Job(company : new Company(company_name : company_name), job_name: job_name)
if (job.validate() && job.save())
{
redirect(url:"https//localhost:8080")
}
}
I can create object for example Company:nvidia, Job:worker but if I try to create another position for the company nvidia- Company:nvidia, Job:owner I get an error why?It is in the if clause
You have a unique constraint at the company_name attribute and your code is trying to create a new Company instance with every new Job instance.
You should do it like this:
def nvidiaCompany = new Company(company_name: 'nVidia').save()
def jobDesigner = new Job(company: nvidiaCompany, job_name: 'designer')
def jobDeveloper = new Job(company: nvidiaCompany, job_name: 'developer')
So you're reusing the Company instance. If you create just one Job instance in the code, you have to obtain the Company instance first:
def nvidiaCompany = Company.findByCompanyName('nvidia')
def jobDesigner = new Job(company: nvidiaCompany, job_name: 'designer')

Cannot redirect for object [ManageVehicle : (unsaved)] it is not a domain or has no identifier. Use an explicit redirect instead

This is my domain.
import org.apache.commons.lang.builder.EqualsBuilder
import org.apache.commons.lang.builder.HashCodeBuilder
class ManageVehicle implements Serializable {
String vehicle
Short truckKey=0
Short siteID
Integer unitID
String wheelConfig
String model
Short period=0
String usid=UUID.randomUUID().toString()
//static belongsTo = [wheelConfig : TTType,model:TTModel]
int hashCode() {
def builder = new HashCodeBuilder()
builder.append truckKey
builder.append siteID
builder.toHashCode()
}
boolean equals(other) {
if (other == null) return false
def builder = new EqualsBuilder()
builder.append truckKey, other.truckKey
builder.append siteID, other.siteID
builder.isEquals()
}
static mapping = {
table 'TF_Truck'
version false
truckKey column :'TruckKey'
siteID column :'SiteID'
id composite:['truckKey','siteID']
vehicle column: 'TruckID' ,sqlType: 'nvarchar'
wheelConfig column: 'TypID',sqlType: 'tinyint'
model column: 'ModID' ,sqlType: 'tinyint'
unitID column: 'McmID',sqlType: 'tinyint'
period column: 'Period'
usid generator: 'assigned', column:'USID', sqlType:'uniqueidentifier'
}
static constraints = {
period nullable: false
truckKey nullable: false
siteID nullable: false
}
}
And my save method in controller is .
def save(ManageVehicle manageVehicleInstance) {
manageVehicleInstance.siteID=RequestContextHolder.currentRequestAttributes().getAttribute("SiteID",RequestAttributes.SCOPE_SESSION) as Short
if (manageVehicleInstance == null) {
notFound()
return
}
if (manageVehicleInstance.hasErrors()) {
respond manageVehicleInstance.errors, view:'create'
return
}
manageVehicleInstance.save(flush:true,failOnError: true)
request.withFormat {
form {
flash.message = message(code: 'default.created.message', args: [message(code: 'manageVehicle.label', default: 'Manage Vehicle'), manageVehicleInstance.id])
redirect manageVehicleInstance
}
'*' { respond manageVehicleInstance, [status: CREATED] }
}
}
while i am applying save operation on this domain i am getting following exception .
Message: Cannot redirect for object [ManageVehicle : (unsaved)] it is not a domain or has no identifier. Use an explicit redirect instead
Suggest me some solution.
I guess that Grails cannot handle composite id in redirection.
I have two options in my mind to fix that case
Create id field for your domain class by removing id line from mappings
Implement custom redirection for your saved object like
redirect(action:'show', params:[truckKey:manageVehicleInstance.truckKey,siteID:manageVehicleInstance.siteID])
With 2. option you may also have to implement custom object loading into 'show' action using composite id like def manageVehicleInstance = ManageVehicle.findByTruckKeyAndSiteID(params.truckKey, params.siteID).

Grails clear hasMany entries and add new ones error?

I am currently working on a grails applications and I have a list of addresses that are attached to an account. Basically what I want to do is when the Account is edited is displays a current list of all the attached Addresses and then I can delete/add as many as I want from the view. When this data is captured it is picked up by the controller and what I want to do is to be able to clear all of the current Addresses from this account and then create the list again with what exists on the view, my code is below:
Account Domain:
class Account {
String name
Date dateCreated
Date lastUpdated
static hasMany = [addresses:Addresses]
static mapping = {
addresses cascade:"all-delete-orphan"
}
def getAddressesList() {
return LazyList.decorate(
addresses,
FactoryUtils.instantiateFactory(Addresses.class))
}
static constraints = {
name(blank:false, unique: true)
}
}
Address Domain:
class Addresses {
int indexVal
String firstLine
String postcode
String area
static belongsTo = [account:Account]
static mapping = {
}
static transients = [ 'deleted' ]
static constraints = {
indexVal(blank:false, min:0)
}
}
Account Controller:
def update() {
def accountInstance = Account.get(params.id)
if (!accountInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'account.label', default: 'Account'), params.id])
redirect(action: "list")
return
}
if (params.version) {
def version = params.version.toLong()
if (accountInstance.version > version) {
accountInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'subscriptions.label', default: 'Subscriptions')] as Object[],
"Another user has updated this Account while you were editing")
render(view: "edit", model: [accountInstance: accountInstance])
return
}
}
accountInstance.properties = params
accountInstance.addresses.clear()
accountInstance.save(flush: true)
....
}
Error:
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.tool.Account.addresses. Stacktrace follows:
Message: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.tool.Account.addresses
This error seems to be occurring in the controller on line:
accountInstance.save(flush: true)
I have tried several different way to get this to work and would really appreciate some help.
So it seems like you have done some work that Grails can do for you.
class Account {
String name
Date dateCreated
Date lastUpdated
List addresses
static hasMany = [addresses:Address]
static mapping = {
addresses cascade:"all-delete-orphan"
}
static constraints = {
name(blank:false, unique: true)
}
}
class Address {
String firstLine
String postcode
String area
static belongsTo = [account:Account]
}
This will produce the effect you want of having addresses being a list.
I've found either
instance.addresses = null
or
instance.addresses.clear()
to work for me
When you define addresses cascade:"all-delete-orphan" in Account class you don't need static belongsTo = [account:Account] in Addresses. So just try to remove that statement and test your code. See related link.

grails 2 integration test

I have created an Integration Test in Grails 2.x with the purpouse of testing several controllers in order to simulate a complete user flow within a Web App.
First I saved two simple domain object records, instantiating two controller instances within my test case. This two simple classes are:
- Category (category of a product) and
- Store (mini market where products are sale)
I wrote two testing methods to control the controller instances (StoreController and CategoryController) and worked fine, one category record was created and several store records as well.
Then, I wrote a third method to try to save a Product record. This record must reference a Category. So, I take the category instance from previous methods and try to pass it to the ProductController among the other Product parameters.
For an unknown reason, I'm only able to create Product records through the web app instance typing the data in the web browsers, but not by the test class code.
I think the problem issue is with the relationship between Product and Category, but I could not figured out how I should pass the parameters to the ProductController from my test case in order to create a new Product record.
This is my Product class:
class Product {
int id
String barCode
String shortDesc
String longDesc
String format
Category category
static belongsTo = [category: Category]
//static hasManySurveyRecord = [surveyRecord: SurveyRecord]
static constraints = {
id editable: false
barCode blank: false, nullable: false, maxSize: 64
shortDesc blank: false, nullable: false, maxSize: 32
longDesc blank: false, nullable: false, maxSize: 128
category blank: false
}
static mapping = {
table 'product_catalog'
version false
columns {
id column: 'id'
barCode column: 'bar_code'
shorDesc column: 'short_desc'
longDesc column: 'long_desc'
format column: 'format'
}
}
String toString() {
return longDesc
}
}
This is my Category class:
class Category {
int id
String description
static hasManyProduct = [products: Product]
static constraints = {
id editable: false
description blank: false, nullable: false, maxSize: 64
}
static mapping = {
table 'categories'
version false
columns {
id column: 'id'
description column: 'description'
}
}
String toString() {
return description
}
}
This is my integration test for this app:
static transactional = false
static storeList = []
static storesData = []
static categoryIns
static categoryData = []
static productIns = new Product()
static productData = []
#Before
void setup() {
storesData.add([zipCode: 926260001, address: "first test avenue, first city, CA" , description: "first testing store"])
storesData.add([zipCode: 926260002, address: "second test avenue, second city, CA" , description: "second testing store"])
storesData.add([zipCode: 926260003, address: "third test avenue, third city, CA" , description: "third testing store"])
storesData.add([zipCode: 926260004, address: "fourth test avenue, fourth city, CA" , description: "fourth testing store"])
storesData.add([zipCode: 926260005, address: "fifth test avenue, fifth city, CA" , description: "fifth testing store"])
storesData.add([zipCode: 926260006, address: "sixth test avenue, sixth city, CA" , description: "sixth testing store"])
storesData.add([zipCode: 926260007, address: "seventh test avenue, seventh city, CA", description: "seventh testing store"])
storesData.add([zipCode: 926260008, address: "eighth test avenue, eighth city, CA" , description: "eighth testing store"])
categoryData = [description: "testing category"]
productData = [barCode: "0114B", shorDesc: "The short testing description", longDesc: "The long testing description ....", format: "1 LT"]
}
#Test
void _01__createStores() {
def storeCtl = new StoreController()
(0..7).each { i ->
def model = storeCtl.create()
assert model.storeInstance != null
storeCtl.response.reset()
storeCtl.params.putAll( storesData.get(i) )
storeCtl.save()
assert Store.count() == (i+1)
}
storeList.addAll( Store.list() )
assert storeList.size() == Store.list().size()
}
#Test
void _02__createCategory() {
// Test if there is a previous store list created
assert storeList.size() == Store.list().size()
def categoryCtl = new CategoryController()
def model = categoryCtl.create()
assert model.categoryInstance != null
categoryCtl.response.reset()
categoryCtl.params.putAll( categoryData )
categoryCtl.save()
assert Category.count() == 1
categoryIns = Category.list()[0]
}
#Test
void _03__createProduct() {
// Test if there is a previous store list created
assert storeList.size() == Store.list().size()
// Test if there is a previous category created
assert categoryIns != null
assert categoryIns.id > 0
def productCtl = new ProductController()
def model = productCtl.create()
assert model.productInstance != null
productCtl.response.reset()
productData.put("category", [id: categoryIns.id])
productData.put("category.id", categoryIns.id)
productCtl.params.putAll( productData )
//productCtl.params.category = Category.get(categoryIns.id)
productCtl.save()
assert Product.count() == 1
}
This is my ProductController code extract:
def save() {
def productInstance = new Product(params)
if (!productInstance.save(flush: true)) {
render(view: "create", model: [productInstance: productInstance])
return
}
flash.message = message(code: 'default.created.message', args: [message(code: 'product.label', default: 'Product'), productInstance.id])
redirect(action: "show", id: productInstance.id)
}
Both recomendations worked fine. After hours and hours trying to figure out what were going wrong, thanks to you I saw clear my silly silly mistake: a field "shortDesc" was misspelled as "shorDesc".
So, one little tip to my book has been written.
Thank you very much!

Resources