Skip to content

Commit a0bb4bc

Browse files
Merge pull request #26 from leonard1thecoder/21-count-api-for-country-doesnt-count
21 count api for country doesnt count
2 parents cfd6399 + 3cd1fb1 commit a0bb4bc

File tree

6 files changed

+37
-87
lines changed

6 files changed

+37
-87
lines changed

src/main/java/org/airpenthouse/GoTel/controllers/CountApiUsersController.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package org.airpenthouse.GoTel.controllers;
22

3-
import org.airpenthouse.GoTel.dtos.countApiUsers.CountApiUsersRequest;
43
import org.airpenthouse.GoTel.util.CountApiUsers;
5-
import org.airpenthouse.GoTel.util.mappers.CountApiUsersMapper;
6-
import org.springframework.beans.factory.annotation.Autowired;
74
import org.springframework.http.ResponseEntity;
85
import org.springframework.web.bind.annotation.CrossOrigin;
96
import org.springframework.web.bind.annotation.GetMapping;
@@ -17,13 +14,10 @@
1714
@CrossOrigin(origins="http://localhost:4200")
1815
public class CountApiUsersController extends CountApiUsers {
1916

20-
@Autowired
21-
private CountApiUsersMapper mapper;
22-
2317

2418
@GetMapping("/")
25-
public ResponseEntity<List<CountApiUsersRequest>> getAllCounts() {
26-
var entities = prepareToGetAllCounties().stream().map(mapper::toDto).toList();
19+
public ResponseEntity<List<CountApiUsers>> getAllCounts() {
20+
var entities = prepareToGetAllCounties();
2721
if (entities.isEmpty()) {
2822
return ResponseEntity.notFound().build();
2923
} else {

src/main/java/org/airpenthouse/GoTel/dtos/countApiUsers/CountApiUsersRequest.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/main/java/org/airpenthouse/GoTel/services/country/CountriesService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.airpenthouse.GoTel.services.country;
22

33
import org.airpenthouse.GoTel.dtos.countries.MembershipCountriesRequest;
4+
import org.airpenthouse.GoTel.util.CountApiUsers;
45
import org.airpenthouse.GoTel.util.dto.binder.CountriesRequestCombiner;
56
import org.airpenthouse.GoTel.util.executors.CountriesExecutors;
67
import org.airpenthouse.GoTel.util.mappers.CountriesMapper;
@@ -25,6 +26,9 @@ private Set<? extends CountriesRequestCombiner> getAllCountries() {
2526
}
2627

2728
private Set<? extends CountriesRequestCombiner> getRequest() {
29+
var count = new CountApiUsers();
30+
31+
count.updateWorldCountriesCount();
2832
if (checkMemberShipStatusAndTokenMatch())
2933
return initializeCountriesEntity().stream().map(mapper::mapToMembershipCountryRequest).collect(Collectors.toSet());
3034
else

src/main/java/org/airpenthouse/GoTel/util/CountApiUsers.java

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
55
import lombok.ToString;
6-
import org.springframework.stereotype.Component;
76

87
import java.sql.PreparedStatement;
98
import java.sql.ResultSet;
@@ -12,56 +11,52 @@
1211
import java.time.format.DateTimeFormatter;
1312
import java.util.List;
1413
import java.util.concurrent.*;
15-
import java.util.concurrent.locks.Lock;
16-
import java.util.concurrent.locks.ReentrantLock;
14+
import java.util.concurrent.atomic.AtomicInteger;
1715

1816
@ToString
1917
@AllArgsConstructor
2018
public class CountApiUsers extends CommonEntityMethod {
2119

22-
private static final CountApiUsers instance = new CountApiUsers();
2320
@Getter
24-
private Integer countWorldLanguagesUsers, countWorldCountriesUsers, countWorldCitiesUsersUsers;
21+
private AtomicInteger countWorldLanguagesUsers = new AtomicInteger()
22+
,countWorldCountriesUsers = new AtomicInteger()
23+
,countWorldCitiesUsersUsers = new AtomicInteger();
2524
private String getCountsByMembershipQuery, getCountsQuery, upgradeWorldLanguageCountQuery, upgradeWorldCountriesCountQuery, upgradeWorldCitiesCountQuery;
2625
private PreparedStatement st, updatePreparedStatement;
2726
private ExecutorService executorService;
28-
private Lock lock;
27+
2928
@Getter
3029
private LocalDateTime modifiedDate;
3130
private DateTimeFormatter format;
3231

33-
protected CountApiUsers() {
32+
public CountApiUsers() {
3433
super();
35-
lock = new ReentrantLock();
34+
3635
this.format = DateTimeFormatter.ofPattern("dd/MMM/yyyy hh:mm");
3736
executorService = Executors.newSingleThreadExecutor();
3837
getCountsByMembershipQuery = PropertiesUtilManager.getPropertiesValue("jdbc.query.getAllCountByPrivilegeName");
3938
getCountsQuery = PropertiesUtilManager.getPropertiesValue("jdbc.query.getAllCounts");
4039
upgradeWorldLanguageCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateLanguageCount");
41-
upgradeWorldCountriesCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateCitiesCount");
42-
upgradeWorldCitiesCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateCountriesCount");
40+
upgradeWorldCitiesCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateCitiesCount");
41+
upgradeWorldCountriesCountQuery = PropertiesUtilManager.getPropertiesValue("jdbc.update.updateCountriesCount");
4342
}
4443

4544
private CountApiUsers(Integer countWorldLanguagesUsers, Integer countWorldCountriesUsers, Integer countWorldCitiesUsersUsers) {
46-
this.countWorldLanguagesUsers = countWorldLanguagesUsers;
47-
this.countWorldCountriesUsers = (countWorldCountriesUsers);
48-
this.countWorldCitiesUsersUsers = (countWorldCitiesUsersUsers);
49-
}
50-
51-
public static CountApiUsers getInstance() {
52-
return instance;
45+
this.countWorldLanguagesUsers.set(countWorldLanguagesUsers);
46+
this.countWorldCountriesUsers.set(countWorldCountriesUsers);
47+
this.countWorldCitiesUsersUsers.set(countWorldCitiesUsersUsers);
5348
}
5449

5550
protected void updateWorldLanguageCountForMembers() {
5651
try {
57-
lock.lock();
52+
5853
modifiedDate = LocalDateTime.now();
5954
updatePreparedStatement = databaseConfig(upgradeWorldLanguageCountQuery);
6055
updatePreparedStatement.setString(3, Privileges.MEMBERSHIP.getMembershipName());
6156
var list = getCountiesByNoMembership();
6257
if (list.size() == 1) {
6358

64-
updatePreparedStatement.setInt(1, list.get(0).countWorldLanguagesUsers++);
59+
updatePreparedStatement.setInt(1, list.get(0).countWorldLanguagesUsers.incrementAndGet());
6560
updatePreparedStatement.setString(2, modifiedDate.format(format));
6661
updatePreparedStatement.executeUpdate();
6762
} else {
@@ -70,21 +65,19 @@ protected void updateWorldLanguageCountForMembers() {
7065

7166
} catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) {
7267
throw new RuntimeException(e);
73-
} finally {
74-
lock.unlock();
7568
}
7669
}
7770

7871
protected void updateWorldCitiesCountForMembers() {
7972
try {
80-
lock.lock();
73+
8174
modifiedDate = LocalDateTime.now();
8275
updatePreparedStatement = databaseConfig(upgradeWorldCitiesCountQuery);
8376
updatePreparedStatement.setString(3, Privileges.MEMBERSHIP.getMembershipName());
8477
var list = getCountiesByNoMembership();
8578
if (list.size() == 1) {
8679

87-
updatePreparedStatement.setInt(1, list.get(0).countWorldCitiesUsersUsers++);
80+
updatePreparedStatement.setInt(1, list.get(0).countWorldCitiesUsersUsers.incrementAndGet());
8881
updatePreparedStatement.setString(2, modifiedDate.format(format));
8982
updatePreparedStatement.executeUpdate();
9083
} else {
@@ -93,20 +86,18 @@ protected void updateWorldCitiesCountForMembers() {
9386

9487
} catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) {
9588
throw new RuntimeException(e);
96-
} finally {
97-
lock.unlock();
9889
}
9990
}
10091

10192
protected void updateWorldCountriesCountForMembers() {
10293
try {
103-
lock.lock();
94+
10495
modifiedDate = LocalDateTime.now();
10596
updatePreparedStatement = databaseConfig(upgradeWorldCountriesCountQuery);
10697
updatePreparedStatement.setString(3, Privileges.MEMBERSHIP.getMembershipName());
10798
var list = getCountiesByNoMembership();
10899
if (list.size() == 1) {
109-
updatePreparedStatement.setInt(1, list.get(0).countWorldCountriesUsers++);
100+
updatePreparedStatement.setInt(1, list.get(0).countWorldCountriesUsers.incrementAndGet());
110101
updatePreparedStatement.setString(2, modifiedDate.format(format));
111102
updatePreparedStatement.executeUpdate();
112103
} else {
@@ -115,21 +106,19 @@ protected void updateWorldCountriesCountForMembers() {
115106

116107
} catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) {
117108
throw new RuntimeException(e);
118-
} finally {
119-
lock.unlock();
120109
}
121110
}
122111

123112
protected void updateWorldLanguageCount() {
124113
try {
125-
lock.lock();
114+
126115
modifiedDate = LocalDateTime.now();
127116
updatePreparedStatement = databaseConfig(upgradeWorldLanguageCountQuery);
128117
updatePreparedStatement.setString(3, Privileges.NO_MEMBERSHIP.getMembershipName());
129118
var list = getCountiesByNoMembership();
130119
if (list.size() == 1) {
131120

132-
updatePreparedStatement.setInt(1, list.get(0).countWorldLanguagesUsers++);
121+
updatePreparedStatement.setInt(1, list.get(0).countWorldLanguagesUsers.incrementAndGet());
133122
updatePreparedStatement.setString(2, modifiedDate.format(format));
134123
updatePreparedStatement.executeUpdate();
135124
} else {
@@ -138,21 +127,19 @@ protected void updateWorldLanguageCount() {
138127

139128
} catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) {
140129
throw new RuntimeException(e);
141-
} finally {
142-
lock.unlock();
143130
}
144131
}
145132

146133
protected void updateWorldCitiesCount() {
147134
try {
148-
lock.lock();
135+
149136
modifiedDate = LocalDateTime.now();
150137
updatePreparedStatement = databaseConfig(upgradeWorldCitiesCountQuery);
151138
updatePreparedStatement.setString(3, Privileges.NO_MEMBERSHIP.getMembershipName());
152139
var list = getCountiesByNoMembership();
153140
if (list.size() == 1) {
154141

155-
updatePreparedStatement.setInt(1, list.get(0).countWorldCitiesUsersUsers++);
142+
updatePreparedStatement.setInt(1, list.get(0).countWorldCitiesUsersUsers.incrementAndGet());
156143
updatePreparedStatement.setString(2, modifiedDate.format(format));
157144
updatePreparedStatement.executeUpdate();
158145
} else {
@@ -161,21 +148,19 @@ protected void updateWorldCitiesCount() {
161148

162149
} catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) {
163150
throw new RuntimeException(e);
164-
} finally {
165-
lock.unlock();
166151
}
167152
}
168153

169-
protected void updateWorldCountriesCount() {
154+
public void updateWorldCountriesCount() {
170155
try {
171-
lock.lock();
156+
172157
modifiedDate = LocalDateTime.now();
173158
updatePreparedStatement = databaseConfig(upgradeWorldCountriesCountQuery);
174159
updatePreparedStatement.setString(3, Privileges.NO_MEMBERSHIP.getMembershipName());
175160
var list = getCountiesByNoMembership();
176161
if (list.size() == 1) {
177-
var no = list.get(0).countWorldCountriesUsers++;
178-
updatePreparedStatement.setInt(1, no);
162+
163+
updatePreparedStatement.setInt(1, list.get(0).countWorldCountriesUsers.incrementAndGet());
179164
updatePreparedStatement.setString(2, modifiedDate.format(format));
180165
updatePreparedStatement.executeUpdate();
181166
} else {
@@ -184,14 +169,12 @@ protected void updateWorldCountriesCount() {
184169

185170
} catch (ExecutionException | InterruptedException | TimeoutException | SQLException e) {
186171
throw new RuntimeException(e);
187-
} finally {
188-
lock.unlock();
189172
}
190173
}
191174

192175

193176
public List<CountApiUsers> prepareToGetAllCounties() {
194-
Future<List<CountApiUsers>> future = executorService.submit(instance::getAllCounties);
177+
Future<List<CountApiUsers>> future = executorService.submit(this::getAllCounties);
195178
try {
196179
return future.get();
197180
} catch (InterruptedException | ExecutionException e) {
@@ -224,6 +207,9 @@ private List<CountApiUsers> addDataFromDBToList() throws SQLException {
224207
ResultSet set = st.executeQuery();
225208

226209
while (set.next()) {
210+
Log.info(" checking1: " + set.getInt(6));
211+
Log.info(" checking2: " + set.getInt(4));
212+
Log.info(" checking: " + set.getInt(5));
227213
list.add(new CountApiUsers(set.getInt(4), set.getInt(6), set.getInt(5)));
228214
}
229215
set.close();

src/main/java/org/airpenthouse/GoTel/util/executors/CountriesExecutors.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private ExecutorService determineNoThreadToUse() {
4141
private Set<CountriesEntity> executeCountriesEntity() {
4242
try {
4343
var set = this.implCountriesEntityExecution();
44-
super.updateWorldCountriesCount();
44+
4545
return set;
4646
} catch (ExecutionException | TimeoutException | InterruptedException | NullPointerException e) {
4747
throw new RuntimeException("Error occurred :" + e.getMessage());

src/main/java/org/airpenthouse/GoTel/util/mappers/CountApiUsersMapper.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)