<commit_before>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

package org.openlmis.referencedata.repository;

import org.javers.spring.annotation.JaversSpringDataAuditable;
import org.openlmis.referencedata.domain.Facility;
import org.openlmis.referencedata.domain.Program;
import org.openlmis.referencedata.domain.Right;
import org.openlmis.referencedata.domain.SupervisoryNode;
import org.openlmis.referencedata.domain.User;
import org.openlmis.referencedata.repository.custom.UserRepositoryCustom;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@JaversSpringDataAuditable
public interface UserRepository extends
    PagingAndSortingRepository<User, UUID>,
    UserRepositoryCustom {

  @Override
  <S extends User> S save(S entity);

  @Override
  <S extends User> Iterable<S> save(Iterable<S> entities);

  User findOneByUsername(@Param("username") String username);

  User findOneByEmail(@Param("email") String email);

  @Query(value = "SELECT u.*"
      + " FROM referencedata.users u"
      + " WHERE u.extradata @> (:extraData)\\:\\:jsonb",
      nativeQuery = true
  )
  List<User> findByExtraData(@Param("extraData") String extraData);

  @Query(value = "SELECT DISTINCT u.*"
      + " FROM referencedata.users u"
      + "   JOIN referencedata.role_assignments ra ON ra.userid = u.id" 
      + "   JOIN referencedata.roles r ON r.id = ra.roleid" 
      + "   JOIN referencedata.role_rights rr ON rr.roleid = r.id"
      + " WHERE rr.rightid = :right" 
      + "   AND ra.supervisorynodeid = :supervisoryNode" 
      + "   AND ra.programid = :program",
      nativeQuery = true)
  Set<User> findSupervisingUsersBy(@Param("right") Right right,
      @Param("supervisoryNode") SupervisoryNode supervisoryNode,
      @Param("program") Program program);

  @Query(value = "SELECT DISTINCT u.*"
      + " FROM referencedata.users u"
      + "   JOIN referencedata.role_assignments ra ON ra.userid = u.id"
      + "   JOIN referencedata.roles r ON r.id = ra.roleid"
      + "   JOIN referencedata.role_rights rr ON rr.roleid = r.id"
      + " WHERE rr.rightid = :right"
      + "   AND ra.warehouseid = :warehouse",
      nativeQuery = true)
  Set<User> findUsersByFulfillmentRight(@Param("right") Right right,
                        @Param("warehouse") Facility warehouse);

  @Query(value = "SELECT DISTINCT u.*"
      + " FROM referencedata.users u"
      + "   JOIN referencedata.role_assignments ra ON ra.userid = u.id"
      + "   JOIN referencedata.roles r ON r.id = ra.roleid"
      + "   JOIN referencedata.role_rights rr ON rr.roleid = r.id"
      + " WHERE rr.rightid = :right",
      nativeQuery = true)
  Set<User> findUsersByDirectRight(@Param("right") Right right);

  @Query("SELECT u FROM User u WHERE u.id in ?1")
  Page<User> findAllByIds(Iterable<UUID> ids, Pageable pageable);
<commit_msg>OLMIS-3600: improve query method for fetch by ids<commit_after>/*
 * This program is part of the OpenLMIS logistics management information system platform software.
 * Copyright © 2017 VillageReach
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Affero General Public License for more details. You should have received a copy of
 * the GNU Affero General Public License along with this program. If not, see
 * http://www.gnu.org/licenses.  For additional information contact <EMAIL>. 
 */

package org.openlmis.referencedata.repository;

import org.javers.spring.annotation.JaversSpringDataAuditable;
import org.openlmis.referencedata.domain.Facility;
import org.openlmis.referencedata.domain.Program;
import org.openlmis.referencedata.domain.Right;
import org.openlmis.referencedata.domain.SupervisoryNode;
import org.openlmis.referencedata.domain.User;
import org.openlmis.referencedata.repository.custom.UserRepositoryCustom;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@JaversSpringDataAuditable
public interface UserRepository extends
    PagingAndSortingRepository<User, UUID>,
    UserRepositoryCustom {

  @Override
  <S extends User> S save(S entity);

  @Override
  <S extends User> Iterable<S> save(Iterable<S> entities);

  User findOneByUsername(@Param("username") String username);

  User findOneByEmail(@Param("email") String email);

  @Query(value = "SELECT u.*"
      + " FROM referencedata.users u"
      + " WHERE u.extradata @> (:extraData)\\:\\:jsonb",
      nativeQuery = true
  )
  List<User> findByExtraData(@Param("extraData") String extraData);

  @Query(value = "SELECT DISTINCT u.*"
      + " FROM referencedata.users u"
      + "   JOIN referencedata.role_assignments ra ON ra.userid = u.id" 
      + "   JOIN referencedata.roles r ON r.id = ra.roleid" 
      + "   JOIN referencedata.role_rights rr ON rr.roleid = r.id"
      + " WHERE rr.rightid = :right" 
      + "   AND ra.supervisorynodeid = :supervisoryNode" 
      + "   AND ra.programid = :program",
      nativeQuery = true)
  Set<User> findSupervisingUsersBy(@Param("right") Right right,
      @Param("supervisoryNode") SupervisoryNode supervisoryNode,
      @Param("program") Program program);

  @Query(value = "SELECT DISTINCT u.*"
      + " FROM referencedata.users u"
      + "   JOIN referencedata.role_assignments ra ON ra.userid = u.id"
      + "   JOIN referencedata.roles r ON r.id = ra.roleid"
      + "   JOIN referencedata.role_rights rr ON rr.roleid = r.id"
      + " WHERE rr.rightid = :right"
      + "   AND ra.warehouseid = :warehouse",
      nativeQuery = true)
  Set<User> findUsersByFulfillmentRight(@Param("right") Right right,
                        @Param("warehouse") Facility warehouse);

  @Query(value = "SELECT DISTINCT u.*"
      + " FROM referencedata.users u"
      + "   JOIN referencedata.role_assignments ra ON ra.userid = u.id"
      + "   JOIN referencedata.roles r ON r.id = ra.roleid"
      + "   JOIN referencedata.role_rights rr ON rr.roleid = r.id"
      + " WHERE rr.rightid = :right",
      nativeQuery = true)
  Set<User> findUsersByDirectRight(@Param("right") Right right);

  @Query("SELECT u FROM User u WHERE u.id in :ids")
  Page<User> findAllByIds(@Param("ids") Iterable<UUID> ids, Pageable pageable);

{'fraction_non_alphanumeric': 0.10295961876097316, 'fraction_numerical': 0.0018811136192626034, 'mean_word_length': 3.8333333333333335, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '377022', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2377, 'n_words': 682}