class ResourceServiceImpl implements ResourceService {

    @Override
    public void updateSecurityRules(long id, List<SecurityRule> rules)
            throws BadRequestServiceEx, InternalErrorServiceEx, NotFoundServiceEx
    {
        Resource resource = resourceDAO.find(id);

        if (resource != null) {
            Search searchCriteria = new Search();
            searchCriteria.addFilterEqual("resource.id", id);

            List<SecurityRule> resourceRules = this.securityDAO.search(searchCriteria);

            // remove previous rules
            for (SecurityRule rule : resourceRules) {
                securityDAO.remove(rule);
            }
            // insert new rules
            for (SecurityRule rule : rules) {
                rule.setResource(resource);
                if (rule.getGroup() != null) {
                    UserGroup ug = userGroupDAO.find(rule.getGroup().getId());
                    if (ug != null) {
                        rule.setGroup(ug);
                    }
                }
                securityDAO.persist(rule);
            }
        } else {
            throw new NotFoundServiceEx("Resource not found " + id);
        }
    }

}