/**
  * This method executes adds new user query on the azure SQL user table.
  *
  * @return userEntity
  */
 public UserEntity saveNewUser(UserEntity user) throws Exception {
   LOGGER.info("[UserDAOImpl][saveNewUser] start ");
   int userId = 0;
   Connection connection = null;
   PreparedStatement preparedStatement = null;
   ResultSet resultSet = null;
   try {
     connection = AzureChatUtils.getConnection(AzureChatUtils.buildConnectionString());
     preparedStatement =
         connection.prepareStatement(
             AzureChatSQLConstants.SAVE_NEW_USER, Statement.RETURN_GENERATED_KEYS);
     preparedStatement = generatePreparedStatement(preparedStatement, user);
     preparedStatement.executeUpdate();
     resultSet = preparedStatement.getGeneratedKeys();
     if (resultSet.next()) {
       userId = resultSet.getInt(1);
     }
   } catch (Exception e) {
     LOGGER.error(
         "Exception occurred while executing save user query on azure SQL table. Exception Message : "
             + e.getMessage());
     throw new AzureChatSystemException(
         "Exception occurred while executing save user query on azure SQL table. Exception Message : "
             + e.getMessage());
   } finally {
     AzureChatUtils.closeDatabaseResources(preparedStatement, resultSet, connection);
   }
   user.setUserID(userId);
   LOGGER.info("[UserDAOImpl][saveNewUser] end ");
   return user;
 }
 /**
  * This method executes the update user details query in the azure SQL database for the input user
  * entity.
  */
 @Override
 public UserEntity updateNewUser(UserEntity user) throws Exception {
   LOGGER.info("[UserDAOImpl][updateNewUser] start ");
   Connection connection = null;
   PreparedStatement preparedStatement = null;
   ResultSet resultSet = null;
   try {
     connection = AzureChatUtils.getConnection(AzureChatUtils.buildConnectionString());
     preparedStatement = connection.prepareStatement(AzureChatSQLConstants.UPDATE_NEW_USER);
     preparedStatement.setString(1, user.getFirstName());
     preparedStatement.setString(2, user.getLastName());
     preparedStatement.setString(3, user.getPhotoBlobUrl());
     preparedStatement.setString(4, user.getEmailAddress());
     preparedStatement.setInt(5, user.getPhoneCountryCode());
     preparedStatement.setLong(6, user.getPhoneNumber());
     preparedStatement.setString(7, user.getNameId());
     preparedStatement.executeUpdate();
   } catch (Exception e) {
     LOGGER.error(
         "Exception occurred while executing update user query on the azure SQL table. Exception Message : "
             + e.getMessage());
     throw new AzureChatSystemException(
         "Exception occurred while executing update user query on the azure SQL table. Exception Message : "
             + e.getMessage());
   } finally {
     AzureChatUtils.closeDatabaseResources(preparedStatement, resultSet, connection);
   }
   LOGGER.info("[UserDAOImpl][updateNewUser] end ");
   return user;
 }
 /**
  * This method populates userEntity from the result set object.
  *
  * @param resultSet
  * @return
  * @throws SQLException
  */
 public UserEntity generateUserObject(ResultSet resultSet) throws SQLException {
   UserEntity user = new UserEntity();
   user.setUserID(resultSet.getInt(1));
   user.setNameId(resultSet.getString(2));
   user.setIdentityProvider(resultSet.getString(3));
   user.setFirstName(resultSet.getString(4));
   user.setLastName(resultSet.getString(5));
   user.setPhotoBlobUrl(resultSet.getString(6));
   user.setEmailAddress(resultSet.getString(7));
   user.setPhoneCountryCode(resultSet.getInt(8));
   user.setPhoneNumber(resultSet.getLong(9));
   user.setDateCreated(resultSet.getDate(10));
   user.setCreatedBy(resultSet.getDate(11));
   user.setDateModified(resultSet.getDate(12));
   user.setModifiedBy(resultSet.getDate(13));
   return user;
 }
 /**
  * This method generates the prepare statement from userEntity object.
  *
  * @param preparedStatement
  * @param user
  * @return
  * @throws SQLException
  */
 public PreparedStatement generatePreparedStatement(
     PreparedStatement preparedStatement, UserEntity user) throws SQLException {
   preparedStatement.setString(1, user.getNameId());
   preparedStatement.setString(2, user.getIdentityProvider());
   preparedStatement.setString(3, user.getFirstName());
   preparedStatement.setString(4, user.getLastName());
   preparedStatement.setString(5, user.getPhotoBlobUrl());
   preparedStatement.setString(6, user.getEmailAddress());
   preparedStatement.setInt(7, user.getPhoneCountryCode());
   preparedStatement.setLong(8, user.getPhoneNumber());
   preparedStatement.setDate(9, new java.sql.Date(user.getDateCreated().getTime()));
   preparedStatement.setDate(10, new java.sql.Date(user.getCreatedBy().getTime()));
   preparedStatement.setDate(11, new java.sql.Date(user.getDateModified().getTime()));
   preparedStatement.setDate(12, new java.sql.Date(user.getModifiedBy().getTime()));
   return preparedStatement;
 }