Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 01b85c2

Browse files
author
vlad-ciucescu
authored
* SORMAS-Foundation#2993 add CountryResource * SORMAS-Foundation#2993 fix App sync
1 parent 52318d0 commit 01b85c2

7 files changed

Lines changed: 202 additions & 13 deletions

File tree

sormas-api/src/main/java/de/symeda/sormas/api/region/CountryDto.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import de.symeda.sormas.api.EntityDto;
66
import de.symeda.sormas.api.utils.DataHelper;
77

8+
import java.util.Date;
9+
810
public class CountryDto extends EntityDto {
911

1012
private static final long serialVersionUID = 8309822957203823162L;
@@ -23,6 +25,27 @@ public class CountryDto extends EntityDto {
2325
private String unoCode;
2426
private boolean archived;
2527

28+
public CountryDto(Date creationDate,
29+
Date changeDate,
30+
String uuid,
31+
boolean archived,
32+
String defaultName,
33+
String externalId,
34+
String isoCode,
35+
String unoCode) {
36+
37+
super(creationDate, changeDate, uuid);
38+
this.archived = archived;
39+
this.defaultName = defaultName;
40+
this.externalId = externalId;
41+
this.isoCode = isoCode;
42+
this.unoCode = unoCode;
43+
}
44+
45+
public CountryDto() {
46+
super();
47+
}
48+
2649
public String getDefaultName() {
2750
return defaultName;
2851
}

sormas-api/src/main/java/de/symeda/sormas/api/region/CountryFacade.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.symeda.sormas.api.region;
22

3+
import java.util.Date;
34
import java.util.List;
45

56
import javax.ejb.Remote;
@@ -22,4 +23,10 @@ public interface CountryFacade {
2223
void archive(String countryUuid);
2324

2425
void dearchive(String countryUuid);
26+
27+
List<CountryDto> getAllAfter(Date date);
28+
29+
List<CountryDto> getByUuids(List<String> uuids);
30+
31+
List<String> getAllUuids();
2532
}

sormas-app/app/src/main/java/de/symeda/sormas/app/backend/common/DatabaseHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int
17301730

17311731
case 242:
17321732
currentVersion = 242;
1733-
TableUtils.createTable(connectionSource, Country.class);
1733+
TableUtils.createTableIfNotExists(connectionSource, Country.class);
17341734

17351735
case 243:
17361736
currentVersion = 243;

sormas-backend/src/main/java/de/symeda/sormas/backend/infrastructure/InfrastructureFacadeEjb.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import de.symeda.sormas.backend.infrastructure.PointOfEntryFacadeEjb.PointOfEntryFacadeEjbLocal;
1919
import de.symeda.sormas.backend.region.CommunityFacadeEjb.CommunityFacadeEjbLocal;
2020
import de.symeda.sormas.backend.region.CommunityService;
21+
import de.symeda.sormas.backend.region.CountryFacadeEjb.CountryFacadeEjbLocal;
2122
import de.symeda.sormas.backend.region.DistrictFacadeEjb.DistrictFacadeEjbLocal;
2223
import de.symeda.sormas.backend.region.RegionFacadeEjb.RegionFacadeEjbLocal;
2324
import de.symeda.sormas.backend.user.UserFacadeEjb.UserFacadeEjbLocal;
@@ -26,6 +27,8 @@
2627
@Stateless(name = "InfrastructureFacade")
2728
public class InfrastructureFacadeEjb implements InfrastructureFacade {
2829

30+
@EJB
31+
private CountryFacadeEjbLocal countryFacade;
2932
@EJB
3033
private RegionFacadeEjbLocal regionFacade;
3134
@EJB
@@ -68,6 +71,7 @@ public InfrastructureSyncDto getInfrastructureSyncData(InfrastructureChangeDates
6871
return sync;
6972
}
7073

74+
sync.setCountries(countryFacade.getAllAfter(changeDates.getCountryChangeDate()));
7175
sync.setRegions(regionFacade.getAllAfter(changeDates.getRegionChangeDate()));
7276
sync.setDistricts(districtFacade.getAllAfter(changeDates.getDistrictChangeDate()));
7377
sync.setCommunities(communityFacade.getAllAfter(changeDates.getCommunityChangeDate()));

sormas-backend/src/main/java/de/symeda/sormas/backend/region/CountryFacadeEjb.java

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package de.symeda.sormas.backend.region;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Date;
46
import java.util.List;
57
import java.util.stream.Collectors;
68

@@ -17,6 +19,8 @@
1719
import javax.persistence.criteria.Root;
1820
import javax.validation.constraints.NotNull;
1921

22+
import org.apache.commons.lang3.StringUtils;
23+
2024
import de.symeda.sormas.api.i18n.I18nProperties;
2125
import de.symeda.sormas.api.i18n.Validations;
2226
import de.symeda.sormas.api.region.CountryCriteria;
@@ -27,10 +31,9 @@
2731
import de.symeda.sormas.api.utils.EmptyValueException;
2832
import de.symeda.sormas.api.utils.SortProperty;
2933
import de.symeda.sormas.api.utils.ValidationRuntimeException;
34+
import de.symeda.sormas.backend.user.UserService;
3035
import de.symeda.sormas.backend.util.DtoHelper;
3136
import de.symeda.sormas.backend.util.ModelConstants;
32-
import org.apache.commons.lang3.ObjectUtils;
33-
import org.apache.commons.lang3.StringUtils;
3437

3538
@Stateless(name = "CountryFacade")
3639
public class CountryFacadeEjb implements CountryFacade {
@@ -41,14 +44,17 @@ public class CountryFacadeEjb implements CountryFacade {
4144
@EJB
4245
private CountryService countryService;
4346

47+
@EJB
48+
private UserService userService;
49+
4450
@Override
4551
public CountryDto getCountryByUuid(String uuid) {
4652
return toDto(countryService.getByUuid(uuid));
4753
}
4854

4955
@Override
5056
public List<CountryReferenceDto> getByDefaultName(String name, boolean includeArchivedEntities) {
51-
return countryService.getByDefaultName(name, includeArchivedEntities).stream().map(r -> toReferenceDto(r)).collect(Collectors.toList());
57+
return countryService.getByDefaultName(name, includeArchivedEntities).stream().map(CountryFacadeEjb::toReferenceDto).collect(Collectors.toList());
5258
}
5359

5460
@Override
@@ -64,7 +70,7 @@ public List<CountryIndexDto> getIndexList(CountryCriteria criteria, Integer firs
6470
}
6571

6672
if (sortProperties != null && sortProperties.size() > 0) {
67-
List<Order> order = new ArrayList<Order>(sortProperties.size());
73+
List<Order> order = new ArrayList<>(sortProperties.size());
6874
for (SortProperty sortProperty : sortProperties) {
6975
Expression<?> expression;
7076
switch (sortProperty.propertyName) {
@@ -92,10 +98,10 @@ public List<CountryIndexDto> getIndexList(CountryCriteria criteria, Integer firs
9298
.setMaxResults(max)
9399
.getResultList()
94100
.stream()
95-
.map(f -> toIndexDto(f))
101+
.map(this::toIndexDto)
96102
.collect(Collectors.toList());
97103
} else {
98-
return em.createQuery(cq).getResultList().stream().map(f -> toIndexDto(f)).collect(Collectors.toList());
104+
return em.createQuery(cq).getResultList().stream().map(this::toIndexDto).collect(Collectors.toList());
99105
}
100106
}
101107

@@ -124,8 +130,7 @@ public String saveCountry(CountryDto dto) throws ValidationRuntimeException {
124130
Country country = countryService.getByUuid(dto.getUuid());
125131

126132
if (country == null
127-
&& (countryService.getByIsoCode(dto.getIsoCode(), true).isPresent()
128-
|| countryService.getByUnoCode(dto.getUnoCode(), true).isPresent())) {
133+
&& (countryService.getByIsoCode(dto.getIsoCode(), true).isPresent() || countryService.getByUnoCode(dto.getUnoCode(), true).isPresent())) {
129134
throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.importCountryAlreadyExists));
130135
}
131136

@@ -156,8 +161,7 @@ public static CountryReferenceDto toReferenceDto(Country entity) {
156161
if (entity == null) {
157162
return null;
158163
}
159-
CountryReferenceDto dto = new CountryReferenceDto(entity.getUuid(), entity.toString());
160-
return dto;
164+
return new CountryReferenceDto(entity.getUuid(), entity.toString());
161165
}
162166

163167
public CountryDto toDto(Country entity) {
@@ -220,4 +224,48 @@ private Country fillOrBuildEntity(@NotNull CountryDto source, Country target) {
220224
public static class CountryFacadeEjbLocal extends CountryFacadeEjb {
221225

222226
}
227+
228+
@Override
229+
public List<CountryDto> getAllAfter(Date date) {
230+
CriteriaBuilder cb = em.getCriteriaBuilder();
231+
CriteriaQuery<CountryDto> cq = cb.createQuery(CountryDto.class);
232+
Root<Country> country = cq.from(Country.class);
233+
234+
selectDtoFields(cq, country);
235+
236+
Predicate filter = countryService.createChangeDateFilter(cb, country, date);
237+
238+
if (filter != null) {
239+
cq.where(filter);
240+
}
241+
242+
return em.createQuery(cq).getResultList();
243+
}
244+
245+
@Override
246+
public List<CountryDto> getByUuids(List<String> uuids) {
247+
return countryService.getByUuids(uuids).stream().map(this::toDto).collect(Collectors.toList());
248+
}
249+
250+
@Override
251+
public List<String> getAllUuids() {
252+
if (userService.getCurrentUser() == null) {
253+
return Collections.emptyList();
254+
}
255+
return countryService.getAllUuids();
256+
}
257+
258+
// Need to be in the same order as in the constructor
259+
private void selectDtoFields(CriteriaQuery<CountryDto> cq, Root<Country> root) {
260+
261+
cq.multiselect(
262+
root.get(Country.CREATION_DATE),
263+
root.get(Country.CHANGE_DATE),
264+
root.get(Country.UUID),
265+
root.get(Country.ARCHIVED),
266+
root.get(Country.DEFAULT_NAME),
267+
root.get(Country.EXTERNAL_ID),
268+
root.get(Country.ISO_CODE),
269+
root.get(Country.UNO_CODE));
270+
}
223271
}

sormas-backend/src/test/java/de/symeda/sormas/backend/region/CountryFacadeEjbTest.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package de.symeda.sormas.backend.region;
22

3+
import static java.time.temporal.ChronoUnit.DAYS;
4+
import static java.util.stream.Collectors.toList;
5+
import static org.hamcrest.Matchers.containsInAnyOrder;
36
import static org.junit.Assert.assertEquals;
47
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertThat;
59
import static org.junit.Assert.assertTrue;
610

11+
import java.sql.Timestamp;
12+
import java.time.Instant;
713
import java.util.List;
814
import java.util.Objects;
915

10-
import de.symeda.sormas.api.region.CountryIndexDto;
11-
import de.symeda.sormas.api.utils.EmptyValueException;
16+
import org.hamcrest.Matchers;
1217
import org.junit.Test;
1318

19+
import com.google.common.collect.Lists;
20+
1421
import de.symeda.sormas.api.region.CountryCriteria;
1522
import de.symeda.sormas.api.region.CountryDto;
23+
import de.symeda.sormas.api.region.CountryIndexDto;
1624
import de.symeda.sormas.api.region.CountryReferenceDto;
25+
import de.symeda.sormas.api.utils.EmptyValueException;
1726
import de.symeda.sormas.api.utils.ValidationRuntimeException;
1827
import de.symeda.sormas.backend.AbstractBeanTest;
1928

@@ -50,6 +59,41 @@ public void testGetIndexList() {
5059
assertEquals(expected.getIsoCode(), actual.getIsoCode());
5160
}
5261

62+
@Test
63+
public void testGetAllAfter() {
64+
Country country1 = creator.createCountry("Romania", "ROU", "642");
65+
Country country2 = creator.createCountry("Germany", "DEU", "276");
66+
country2.setChangeDate(Timestamp.from(Instant.now().minus(2, DAYS)));
67+
getCountryService().doFlush();
68+
69+
List<CountryDto> actualList = getCountryFacade().getAllAfter(Timestamp.from(Instant.now().minus(1, DAYS)));
70+
CountryDto actual = actualList.get(0);
71+
assertTrue(entityIsEqualToDto(country1, actual));
72+
}
73+
74+
@Test
75+
public void testGetByUuids() {
76+
Country country1 = creator.createCountry("Romania", "ROU", "642");
77+
Country country2 = creator.createCountry("Germany", "DEU", "276");
78+
creator.createCountry("France", "FRA", "123");
79+
getCountryService().doFlush();
80+
List<String> uuids = Lists.newArrayList(country1.getUuid(), country2.getUuid());
81+
82+
List<String> actualList = getCountryFacade().getByUuids(uuids).stream().map(CountryDto::getUuid).collect(toList());
83+
assertTrue(uuids.containsAll(actualList));
84+
}
85+
86+
@Test
87+
public void testGetUuids() {
88+
Country country1 = creator.createCountry("Romania", "ROU", "642");
89+
Country country2 = creator.createCountry("Germany", "DEU", "276");
90+
Country country3 = creator.createCountry("France", "FRA", "123");
91+
getCountryService().doFlush();
92+
List<String> uuids = Lists.newArrayList(country1.getUuid(), country2.getUuid(), country3.getUuid());
93+
List<String> actualList = getCountryFacade().getAllUuids();
94+
assertTrue(uuids.containsAll(actualList));
95+
}
96+
5397
@Test
5498
public void testCount() {
5599
creator.createCountry("Romania", "ROU", "642");
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*******************************************************************************
2+
* SORMAS® - Surveillance Outbreak Response Management & Analysis System
3+
* Copyright © 2016-2018 Helmholtz-Zentrum für Infektionsforschung GmbH (HZI)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*******************************************************************************/
18+
package de.symeda.sormas.rest;
19+
20+
import java.util.Date;
21+
import java.util.List;
22+
23+
import javax.annotation.security.RolesAllowed;
24+
import javax.ws.rs.GET;
25+
import javax.ws.rs.POST;
26+
import javax.ws.rs.Path;
27+
import javax.ws.rs.PathParam;
28+
import javax.ws.rs.Produces;
29+
import javax.ws.rs.core.MediaType;
30+
31+
import de.symeda.sormas.api.FacadeProvider;
32+
import de.symeda.sormas.api.region.CountryDto;
33+
34+
/**
35+
* @see <a href="https://jersey.java.net/documentation/latest/">Jersey documentation</a>
36+
* @see <a href="https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2051">Jersey documentation HTTP Methods</a>
37+
*
38+
*/
39+
@Path("/countries")
40+
@Produces(MediaType.APPLICATION_JSON + "; charset=UTF-8")
41+
@RolesAllowed({
42+
"USER",
43+
"REST_USER" })
44+
public class CountryResource {
45+
46+
@GET
47+
@Path("/all/{since}")
48+
public List<CountryDto> getAll(@PathParam("since") long since) {
49+
return FacadeProvider.getCountryFacade().getAllAfter(new Date(since));
50+
}
51+
52+
@POST
53+
@Path("/query")
54+
public List<CountryDto> getByUuids(List<String> uuids) {
55+
return FacadeProvider.getCountryFacade().getByUuids(uuids);
56+
}
57+
58+
@GET
59+
@Path("/uuids")
60+
public List<String> getAllUuids() {
61+
return FacadeProvider.getCountryFacade().getAllUuids();
62+
}
63+
}

0 commit comments

Comments
 (0)