小编Zac*_*ons的帖子

你如何在Groovy中一起使用GroupBy和Sum?

我有一个这样的集合:

[[patient1,value1], [patient2,value2]]
Run Code Online (Sandbox Code Playgroud)

例如.
x = [[1, 20.28], [1, 11.11], [2, 4.60], [2, 3.68]]

我用它countBy来得到每位患者的计数:

def counts = x.countBy{patient, value -> patient}
Run Code Online (Sandbox Code Playgroud)

我试图得到每个病人的总和没有运气:

def sums = x.groupBy({patient, value -> patient }).collectEntries{p, v -> p:v.sum()}
Run Code Online (Sandbox Code Playgroud)

有什么我想念的吗?

注意:这里的总体目标是获得患者使用的平均值:

def avgs = sums.collect{patient, value -> (value / counts[patient]) }
Run Code Online (Sandbox Code Playgroud)

谢谢!

groovy

5
推荐指数
1
解决办法
2277
查看次数

如何优化Grails中的选择属性?

我有一个生产使用的应用程序,当用户进入提案索引页面时,它需要很长时间,有时会超时.我已经将问题缩小为选择所有Proposal对象的SQL语句.问题是Proposal对象有许多图像(byte [])存储在内存中,而这些图像没有在索引页面中使用.这些图像很大,从而导致了问题.

我可以在Grails中优化此查询以删除该页面上不需要的属性或仅添加我在GSP中的属性的不同方法有哪些?

这是控制器代码(scaffolded):

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond Proposal.list(params), model:[proposalInstanceCount: Proposal.count()]
    }
Run Code Online (Sandbox Code Playgroud)

谢谢!

grails query-optimization grails-orm

2
推荐指数
1
解决办法
247
查看次数

为什么用户可以使用Spring Security和Grails进行更新而无需调用save()?

概述:

  • User,UserRole和Role几乎是Grails中SpringSecurity插件的标准模型
  • 我确保(在控制器中)的一件事是每个用户只有一个角色
  • 我的引导程序中创建了3个角色:管理员,管理员和用户
    • 管理员可以执行所有操作
    • 管理员可以更新其他管理员和用户
    • 用户只能更新自己(但不能更新他们的角色)

在我的控制器中,我通常使用注释来控制大多数安全性,但是对于更新和保存操作,我添加了用于更高级检查的逻辑:

@Transactional
def update(User userInstance) {
    User currentUser = User.get(springSecurityService.currentUser?.id)
    Role currentRole = currentUser.getAuthorities().getAt(0)    // There is only 1 role per user, so give the first

    Role roleInstance = Role.get(params['role.id'])

    // SECURITY LOGIC -> Move to service
    if (currentRole.authority.equals("ROLE_USER")) {

        if (userInstance != currentUser || !roleInstance.authority.equals("ROLE_USER")) {
            notAllowed(userInstance)
            return
        }
    } else if (currentRole.authority.equals('ROLE_MANAGER')) {
        ...
    }
    ...
    // REST OF CODE - User is saved here
}
Run Code Online (Sandbox Code Playgroud)

现在这是我遇到一个奇怪的问题的地方。如果我以ROLE_USER身份登录并更新ROLE_ADMIN,则会收到我应有的notAllowed错误消息,并且该操作将立即返回,因此不会继续到实际保存用户的REST OF CODE。

如果我查看管理员,则实际上它已被更新(持久化​​)。为什么会这样,因为它从来没有调用过save()?

谢谢!

grails spring-security

2
推荐指数
1
解决办法
277
查看次数