<commit_before>package org.openlmis.core.service;

import lombok.NoArgsConstructor;
import org.openlmis.core.domain.*;
import org.openlmis.core.repository.RoleRightsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import static java.util.Arrays.asList;

@Service
@NoArgsConstructor
public class RoleRightsService {

  private RoleRightsRepository roleRightsRepository;
  private SupervisoryNodeService supervisoryNodeService;
  private FacilityService facilityService;

  @Autowired
  public RoleRightsService(RoleRightsRepository roleRightsRepository, SupervisoryNodeService supervisoryNodeService, FacilityService facilityService) {
    this.roleRightsRepository = roleRightsRepository;
    this.supervisoryNodeService = supervisoryNodeService;
    this.facilityService = facilityService;
  }

  public Set<Right> getRights(String username) {
    return roleRightsRepository.getAllRightsForUser(username);
  }

  public Set<Right> getAllRights() {
    return new LinkedHashSet<>(asList(Right.values()));
  }

  public void saveRole(Role role) {
    role.validate();
    roleRightsRepository.createRole(role);
  }

  public List<Role> getAllRoles() {
    return roleRightsRepository.getAllRoles();
  }

  public Role getRole(int id) {
    return roleRightsRepository.getRole(id);
  }

  public void updateRole(Role role) {
    roleRightsRepository.updateRole(role);
  }

  public Set<Right> getRights(Integer userId) {
    return roleRightsRepository.getAllRightsForUser(userId);
  }

  public List<Right> getRightsForUserAndFacilityProgram(Integer userId, Facility facility, Program program) {
    List<Right> result = new ArrayList<>();
    Facility homeFacility = facilityService.getHomeFacility(userId);
    if (homeFacility!=null && homeFacility.getId().equals(facility.getId()))
      result.addAll(roleRightsRepository.getRightsForUserOnHomeFacilityAndProgram(userId, program));

    SupervisoryNode supervisoryNode = supervisoryNodeService.getFor(facility, program);
    List<SupervisoryNode> supervisoryNodes = supervisoryNodeService.getAllParentSupervisoryNodesInHierarchy(supervisoryNode);

    if (supervisoryNode != null)
      result.addAll(roleRightsRepository.getRightsForUserOnSupervisoryNodeAndProgram(userId, supervisoryNodes, program));
    return result;
  }
}<commit_msg>#496 | Sandeep | minor refactoring<commit_after>package org.openlmis.core.service;

import lombok.NoArgsConstructor;
import org.openlmis.core.domain.*;
import org.openlmis.core.repository.RoleRightsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import static java.util.Arrays.asList;

@Service
@NoArgsConstructor
public class RoleRightsService {

  private RoleRightsRepository roleRightsRepository;
  private SupervisoryNodeService supervisoryNodeService;
  private FacilityService facilityService;

  @Autowired
  public RoleRightsService(RoleRightsRepository roleRightsRepository, SupervisoryNodeService supervisoryNodeService, FacilityService facilityService) {
    this.roleRightsRepository = roleRightsRepository;
    this.supervisoryNodeService = supervisoryNodeService;
    this.facilityService = facilityService;
  }

  public Set<Right> getRights(String username) {
    return roleRightsRepository.getAllRightsForUser(username);
  }

  public Set<Right> getAllRights() {
    return new LinkedHashSet<>(asList(Right.values()));
  }

  public void saveRole(Role role) {
    role.validate();
    roleRightsRepository.createRole(role);
  }

  public List<Role> getAllRoles() {
    return roleRightsRepository.getAllRoles();
  }

  public Role getRole(int id) {
    return roleRightsRepository.getRole(id);
  }

  public void updateRole(Role role) {
    roleRightsRepository.updateRole(role);
  }

  public Set<Right> getRights(Integer userId) {
    return roleRightsRepository.getAllRightsForUser(userId);
  }

  public List<Right> getRightsForUserAndFacilityProgram(Integer userId, Facility facility, Program program) {
    List<Right> result = new ArrayList<>();
    Facility homeFacility = facilityService.getHomeFacility(userId);
    if (homeFacility != null && homeFacility.getId().equals(facility.getId()))
      result.addAll(roleRightsRepository.getRightsForUserOnHomeFacilityAndProgram(userId, program));

    SupervisoryNode supervisoryNode = supervisoryNodeService.getFor(facility, program);

    if (supervisoryNode != null) {
      List<SupervisoryNode> supervisoryNodes = supervisoryNodeService.getAllParentSupervisoryNodesInHierarchy(supervisoryNode);
      result.addAll(roleRightsRepository.getRightsForUserOnSupervisoryNodeAndProgram(userId, supervisoryNodes, program));
    }
    return result;
  }
}
{'fraction_non_alphanumeric': 0.08693033527404136, 'fraction_numerical': 0.0006022886970487854, 'mean_word_length': 5.9581005586592175, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6205045', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1417, 'n_words': 256}