Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

UserPropertiesDao.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.List;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.core.util.UuidFactory;
  25. import org.sonar.db.Dao;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.audit.AuditPersister;
  28. import org.sonar.db.audit.model.PropertyNewValue;
  29. public class UserPropertiesDao implements Dao {
  30. private final System2 system2;
  31. private final UuidFactory uuidFactory;
  32. private final AuditPersister auditPersister;
  33. public UserPropertiesDao(System2 system2, UuidFactory uuidFactory, AuditPersister auditPersister) {
  34. this.system2 = system2;
  35. this.uuidFactory = uuidFactory;
  36. this.auditPersister = auditPersister;
  37. }
  38. public List<UserPropertyDto> selectByUser(DbSession session, UserDto user) {
  39. return mapper(session).selectByUserUuid(user.getUuid());
  40. }
  41. public UserPropertyDto insertOrUpdate(DbSession session, UserPropertyDto dto, @Nullable String login) {
  42. long now = system2.now();
  43. boolean isUpdate = true;
  44. if (mapper(session).update(dto, now) == 0) {
  45. mapper(session).insert(dto.setUuid(uuidFactory.create()), now);
  46. isUpdate = false;
  47. }
  48. if (isUpdate) {
  49. auditPersister.updateProperty(session, new PropertyNewValue(dto, login), true);
  50. } else {
  51. auditPersister.addProperty(session, new PropertyNewValue(dto, login), true);
  52. }
  53. return dto;
  54. }
  55. public void deleteByUser(DbSession session, UserDto user) {
  56. List<UserPropertyDto> userProperties = selectByUser(session, user);
  57. int deletedRows = mapper(session).deleteByUserUuid(user.getUuid());
  58. if (deletedRows > 0) {
  59. userProperties.stream()
  60. .filter(p -> auditPersister.isTrackedProperty(p.getKey()))
  61. .forEach(p -> auditPersister.deleteProperty(session, new PropertyNewValue(p, user.getLogin()), true));
  62. }
  63. }
  64. private static UserPropertiesMapper mapper(DbSession session) {
  65. return session.getMapper(UserPropertiesMapper.class);
  66. }
  67. }