0

関連するいくつかのドメイン クラスがあり、複数のドメインに依存する制約を実装する方法を見つけようとしています。問題の要点は次のとおりです。

資産には多くの容量プール オブジェクトがあります

アセットには多くの 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
}

私が抱えている問題は、この方法を検証に使用することです。JqG​​rid プラグイン (インライン編集あり) を使用していますが、エラー報告に問題があります。ドメインでこの種の検証を行うことができれば、作業はずっと簡単になります。助言がありますか?

本当にありがとう!

4

2 に答える 2

0

どうですか:

def resourceCount = Resource.countByAsset(assetId)
def assetCapacityCount = AssetCapacity.countByAsset(assetId)
if(resourceCount < assetCapacityCount) return true
return false

HTH

于 2012-07-24T00:51:58.613 に答える
0

サービス メソッドをバリデーターとして使用するには、サービスをドメインに挿入し、それを呼び出すカスタム バリデーターを追加する必要があります。次のようになると思います。

class Asset {

    def assetService

    static hasMany = [resources: Resource]

    static constraints = {
        resources(validator: { val, obj ->
            obj.assetService.checkCapacityAllocation(obj, val)
        })
    }
}
于 2012-07-24T03:07:28.350 に答える