|
1 | 1 | package de.symeda.sormas.backend.region; |
2 | 2 |
|
3 | | -import java.util.Collection; |
4 | | -import java.util.Collections; |
| 3 | +import java.util.ArrayList; |
5 | 4 | import java.util.List; |
| 5 | +import java.util.stream.Collectors; |
6 | 6 |
|
| 7 | +import javax.ejb.EJB; |
7 | 8 | import javax.ejb.LocalBean; |
8 | 9 | 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; |
10 | 22 | import de.symeda.sormas.api.region.CountryCriteria; |
11 | 23 | import de.symeda.sormas.api.region.CountryDto; |
12 | 24 | import de.symeda.sormas.api.region.CountryFacade; |
13 | 25 | import de.symeda.sormas.api.region.CountryReferenceDto; |
14 | 26 | import de.symeda.sormas.api.utils.SortProperty; |
15 | 27 | 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; |
16 | 31 |
|
17 | 32 | @Stateless(name = "CountryFacade") |
18 | 33 | public class CountryFacadeEjb implements CountryFacade { |
19 | 34 |
|
| 35 | + @PersistenceContext(unitName = ModelConstants.PERSISTENCE_UNIT_NAME) |
| 36 | + private EntityManager em; |
| 37 | + |
| 38 | + @EJB |
| 39 | + private CountryService countryService; |
| 40 | + |
20 | 41 | @Override |
21 | 42 | public CountryDto getCountryByUuid(String uuid) { |
22 | | - return new CountryDto(); |
| 43 | + return toDto(countryService.getByUuid(uuid)); |
23 | 44 | } |
24 | 45 |
|
25 | 46 | @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()); |
28 | 49 | } |
29 | 50 |
|
30 | 51 | @Override |
31 | 52 | 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 | + } |
33 | 97 | } |
34 | 98 |
|
35 | 99 | @Override |
36 | 100 | 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(); |
38 | 113 | } |
39 | 114 |
|
40 | 115 | @Override |
41 | 116 | 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 | + } |
42 | 124 |
|
| 125 | + country = fillOrBuildEntity(dto, country); |
| 126 | + countryService.ensurePersisted(country); |
43 | 127 | } |
44 | 128 |
|
45 | 129 | @Override |
46 | 130 | 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 | + } |
48 | 136 | } |
49 | 137 |
|
50 | 138 | @Override |
51 | 139 | 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()); |
52 | 188 |
|
| 189 | + return target; |
53 | 190 | } |
54 | 191 |
|
55 | 192 | @LocalBean |
|
0 commit comments