• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

opensrp / opensrp-server-core / #193

17 Oct 2024 10:04AM UTC coverage: 77.377% (-0.2%) from 77.619%
#193

push

github

web-flow
Merge pull request #636 from opensrp/sanitize-spaces-from-productName

sanitize whitespaces from productName

7 of 7 new or added lines in 3 files covered. (100.0%)

27 existing lines in 3 files now uncovered.

8277 of 10697 relevant lines covered (77.38%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.84
/src/main/java/org/opensrp/repository/postgres/LocationRepositoryImpl.java
1
package org.opensrp.repository.postgres;
2

3
import static org.smartregister.domain.LocationProperty.PropertyStatus.ACTIVE;
4
import static org.smartregister.domain.LocationProperty.PropertyStatus.PENDING_REVIEW;
5

6
import java.util.ArrayList;
7
import java.util.Arrays;
8
import java.util.Collection;
9
import java.util.Collections;
10
import java.util.Date;
11
import java.util.HashMap;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Set;
15
import java.util.stream.Collectors;
16

17
import com.ibm.fhir.model.resource.Bundle;
18
import org.apache.commons.lang.StringUtils;
19
import org.apache.commons.lang3.tuple.Pair;
20
import org.opensrp.domain.LocationDetail;
21
import org.opensrp.domain.LocationTagMap;
22
import org.opensrp.domain.LocationAndStock;
23
import org.opensrp.domain.StructureCount;
24
import org.opensrp.domain.StructureDetails;
25
import org.opensrp.domain.postgres.Location;
26
import org.opensrp.domain.postgres.LocationMetadata;
27
import org.opensrp.domain.postgres.LocationMetadataExample;
28
import org.opensrp.domain.postgres.LocationMetadataExample.Criteria;
29
import org.opensrp.domain.postgres.Stock;
30
import org.opensrp.domain.postgres.StockMetadataExample;
31
import org.opensrp.domain.postgres.Structure;
32
import org.opensrp.domain.postgres.StructureFamilyDetails;
33
import org.opensrp.domain.postgres.StructureMetadata;
34
import org.opensrp.domain.postgres.StructureMetadataExample;
35
import org.opensrp.repository.LocationRepository;
36
import org.opensrp.repository.StructureCreateOrUpdateEvent;
37
import org.opensrp.repository.postgres.mapper.custom.CustomLocationMapper;
38
import org.opensrp.repository.postgres.mapper.custom.CustomLocationMetadataMapper;
39
import org.opensrp.repository.postgres.mapper.custom.CustomStructureMapper;
40
import org.opensrp.repository.postgres.mapper.custom.CustomStructureMetadataMapper;
41
import org.opensrp.search.LocationSearchBean;
42
import org.opensrp.service.LocationTagService;
43
import org.opensrp.util.RepositoryUtil;
44
import org.smartregister.converters.LocationConverter;
45
import org.smartregister.converters.PhysicalLocationAndStocksConverter;
46
import org.smartregister.domain.LocationTag;
47
import org.smartregister.domain.PhysicalLocation;
48
import org.smartregister.domain.PhysicalLocationAndStocks;
49
import org.springframework.beans.factory.annotation.Autowired;
50
import org.springframework.context.ApplicationEventPublisher;
51
import org.springframework.stereotype.Repository;
52
import org.springframework.transaction.annotation.Transactional;
53

54
@Repository
55
public class LocationRepositoryImpl extends BaseRepositoryImpl<PhysicalLocation> implements LocationRepository {
1✔
56
        
57
        @Autowired
58
        private CustomLocationMapper locationMapper;
59
        
60
        @Autowired
61
        private CustomLocationMetadataMapper locationMetadataMapper;
62
        
63
        @Autowired
64
        private CustomStructureMapper structureMapper;
65
        
66
        @Autowired
67
        private CustomStructureMetadataMapper structureMetadataMapper;
68
        
69
        @Autowired
70
        private LocationTagService locationTagService;
71
        
72
        @Autowired
73
        private ApplicationEventPublisher applicationEventPublisher;
74
        @Override
75
        public PhysicalLocation get(String id) {
76
                return convert(locationMetadataMapper.findById(id, true, false));
1✔
77
        }
78
        
79
        @Override
80
        public PhysicalLocation get(String id, boolean returnGeography, boolean includeInactive) {
81
                return convert(locationMetadataMapper.findById(id, returnGeography, includeInactive));
×
82
        }
83
        
84
        @Override
85
        public PhysicalLocation getStructure(String id, boolean returnGeography) {
86
                return convert(structureMetadataMapper.findById(id, returnGeography));
1✔
87
        }
88
        
89
        @Override
90
        @Transactional
91
        public void add(PhysicalLocation entity) {
92
                if (getUniqueField(entity) == null) {
1✔
93
                        return;
1✔
94
                }
95
                
96
                if (retrievePrimaryKey(entity) != null) { // PhysicalLocation already added
1✔
97
                        return;
1✔
98
                }
99
                
100
                if (entity.isJurisdiction())
1✔
101
                        addLocation(entity);
1✔
102
                else
103
                        addStructure(entity);
1✔
104
                
105
        }
1✔
106
        
107
        private void updateLocationServerVersion(Location pgLocation, PhysicalLocation entity) {
108
                long serverVersion = locationMapper.selectServerVersionByPrimaryKey(pgLocation.getId());
1✔
109
                entity.setServerVersion(serverVersion);
1✔
110
                pgLocation.setJson(entity);
1✔
111
                pgLocation.setServerVersion(null);
1✔
112
                int rowsAffected = locationMapper.updateByPrimaryKeySelective(pgLocation);
1✔
113
                if (rowsAffected < 1) {
1✔
114
                        throw new IllegalStateException();
×
115
                }
116
        }
1✔
117
        
118
        private void updateStructureServerVersion(Structure pgStructure, PhysicalLocation entity) {
119
                long serverVersion = structureMapper.selectServerVersionByPrimaryKey(pgStructure.getId());
1✔
120
                entity.setServerVersion(serverVersion);
1✔
121
                pgStructure.setJson(entity);
1✔
122
                pgStructure.setServerVersion(null);
1✔
123
                int rowsAffected = structureMapper.updateByPrimaryKeySelective(pgStructure);
1✔
124
                if (rowsAffected < 1) {
1✔
125
                        throw new IllegalStateException();
×
126
                }
127
        }
1✔
128
        
129
        
130
        private void addLocation(PhysicalLocation entity) {
131
                
132
                Location pgLocation = convert(entity, null);
1✔
133
                if (pgLocation == null) {
1✔
134
                        throw new IllegalStateException();
×
135
                }
136
                
137
                int rowsAffected = locationMapper.insertSelectiveAndSetId(pgLocation);
1✔
138
                
139
                if (rowsAffected < 1 || pgLocation.getId() == null) {
1✔
140
                        throw new IllegalStateException();
×
141
                }
142
                
143
                updateLocationServerVersion(pgLocation, entity);
1✔
144
                
145
                LocationMetadata locationMetadata = createMetadata(entity, pgLocation.getId());
1✔
146
                
147
                locationMetadataMapper.insertSelective(locationMetadata);
1✔
148
                saveLocationTag(entity, pgLocation.getId(), false);
1✔
149
        }
1✔
150
        
151
        private void addStructure(PhysicalLocation entity) {
152
                
153
                Structure pgStructure = convertStructure(entity, null);
1✔
154
                if (pgStructure == null) {
1✔
155
                        throw new IllegalStateException();
×
156
                }
157
                
158
                int rowsAffected = structureMapper.insertSelectiveAndSetId(pgStructure);
1✔
159
                if (rowsAffected < 1 || pgStructure.getId() == null) {
1✔
160
                        throw new IllegalStateException();
×
161
                }
162
                
163
                updateStructureServerVersion(pgStructure, entity);
1✔
164
                
165
                StructureMetadata structureMetadata = createStructureMetadata(entity, pgStructure.getId());
1✔
166
                
167
                structureMetadataMapper.insertSelective(structureMetadata);
1✔
168
                StructureCreateOrUpdateEvent structureEvent = new StructureCreateOrUpdateEvent(pgStructure);
1✔
169
                applicationEventPublisher.publishEvent(structureEvent);
1✔
170
        }
1✔
171
        
172
        @Override
173
        @Transactional
174
        public void update(PhysicalLocation entity) {
175
                if (getUniqueField(entity) == null) {
1✔
176
                        return;
×
177
                }
178
                
179
                Long id = retrievePrimaryKey(entity);
1✔
180
                if (id == null) { // PhysicalLocation does not exist
1✔
181
                        return;
1✔
182
                }
183
                if (entity.isJurisdiction())
1✔
184
                        updateLocation(entity, id);
1✔
185
                else
186
                        updateStructure(entity, id);
1✔
187
                
188
        }
1✔
189
        
190
        private void updateLocation(PhysicalLocation entity, Long id) {
191
                Location pgLocation = convert(entity, id);
1✔
192
                if (pgLocation == null) {
1✔
193
                        throw new IllegalStateException();
×
194
                }
195
                
196
                
197
                int rowsAffected = locationMapper.updateByPrimaryKeyAndGenerateServerVersion(pgLocation);
1✔
198
                if (rowsAffected < 1) {
1✔
199
                        return;
×
200
                }
201
                
202
                updateLocationServerVersion(pgLocation, entity);
1✔
203
                
204
                
205
                LocationMetadata locationMetadata = createMetadata(entity, pgLocation.getId());
1✔
206
                
207
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
208
                locationMetadataExample.createCriteria().andLocationIdEqualTo(id);
1✔
209
                LocationMetadata metadata = locationMetadataMapper.selectByExample(locationMetadataExample).get(0);
1✔
210
                locationMetadata.setId(metadata.getId());
1✔
211
                locationMetadata.setDateCreated(metadata.getDateCreated());
1✔
212
                locationMetadataMapper.updateByPrimaryKey(locationMetadata);
1✔
213
                saveLocationTag(entity, pgLocation.getId(), true);
1✔
214
        }
1✔
215
        
216
        private void updateStructure(PhysicalLocation entity, Long id) {
217
                Structure pgStructure = convertStructure(entity, id);
1✔
218
                if (pgStructure == null) {
1✔
219
                        throw new IllegalStateException();
×
220
                }
221
                
222
                
223
                int rowsAffected = structureMapper.updateByPrimaryKeyAndGenerateServerVersion(pgStructure);
1✔
224
                if (rowsAffected < 1) {
1✔
225
                        return;
×
226
                }
227
                
228
                updateStructureServerVersion(pgStructure, entity);
1✔
229
                
230
                StructureMetadata structureMetadata = createStructureMetadata(entity, pgStructure.getId());
1✔
231
                
232
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
233
                structureMetadataExample.createCriteria().andStructureIdEqualTo(id);
1✔
234
                StructureMetadata metadata = structureMetadataMapper.selectByExample(structureMetadataExample).get(0);
1✔
235
                structureMetadata.setId(metadata.getId());
1✔
236
                structureMetadata.setDateCreated(metadata.getDateCreated());
1✔
237
                structureMetadataMapper.updateByPrimaryKey(structureMetadata);
1✔
238
                StructureCreateOrUpdateEvent structureEvent = new StructureCreateOrUpdateEvent(pgStructure);
1✔
239
                applicationEventPublisher.publishEvent(structureEvent);
1✔
240
        }
1✔
241
        
242
        @Override
243
        public List<PhysicalLocation> getAll() {
244
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
245
                locationMetadataExample.createCriteria().andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
246
                List<Location> locations = locationMetadataMapper.selectMany(locationMetadataExample, 0, DEFAULT_FETCH_SIZE);
1✔
247
                return convert(locations);
1✔
248
        }
249
        
250
        @Override
251
        public List<PhysicalLocation> getAllStructures() {
252
                List<Structure> structures = structureMetadataMapper.selectMany(new StructureMetadataExample(), 0,
1✔
253
                    DEFAULT_FETCH_SIZE);
254
                return convertStructures(structures);
1✔
255
        }
256
        
257
        @Override
258
        @Transactional
259
        public void safeRemove(PhysicalLocation entity) {
260
                if (entity == null) {
1✔
261
                        return;
1✔
262
                }
263
                
264
                Long id = retrievePrimaryKey(entity);
1✔
265
                if (id == null) {
1✔
266
                        return;
1✔
267
                }
268
                
269
                if (entity.isJurisdiction()) {
1✔
270
                        LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
271
                        locationMetadataExample.createCriteria().andLocationIdEqualTo(id);
1✔
272
                        int rowsAffected = locationMetadataMapper.deleteByExample(locationMetadataExample);
1✔
273
                        if (rowsAffected < 1) {
1✔
274
                                return;
×
275
                        }
276
                        
277
                        locationMapper.deleteByPrimaryKey(id);
1✔
278
                } else {
1✔
279
                        StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
280
                        structureMetadataExample.createCriteria().andStructureIdEqualTo(id);
1✔
281
                        int rowsAffected = structureMetadataMapper.deleteByExample(structureMetadataExample);
1✔
282
                        if (rowsAffected < 1) {
1✔
283
                                return;
×
284
                        }
285
                        
286
                        structureMapper.deleteByPrimaryKey(id);
1✔
287
                }
288
                
289
        }
1✔
290
        
291
        @Override
292
        public List<PhysicalLocation> findLocationsByServerVersion(long serverVersion) {
293
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
294
                locationMetadataExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion)
1✔
295
                        .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
296
                locationMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
1✔
297
                List<Location> locations = locationMetadataMapper.selectMany(locationMetadataExample, 0, DEFAULT_FETCH_SIZE);
1✔
298
                return convert(locations);
1✔
299
        }
300
        
301
        @Override
302
        public List<PhysicalLocation> findLocationsByNames(String locationNames, long serverVersion) {
303
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
304
                locationMetadataExample.createCriteria()
1✔
305
                        .andNameIn(Arrays.asList(org.apache.commons.lang.StringUtils.split(locationNames, ",")))
1✔
306
                        .andServerVersionGreaterThanOrEqualTo(serverVersion)
1✔
307
                        .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
308
                locationMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
1✔
309
                List<Location> locations = locationMetadataMapper.selectMany(locationMetadataExample, 0, DEFAULT_FETCH_SIZE);
1✔
310
                return convert(locations);
1✔
311
        }
312
        
313
        @Override
314
        public List<PhysicalLocation> findStructuresByParentAndServerVersion(String parentIds, long serverVersion) {
315
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
316
                structureMetadataExample.createCriteria()
1✔
317
                        .andParentIdIn(Arrays.asList(org.apache.commons.lang.StringUtils.split(parentIds, ",")))
1✔
318
                        .andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
319
                structureMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
1✔
320
                List<Structure> locations = structureMetadataMapper.selectMany(structureMetadataExample, 0, DEFAULT_FETCH_SIZE);
1✔
321
                return convertStructures(locations);
1✔
322
        }
323
        
324
        @Override
325
        public List<PhysicalLocation> findByEmptyServerVersion() {
326
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
327
                locationMetadataExample.createCriteria().andServerVersionEqualTo(0l);
1✔
328
                locationMetadataExample.or(locationMetadataExample.createCriteria().andServerVersionIsNull());
1✔
329
                List<Location> locations = locationMetadataMapper.selectMany(locationMetadataExample, 0, DEFAULT_FETCH_SIZE);
1✔
330
                return convert(locations);
1✔
331
        }
332
        
333
        @Override
334
        public List<PhysicalLocation> findStructuresByEmptyServerVersion() {
335
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
336
                structureMetadataExample.createCriteria().andServerVersionEqualTo(0l);
1✔
337
                structureMetadataExample.or(structureMetadataExample.createCriteria().andServerVersionIsNull());
1✔
338
                List<Structure> locations = structureMetadataMapper.selectMany(structureMetadataExample, 0, DEFAULT_FETCH_SIZE);
1✔
339
                return convertStructures(locations);
1✔
340
        }
341
        
342
        @Override
343
        public Collection<StructureDetails> findStructureAndFamilyDetails(double latitude, double longitude, double radius) {
344
                List<StructureFamilyDetails> pgList = structureMapper.selectStructureAndFamilyWithinRadius(latitude, longitude,
1✔
345
                    radius);
346
                Map<String, StructureDetails> structureDetails = new HashMap<>();
1✔
347
                for (StructureFamilyDetails detail : pgList) {
1✔
348
                        StructureDetails structure;
349
                        if (!structureDetails.containsKey(detail.getId())) {
1✔
350
                                structure = new StructureDetails(detail.getId(), detail.getParentId(), detail.getType());
1✔
351
                                
352
                                structureDetails.put(detail.getId(), structure);
1✔
353
                        } else {
354
                                structure = structureDetails.get(detail.getId());
1✔
355
                        }
356
                        if (StringUtils.isNotBlank(detail.getBaseEntityId())) {
1✔
357
                                if ("Family".equalsIgnoreCase(detail.getLastName()))
1✔
358
                                        structure.setFamilyId(detail.getBaseEntityId());
1✔
359
                                else
360
                                        structure.getFamilyMembers().add(detail.getBaseEntityId());
1✔
361
                        }
362
                }
1✔
363
                return structureDetails.values();
1✔
364
        }
365
        
366
        /**
367
         * {@inheritDoc}
368
         */
369
        @Override
370
        public List<PhysicalLocation> findLocationsByProperties(boolean returnGeometry, String parentId,
371
                Map<String, String> properties) {
372
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
373
                if (parentId != null) {
1✔
374
                        locationMetadataExample.createCriteria().andParentIdEqualTo(parentId);
1✔
375
                }
376
                List<Location> locations = locationMetadataMapper.selectManyByProperties(locationMetadataExample, properties,
1✔
377
                    returnGeometry, 0, DEFAULT_FETCH_SIZE);
378
                return convert(locations);
1✔
379
        }
380
        
381
        /**
382
         * {@inheritDoc}
383
         */
384
        @Override
385
        public List<PhysicalLocation> findStructuresByProperties(boolean returnGeometry, String parentId,
386
                Map<String, String> properties, int limit) {
387
                int fetchLimit = limit > 0 ? limit : DEFAULT_FETCH_SIZE;
1✔
388
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
389
                if (StringUtils.isNotBlank(parentId)) {
1✔
390
                        structureMetadataExample.createCriteria().andParentIdEqualTo(parentId);
1✔
391
                }
392
                List<Location> locations = structureMetadataMapper.selectManyByProperties(structureMetadataExample, properties,
1✔
393
                    returnGeometry, 0, fetchLimit);
394
                return convert(locations);
1✔
395
        }
396

397
        /**
398
         * {@inheritDoc}
399
         */
400
        @Override
401
        public List<PhysicalLocation> findStructuresByProperties(boolean returnGeometry, String parentId,
402
                        Map<String, String> properties) {
403
                return findStructuresByProperties(returnGeometry,parentId,properties,DEFAULT_FETCH_SIZE);
1✔
404
        }
405
        
406
        /**
407
         * {@inheritDoc}
408
         */
409
        @Override
410
        public List<PhysicalLocation> findLocationsByIds(boolean returnGeometry, List<String> ids, Long serverVersion) {
411
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
412
                if (ids == null || ids.isEmpty()) {
1✔
413
                        return null;
×
414
                }
415
                
416
                Criteria criteria = locationMetadataExample.createCriteria().andGeojsonIdIn(ids)
1✔
417
                        .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
418
                if (serverVersion != null) {
1✔
419
                        criteria.andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
420
                }
421
                
422
                List<Location> locations = locationMetadataMapper.selectManyWithOptionalGeometry(locationMetadataExample,
1✔
423
                    returnGeometry, 0, DEFAULT_FETCH_SIZE);
424
                return convert(locations);
1✔
425
        }
426
        
427
        /**
428
         * {@inheritDoc}
429
         */
430
        @Override
431
        public List<PhysicalLocation> findLocationsByIdsOrParentIds(boolean returnGeometry, List<String> ids) {
432
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
×
433
                if (ids == null || ids.isEmpty()) {
×
434
                        return null;
×
435
                }
436
                
437
                locationMetadataExample.createCriteria().andGeojsonIdIn(ids)
×
438
                        .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
×
439
                
440
                locationMetadataExample.or(locationMetadataExample.createCriteria().andParentIdIn(ids)
×
441
                        .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name())));
×
442
                List<Location> locations = locationMetadataMapper.selectManyWithOptionalGeometry(locationMetadataExample,
×
443
                    returnGeometry, 0, DEFAULT_FETCH_SIZE);
444
                return convert(locations);
×
445
        }
446
        
447
        @Override
448
        public Pair<List<String>, Long> findAllStructureIds(Long serverVersion, int limit) {
449
                Long lastServerVersion = null;
1✔
450
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
451
                structureMetadataExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
452
                structureMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
1✔
453
                return getStructuresListLongPair(limit, lastServerVersion, structureMetadataExample);
1✔
454
        }
455
        
456
        @Override
457
        public Pair<List<String>, Long> findAllStructureIds(Long serverVersion, int limit, Date fromDate, Date toDate) {
458
                if (fromDate == null && toDate == null) {
1✔
459
                        return findAllStructureIds(serverVersion, limit);
×
460
                } else {
461
                        Long lastServerVersion = null;
1✔
462
                        StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
463
                        structureMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
1✔
464
                        StructureMetadataExample.Criteria criteria = structureMetadataExample.createCriteria();
1✔
465
                        criteria.andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
466
                        if (toDate != null && fromDate != null) {
1✔
467
                                criteria.andDateCreatedBetween(fromDate, toDate);
1✔
468
                        } else if (fromDate != null) {
1✔
469
                                criteria.andDateCreatedGreaterThanOrEqualTo(fromDate);
1✔
470
                        } else {
471
                                criteria.andDateCreatedLessThanOrEqualTo(toDate);
1✔
472
                        }
473
                        
474
                        return getStructuresListLongPair(limit, lastServerVersion, structureMetadataExample);
1✔
475
                }
476
                
477
        }
478
        
479
        private Pair<List<String>, Long> getStructuresListLongPair(int limit, Long lastServerVersion,
480
                StructureMetadataExample structureMetadataExample) {
481
                int fetchLimit = limit > 0 ? limit : DEFAULT_FETCH_SIZE;
1✔
482
                Long serverVersion = lastServerVersion;
1✔
483
                StructureMetadataExample example = structureMetadataExample;
1✔
484
                List<String> structureIdentifiers = structureMetadataMapper.selectManyIds(example, 0, fetchLimit);
1✔
485
                
486
                if (structureIdentifiers != null && !structureIdentifiers.isEmpty()) {
1✔
487
                        example = new StructureMetadataExample();
1✔
488
                        example.createCriteria()
1✔
489
                                .andGeojsonIdEqualTo(structureIdentifiers.get(structureIdentifiers.size() - 1));
1✔
490
                        List<StructureMetadata> structureMetaDataList = structureMetadataMapper
1✔
491
                                .selectByExample(example);
1✔
492

493
                        serverVersion = structureMetaDataList != null && !structureMetaDataList.isEmpty()
1✔
494
                                ? structureMetaDataList.get(0).getServerVersion()
1✔
495
                                : 0;
×
496
                }
497
                
498
                return Pair.of(structureIdentifiers, serverVersion);
1✔
499
        }
500
        
501
        /**
502
         * {@inheritDoc}
503
         */
504
        @Override
505
        public Set<LocationDetail> findLocationDetailsByPlanId(String planIdentifier) {
506
                
507
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
508
                locationMetadataExample.createCriteria().andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
509
                return locationMetadataMapper.selectDetailsByPlanId(locationMetadataExample, planIdentifier);
1✔
510
        }
511
        
512
        /**
513
         * {@inheritDoc}
514
         */
515
        @Override
516
        public List<PhysicalLocation> findLocationByIdWithChildren(boolean returnGeometry, String id, int pageSize) {
517
                return findLocationByIdsWithChildren(returnGeometry, Collections.singleton(id), pageSize);
1✔
518
        }
519
        
520
        /**
521
         * {@inheritDoc}
522
         */
523
        @Override
524
        public List<PhysicalLocation> findLocationByIdsWithChildren(boolean returnGeometry, Set<String> identifiers,
525
                int pageSize) {
526
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
527
                if (identifiers == null) {
1✔
528
                        return null;
×
529
                }
530
                
531
                int limit = Math.abs(pageSize);
1✔
532
                List<Location> locations = locationMetadataMapper.selectWithChildren(locationMetadataExample, returnGeometry,
1✔
533
                    identifiers, 0, limit);
534
                return convert(locations);
1✔
535
        }
536
        
537
        /**
538
         * {@inheritDoc}
539
         */
540
        @Override
541
        public List<PhysicalLocation> findAllLocations(boolean returnGeometry, Long serverVersion, int limit, boolean includeInactive) {
542
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
543
                LocationMetadataExample.Criteria criteria = locationMetadataExample.createCriteria();
1✔
544
                if(!includeInactive) {
1✔
545
                        criteria.andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
546
                }
547
                criteria.andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
548
                locationMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
1✔
549
                
550
                List<Location> locations = locationMetadataMapper.selectManyWithOptionalGeometry(locationMetadataExample,
1✔
551
                    returnGeometry, 0, limit);
552
                return convert(locations);
1✔
553
        }
554

555
        /**
556
         * {@inheritDoc}
557
         */
558
        @Override
559
        public Long countAllLocations(Long serverVersion) {
560
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
561
                locationMetadataExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion)
1✔
562
                                .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
563
                return locationMetadataMapper.countMany(locationMetadataExample);
1✔
564
        }
565
        
566
        /**
567
         * {@inheritDoc}
568
         */
569
        @Override
570
        public List<PhysicalLocation> findAllStructures(boolean returnGeometry, Long serverVersion, int limit, Integer pageNumber, String orderByType, String orderByFieldName) {
571
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
572
                structureMetadataExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
573
                String sortBy = orderByFieldName != null ? orderByFieldName : null;
1✔
574
                String sortOrder = orderByType != null ? orderByType : null;
1✔
575
                if (sortBy != null && sortOrder != null) {
1✔
576
                        structureMetadataExample.setOrderByClause(getOrderByClause(sortBy, sortOrder));
×
577
                } else {
578
                        structureMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
1✔
579
                }
580
                Pair<Integer, Integer> pageLimitAndOffSet = RepositoryUtil.getPageSizeAndOffset(pageNumber, limit);
1✔
581
                List<Location> locations = structureMetadataMapper.selectManyByProperties(structureMetadataExample, null,
1✔
582
                    returnGeometry, pageLimitAndOffSet.getRight(), pageLimitAndOffSet.getLeft());
1✔
583
                return convert(locations);
1✔
584
        }
585

586
        /**
587
         * {@inheritDoc}
588
         */
589
        @Override
590
        public Long countAllStructures(Long serverVersion) {
591
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
592
                structureMetadataExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
593
                return structureMetadataMapper.countMany(structureMetadataExample);
1✔
594
        }
595
        
596
        /**
597
         * {@inheritDoc}
598
         */
599
        @Override
600
        public Pair<List<String>, Long> findAllLocationIds(Long serverVersion, int limit) {
601
                Long lastServerVersion = null;
1✔
602
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
603
                locationMetadataExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
604
                locationMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
1✔
605
                
606
                return getLocationListLongPair(limit, lastServerVersion, locationMetadataExample);
1✔
607
        }
608
        
609
        @Override
610
        public Pair<List<String>, Long> findAllLocationIds(Long serverVersion, int limit, Date fromDate, Date toDate) {
611
                if (fromDate == null && toDate == null) {
×
612
                        return findAllLocationIds(serverVersion, limit);
×
613
                } else {
614
                        Long lastServerVersion = null;
×
615
                        LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
×
616
                        LocationMetadataExample.Criteria criteria = locationMetadataExample.createCriteria();
×
617
                        criteria.andServerVersionGreaterThanOrEqualTo(serverVersion);
×
618
                        locationMetadataExample.setOrderByClause(getOrderByClause(SERVER_VERSION, ASCENDING));
×
619
                        if (toDate != null && fromDate != null) {
×
620
                                criteria.andDateCreatedBetween(fromDate, toDate);
×
621
                        } else if (fromDate != null) {
×
622
                                criteria.andDateCreatedGreaterThanOrEqualTo(fromDate);
×
623
                        } else {
624
                                criteria.andDateCreatedLessThanOrEqualTo(toDate);
×
625
                        }
626
                        return getLocationListLongPair(limit, lastServerVersion, locationMetadataExample);
×
627
                }
628
        }
629
        
630
        private Pair<List<String>, Long> getLocationListLongPair(int limit, Long lastServerVersion,
631
                LocationMetadataExample locationMetadataExample) {
632
                int fetchLimit = limit > 0 ? limit : DEFAULT_FETCH_SIZE;
1✔
633
                Long serverVersion = lastServerVersion;
1✔
634
                LocationMetadataExample metadataExample = locationMetadataExample;
1✔
635
                List<String> locationIdentifiers = locationMetadataMapper.selectManyIds(metadataExample, 0, fetchLimit);
1✔
636
                
637
                if (locationIdentifiers != null && !locationIdentifiers.isEmpty()) {
1✔
638
                        metadataExample = new LocationMetadataExample();
1✔
639
                        metadataExample.createCriteria()
1✔
640
                                .andGeojsonIdEqualTo(locationIdentifiers.get(locationIdentifiers.size() - 1));
1✔
641
                        List<LocationMetadata> locationMetadataList = locationMetadataMapper.selectByExample(metadataExample);
1✔
642

643
                        serverVersion = locationMetadataList != null && !locationMetadataList.isEmpty()
1✔
644
                                ? locationMetadataList.get(0).getServerVersion()
1✔
645
                                : 0;
×
646
                }
647
                
648
                return Pair.of(locationIdentifiers, serverVersion);
1✔
649
        }
650
        
651
        /**
652
         * {@inheritDoc}
653
         */
654
        @Override
655
        public Set<LocationDetail> findParentLocationsInclusive(Set<String> identifiers) {
656
                return locationMetadataMapper.selectLocationHierachy(identifiers, true);
1✔
657
        }
658
        
659
        /**
660
         * {@inheritDoc}
661
         */
662
        @Override
663
        public Set<LocationDetail> findParentLocationsInclusive(Set<String> identifiers, boolean returnTags) {
664
                return locationMetadataMapper.selectLocationHierachy(identifiers, returnTags);
1✔
665
        }
666
        
667
        @Override
668
        public PhysicalLocation findLocationByIdentifierAndStatus(String identifier, List<String> status,
669
                boolean returnGeometry) {
670
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
671
                locationMetadataExample.createCriteria().andGeojsonIdEqualTo(identifier).andStatusIn(status);
1✔
672
                locationMetadataExample.setOrderByClause(getOrderByClause(VERSION, DESCENDING));
1✔
673
                
674
                List<Location> locations = locationMetadataMapper.selectManyWithOptionalGeometry(locationMetadataExample,
1✔
675
                    returnGeometry, 0, 1);
676
                if (locations == null || locations.isEmpty()) {
1✔
677
                        return null;
1✔
678
                }
679
                
680
                PhysicalLocation locationEntity = convert(locations.get(0));
1✔
681
                return locationEntity;
1✔
682
        }
683
        
684
        /**
685
         * {@inheritDoc}
686
         */
687
        @Override
688
        public Long countStructuresByParentAndServerVersion(String parentIds, long serverVersion) {
689
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
690
                structureMetadataExample.createCriteria()
1✔
691
                        .andParentIdIn(Arrays.asList(org.apache.commons.lang.StringUtils.split(parentIds, ",")))
1✔
692
                        .andServerVersionGreaterThanOrEqualTo(serverVersion);
1✔
693
                return structureMetadataMapper.countByExample(structureMetadataExample);
1✔
694
        }
695
        
696
        /**
697
         * {@inheritDoc}
698
         */
699
        @Override
700
        public Long countLocationsByServerVersion(long serverVersion) {
701
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
702
                locationMetadataExample.createCriteria().andServerVersionGreaterThanOrEqualTo(serverVersion)
1✔
703
                        .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
704
                return locationMetadataMapper.countByExample(locationMetadataExample);
1✔
705
        }
706
        
707
        /**
708
         * {@inheritDoc}
709
         */
710
        @Override
711
        public Long countLocationsByNames(String locationNames, long serverVersion) {
712
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
713
                locationMetadataExample.createCriteria()
1✔
714
                        .andNameIn(Arrays.asList(org.apache.commons.lang.StringUtils.split(locationNames, ",")))
1✔
715
                        .andServerVersionGreaterThanOrEqualTo(serverVersion)
1✔
716
                        .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
717
                return locationMetadataMapper.countByExample(locationMetadataExample);
1✔
718
        }
719
        
720
        /**
721
         * {@inheritDoc}
722
         */
723
        @Override
724
        public long countLocationsByIds(List<String> locationIds, long serverVersion) {
725
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
726
                locationMetadataExample.createCriteria().andGeojsonIdIn(locationIds)
1✔
727
                        .andServerVersionGreaterThanOrEqualTo(serverVersion)
1✔
728
                        .andStatusIn(Arrays.asList(ACTIVE.name(), PENDING_REVIEW.name()));
1✔
729
                return locationMetadataMapper.countByExample(locationMetadataExample);
1✔
730
        }
731

732
        @Override
733
        public List<Bundle> findLocationAndStocksByJurisdiction(String parentId) {
734
                List<PhysicalLocationAndStocks> locationAndStocks = findLocationAndStocksByJurisdiction(parentId, null, true, -1);
1✔
735
                List<Bundle> bundleList = new ArrayList<>();
1✔
736
                for(PhysicalLocationAndStocks physicalLocationAndStock: locationAndStocks){
1✔
737
                        bundleList.add(PhysicalLocationAndStocksConverter
1✔
738
                                .convertLocationAndStocksToBundleResource(physicalLocationAndStock));
1✔
739
                }
1✔
740
                return bundleList;
1✔
741
        }
742

743
        private List<PhysicalLocationAndStocks> convertToPhysicalLocationAndStock(List<LocationAndStock> locationAndStocks){
744
                if (locationAndStocks == null || locationAndStocks.isEmpty()) {
1✔
745
                        return new ArrayList<>();
×
746
                }
747

748
                List<PhysicalLocationAndStocks> convertedLocations = new ArrayList<>();
1✔
749
                for (LocationAndStock locationAndStock : locationAndStocks) {
1✔
750
                        PhysicalLocationAndStocks convertedLocation = convertToPhysicalLocationAndStock(locationAndStock);
1✔
751
                        if (convertedLocation != null) {
1✔
752
                                convertedLocations.add(convertedLocation);
1✔
753
                        }
754
                }
1✔
755

756
                return convertedLocations;
1✔
757
        }
758

759
        @Override
760
        public List<PhysicalLocationAndStocks> findLocationAndStocksByJurisdiction(String parentId, Map<String, String> properties,
761
                        boolean returnGeometry, int limit) {
762
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
763
                if (StringUtils.isNotBlank(parentId)) {
1✔
764
                        structureMetadataExample.createCriteria().andParentIdEqualTo(parentId);
1✔
765
                }
766
                StockMetadataExample stockMetadataExample = new StockMetadataExample();
1✔
767
                stockMetadataExample.createCriteria().andDateDeletedIsNull();
1✔
768
                return convertToPhysicalLocationAndStock(structureMetadataMapper.findStructureAndStocksByJurisdiction(structureMetadataExample,
1✔
769
                                stockMetadataExample,null,
770
                    returnGeometry, 0, limit));
771
        }
772

773
        private PhysicalLocationAndStocks convertToPhysicalLocationAndStock(LocationAndStock entity) {
774
                if (entity == null || entity.getJson() == null || !(entity.getJson() instanceof PhysicalLocation)) {
1✔
775
                        return null;
×
776
                }
777

778
                PhysicalLocationAndStocks location = (PhysicalLocationAndStocks) entity.getJson();
1✔
779
                location.setJurisdiction(false);
1✔
780
                List<org.smartregister.domain.Stock> stocks = new ArrayList<>();
1✔
781
                for(Stock stock: entity.getStocks()){
1✔
782
                        if (stock != null && stock.getJson() != null && (stock.getJson() instanceof org.smartregister.domain.Stock)) {
1✔
783
                                stocks.add((org.smartregister.domain.Stock) stock.getJson());
1✔
784
                        }
785
                }
1✔
786
                location.setStocks(stocks);
1✔
787
                return location;
1✔
788
        }
789

790
        @Override
791
        protected Long retrievePrimaryKey(PhysicalLocation entity) {
792
                Object uniqueId = getUniqueField(entity);
1✔
793
                if (uniqueId == null) {
1✔
794
                        return null;
1✔
795
                }
796
                
797
                String identifier = uniqueId.toString();
1✔
798
                return retrievePrimaryKey(identifier, entity.isJurisdiction());
1✔
799
        }
800
        
801
        @Override
802
        public Long retrievePrimaryKey(String identifier, boolean isJurisdiction) {
803
                
804
                if (isJurisdiction) {
1✔
805
                        Location pgEntity = locationMetadataMapper.findById(identifier, true, false);
1✔
806
                        if (pgEntity == null) {
1✔
807
                                return null;
1✔
808
                        }
809
                        return pgEntity.getId();
1✔
810
                } else {
811
                        Structure pgEntity = structureMetadataMapper.findById(identifier, true);
1✔
812
                        if (pgEntity == null) {
1✔
813
                                return null;
1✔
814
                        }
815
                        return pgEntity.getId();
1✔
816
                }
817
        }
818
        
819
        @Override
820
        public PhysicalLocation get(String id, boolean returnGeometry, int version) {
821
                return convert(locationMetadataMapper.findByIdAndVersion(id, true, version));
1✔
822
        }
823
        
824
        @Override
825
        protected Object getUniqueField(PhysicalLocation entity) {
826
                if (entity == null) {
1✔
827
                        return null;
×
828
                }
829
                return entity.getId();
1✔
830
        }
831
        
832
        private PhysicalLocation convert(Location entity) {
833
                if (entity == null || entity.getJson() == null || !(entity.getJson() instanceof PhysicalLocation)) {
1✔
834
                        return null;
1✔
835
                }
836
                
837
                PhysicalLocation location = (PhysicalLocation) entity.getJson();
1✔
838
                location.setJurisdiction(true);
1✔
839
                return location;
1✔
840
        }
841
        
842
        private PhysicalLocation convert(Structure entity) {
843
                if (entity == null || entity.getJson() == null || !(entity.getJson() instanceof PhysicalLocation)) {
1✔
844
                        return null;
1✔
845
                }
846
                return (PhysicalLocation) entity.getJson();
1✔
847
        }
848
        
849
        private List<PhysicalLocation> convert(List<Location> locations) {
850
                if (locations == null || locations.isEmpty()) {
1✔
851
                        return new ArrayList<>();
1✔
852
                }
853
                
854
                List<PhysicalLocation> convertedLocations = new ArrayList<>();
1✔
855
                for (Location location : locations) {
1✔
856
                        PhysicalLocation convertedLocation = convert(location);
1✔
857
                        if (convertedLocation != null) {
1✔
858
                                convertedLocations.add(convertedLocation);
1✔
859
                        }
860
                }
1✔
861
                
862
                return convertedLocations;
1✔
863
        }
864
        
865
        private List<PhysicalLocation> convertStructures(List<Structure> structures) {
866
                if (structures == null || structures.isEmpty()) {
1✔
867
                        return new ArrayList<>();
1✔
868
                }
869
                
870
                List<PhysicalLocation> convertedStructures = new ArrayList<>();
1✔
871
                for (Structure structure : structures) {
1✔
872
                        PhysicalLocation convertedStructure = convert(structure);
1✔
873
                        if (convertedStructure != null) {
1✔
874
                                convertedStructures.add(convertedStructure);
1✔
875
                        }
876
                }
1✔
877
                
878
                return convertedStructures;
1✔
879
        }
880
        
881
        private Location convert(PhysicalLocation physicalLocation, Long primaryKey) {
882
                if (physicalLocation == null) {
1✔
883
                        return null;
×
884
                }
885
                
886
                Location pgLocation = new Location();
1✔
887
                pgLocation.setId(primaryKey);
1✔
888
                pgLocation.setJson(physicalLocation);
1✔
889
                
890
                return pgLocation;
1✔
891
        }
892
        
893
        private Structure convertStructure(PhysicalLocation physicalLocation, Long primaryKey) {
894
                if (physicalLocation == null) {
1✔
895
                        return null;
×
896
                }
897
                
898
                Structure pgStructure = new Structure();
1✔
899
                pgStructure.setId(primaryKey);
1✔
900
                pgStructure.setJson(physicalLocation);
1✔
901
                
902
                return pgStructure;
1✔
903
        }
904
        
905
        private LocationMetadata createMetadata(PhysicalLocation entity, Long id) {
906
                LocationMetadata locationMetadata = new LocationMetadata();
1✔
907
                locationMetadata.setLocationId(id);
1✔
908
                locationMetadata.setGeojsonId(entity.getId());
1✔
909
                if (entity.getProperties() != null) {
1✔
910
                        locationMetadata.setParentId(entity.getProperties().getParentId());
1✔
911
                        locationMetadata.setUuid(entity.getProperties().getUid());
1✔
912
                        locationMetadata.setType(entity.getProperties().getType());
1✔
913
                        locationMetadata.setName(entity.getProperties().getName());
1✔
914
                        if (entity.getProperties().getStatus() != null) {
1✔
915
                                locationMetadata.setStatus(entity.getProperties().getStatus().name());
1✔
916
                        }
917
                        locationMetadata.setVersion(entity.getProperties().getVersion());
1✔
918
                }
919
                locationMetadata.setServerVersion(entity.getServerVersion());
1✔
920

921
                if(id != null){
1✔
922
                        locationMetadata.setDateEdited(new Date());
1✔
923
                }
924
                return locationMetadata;
1✔
925
        }
926
        
927
        private StructureMetadata createStructureMetadata(PhysicalLocation entity, Long id) {
928
                StructureMetadata structureMetadata = new StructureMetadata();
1✔
929
                structureMetadata.setStructureId(id);
1✔
930
                structureMetadata.setGeojsonId(entity.getId());
1✔
931
                if (entity.getProperties() != null) {
1✔
932
                        structureMetadata.setParentId(entity.getProperties().getParentId());
1✔
933
                        structureMetadata.setUuid(entity.getProperties().getUid());
1✔
934
                        structureMetadata.setType(entity.getProperties().getType());
1✔
935
                        structureMetadata.setName(entity.getProperties().getName());
1✔
936
                        if (entity.getProperties().getStatus() != null) {
1✔
937
                                structureMetadata.setStatus(entity.getProperties().getStatus().name());
1✔
938
                        }
939
                }
940
                structureMetadata.setServerVersion(entity.getServerVersion());
1✔
941

942
                if(id != null){
1✔
943
                        structureMetadata.setDateEdited(new Date());
1✔
944
                }
945
                return structureMetadata;
1✔
946
        }
947
        
948
        private void saveLocationTag(PhysicalLocation physicalLocation, Long locationId, boolean isUpdate) {
949
                Set<LocationTag> locationTagMaps = physicalLocation.getLocationTags();
1✔
950
                
951
                if (isUpdate) {
1✔
952
                        locationTagService.deleteLocationTagMapByLocationId(locationId);
1✔
953
                }
954
                if (locationTagMaps != null) {
1✔
955
                        for (LocationTag locationTag : locationTagMaps) {
1✔
956
                                
957
                                LocationTagMap locationTagMap = new LocationTagMap();
1✔
958
                                locationTagMap.setLocationId(locationId);
1✔
959
                                locationTagMap.setLocationTagId(locationTag.getId());
1✔
960
                                locationTagService.addLocationTagMap(locationTagMap);
1✔
961
                                
962
                        }
1✔
963
                }
964
        }
1✔
965
        
966
        @Override
967
        public List<PhysicalLocation> searchLocations(LocationSearchBean locationSearchBean) {
968
                Integer offset = 0;
1✔
969
                if (locationSearchBean.getPageSize() == null || locationSearchBean.getPageSize() == 0) {
1✔
970
                        return convert(locationMetadataMapper.selectLocations(locationSearchBean, null, null));
1✔
971
                        
972
                } else if (locationSearchBean.getPageNumber() != null && locationSearchBean.getPageNumber() == 0) {
1✔
973
                        throw new IllegalArgumentException("pageNumber should be greater than 0");
1✔
974
                        
975
                } else if (locationSearchBean.getPageNumber() != null) {
1✔
976
                        
977
                        offset = locationSearchBean.getPageSize() * (locationSearchBean.getPageNumber() - 1);
1✔
978
                }
979
                return convert(locationMetadataMapper.selectLocations(locationSearchBean, offset, locationSearchBean.getPageSize()));
1✔
980
        }
981
        
982
        @Override
983
        public int countSearchLocations(LocationSearchBean locationSearchBean) {
984
                return locationMetadataMapper.selectCountLocations(locationSearchBean);
1✔
985
        }
986
        
987
        /**
988
         * {@inheritDoc}
989
         */
990
        @Override
991
        public Set<LocationDetail> findLocationWithDescendants(String locationId, boolean returnTags) {
992
                return locationMetadataMapper.selectLocationWithDescendants(locationId, returnTags);
1✔
993
        }
994
        
995
        /**
996
         * {@inheritDoc}
997
         */
998
        @Override
999
        public List<StructureCount> findStructureCountsForLocation(Set<String> locationIds) {
1000
                return structureMetadataMapper.findStructureCountsForLocation(locationIds);
1✔
1001
        }
1002
        
1003
        @Override
1004
        public List<com.ibm.fhir.model.resource.Location> findJurisdictionsById(String id) {
1005
                PhysicalLocation location = get(id, false, false);
×
1006
                return location == null ? Collections.emptyList() : convertToFHIRLocation(Collections.singletonList(location));
×
1007
        }
1008
        
1009
        @Override
1010
        public List<com.ibm.fhir.model.resource.Location> findLocationsById(String id) {
1011
                PhysicalLocation location=getStructure(id, false);
×
1012
                return location == null ? Collections.emptyList() :convertToFHIRLocation(Collections.singletonList(location));
×
1013
        }
1014
        
1015
        @Override
1016
        public List<com.ibm.fhir.model.resource.Location> findLocationByJurisdiction(String jurisdiction) {
UNCOV
1017
                return convertToFHIRLocation(findStructuresByProperties(true, jurisdiction, null, Integer.MAX_VALUE));
×
1018
        }
1019
        
1020
        @Override
1021
        public List<String> findChildLocationByJurisdiction(String id) {
1022
                return locationMetadataMapper.selectChildrenIds(id);
1✔
1023
        }
1024
        
1025
        private List<com.ibm.fhir.model.resource.Location> convertToFHIRLocation(List<PhysicalLocation> locations) {
UNCOV
1026
                return locations.stream().map(location -> LocationConverter.convertPhysicalLocationToLocationResource(location))
×
UNCOV
1027
                        .collect(Collectors.toList());
×
1028
        }
1029

1030
        @Override
1031
        public long countLocationsByProperties(List<String> parentIds, Map<String, String> properties) {
1032
                LocationMetadataExample locationMetadataExample = new LocationMetadataExample();
1✔
1033
                if (parentIds != null && !parentIds.isEmpty()) {
1✔
1034
                        locationMetadataExample.createCriteria().andParentIdIn(parentIds);
1✔
1035
                }
1036
                return locationMetadataMapper.countManyByProperties(locationMetadataExample, properties);
1✔
1037
        }
1038

1039
        /**
1040
         * {@inheritDoc}
1041
         */
1042
        @Override
1043
        public long countStructuresByProperties(List<String> parentIds, Map<String, String> properties) {
1044
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
1045
                if (parentIds != null && !parentIds.isEmpty()) {
1✔
1046
                        structureMetadataExample.createCriteria().andParentIdIn(parentIds);
1✔
1047
                }
1048
                return structureMetadataMapper.countManyByProperties(structureMetadataExample, properties);
1✔
1049
        }
1050

1051

1052
        /**
1053
         * {@inheritDoc}
1054
         */
1055
        @Override
1056
        public List<String> findStructureIdsByProperties(List<String> parentIds, Map<String, String> properties, int limit) {
1057
                int fetchLimit = limit > 0 ? limit : DEFAULT_FETCH_SIZE;
1✔
1058
                StructureMetadataExample structureMetadataExample = new StructureMetadataExample();
1✔
1059
                if (parentIds != null && !parentIds.isEmpty()) {
1✔
1060
                        structureMetadataExample.createCriteria().andParentIdIn(parentIds);
1✔
1061
                }
1062
                List<String> structureIds = structureMetadataMapper.selectManyIdsByProperties(structureMetadataExample, properties,
1✔
1063
                                 0, fetchLimit);
1064
                return structureIds;
1✔
1065
        }
1066
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc