関連するいくつかのドメイン クラスがあり、複数のドメインに依存する制約を実装する方法を見つけようとしています。問題の要点は次のとおりです。
資産には多くの容量プール オブジェクトがあります
アセットには多くの Resource オブジェクトがあります
リソースを作成/編集するとき、資産の合計リソースが容量を超えていないことを確認する必要があります。
これを実現するサービス メソッドを作成しましたが、これはリソース ドメインのバリデータを介して行うべきではありませんか? 以下にリストされている私のサービスクラス:
def checkCapacityAllocation(Asset asset, VirtualResource newItem) {
// Get total Resources allocated from "asset"
def allAllocated = Resource.createCriteria().list() {
like("asset", asset)
}
def allocArray = allAllocated.toArray()
def allocTotal=0.0
for (def i=0; i<allocArray.length; i++) {
allocTotal = allocTotal.plus(allocArray[i].resourceAllocated)
}
// Get total capacities for "asset"
def allCapacities = AssetCapacity.createCriteria().list() {
like("asset", asset)
}
def capacityArray = allCapacities.toArray()
def capacityTotal = 0.0
for (def i=0; i<capacityArray.length; i++) {
capacityTotal += capacityArray[i].actualAvailableCapacity
}
if (allocTotal > capacityTotal) {
return false
}
}
return true
}
私が抱えている問題は、この方法を検証に使用することです。JqGrid プラグイン (インライン編集あり) を使用していますが、エラー報告に問題があります。ドメインでこの種の検証を行うことができれば、作業はずっと簡単になります。助言がありますか?
本当にありがとう!