You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GroupDao.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.db.user;
  21. import java.util.Collection;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.Locale;
  25. import java.util.Optional;
  26. import javax.annotation.CheckForNull;
  27. import javax.annotation.Nullable;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.apache.ibatis.session.RowBounds;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.db.Dao;
  32. import org.sonar.db.DaoUtils;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.WildcardPosition;
  35. import static org.sonar.db.DatabaseUtils.executeLargeInputs;
  36. public class GroupDao implements Dao {
  37. private final System2 system;
  38. public GroupDao(System2 system) {
  39. this.system = system;
  40. }
  41. /**
  42. * @param dbSession
  43. * @param name non-null group name
  44. * @return the group with the given organization key and name
  45. */
  46. public Optional<GroupDto> selectByName(DbSession dbSession, String name) {
  47. return Optional.ofNullable(mapper(dbSession).selectByName(name));
  48. }
  49. public List<GroupDto> selectByNames(DbSession dbSession, Collection<String> names) {
  50. return executeLargeInputs(names, pageOfNames -> mapper(dbSession).selectByNames(pageOfNames));
  51. }
  52. @CheckForNull
  53. public GroupDto selectByUuid(DbSession dbSession, String groupUuid) {
  54. return mapper(dbSession).selectByUuid(groupUuid);
  55. }
  56. public List<GroupDto> selectByUuids(DbSession dbSession, List<String> uuids) {
  57. return executeLargeInputs(uuids, mapper(dbSession)::selectByUuids);
  58. }
  59. public void deleteByUuid(DbSession dbSession, String groupUuid) {
  60. mapper(dbSession).deleteByUuid(groupUuid);
  61. }
  62. public int countByQuery(DbSession session, @Nullable String query) {
  63. return mapper(session).countByQuery(groupSearchToSql(query));
  64. }
  65. public List<GroupDto> selectByQuery(DbSession session, @Nullable String query, int offset, int limit) {
  66. return mapper(session).selectByQuery(groupSearchToSql(query), new RowBounds(offset, limit));
  67. }
  68. public GroupDto insert(DbSession session, GroupDto item) {
  69. Date createdAt = new Date(system.now());
  70. item.setCreatedAt(createdAt)
  71. .setUpdatedAt(createdAt);
  72. mapper(session).insert(item);
  73. return item;
  74. }
  75. public GroupDto update(DbSession session, GroupDto item) {
  76. item.setUpdatedAt(new Date(system.now()));
  77. mapper(session).update(item);
  78. return item;
  79. }
  80. public List<GroupDto> selectByUserLogin(DbSession session, String login) {
  81. return mapper(session).selectByUserLogin(login);
  82. }
  83. @CheckForNull
  84. private static String groupSearchToSql(@Nullable String query) {
  85. if (query == null) {
  86. return null;
  87. }
  88. String upperCasedNameQuery = StringUtils.upperCase(query, Locale.ENGLISH);
  89. return DaoUtils.buildLikeValue(upperCasedNameQuery, WildcardPosition.BEFORE_AND_AFTER);
  90. }
  91. private static GroupMapper mapper(DbSession session) {
  92. return session.getMapper(GroupMapper.class);
  93. }
  94. }