From 219203f3d4854c707eca884bbef673a286f2034c Mon Sep 17 00:00:00 2001 From: Sizolwakhe Leonard Mthimunye Date: Tue, 9 Sep 2025 18:54:34 +0200 Subject: [PATCH 1/4] removing dto counting api, not needed --- .../countApiUsers/CountApiUsersRequest.java | 22 ------------------- .../util/mappers/CountApiUsersMapper.java | 12 ---------- 2 files changed, 34 deletions(-) delete mode 100644 src/main/java/org/airpenthouse/GoTel/dtos/countApiUsers/CountApiUsersRequest.java delete mode 100644 src/main/java/org/airpenthouse/GoTel/util/mappers/CountApiUsersMapper.java diff --git a/src/main/java/org/airpenthouse/GoTel/dtos/countApiUsers/CountApiUsersRequest.java b/src/main/java/org/airpenthouse/GoTel/dtos/countApiUsers/CountApiUsersRequest.java deleted file mode 100644 index 0a452e7..0000000 --- a/src/main/java/org/airpenthouse/GoTel/dtos/countApiUsers/CountApiUsersRequest.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.airpenthouse.GoTel.dtos.countApiUsers; - -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import java.time.LocalDateTime; - -@AllArgsConstructor -@Getter -public class CountApiUsersRequest { - @JsonProperty("_world_language_api_users_count") - private Integer countWorldLanguagesUsers; - @JsonProperty("_world_countries_api_users_count") - private Integer countWorldCountriesUsers; - @JsonProperty("_world_cities_api_users_count") - private Integer countWorldCitiesUsersUsers; - @JsonIgnore - @JsonProperty("_last_modified_date") - private LocalDateTime modifiedDate; -} diff --git a/src/main/java/org/airpenthouse/GoTel/util/mappers/CountApiUsersMapper.java b/src/main/java/org/airpenthouse/GoTel/util/mappers/CountApiUsersMapper.java deleted file mode 100644 index 721073c..0000000 --- a/src/main/java/org/airpenthouse/GoTel/util/mappers/CountApiUsersMapper.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.airpenthouse.GoTel.util.mappers; - -import org.airpenthouse.GoTel.dtos.countApiUsers.CountApiUsersRequest; -import org.airpenthouse.GoTel.util.CountApiUsers; -import org.mapstruct.Mapper; -import org.springframework.stereotype.Component; - -@Component -@Mapper(componentModel = "spring") -public interface CountApiUsersMapper { - CountApiUsersRequest toDto(CountApiUsers request); -} From df9679e4abe840fc3cc10c58538d108159a833de Mon Sep 17 00:00:00 2001 From: Sizolwakhe Leonard Mthimunye Date: Tue, 9 Sep 2025 18:55:54 +0200 Subject: [PATCH 2/4] changing from integer to atomic and remove locks amending the pointed queries making protected method to public --- .../GoTel/util/CountApiUsers.java | 74 ++++++++----------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/airpenthouse/GoTel/util/CountApiUsers.java b/src/main/java/org/airpenthouse/GoTel/util/CountApiUsers.java index 2c7ef93..6f69f11 100644 --- a/src/main/java/org/airpenthouse/GoTel/util/CountApiUsers.java +++ b/src/main/java/org/airpenthouse/GoTel/util/CountApiUsers.java @@ -3,7 +3,6 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.ToString; -import org.springframework.stereotype.Component; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -12,56 +11,52 @@ import java.time.format.DateTimeFormatter; import java.util.List; import java.util.concurrent.*; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.atomic.AtomicInteger; @ToString @AllArgsConstructor public class CountApiUsers extends CommonEntityMethod { - private static final CountApiUsers instance = new CountApiUsers(); @Getter - private Integer countWorldLanguagesUsers, countWorldCountriesUsers, countWorldCitiesUsersUsers; + private AtomicInteger countWorldLanguagesUsers = new AtomicInteger() + ,countWorldCountriesUsers = new AtomicInteger() + ,countWorldCitiesUsersUsers = new AtomicInteger(); private String getCountsByMembershipQuery, getCountsQuery, upgradeWorldLanguageCountQuery, upgradeWorldCountriesCountQuery, upgradeWorldCitiesCountQuery; private PreparedStatement st, updatePreparedStatement; private ExecutorService executorService; - private Lock lock; + @Getter private LocalDateTime modifiedDate; private DateTimeFormatter format; - protected CountApiUsers() { + public CountApiUsers() { super(); - lock = new ReentrantLock(); + this.format = DateTimeFormatter.ofPattern("dd/MMM/yyyy hh:mm"); executorService = Executors.newSingleThreadExecutor(); getCountsByMembershipQuery = PropertiesUtilManager.getPropertiesValue("jdbc.query.getAllCountByPrivilegeName"); getCountsQuery = PropertiesUtilManager.getPropertiesValue("jdbc.query.getAllCounts"); upgradeWorldLanguageCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateLanguageCount"); - upgradeWorldCountriesCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateCitiesCount"); - upgradeWorldCitiesCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateCountriesCount"); + upgradeWorldCitiesCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateCitiesCount"); + upgradeWorldCountriesCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateCountriesCount"); } private CountApiUsers(Integer countWorldLanguagesUsers, Integer countWorldCountriesUsers, Integer countWorldCitiesUsersUsers) { - this.countWorldLanguagesUsers = countWorldLanguagesUsers; - this.countWorldCountriesUsers = (countWorldCountriesUsers); - this.countWorldCitiesUsersUsers = (countWorldCitiesUsersUsers); - } - - public static CountApiUsers getInstance() { - return instance; + this.countWorldLanguagesUsers.set(countWorldLanguagesUsers); + this.countWorldCountriesUsers.set(countWorldCountriesUsers); + this.countWorldCitiesUsersUsers.set(countWorldCitiesUsersUsers); } protected void updateWorldLanguageCountForMembers() { try { - lock.lock(); + modifiedDate = LocalDateTime.now(); updatePreparedStatement = databaseConfig(upgradeWorldLanguageCountQuery); updatePreparedStatement.setString(3, Privileges.MEMBERSHIP.getMembershipName()); var list = getCountiesByNoMembership(); if (list.size() == 1) { - updatePreparedStatement.setInt(1, list.get(0).countWorldLanguagesUsers++); + updatePreparedStatement.setInt(1, list.get(0).countWorldLanguagesUsers.incrementAndGet()); updatePreparedStatement.setString(2, modifiedDate.format(format)); updatePreparedStatement.executeUpdate(); } else { @@ -70,21 +65,19 @@ protected void updateWorldLanguageCountForMembers() { } catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) { throw new RuntimeException(e); - } finally { - lock.unlock(); } } protected void updateWorldCitiesCountForMembers() { try { - lock.lock(); + modifiedDate = LocalDateTime.now(); updatePreparedStatement = databaseConfig(upgradeWorldCitiesCountQuery); updatePreparedStatement.setString(3, Privileges.MEMBERSHIP.getMembershipName()); var list = getCountiesByNoMembership(); if (list.size() == 1) { - updatePreparedStatement.setInt(1, list.get(0).countWorldCitiesUsersUsers++); + updatePreparedStatement.setInt(1, list.get(0).countWorldCitiesUsersUsers.incrementAndGet()); updatePreparedStatement.setString(2, modifiedDate.format(format)); updatePreparedStatement.executeUpdate(); } else { @@ -93,20 +86,18 @@ protected void updateWorldCitiesCountForMembers() { } catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) { throw new RuntimeException(e); - } finally { - lock.unlock(); } } protected void updateWorldCountriesCountForMembers() { try { - lock.lock(); + modifiedDate = LocalDateTime.now(); updatePreparedStatement = databaseConfig(upgradeWorldCountriesCountQuery); updatePreparedStatement.setString(3, Privileges.MEMBERSHIP.getMembershipName()); var list = getCountiesByNoMembership(); if (list.size() == 1) { - updatePreparedStatement.setInt(1, list.get(0).countWorldCountriesUsers++); + updatePreparedStatement.setInt(1, list.get(0).countWorldCountriesUsers.incrementAndGet()); updatePreparedStatement.setString(2, modifiedDate.format(format)); updatePreparedStatement.executeUpdate(); } else { @@ -115,21 +106,19 @@ protected void updateWorldCountriesCountForMembers() { } catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) { throw new RuntimeException(e); - } finally { - lock.unlock(); } } protected void updateWorldLanguageCount() { try { - lock.lock(); + modifiedDate = LocalDateTime.now(); updatePreparedStatement = databaseConfig(upgradeWorldLanguageCountQuery); updatePreparedStatement.setString(3, Privileges.NO_MEMBERSHIP.getMembershipName()); var list = getCountiesByNoMembership(); if (list.size() == 1) { - updatePreparedStatement.setInt(1, list.get(0).countWorldLanguagesUsers++); + updatePreparedStatement.setInt(1, list.get(0).countWorldLanguagesUsers.incrementAndGet()); updatePreparedStatement.setString(2, modifiedDate.format(format)); updatePreparedStatement.executeUpdate(); } else { @@ -138,21 +127,19 @@ protected void updateWorldLanguageCount() { } catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) { throw new RuntimeException(e); - } finally { - lock.unlock(); } } protected void updateWorldCitiesCount() { try { - lock.lock(); + modifiedDate = LocalDateTime.now(); updatePreparedStatement = databaseConfig(upgradeWorldCitiesCountQuery); updatePreparedStatement.setString(3, Privileges.NO_MEMBERSHIP.getMembershipName()); var list = getCountiesByNoMembership(); if (list.size() == 1) { - updatePreparedStatement.setInt(1, list.get(0).countWorldCitiesUsersUsers++); + updatePreparedStatement.setInt(1, list.get(0).countWorldCitiesUsersUsers.incrementAndGet()); updatePreparedStatement.setString(2, modifiedDate.format(format)); updatePreparedStatement.executeUpdate(); } else { @@ -161,21 +148,19 @@ protected void updateWorldCitiesCount() { } catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) { throw new RuntimeException(e); - } finally { - lock.unlock(); } } - protected void updateWorldCountriesCount() { + public void updateWorldCountriesCount() { try { - lock.lock(); + modifiedDate = LocalDateTime.now(); updatePreparedStatement = databaseConfig(upgradeWorldCountriesCountQuery); updatePreparedStatement.setString(3, Privileges.NO_MEMBERSHIP.getMembershipName()); var list = getCountiesByNoMembership(); if (list.size() == 1) { - var no = list.get(0).countWorldCountriesUsers++; - updatePreparedStatement.setInt(1, no); + + updatePreparedStatement.setInt(1, list.get(0).countWorldCountriesUsers.incrementAndGet()); updatePreparedStatement.setString(2, modifiedDate.format(format)); updatePreparedStatement.executeUpdate(); } else { @@ -184,14 +169,12 @@ protected void updateWorldCountriesCount() { } catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) { throw new RuntimeException(e); - } finally { - lock.unlock(); } } public List prepareToGetAllCounties() { - Future> future = executorService.submit(instance::getAllCounties); + Future> future = executorService.submit(this::getAllCounties); try { return future.get(); } catch (InterruptedException | ExecutionException e) { @@ -224,6 +207,9 @@ private List addDataFromDBToList() throws SQLException { ResultSet set = st.executeQuery(); while (set.next()) { + Log.info(" checking1: " + set.getInt(6)); + Log.info(" checking2: " + set.getInt(4)); + Log.info(" checking: " + set.getInt(5)); list.add(new CountApiUsers(set.getInt(4), set.getInt(6), set.getInt(5))); } set.close(); From 1ee06510e5d2847465d566bf14905aeed7bfaef7 Mon Sep 17 00:00:00 2001 From: Sizolwakhe Leonard Mthimunye Date: Tue, 9 Sep 2025 18:56:25 +0200 Subject: [PATCH 3/4] removing the use of dto in controller --- .../GoTel/controllers/CountApiUsersController.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/airpenthouse/GoTel/controllers/CountApiUsersController.java b/src/main/java/org/airpenthouse/GoTel/controllers/CountApiUsersController.java index 5e2b223..512ea80 100644 --- a/src/main/java/org/airpenthouse/GoTel/controllers/CountApiUsersController.java +++ b/src/main/java/org/airpenthouse/GoTel/controllers/CountApiUsersController.java @@ -1,9 +1,6 @@ package org.airpenthouse.GoTel.controllers; -import org.airpenthouse.GoTel.dtos.countApiUsers.CountApiUsersRequest; import org.airpenthouse.GoTel.util.CountApiUsers; -import org.airpenthouse.GoTel.util.mappers.CountApiUsersMapper; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; @@ -17,13 +14,10 @@ @CrossOrigin(origins="http://localhost:4200") public class CountApiUsersController extends CountApiUsers { - @Autowired - private CountApiUsersMapper mapper; - @GetMapping("/") - public ResponseEntity> getAllCounts() { - var entities = prepareToGetAllCounties().stream().map(mapper::toDto).toList(); + public ResponseEntity> getAllCounts() { + var entities = prepareToGetAllCounties(); if (entities.isEmpty()) { return ResponseEntity.notFound().build(); } else { From 3cd1fb16cacdda3ea3668d81943581a44b49e055 Mon Sep 17 00:00:00 2001 From: Sizolwakhe Leonard Mthimunye Date: Tue, 9 Sep 2025 18:57:13 +0200 Subject: [PATCH 4/4] removing the count countries to service class --- .../airpenthouse/GoTel/services/country/CountriesService.java | 4 ++++ .../airpenthouse/GoTel/util/executors/CountriesExecutors.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/airpenthouse/GoTel/services/country/CountriesService.java b/src/main/java/org/airpenthouse/GoTel/services/country/CountriesService.java index 3223528..9f7315d 100644 --- a/src/main/java/org/airpenthouse/GoTel/services/country/CountriesService.java +++ b/src/main/java/org/airpenthouse/GoTel/services/country/CountriesService.java @@ -1,6 +1,7 @@ package org.airpenthouse.GoTel.services.country; import org.airpenthouse.GoTel.dtos.countries.MembershipCountriesRequest; +import org.airpenthouse.GoTel.util.CountApiUsers; import org.airpenthouse.GoTel.util.dto.binder.CountriesRequestCombiner; import org.airpenthouse.GoTel.util.executors.CountriesExecutors; import org.airpenthouse.GoTel.util.mappers.CountriesMapper; @@ -25,6 +26,9 @@ private Set getAllCountries() { } private Set getRequest() { + var count = new CountApiUsers(); + + count.updateWorldCountriesCount(); if (checkMemberShipStatusAndTokenMatch()) return initializeCountriesEntity().stream().map(mapper::mapToMembershipCountryRequest).collect(Collectors.toSet()); else diff --git a/src/main/java/org/airpenthouse/GoTel/util/executors/CountriesExecutors.java b/src/main/java/org/airpenthouse/GoTel/util/executors/CountriesExecutors.java index ec6eeda..e2e9948 100644 --- a/src/main/java/org/airpenthouse/GoTel/util/executors/CountriesExecutors.java +++ b/src/main/java/org/airpenthouse/GoTel/util/executors/CountriesExecutors.java @@ -41,7 +41,7 @@ private ExecutorService determineNoThreadToUse() { private Set executeCountriesEntity() { try { var set = this.implCountriesEntityExecution(); - super.updateWorldCountriesCount(); + return set; } catch (ExecutionException | TimeoutException | InterruptedException | NullPointerException e) { throw new RuntimeException("Error occurred :" + e.getMessage());