UserProfileDao.java

  1. /*
  2.  * Copyright (C) 2020-2024 by Savoir-faire Linux
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  16.  */
  17. package net.jami.datastore.dao;

  18. import net.jami.jams.common.objects.user.UserProfile;

  19. import java.sql.SQLException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Optional;

  23. public class UserProfileDao extends AbstractDao<UserProfile> {

  24.     // Fix this to include the fields from AD/LDAP.
  25.     public UserProfileDao() {
  26.         this.setTableName("local_directory");
  27.         this.setTClass(UserProfile.class);
  28.     }

  29.     public void insertIfNotExists(UserProfile userProfile) throws SQLException {
  30.         String update =
  31.                 "UPDATE local_directory SET username = ?, firstName = ?, lastName = ?, email = ?, profilePicture = ?, organization=?, phoneNumber=?, phoneNumberExtension=?, faxNumber=?, mobileNumber=?"
  32.                         + "WHERE username = ?";

  33.         String insert =
  34.                 "INSERT INTO local_directory (username, firstName, lastName, email, profilePicture, organization, phoneNumber, phoneNumberExtension, faxNumber, mobileNumber) VALUES "
  35.                         + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

  36.         List<String> params = new ArrayList<>();
  37.         params.add(userProfile.getUsername());
  38.         params.add(userProfile.getFirstName());
  39.         params.add(userProfile.getLastName());
  40.         params.add(userProfile.getEmail());
  41.         params.add(userProfile.getProfilePicture());
  42.         params.add(userProfile.getOrganization());
  43.         params.add(userProfile.getPhoneNumber());
  44.         params.add(userProfile.getPhoneNumberExtension());
  45.         params.add(userProfile.getFaxNumber());
  46.         params.add(userProfile.getMobileNumber());
  47.         params.add(userProfile.getUsername());

  48.         // Attempt to update the user profile first
  49.         boolean updateSuccess = this.executeUpdate(update, params);
  50.         if (!updateSuccess) {
  51.             // Remove the last parameter from the list since it is not needed for the insert
  52.             // statement
  53.             if (!params.isEmpty()) {
  54.                 params.remove(params.size() - 1);
  55.             }
  56.             // If the update fails, insert the user profile
  57.             this.executeUpdate(insert, params);
  58.         }
  59.     }

  60.     public List<UserProfile> getAllUserProfile() {
  61.         return getObjectsFromResultSet("SELECT * FROM local_directory");
  62.     }

  63.     public List<UserProfile> searchUsername(String username) {
  64.         String query = "SELECT * FROM local_directory WHERE username LIKE ?";
  65.         return getObjectsFromResultSet(query, username + "%");
  66.     }

  67.     public List<UserProfile> searchFullName(String name) {
  68.         String query = "SELECT * FROM local_directory WHERE firstName LIKE ? OR lastName LIKE ?";
  69.         name = name + "%";
  70.         return getObjectsFromResultSet(query, List.of(name, name));
  71.     }

  72.     public Optional<UserProfile> getByUsername(String username) {
  73.         return getFirstObjectFromResultSet(
  74.                 "SELECT * FROM local_directory WHERE username = ?", username);
  75.     }

  76.     @Override
  77.     public boolean storeObject(UserProfile object) {
  78.         String query =
  79.                 "INSERT INTO local_directory "
  80.                         + "(firstName, lastName, email, profilePicture, organization, phoneNumber, phoneNumberExtension, faxNumber, mobileNumber, username)"
  81.                         + " VALUES "
  82.                         + "(?,?,?,?,?,?,?,?,?,?)";
  83.         return executeInsert(query, object);
  84.     }

  85.     public boolean updateUserProfile(UserProfile userProfile) {
  86.         String query =
  87.                 "UPDATE local_directory SET"
  88.                         + " firstname = ?,"
  89.                         + " lastName = ?,"
  90.                         + " email = ?,"
  91.                         + " profilePicture = ?,"
  92.                         + " organization = ?,"
  93.                         + " phoneNumber = ?,"
  94.                         + " phoneNumberExtension = ?,"
  95.                         + " faxNumber = ?,"
  96.                         + " mobileNumber = ?"
  97.                         + " WHERE username = ?";
  98.         return executeInsert(query, userProfile);
  99.     }

  100.     public boolean deleteUserProfile(String username) {
  101.         String query = "DELETE FROM local_directory WHERE username = ?";
  102.         List<String> params = new ArrayList<>();
  103.         params.add(username);
  104.         return this.executeUpdate(query, params);
  105.     }
  106. }