-
Notifications
You must be signed in to change notification settings - Fork 1
/
EntityIndexerService.java
1265 lines (1180 loc) · 66.8 KB
/
EntityIndexerService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2017-2022 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
*
* This file is part of REGARDS.
*
* REGARDS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* REGARDS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with REGARDS. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.cnes.regards.modules.crawler.service;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import fr.cnes.regards.framework.geojson.GeoJsonType;
import fr.cnes.regards.framework.geojson.geometry.IGeometry;
import fr.cnes.regards.framework.jpa.multitenant.transactional.MultitenantTransactional;
import fr.cnes.regards.framework.module.rest.exception.EntityInvalidException;
import fr.cnes.regards.framework.module.rest.exception.ModuleException;
import fr.cnes.regards.framework.modules.plugins.service.IPluginService;
import fr.cnes.regards.framework.modules.session.commons.dao.ISessionStepLight;
import fr.cnes.regards.framework.modules.session.commons.dao.ISessionStepRepository;
import fr.cnes.regards.framework.multitenant.IRuntimeTenantResolver;
import fr.cnes.regards.framework.notification.NotificationLevel;
import fr.cnes.regards.framework.notification.client.INotificationClient;
import fr.cnes.regards.framework.security.role.DefaultRole;
import fr.cnes.regards.framework.urn.EntityType;
import fr.cnes.regards.framework.urn.UniformResourceName;
import fr.cnes.regards.framework.utils.RsRuntimeException;
import fr.cnes.regards.framework.utils.plugins.exception.NotAvailablePluginConfigurationException;
import fr.cnes.regards.modules.crawler.dao.IDatasourceIngestionRepository;
import fr.cnes.regards.modules.crawler.domain.DatasourceIngestion;
import fr.cnes.regards.modules.crawler.service.consumer.*;
import fr.cnes.regards.modules.crawler.service.event.DataSourceMessageEvent;
import fr.cnes.regards.modules.crawler.service.session.SessionNotifier;
import fr.cnes.regards.modules.dam.domain.dataaccess.accessright.AccessLevel;
import fr.cnes.regards.modules.dam.domain.dataaccess.accessright.plugins.IDataObjectAccessFilterPlugin;
import fr.cnes.regards.modules.dam.domain.entities.AbstractEntity;
import fr.cnes.regards.modules.dam.domain.entities.DataObject;
import fr.cnes.regards.modules.dam.domain.entities.Dataset;
import fr.cnes.regards.modules.dam.domain.entities.feature.DataObjectFeature;
import fr.cnes.regards.modules.dam.domain.entities.metadata.DatasetMetadata.DataObjectGroup;
import fr.cnes.regards.modules.dam.service.dataaccess.IAccessRightService;
import fr.cnes.regards.modules.dam.service.entities.DataObjectService;
import fr.cnes.regards.modules.dam.service.entities.ICollectionService;
import fr.cnes.regards.modules.dam.service.entities.IDatasetService;
import fr.cnes.regards.modules.dam.service.entities.IEntitiesService;
import fr.cnes.regards.modules.dam.service.entities.visitor.AttributeBuilderVisitor;
import fr.cnes.regards.modules.indexer.dao.BulkSaveResult;
import fr.cnes.regards.modules.indexer.dao.IEsRepository;
import fr.cnes.regards.modules.indexer.dao.spatial.GeoHelper;
import fr.cnes.regards.modules.indexer.dao.spatial.ProjectGeoSettings;
import fr.cnes.regards.modules.indexer.domain.SimpleSearchKey;
import fr.cnes.regards.modules.indexer.domain.builders.GeoPointBuilder;
import fr.cnes.regards.modules.indexer.domain.criterion.ICriterion;
import fr.cnes.regards.modules.indexer.domain.criterion.StringMatchType;
import fr.cnes.regards.modules.indexer.domain.spatial.Crs;
import fr.cnes.regards.modules.model.domain.IComputedAttribute;
import fr.cnes.regards.modules.model.dto.properties.IProperty;
import fr.cnes.regards.modules.model.dto.properties.ObjectProperty;
import fr.cnes.regards.modules.model.service.validation.ValidationMode;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.geo.GeoPoint;
import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
import org.locationtech.spatial4j.context.jts.JtsSpatialContextFactory;
import org.locationtech.spatial4j.exception.InvalidShapeException;
import org.locationtech.spatial4j.io.GeoJSONReader;
import org.locationtech.spatial4j.shape.Shape;
import org.locationtech.spatial4j.shape.jts.JtsShapeFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.MapBindingResult;
import org.springframework.validation.ObjectError;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.IOException;
import java.text.ParseException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author oroussel
*/
@Service
public class EntityIndexerService implements IEntityIndexerService {
private static final Logger LOGGER = LoggerFactory.getLogger(EntityIndexerService.class);
private static final DateTimeFormatter ISO_TIME_UTC = new DateTimeFormatterBuilder().parseCaseInsensitive()
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter();
/**
* Current tenant resolver
*/
@Autowired
protected IRuntimeTenantResolver runtimeTenantResolver;
@Autowired
protected IEsRepository esRepos;
@Autowired
protected IPluginService pluginService;
@Autowired
private IEntitiesService entitiesService;
@Autowired
private IAccessRightService accessRightService;
@PersistenceContext
private EntityManager em;
@Autowired
private DataObjectService dataObjectService;
@Autowired
private IDatasetService datasetService;
@Autowired
private ICollectionService collectionService;
@Autowired
@Lazy
private IEntityIndexerService self;
/**
* The spring application name ~= microservice type
*/
@Value("${spring.application.name}")
private String applicationName;
@Autowired
private INotificationClient notifClient;
@Autowired
private ApplicationEventPublisher eventPublisher;
@Autowired
private IDatasourceIngestionRepository dsIngestionRepository;
@Autowired
private ProjectGeoSettings projectGeoSettings;
@Autowired
private SessionNotifier sessionNotifier;
@Value("${regards.crawler.max.bulk.size:10000}")
private Integer maxBulkSize;
@Value("${regards.crawler.max.session.step.size:10000}")
private int sessionStepBulkSize;
@Autowired
private Gson gson;
@Autowired
private ISessionStepRepository sessionStepRepository;
@Autowired
private IndexService indexService;
private static List<String> toErrors(Errors errorsObject) {
List<String> errors = new ArrayList<>(errorsObject.getErrorCount());
for (ObjectError objError : errorsObject.getAllErrors()) {
if (objError instanceof FieldError) {
StringBuilder buf = new StringBuilder();
buf.append("Field error in object ").append(objError.getObjectName());
buf.append(" on field '").append(((FieldError) objError).getField());
buf.append("', rejected value '").append(((FieldError) objError).getRejectedValue());
buf.append("': ").append(((FieldError) objError).getField());
buf.append(" ").append(objError.getDefaultMessage());
errors.add(buf.toString());
} else {
errors.add(objError.toString());
}
}
return errors;
}
/**
* Load given entity from database and update Elasticsearch
*
* @param tenant concerned tenant (also index intoES)
* @param ipId concerned entity IpId
* @param lastUpdateDate for dataset entity, if this date is provided, only more recent data objects must be taken
* into account
* @param forceAssociatedEntitiesUpdate for dataset entity, force associated entities (ie data objects) update
* @param dsiId DataSourceIngestion identifier
*/
@Override
public void updateEntityIntoEs(String tenant,
UniformResourceName ipId,
OffsetDateTime lastUpdateDate,
OffsetDateTime updateDate,
boolean forceAssociatedEntitiesUpdate,
String dsiId) throws ModuleException {
LOGGER.info("Updating {}", ipId.toString());
runtimeTenantResolver.forceTenant(tenant);
AbstractEntity<?> entity = entitiesService.loadWithRelations(ipId);
// If entity does no more exist in database, it must be deleted from ES
if (entity == null) {
LOGGER.debug("Entity is null !!");
if (ipId.getEntityType() == EntityType.DATASET) {
sendDataSourceMessage(String.format(" Dataset with IP_ID %s no more exists...", ipId.toString()),
dsiId);
manageDatasetDelete(tenant, ipId.toString(), dsiId);
}
esRepos.delete(tenant, ipId.getEntityType().toString(), ipId.toString());
sendDataSourceMessage(String.format(" ...Dataset with IP_ID %s de-indexed.", ipId.toString()), dsiId);
} else { // entity has been created or updated, it must be saved into ES
indexService.createIndexIfNeeded(tenant);
indexService.configureMappings(tenant, entity.getModel().getName());
ICriterion savedSubsettingClause = null;
// Remove parameters of dataset datasource to avoid expose security values
if (entity instanceof Dataset) {
Dataset dataset = (Dataset) entity;
savedSubsettingClause = dataset.getSubsettingClause();
prepareDatasetForEs(dataset);
// update dataset groups
for (Entry<String, DataObjectGroup> entry : dataset.getMetadata()
.getDataObjectsGroupsMap()
.entrySet()) {
// remove group if no access
if (!entry.getValue().getDatasetAccess()) {
dataset.getGroups().remove(entry.getKey());
} else { // add (or let) group if FULL_ACCESS or RESTRICTED_ACCESS
dataset.getGroups().add(entry.getKey());
}
}
}
// Lets handle virtual_id here
if (entity.isLast()) {
entity.setVirtualId();
} else {
entity.removeVirtualId();
}
// Then save entity
LOGGER.debug("Saving entity {}", entity);
// If lastUpdateDate is provided, this means that update comes from an ingestion, in this case all data
// objects must be updated.
// If lastUpdatedDate isn't provided it means update come from a change into dataset
// (cf. DatasetCrawlerService.handle(...)) and so it is necessary to check if differences exist between
// previous version of dataset and current one.
// It may also mean that it comes from a first ingestion. In this case, lastUpdateDate is null but all
// data objects must be updated
boolean needAssociatedDataObjectsUpdate = (lastUpdateDate != null) || forceAssociatedEntitiesUpdate;
// A dataset change may need associated data objects update
if (!needAssociatedDataObjectsUpdate && (entity instanceof Dataset)) {
Dataset dataset = (Dataset) entity;
needAssociatedDataObjectsUpdate |= needAssociatedDataObjectsUpdate(dataset,
esRepos.get(Optional.of(tenant),
dataset));
}
boolean created = esRepos.save(tenant, entity);
LOGGER.debug("Elasticsearch saving result : {}", created);
if ((entity instanceof Dataset) && needAssociatedDataObjectsUpdate) {
// Subsetting clause is needed by many things
((Dataset) entity).setSubsettingClause(savedSubsettingClause);
manageDatasetUpdate((Dataset) entity, lastUpdateDate, updateDate, dsiId);
}
}
LOGGER.info(ipId.toString() + " managed into Elasticsearch");
}
private void prepareDatasetForEs(Dataset dataset) throws ModuleException {
// entity must be detached else Hibernate tries to commit update (datasource is cascade.DETACHED)
em.detach(dataset);
if (dataset.getDataSource() != null) {
dataset.getDataSource().getParameters().clear();
}
// Subsetting clause must not be jsonify into Elasticsearch
dataset.setSubsettingClause(null);
// Retrieve dataset metadata information for indexer
dataset.setMetadata(accessRightService.retrieveDatasetMetadata(dataset.getIpId()));
}
/**
* Compare new dataset and current indexed one and determine if an update of all associated data objects is needed
* or no
*/
private boolean needAssociatedDataObjectsUpdate(Dataset newDataset, Dataset curDataset) {
if (curDataset == null) {
return true;
}
if (newDataset == null) {
return false;
}
boolean need = false;
if (newDataset.getOpenSearchSubsettingClause() != null) {
need = !newDataset.getOpenSearchSubsettingClause().equals(curDataset.getOpenSearchSubsettingClause());
} else if (curDataset.getOpenSearchSubsettingClause() != null) {
need = true;
}
Map<String, DataObjectGroup> curentMetadata = curDataset.getMetadata() != null ?
curDataset.getMetadata().getDataObjectsGroupsMap() :
null;
Map<String, DataObjectGroup> newMetadata = newDataset.getMetadata() != null ?
newDataset.getMetadata().getDataObjectsGroupsMap() :
null;
if (curentMetadata != null) {
need = need || !curentMetadata.equals(newMetadata);
} else {
need = true;
}
return need;
}
/**
* Search and update associated dataset data objects (ie remove dataset IpId from tags)
*
* @param tenant concerned tenant
* @param ipId dataset identifier
*/
private void manageDatasetDelete(String tenant, String ipId, String dsiId) throws ModuleException {
// Search all DataObjects tagging this Dataset (only DataObjects because all other entities are already managed
// with the system Postgres/RabbitMQ)
sendDataSourceMessage(String.format(" Searching for all data objects tagging dataset IP_ID %s", ipId),
dsiId);
AtomicInteger objectsCount = new AtomicInteger(0);
ICriterion taggingObjectsCrit = ICriterion.eq("tags", ipId, StringMatchType.KEYWORD);
Set<DataObject> toSaveObjects = new HashSet<>();
OffsetDateTime updateDate = OffsetDateTime.now().withOffsetSameInstant(ZoneOffset.UTC);
// Function to update an object (tags, groups, lastUpdate, ...)
Consumer<DataObject> updateDataObject = object -> {
object.removeTags(Arrays.asList(ipId));
// reset datasetModelIds
object.getDatasetModelNames().clear();
// Remove dataset ipId from metadata.groups dans modelNames
object.getMetadata().removeDatasetIpId(ipId);
// update groups
object.setGroups(object.getMetadata().getGroups());
// update modelNames
object.setDatasetModelNames(object.getMetadata().getModelNames());
object.setLastUpdate(updateDate);
toSaveObjects.add(object);
if (toSaveObjects.size() == maxBulkSize) {
try {
esRepos.saveBulk(tenant, toSaveObjects);
objectsCount.addAndGet(toSaveObjects.size());
toSaveObjects.clear();
} catch (ElasticsearchException e) {
LOGGER.error(e.getMessage(), e);
}
}
};
// Apply updateTag function to all tagging objects
SimpleSearchKey<DataObject> searchKey = new SimpleSearchKey<>(EntityType.DATA.toString(), DataObject.class);
addProjectInfos(tenant, searchKey);
try {
esRepos.searchAll(searchKey, updateDataObject, taggingObjectsCrit);
// Bulk save remaining objects to save
if (!toSaveObjects.isEmpty()) {
esRepos.saveBulk(tenant, toSaveObjects);
objectsCount.addAndGet(toSaveObjects.size());
}
sendDataSourceMessage(String.format(" ...Removed dataset IP_ID from %d data objects tags.",
objectsCount.get()), dsiId);
} catch (ElasticsearchException e) {
throw new ModuleException(e);
}
}
/**
* Search and update associated dataset data objects (ie add dataset IpId into tags)
*
* @param dataset concerned dataset
*/
private void manageDatasetUpdate(Dataset dataset,
OffsetDateTime lastUpdateDate,
OffsetDateTime updateDate,
String dsiId) throws ModuleException {
String tenant = runtimeTenantResolver.getTenant();
sendDataSourceMessage(String.format(
" Updating dataset %s indexation and all its associated data objects...",
dataset.getLabel()), dsiId);
sendDataSourceMessage(String.format(" Searching for dataset %s associated data objects...",
dataset.getLabel()), dsiId);
SimpleSearchKey<DataObject> searchKey = new SimpleSearchKey<>(EntityType.DATA.toString(), DataObject.class);
addProjectInfos(tenant, searchKey);
ExecutorService executor = Executors.newFixedThreadPool(1);
// Create a callable which bulk save into ES a set of data objects
SaveDataObjectsCallable saveDataObjectsCallable = new SaveDataObjectsCallable(runtimeTenantResolver,
esRepos,
tenant,
dataset.getId());
// Remove association between dataobjects and dataset for all dataobjects which does not match the dataset filter anymore.
try {
removeOldDatasetDataObjectsAssoc(dataset, updateDate, searchKey, executor, saveDataObjectsCallable, dsiId);
} catch (ModuleException e) {
LOGGER.error(e.getMessage(), e);
sendDataSourceMessage(String.format("Error removing all dataset objects. Cause: %s.", e.getMessage()),
dsiId);
}
// Associate dataset to all dataobjets. Associate groups of dataset to the dataobjets through metadata
try {
addOrUpdateDatasetDataObjectsAssoc(dataset,
lastUpdateDate,
updateDate,
searchKey,
executor,
saveDataObjectsCallable,
dsiId);
} catch (ModuleException e) {
LOGGER.error(e.getMessage(), e);
sendDataSourceMessage(String.format("Error updating new dataset objects. Cause: %s.", e.getMessage()),
dsiId);
}
// Update dataset access groups for dynamic plugin access rights
try {
manageDatasetUpdateFilteredAccessrights(tenant,
dataset,
updateDate,
executor,
saveDataObjectsCallable,
dsiId);
} catch (ModuleException e) {
LOGGER.error(e.getMessage(), e);
sendDataSourceMessage(String.format("Error updating dataset access rights. Cause: %s.", e.getMessage()),
dsiId);
}
// To remove thread used by executor
executor.shutdown();
computeComputedAttributes(dataset, dsiId, tenant);
prepareDatasetForEs(dataset);
esRepos.save(tenant, dataset);
LOGGER.info("Dataset {} updated", dataset.getId());
sendDataSourceMessage(" ...Dataset indexation updated.", dsiId);
}
private void addProjectInfos(String tenant, SimpleSearchKey<DataObject> searchKey) {
searchKey.setSearchIndex(tenant);
searchKey.setCrs(projectGeoSettings.getCrs());
}
/**
* Handle Access rights filter for the given dataset. An Access right filter is an accessRight with a {@link IDataObjectAccessFilterPlugin}.
*/
private void manageDatasetUpdateFilteredAccessrights(String tenant,
Dataset dataset,
OffsetDateTime updateDate,
ExecutorService executor,
SaveDataObjectsCallable saveDataObjectsCallable,
String dsiId) throws ModuleException {
SimpleSearchKey<DataObject> searchKey = new SimpleSearchKey<>(EntityType.DATA.toString(), DataObject.class);
addProjectInfos(tenant, searchKey);
// handle association between dataobjects and groups for all access rights set by plugin
for (DataObjectGroup group : dataset.getMetadata().getDataObjectsGroupsMap().values()) {
// If access to the dataset is allowed and a plugin access filter is set on dataobject metadata, calculate which dataObjects are in the given group
if (group.getDatasetAccess() && (group.getMetaDataObjectAccessFilterPluginBusinessId() != null)) {
try {
IDataObjectAccessFilterPlugin plugin = pluginService.getPlugin(group.getMetaDataObjectAccessFilterPluginBusinessId());
ICriterion searchFilter = plugin.getSearchFilter();
if (searchFilter != null) {
removeOldDataObjectsGroupAssoc(dataset,
updateDate,
searchKey,
executor,
saveDataObjectsCallable,
dsiId,
group.getGroupName(),
searchFilter);
// Handle specific dataobjet groups by access filter plugin
addOrUpdateDataObectGroupAssoc(dataset,
updateDate,
searchKey,
executor,
saveDataObjectsCallable,
dsiId,
group.getGroupName(),
searchFilter);
}
} catch (ModuleException | NotAvailablePluginConfigurationException e) {
// Plugin conf doesn't exists anymore, so remove all group assoc
removeOldDataObjectsGroupAssoc(dataset,
updateDate,
searchKey,
executor,
saveDataObjectsCallable,
dsiId,
group.getGroupName(),
ICriterion.all());
}
}
}
}
/**
* Manage computed attributes computation
*
* @param dataset concerned dataset
* @param dsiId can be null (in this case, no notification is sent)
*/
@Override
public void computeComputedAttributes(Dataset dataset, String dsiId, String tenant) {
// lets compute computed attributes from the dataset model
Set<IComputedAttribute<Dataset, ?>> computationPlugins = entitiesService.getComputationPlugins(dataset);
LOGGER.info("Starting parallel computing of {} attributes (dataset {})...",
computationPlugins.size(),
dataset.getId());
sendDataSourceMessage(String.format(" Starting computing of %d attributes...",
computationPlugins.size()), dsiId);
computationPlugins.parallelStream().forEach(p -> {
runtimeTenantResolver.forceTenant(tenant);
p.compute(dataset);
});
// Once computations has been done, associated attributes are created or updated
createComputedAttributes(dataset, computationPlugins);
List<IComputedAttribute<Dataset, ?>> ll = new ArrayList<>(computationPlugins);
ll.stream()
.forEach(comAtt -> LOGGER.info("attribute {} is computed", comAtt.getAttributeToCompute().getName()));
LOGGER.info("...computing OK");
sendDataSourceMessage(String.format(" ...Computing ended.", computationPlugins.size()), dsiId);
}
/**
* Associate all DATA entities matching the subsetting clause to the given DATASET entity and dataset groups.<br/>
* Only groups with no {@link AccessLevel#CUSTOM_ACCESS} are associated the the dataobjects in this method.<br/>
* The association is done by the {@link DataObjectUpdater} consumer.<br/>
* To handle the groups with {@link AccessLevel#CUSTOM_ACCESS} see {@link EntityIndexerService#addOrUpdateDataObectGroupAssoc}.<br/>
* <b>NOTE</b> : The subsetting clause to find DATA entities is computed by adding dataset subsetting clause and "lastUpdate > lastUpdateDate parameter".<br/>
*
* @param dataset {@link Dataset} to associate to DATA entities
* @param lastUpdateDate {@link OffsetDateTime}. If not null, add a datatime criterion in the subsesstin clause
* to find only DATA with a lastUpdateDate greter than this parameter
* @param updateDate {@link OffsetDateTime} of the current update process
* @param searchKey {@link SimpleSearchKey} used to run elasticsearch searh of DATA entities to update
* @param executor {@link ExecutorService}
* @param saveDataObjectsCallable {@link SaveDataObjectsCallable} used to save data
* @param dsiId {@link DatasourceIngestion} identifier
*/
private void addOrUpdateDatasetDataObjectsAssoc(Dataset dataset,
OffsetDateTime lastUpdateDate,
OffsetDateTime updateDate,
SimpleSearchKey<DataObject> searchKey,
ExecutorService executor,
SaveDataObjectsCallable saveDataObjectsCallable,
String dsiId) throws ModuleException {
// A set used to accumulate data objects to save into ES
HashSet<DataObject> toSaveObjects = new HashSet<>();
sendDataSourceMessage(" Adding or updating dataset data objects association...", dsiId);
// Create an updater to be executed on each data object of dataset subsetting criteria results
DataObjectUpdater dataObjectUpdater = new DataObjectUpdater(dataset,
updateDate,
toSaveObjects,
saveDataObjectsCallable,
executor,
maxBulkSize);
ICriterion subsettingCrit = dataset.getSubsettingClause();
// Add lastUpdate restriction if a date is provided
if (lastUpdateDate != null) {
subsettingCrit = ICriterion.and(subsettingCrit, ICriterion.gt(Dataset.LAST_UPDATE, lastUpdateDate));
}
try {
esRepos.searchAll(searchKey, dataObjectUpdater, subsettingCrit);
} catch (ElasticsearchException e) {
throw new ModuleException(e);
}
// Saving remaining objects...
dataObjectUpdater.finalSave();
sendDataSourceMessage(String.format(" ...%d data objects dataset association saved.",
dataObjectUpdater.getObjectsCount()), dsiId);
}
/**
* Associates all DATA entities matching the subsetting clause to the given DATASET groups with {@link AccessLevel#CUSTOM_ACCESS}.<br/>
* The association is done by the {@link DataObjectGroupAssocUpdater} consumer.<br/>
*
* @param dataset {@link Dataset} to associate to DATA entities
* @param updateDate {@link OffsetDateTime} of the current update process
* @param searchKey {@link SimpleSearchKey} used to run elasticsearch searh of DATA entities to update
* @param executor {@link ExecutorService}
* @param saveDataObjectsCallable {@link SaveDataObjectsCallable} used to save data
* @param dsiId {@link DatasourceIngestion} identifier
* @param groupName Name of the group to associate to DATA entities.
* @param groupSubsettingClause {@link ICriterion} group subsetting clause. Caculate by {@link IDataObjectAccessFilterPlugin} plugin.
*/
private void addOrUpdateDataObectGroupAssoc(Dataset dataset,
OffsetDateTime updateDate,
SimpleSearchKey<DataObject> searchKey,
ExecutorService executor,
SaveDataObjectsCallable saveDataObjectsCallable,
String dsiId,
String groupName,
ICriterion groupSubsettingClause) throws ModuleException {
// A set used to accumulate data objects to save into ES
HashSet<DataObject> toSaveObjects = new HashSet<>();
sendDataSourceMessage(String.format(" Adding or Updating data objects group <%s> association...",
groupName), dsiId);
ICriterion subsettingCrit = dataset.getSubsettingClause();
// First : Retrieve objects associated matching the groupSubsettingClause
subsettingCrit = ICriterion.and(subsettingCrit, groupSubsettingClause);
// For each objet remove group if not associated throught an other dataset
DataObjectGroupAssocUpdater dataObjectAssocUpdater = new DataObjectGroupAssocUpdater(dataset,
updateDate,
toSaveObjects,
saveDataObjectsCallable,
executor,
groupName,
maxBulkSize);
try {
esRepos.searchAll(searchKey, dataObjectAssocUpdater, subsettingCrit);
// Saving remaining objects...
dataObjectAssocUpdater.finalSave();
sendDataSourceMessage(String.format(" ...%d data objects group <%s> association saved.",
dataObjectAssocUpdater.getObjectsCount(),
groupName), dsiId);
} catch (ElasticsearchException e) {
throw new ModuleException(e);
}
}
/**
* Remove association between DATASET entity and DATA entities that are no more into the subsetting clause.<br/>
* Only groups with no {@link AccessLevel#CUSTOM_ACCESS} are dissociated the the dataobjects in this method.<br/>
* The dissociation is done by the {@link DataObjectAssocRemover} consumer.<br/>
* To handle the groups with {@link AccessLevel#CUSTOM_ACCESS} see {@link EntityIndexerService#removeOldDataObjectsGroupAssoc}.<br/>
*
* @param dataset {@link Dataset} to associate to DATA entities
* @param updateDate {@link OffsetDateTime} of the current update process
* @param searchKey {@link SimpleSearchKey} used to run elasticsearch searh of DATA entities to update
* @param executor {@link ExecutorService}
* @param saveDataObjectsCallable {@link SaveDataObjectsCallable} used to save data
* @param dsiId {@link DatasourceIngestion} identifier
*/
private void removeOldDatasetDataObjectsAssoc(Dataset dataset,
OffsetDateTime updateDate,
SimpleSearchKey<DataObject> searchKey,
ExecutorService executor,
SaveDataObjectsCallable saveDataObjectsCallable,
String dsiId) throws ModuleException {
// A set used to accumulate data objects to save into ES
HashSet<DataObject> toSaveObjects = new HashSet<>();
sendDataSourceMessage(" Removing old dataset data objects association...", dsiId);
// First : remove association between dataset and data objects for data objects that are no more associated to
// new subsetting clause so search data objects that are tagged with dataset IPID and with NOT(user subsetting
// clause)
ICriterion oldAssociatedObjectsCrit = ICriterion.and(ICriterion.eq("tags",
dataset.getIpId().toString(),
StringMatchType.KEYWORD),
ICriterion.not(dataset.getUserSubsettingClause()));
// Create a Consumer to be executed on each data object of dataset subsetting criteria results
DataObjectAssocRemover dataObjectAssocRemover = new DataObjectAssocRemover(dataset,
updateDate,
toSaveObjects,
saveDataObjectsCallable,
executor,
maxBulkSize);
try {
esRepos.searchAll(searchKey, dataObjectAssocRemover, oldAssociatedObjectsCrit);
// Saving remaining objects...
dataObjectAssocRemover.finalSave();
sendDataSourceMessage(String.format(" ...%d data objects dataset association removed.",
dataObjectAssocRemover.getObjectsCount()), dsiId);
} catch (ElasticsearchException e) {
throw new ModuleException(e);
}
}
/**
* Remove association between DATASET entity and DATA entities that are no more into the subsetting clause.<br/>
* Only groups with {@link AccessLevel#CUSTOM_ACCESS} are dissociated the the dataobjects in this method.<br/>
* The dissociation is done by the {@link DataObjectGroupAssocRemover} consumer.<br/>
* To handle the groups with {@link AccessLevel#CUSTOM_ACCESS} see {@link EntityIndexerService#removeOldDataObjectsGroupAssoc}.<br/>
*
* @param dataset {@link Dataset} to associate to DATA entities
* @param updateDate {@link OffsetDateTime} of the current update process
* @param searchKey {@link SimpleSearchKey} used to run elasticsearch searh of DATA entities to update
* @param executor {@link ExecutorService}
* @param saveDataObjectsCallable {@link SaveDataObjectsCallable} used to save data
* @param dsiId {@link DatasourceIngestion} identifier
*/
private void removeOldDataObjectsGroupAssoc(Dataset dataset,
OffsetDateTime updateDate,
SimpleSearchKey<DataObject> searchKey,
ExecutorService executor,
SaveDataObjectsCallable saveDataObjectsCallable,
String dsiId,
String groupName,
ICriterion groupSubsettingClause) throws ModuleException {
// A set used to accumulate data objects to save into ES
HashSet<DataObject> toSaveObjects = new HashSet<>();
sendDataSourceMessage(String.format(" Removing old data objects group <%s> association...", groupName),
dsiId);
// First : Retrieve objects associated to given group and not matching the groupSubsettingClause
ICriterion oldAssociatedObjectsCrit = ICriterion.and(ICriterion.eq("tags",
dataset.getIpId().toString(),
StringMatchType.KEYWORD),
ICriterion.contains("groups",
groupName,
StringMatchType.KEYWORD),
ICriterion.not(groupSubsettingClause));
// For each objet remove group if not associated throught an other dataset
DataObjectGroupAssocRemover dataObjectAssocRemover = new DataObjectGroupAssocRemover(dataset,
updateDate,
toSaveObjects,
saveDataObjectsCallable,
executor,
groupName,
maxBulkSize);
try {
esRepos.searchAll(searchKey, dataObjectAssocRemover, oldAssociatedObjectsCrit);
} catch (ElasticsearchException e) {
throw new ModuleException(e);
}
// Saving remaining objects...
dataObjectAssocRemover.finalSave();
sendDataSourceMessage(String.format(" ...%d data objects group <%s> association removed.",
dataObjectAssocRemover.getObjectsCount(),
groupName), dsiId);
}
/**
* Create computed attributes from computation
*/
private void createComputedAttributes(Dataset dataset, Set<IComputedAttribute<Dataset, ?>> computationPlugins) {
// for each computation plugin lets add the computed attribute
for (IComputedAttribute<Dataset, ?> plugin : computationPlugins) {
IProperty<?> attributeToAdd = plugin.accept(new AttributeBuilderVisitor());
if (attributeToAdd instanceof ObjectProperty) {
ObjectProperty attrInFragment = (ObjectProperty) attributeToAdd;
// the attribute is inside a fragment so lets find the right one to add the attribute inside it
Optional<IProperty<?>> candidate = dataset.getProperties()
.stream()
.filter(attr -> (attr instanceof ObjectProperty)
&& attr.getName()
.equals(attrInFragment.getName()))
.findFirst();
if (candidate.isPresent()) {
Set<IProperty<?>> properties = ((ObjectProperty) candidate.get()).getValue();
// the fragment is already here, lets remove the old properties if they exist
properties.removeAll(attrInFragment.getValue());
// and now set the new ones
properties.addAll(attrInFragment.getValue());
} else {
// the fragment is not here so lets create it by adding it
dataset.addProperty(attrInFragment);
}
} else {
// the attribute is not inside a fragment so lets remove the old one if it exists and add the new one to
// the root
dataset.removeProperty(attributeToAdd);
dataset.addProperty(attributeToAdd);
}
}
}
@Override
@MultitenantTransactional
public void updateDatasets(String tenant,
Collection<Dataset> datasets,
OffsetDateTime lastUpdateDate,
OffsetDateTime updateDate,
boolean forceDataObjectsUpdate,
String dsiId) throws ModuleException {
for (Dataset dataset : datasets) {
LOGGER.info("Updating dataset {} ...", dataset.getLabel());
sendDataSourceMessage(String.format(" Updating dataset %s...", dataset.getLabel()), dsiId);
updateEntityIntoEs(tenant, dataset.getIpId(), lastUpdateDate, updateDate, forceDataObjectsUpdate, dsiId);
sendDataSourceMessage(String.format(" ...Dataset %s updated.", dataset.getLabel()), dsiId);
LOGGER.info("Dataset {} updated.", dataset.getLabel());
}
}
@Override
public void createNotificationForAdmin(String tenant, String title, String message, NotificationLevel level) {
notifClient.notify(message, title, level, DefaultRole.PROJECT_ADMIN);
}
@Override
public void deleteIndexNRecreateEntities(String tenant) throws ModuleException {
//1. Delete existing index
indexService.deleteIndex(tenant);
// get all sessions to notify
Pageable pageToRequest = PageRequest.of(0, sessionStepBulkSize, Sort.by(Sort.Order.asc("source")));
Page<ISessionStepLight> pageSessionStep;
do {
pageSessionStep = this.sessionStepRepository.findBy(pageToRequest);
sessionNotifier.notifyGlobalIndexDeletion(pageSessionStep.getContent());
pageToRequest = pageSessionStep.nextPageable();
} while (pageSessionStep.hasNext());
//2. Then re-create all entities
indexService.createIndexIfNeeded(tenant);
OffsetDateTime updateDate = OffsetDateTime.now();
updateAllDatasets(tenant, updateDate);
updateAllCollections(tenant, updateDate);
}
/**
* Validate given DataObject. If no error, add it to given set else log validation errors
*/
private void validateDataObject(Set<DataObject> toSaveObjects,
DataObject dataObject,
BulkSaveResult bulkSaveResult,
StringBuilder buf,
Long datasourceId) {
Errors errorsObject = new MapBindingResult(new HashMap<>(), dataObject.getIpId().toString());
List<String> errors = null;
// If some validation errors occur, don't index data object
try {
dataObjectService.validate(dataObject, errorsObject, ValidationMode.CREATION);
} catch (EntityInvalidException e) {
// If such an exception has been thrown, it contains all errors ( as a List<String>) else errors are
// described into errorsObject
errors = e.getMessages();
}
// No exception thrown but still validation errors
if ((errors == null) && errorsObject.hasErrors()) {
errors = toErrors(errorsObject);
}
// No error => dataObject is valid
if (errors == null) {
// Check if there is no error already existing on that dataObject (ie from geo manipulation)
boolean noExistingError = bulkSaveResult.getInErrorDocCause(dataObject.getDocId()) == null;
if (noExistingError) {
toSaveObjects.add(dataObject);
}
} else {
// Validation error
StringBuilder dataObjectBuffer = new StringBuilder("Data object with id '");
dataObjectBuffer.append(dataObject.getDocId()).append("' not indexed due to ");
dataObjectBuffer.append(errors.size()).append(" validation error(s):\n");
dataObjectBuffer.append(errors.stream().collect(Collectors.joining("\n")));
String msg = dataObjectBuffer.toString();
// Log error msg
LOGGER.warn(msg);
// Append error msg to buffer
buf.append("\n").append(msg);
// Add data object in error into summary result
bulkSaveResult.addInErrorDoc(dataObject.getDocId(),
new EntityInvalidException(msg),
Optional.ofNullable(dataObject.getFeature().getSession()),
Optional.ofNullable(dataObject.getFeature().getSessionOwner()));
}
}
@Override
public BulkSaveResult createDataObjects(String tenant,
Long datasourceId,
OffsetDateTime now,
List<DataObject> objects,
String datasourceIngestionId) throws ModuleException {
StringBuilder buf = new StringBuilder();
BulkSaveResult bulkSaveResult = new BulkSaveResult();
// For all objects, it is necessary to set datasourceId, creation date AND to validate them
OffsetDateTime creationDate = now;
Set<DataObject> toSaveObjects = new HashSet<>();
for (DataObject dataObject : objects) {
// Lets handle virtual_id here
if (dataObject.isLast()) {
dataObject.setVirtualId();
} else {
dataObject.removeVirtualId();
}
dataObject.setDataSourceId(datasourceId);
dataObject.setCreationDate(creationDate);
dataObject.setLastUpdate(creationDate);
if (Strings.isNullOrEmpty(dataObject.getLabel())) {
dataObject.setLabel(dataObject.getIpId().toString());
}
normalizeAndReprojectGeometry(dataObject, bulkSaveResult, buf);
// Validate data object
validateDataObject(toSaveObjects, dataObject, bulkSaveResult, buf, datasourceId);
}
try {
esRepos.saveBulk(tenant, bulkSaveResult, toSaveObjects, buf);
} catch (ElasticsearchException e) {
throw new ModuleException(e);
} finally {
publishEventsAndManageErrors(tenant, datasourceIngestionId, buf, bulkSaveResult);
}
return bulkSaveResult;
}
@Override
public BulkSaveResult mergeDataObjects(String tenant,
Long datasourceId,
OffsetDateTime now,
List<DataObject> objects,
String datasourceIngestionId) throws ModuleException {
StringBuilder buf = new StringBuilder();
BulkSaveResult bulkSaveResult = new BulkSaveResult();
// Set of data objects to be saved (depends on existence of data objects into ES)
Set<DataObject> toSaveObjects = new HashSet<>();
for (DataObject dataObject : objects) {
normalizeAndReprojectGeometry(dataObject, bulkSaveResult, buf);
mergeDataObject(tenant, datasourceId, now, dataObject);
validateDataObject(toSaveObjects, dataObject, bulkSaveResult, buf, datasourceId);
}
try {
esRepos.saveBulk(tenant, bulkSaveResult, toSaveObjects, buf);
} catch (ElasticsearchException e) {
throw new ModuleException(e);
} finally {
publishEventsAndManageErrors(tenant, datasourceIngestionId, buf, bulkSaveResult);
}
return bulkSaveResult;
}
/**
* Update the dataObject if it contains a geometry
* Compute two different geometry :
* - a normalized version of the geometry in the same CRS
* - a WGS 84 projected version of the normalized geometry
* This normalization can produce errors if the geometry is not valid
*/
private void normalizeAndReprojectGeometry(DataObject dataObject,
BulkSaveResult bulkSaveResult,
StringBuilder errorBuffer) {
DataObjectFeature feature = dataObject.getFeature();
// This geometry has been set by plugin, IT IS NOT NORMALIZED
IGeometry geometry = feature.getGeometry();
if ((geometry != null) && (geometry.getType() != GeoJsonType.UNLOCATED)) {
// Always normalize geometry in its origin CRS
try {
feature.setNormalizedGeometry(GeoHelper.normalize(geometry));
// Then manage projected (or not) geometry into WGS84
if (!feature.getCrs().get().equals(Crs.WGS_84.toString())) {
try {
// Transform to Wgs84...(not normalized one from its origin CRS)
IGeometry wgs84Geometry = GeoHelper.transform(geometry,
Crs.valueOf(feature.getCrs().get()),
Crs.WGS_84);
// ...and save it onto DataObject after having normalized it
dataObject.setWgs84(GeoHelper.normalize(wgs84Geometry));
} catch (IllegalArgumentException e) {
throw new RsRuntimeException(String.format("Given Crs '%s' is not allowed.",
feature.getCrs().get()), e);
}
} else { // Even if Crs is WGS84, don't forget to normalize geometry (already done into feature)
dataObject.setWgs84(feature.getNormalizedGeometry());
}
String json = gson.toJson(geometry);
GeoJSONReader reader = makeGeoJsonReader(makeFactory(projectGeoSettings.getShouldManagePolesOnGeometries()));
Shape read = reader.read(putTypeInFirstPosition(json));
GeoPoint nwPoint = new GeoPointBuilder(read.getBoundingBox().getMaxY(),
read.getBoundingBox().getMinX()).build();
dataObject.setNwPoint(nwPoint);
GeoPoint sePoint = new GeoPointBuilder(read.getBoundingBox().getMinY(),
read.getBoundingBox().getMaxX()).build();
dataObject.setSePoint(sePoint);
} catch (InvalidShapeException e) {
// Validation error
NormalizeGeometryError(dataObject,
bulkSaveResult,
errorBuffer,
feature,
"Failed to normalize the feature geometry : %s.\nFeature label = %s, ProviderId = %s\n",
e.getMessage());
} catch (ParseException e) {