Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,13 +14,10 @@
@CrossOrigin(origins="http://localhost:4200")
public class CountApiUsersController extends CountApiUsers {

@Autowired
private CountApiUsersMapper mapper;


@GetMapping("/")
public ResponseEntity<List<CountApiUsersRequest>> getAllCounts() {
var entities = prepareToGetAllCounties().stream().map(mapper::toDto).toList();
public ResponseEntity<List<CountApiUsers>> getAllCounts() {
var entities = prepareToGetAllCounties();
if (entities.isEmpty()) {
return ResponseEntity.notFound().build();
} else {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,6 +26,9 @@ private Set<? extends CountriesRequestCombiner> getAllCountries() {
}

private Set<? extends CountriesRequestCombiner> getRequest() {
var count = new CountApiUsers();

count.updateWorldCountriesCount();
if (checkMemberShipStatusAndTokenMatch())
return initializeCountriesEntity().stream().map(mapper::mapToMembershipCountryRequest).collect(Collectors.toSet());
else
Expand Down
74 changes: 30 additions & 44 deletions src/main/java/org/airpenthouse/GoTel/util/CountApiUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -184,14 +169,12 @@ protected void updateWorldCountriesCount() {

} catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
}


public List<CountApiUsers> prepareToGetAllCounties() {
Future<List<CountApiUsers>> future = executorService.submit(instance::getAllCounties);
Future<List<CountApiUsers>> future = executorService.submit(this::getAllCounties);
try {
return future.get();
} catch (InterruptedException | ExecutionException e) {
Expand Down Expand Up @@ -224,6 +207,9 @@ private List<CountApiUsers> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private ExecutorService determineNoThreadToUse() {
private Set<CountriesEntity> executeCountriesEntity() {
try {
var set = this.implCountriesEntityExecution();
super.updateWorldCountriesCount();

return set;
} catch (ExecutionException | TimeoutException | InterruptedException | NullPointerException e) {
throw new RuntimeException("Error occurred :" + e.getMessage());
Expand Down

This file was deleted.

Loading