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

Commit 8af58f5

Browse files
SORMAS-Foundation#2993 implement CountryFacadeEjb, UI fixes
1 parent 1880462 commit 8af58f5

14 files changed

Lines changed: 345 additions & 75 deletions

File tree

sormas-api/src/main/java/de/symeda/sormas/api/i18n/Validations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public interface Validations {
5252
String importCasesUnexpectedError = "importCasesUnexpectedError";
5353
String importCommunityAlreadyExists = "importCommunityAlreadyExists";
5454
String importCommunityNotUnique = "importCommunityNotUnique";
55+
String importCountryAlreadyExists = "importCountryAlreadyExists";
5556
String importDistrictAlreadyExists = "importDistrictAlreadyExists";
5657
String importDistrictNotUnique = "importDistrictNotUnique";
5758
String importEntryCommunityNotInUsersJurisdiction = "importEntryCommunityNotInUsersJurisdiction";

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,41 @@ public class CountryDto extends EntityDto {
88
private static final long serialVersionUID = 8309822957203823162L;
99

1010
public static final String I18N_PREFIX = "Country";
11-
public static final String NAME = "name";
12-
public static final String EXTERNAL_ID = "externalID";
11+
public static final String DEFAULT_NAME = "defaultName";
12+
public static final String DISPLAY_NAME = "displayName";
13+
public static final String EXTERNAL_ID = "externalId";
1314
public static final String ISO_CODE = "isoCode";
1415
public static final String UNO_CODE = "unoCode";
1516

16-
private String name;
17-
private String externalID;
17+
private String defaultName;
18+
private String displayName;
19+
private String externalId;
1820
private String isoCode;
1921
private String unoCode;
2022
private boolean archived;
2123

22-
public String getName() {
23-
return name;
24+
public String getDefaultName() {
25+
return defaultName;
2426
}
2527

26-
public void setName(String name) {
27-
this.name = name;
28+
public void setDefaultName(String defaultName) {
29+
this.defaultName = defaultName;
2830
}
2931

30-
public String getExternalID() {
31-
return externalID;
32+
public String getDisplayName() {
33+
return displayName;
3234
}
3335

34-
public void setExternalID(String externalID) {
35-
this.externalID = externalID;
36+
public void setDisplayName(String displayName) {
37+
this.displayName = displayName;
38+
}
39+
40+
public String getExternalId() {
41+
return externalId;
42+
}
43+
44+
public void setExternalId(String externalID) {
45+
this.externalId = externalID;
3646
}
3747

3848
public String getIsoCode() {
@@ -61,7 +71,7 @@ public void setArchived(boolean archived) {
6171

6272
@Override
6373
public String toString() {
64-
return this.name;
74+
return this.displayName;
6575
}
6676

6777
public static CountryDto build() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface CountryFacade {
1111

1212
CountryDto getCountryByUuid(String uuid);
1313

14-
List<CountryReferenceDto> getByName(String name, boolean includeArchivedEntities);
14+
List<CountryReferenceDto> getByDefaultName(String name, boolean includeArchivedEntities);
1515

1616
List<CountryDto> getIndexList(CountryCriteria criteria, Integer first, Integer max, List<SortProperty> sortProperties);
1717

sormas-api/src/main/resources/validations.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ importInvalidDate = Invalid date in column %s; Allowed date formats are dd/MM/yy
4646
importLineTooLong = This line is longer than the header line
4747
importAreaNotUnique = Invalid value %s in column %s; Area name is not unique, make sure there is only one area with this name in the database
4848
importRegionNotUnique = Invalid value %s in column %s; Region name is not unique, make sure there is only one region with this name in the database
49+
importCountryAlreadyExists = The database already contains a country with this ISO code or this UNO code.
4950
importRegionAlreadyExists = The database already contains a region with this name.
5051
importDistrictAlreadyExists = The database already contains a district with this name in the specified region.
5152
importCommunityAlreadyExists = The database already contains a community with this name in the specified district.
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,60 @@
11
package de.symeda.sormas.backend.region;
22

3-
import de.symeda.sormas.backend.common.InfrastructureAdo;
4-
53
import javax.persistence.Entity;
64

5+
import de.symeda.sormas.backend.common.InfrastructureAdo;
6+
77
@Entity
88
public class Country extends InfrastructureAdo {
99

10-
private static final long serialVersionUID = -6050390899060395940L;
10+
private static final long serialVersionUID = -6050390899060395940L;
1111

12-
public static final String TABLE_NAME = "country";
12+
public static final String TABLE_NAME = "country";
1313

14-
public static final String DEFAULT_NAME = "default_name";
15-
public static final String EXTERNAL_ID = "external_id";
16-
public static final String ISO_CODE = "iso_code";
17-
public static final String UNO_CODE = "uno_code";
14+
public static final String DEFAULT_NAME = "defaultName";
15+
public static final String EXTERNAL_ID = "externalId";
16+
public static final String ISO_CODE = "isoCode";
17+
public static final String UNO_CODE = "unoCode";
1818

19-
private String defaultName;
20-
private String externalId;
21-
private String isoCode;
22-
private String unoCode;
19+
private String defaultName;
20+
private String externalId;
21+
private String isoCode;
22+
private String unoCode;
2323

24-
public String getDefaultName() {
25-
return defaultName;
26-
}
24+
public String getDefaultName() {
25+
return defaultName;
26+
}
2727

28-
public void setDefaultName(String defaultName) {
29-
this.defaultName = defaultName;
30-
}
28+
public void setDefaultName(String defaultName) {
29+
this.defaultName = defaultName;
30+
}
3131

32-
public String getExternalId() {
33-
return externalId;
34-
}
32+
public String getExternalId() {
33+
return externalId;
34+
}
3535

36-
public void setExternalId(String externalId) {
37-
this.externalId = externalId;
38-
}
36+
public void setExternalId(String externalId) {
37+
this.externalId = externalId;
38+
}
3939

40-
public String getIsoCode() {
41-
return isoCode;
42-
}
40+
public String getIsoCode() {
41+
return isoCode;
42+
}
4343

44-
public void setIsoCode(String isoCode) {
45-
this.isoCode = isoCode;
46-
}
44+
public void setIsoCode(String isoCode) {
45+
this.isoCode = isoCode;
46+
}
4747

48-
public String getUnoCode() {
49-
return unoCode;
50-
}
48+
public String getUnoCode() {
49+
return unoCode;
50+
}
5151

52-
public void setUnoCode(String unoCode) {
53-
this.unoCode = unoCode;
54-
}
52+
public void setUnoCode(String unoCode) {
53+
this.unoCode = unoCode;
54+
}
5555

56-
@Override
57-
public String toString() {
58-
return this.defaultName;
59-
}
56+
@Override
57+
public String toString() {
58+
return this.defaultName;
59+
}
6060
}

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

Lines changed: 146 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,192 @@
11
package de.symeda.sormas.backend.region;
22

3-
import java.util.Collection;
4-
import java.util.Collections;
3+
import java.util.ArrayList;
54
import java.util.List;
5+
import java.util.stream.Collectors;
66

7+
import javax.ejb.EJB;
78
import javax.ejb.LocalBean;
89
import javax.ejb.Stateless;
9-
10+
import javax.persistence.EntityManager;
11+
import javax.persistence.PersistenceContext;
12+
import javax.persistence.criteria.CriteriaBuilder;
13+
import javax.persistence.criteria.CriteriaQuery;
14+
import javax.persistence.criteria.Expression;
15+
import javax.persistence.criteria.Order;
16+
import javax.persistence.criteria.Predicate;
17+
import javax.persistence.criteria.Root;
18+
import javax.validation.constraints.NotNull;
19+
20+
import de.symeda.sormas.api.i18n.I18nProperties;
21+
import de.symeda.sormas.api.i18n.Validations;
1022
import de.symeda.sormas.api.region.CountryCriteria;
1123
import de.symeda.sormas.api.region.CountryDto;
1224
import de.symeda.sormas.api.region.CountryFacade;
1325
import de.symeda.sormas.api.region.CountryReferenceDto;
1426
import de.symeda.sormas.api.utils.SortProperty;
1527
import de.symeda.sormas.api.utils.ValidationRuntimeException;
28+
import de.symeda.sormas.backend.util.DtoHelper;
29+
import de.symeda.sormas.backend.util.ModelConstants;
30+
import org.apache.commons.lang3.ObjectUtils;
1631

1732
@Stateless(name = "CountryFacade")
1833
public class CountryFacadeEjb implements CountryFacade {
1934

35+
@PersistenceContext(unitName = ModelConstants.PERSISTENCE_UNIT_NAME)
36+
private EntityManager em;
37+
38+
@EJB
39+
private CountryService countryService;
40+
2041
@Override
2142
public CountryDto getCountryByUuid(String uuid) {
22-
return new CountryDto();
43+
return toDto(countryService.getByUuid(uuid));
2344
}
2445

2546
@Override
26-
public List<CountryReferenceDto> getByName(String name, boolean includeArchivedEntities) {
27-
return Collections.emptyList();
47+
public List<CountryReferenceDto> getByDefaultName(String name, boolean includeArchivedEntities) {
48+
return countryService.getByDefaultName(name, includeArchivedEntities).stream().map(r -> toReferenceDto(r)).collect(Collectors.toList());
2849
}
2950

3051
@Override
3152
public List<CountryDto> getIndexList(CountryCriteria criteria, Integer first, Integer max, List<SortProperty> sortProperties) {
32-
return Collections.emptyList();
53+
CriteriaBuilder cb = em.getCriteriaBuilder();
54+
CriteriaQuery<Country> cq = cb.createQuery(Country.class);
55+
Root<Country> country = cq.from(Country.class);
56+
57+
Predicate filter = countryService.buildCriteriaFilter(criteria, cb, country);
58+
59+
if (filter != null) {
60+
cq.where(filter).distinct(true);
61+
}
62+
63+
if (sortProperties != null && sortProperties.size() > 0) {
64+
List<Order> order = new ArrayList<Order>(sortProperties.size());
65+
for (SortProperty sortProperty : sortProperties) {
66+
Expression<?> expression;
67+
switch (sortProperty.propertyName) {
68+
case Country.DEFAULT_NAME:
69+
case Country.EXTERNAL_ID:
70+
case Country.ISO_CODE:
71+
case Country.UNO_CODE:
72+
expression = country.get(sortProperty.propertyName);
73+
break;
74+
default:
75+
throw new IllegalArgumentException(sortProperty.propertyName);
76+
}
77+
order.add(sortProperty.ascending ? cb.asc(expression) : cb.desc(expression));
78+
}
79+
cq.orderBy(order);
80+
} else {
81+
cq.orderBy(cb.asc(country.get(Country.ISO_CODE)));
82+
}
83+
84+
cq.select(country);
85+
86+
if (first != null && max != null) {
87+
return em.createQuery(cq)
88+
.setFirstResult(first)
89+
.setMaxResults(max)
90+
.getResultList()
91+
.stream()
92+
.map(f -> toDto(f))
93+
.collect(Collectors.toList());
94+
} else {
95+
return em.createQuery(cq).getResultList().stream().map(f -> toDto(f)).collect(Collectors.toList());
96+
}
3397
}
3498

3599
@Override
36100
public long count(CountryCriteria criteria) {
37-
return 0;
101+
CriteriaBuilder cb = em.getCriteriaBuilder();
102+
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
103+
Root<Country> root = cq.from(Country.class);
104+
105+
Predicate filter = countryService.buildCriteriaFilter(criteria, cb, root);
106+
107+
if (filter != null) {
108+
cq.where(filter);
109+
}
110+
111+
cq.select(cb.count(root));
112+
return em.createQuery(cq).getSingleResult();
38113
}
39114

40115
@Override
41116
public void saveCountry(CountryDto dto) throws ValidationRuntimeException {
117+
Country country = countryService.getByUuid(dto.getUuid());
118+
119+
if (country == null
120+
&& (countryService.getByIsoCode(dto.getIsoCode(), true).isPresent()
121+
|| countryService.getByUnoCode(dto.getUnoCode(), true).isPresent())) {
122+
throw new ValidationRuntimeException(I18nProperties.getValidationError(Validations.importCountryAlreadyExists));
123+
}
42124

125+
country = fillOrBuildEntity(dto, country);
126+
countryService.ensurePersisted(country);
43127
}
44128

45129
@Override
46130
public void archive(String countryUuid) {
47-
131+
Country country = countryService.getByUuid(countryUuid);
132+
if (country != null) {
133+
country.setArchived(true);
134+
countryService.ensurePersisted(country);
135+
}
48136
}
49137

50138
@Override
51139
public void dearchive(String countryUuid) {
140+
Country country = countryService.getByUuid(countryUuid);
141+
if (country != null) {
142+
country.setArchived(false);
143+
countryService.ensurePersisted(country);
144+
}
145+
}
146+
147+
public static CountryReferenceDto toReferenceDto(Country entity) {
148+
if (entity == null) {
149+
return null;
150+
}
151+
CountryReferenceDto dto = new CountryReferenceDto(entity.getUuid(), entity.toString());
152+
return dto;
153+
}
154+
155+
public CountryDto toDto(Country entity) {
156+
if (entity == null) {
157+
return null;
158+
}
159+
CountryDto dto = new CountryDto();
160+
DtoHelper.fillDto(dto, entity);
161+
162+
String nameLanguageKey = "country." + entity.getIsoCode().toUpperCase() + ".name";
163+
String displayName = ObjectUtils.firstNonNull(I18nProperties.getString(nameLanguageKey), entity.getDefaultName());
164+
dto.setDisplayName(displayName);
165+
dto.setDefaultName(entity.getDefaultName());
166+
dto.setArchived(entity.isArchived());
167+
dto.setExternalId(entity.getExternalId());
168+
dto.setIsoCode(entity.getIsoCode());
169+
dto.setUnoCode(entity.getUnoCode());
170+
171+
return dto;
172+
}
173+
174+
private Country fillOrBuildEntity(@NotNull CountryDto source, Country target) {
175+
176+
if (target == null) {
177+
target = new Country();
178+
target.setUuid(source.getUuid());
179+
}
180+
181+
DtoHelper.validateDto(source, target);
182+
183+
target.setDefaultName(source.getDefaultName());
184+
target.setArchived(source.isArchived());
185+
target.setExternalId(source.getExternalId());
186+
target.setIsoCode(source.getIsoCode());
187+
target.setUnoCode(source.getUnoCode());
52188

189+
return target;
53190
}
54191

55192
@LocalBean

0 commit comments

Comments
 (0)