-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataProcessingContextMongoAdapter.java
More file actions
148 lines (127 loc) · 6.42 KB
/
Copy pathDataProcessingContextMongoAdapter.java
File metadata and controls
148 lines (127 loc) · 6.42 KB
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
package fr.insee.genesis.infrastructure.adapter;
import fr.insee.genesis.Constants;
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
import fr.insee.genesis.domain.model.context.schedule.DeletedExpiredSchedules;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionSchedule;
import fr.insee.genesis.domain.model.context.schedule.KraftwerkExecutionScheduleV2;
import fr.insee.genesis.domain.ports.spi.DataProcessingContextPersistancePort;
import fr.insee.genesis.infrastructure.document.context.DataProcessingContextDocument;
import fr.insee.genesis.infrastructure.mappers.DataProcessingContextMapper;
import fr.insee.genesis.infrastructure.repository.DataProcessingContextMongoDBRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@Service
@Qualifier("dataProcessingContextMongoAdapter")
@Slf4j
public class DataProcessingContextMongoAdapter implements DataProcessingContextPersistancePort {
private final DataProcessingContextMongoDBRepository dataProcessingContextMongoDBRepository;
private final MongoTemplate mongoTemplate;
@Autowired
public DataProcessingContextMongoAdapter(
DataProcessingContextMongoDBRepository dataProcessingContextMongoDBRepository,
MongoTemplate mongoTemplate
) {
this.dataProcessingContextMongoDBRepository = dataProcessingContextMongoDBRepository;
this.mongoTemplate = mongoTemplate;
}
@Override
public DataProcessingContextModel findByCollectionInstrumentId(String collectionInstrumentId) {
List<DataProcessingContextDocument> existingDocuments =
dataProcessingContextMongoDBRepository.findByCollectionInstrumentIdList(List.of(collectionInstrumentId));
return DataProcessingContextMapper.INSTANCE.documentToModel(
existingDocuments.isEmpty() ? null : existingDocuments.getFirst()
);
}
@Override
public List<DataProcessingContextModel> findByCollectionInstrumentIds(List<String> collectionInstrumentIds) {
List<DataProcessingContextDocument> existingDocuments =
dataProcessingContextMongoDBRepository.findByCollectionInstrumentIdList(collectionInstrumentIds);
return DataProcessingContextMapper.INSTANCE.listDocumentToListModel(existingDocuments);
}
@Override
public void save(DataProcessingContextDocument dataProcessingContextDocument) {
dataProcessingContextMongoDBRepository.save(dataProcessingContextDocument);
}
@Override
public void saveAll(List<DataProcessingContextDocument> dataProcessingContextDocuments) {
dataProcessingContextMongoDBRepository.saveAll(dataProcessingContextDocuments);
}
@Override
public List<DataProcessingContextDocument> findAll() {
return dataProcessingContextMongoDBRepository.findAll();
}
@Override
public long count() {
return dataProcessingContextMongoDBRepository.count();
}
@Override
public DeletedExpiredSchedules removeExpiredSchedules(DataProcessingContextModel context) {
LocalDateTime now = LocalDateTime.now();
List<KraftwerkExecutionSchedule> deletedV1 =
Optional.ofNullable(context.getKraftwerkExecutionScheduleList())
.orElse(List.of())
.stream()
.filter(schedule -> schedule.getScheduleEndDate() != null)
.filter(schedule -> schedule.getScheduleEndDate().isBefore(now))
.toList();
List<KraftwerkExecutionScheduleV2> deletedV2 =
Optional.ofNullable(context.getKraftwerkExecutionScheduleV2List())
.orElse(List.of())
.stream()
.filter(schedule -> schedule.getScheduleEndDate() != null)
.filter(schedule -> schedule.getScheduleEndDate().isBefore(now))
.toList();
Query query = Query.query(
Criteria.where("collectionInstrumentId").is(context.getCollectionInstrumentId())
);
for (KraftwerkExecutionSchedule scheduleToRemove : deletedV1) {
Update update = new Update().pull(
"kraftwerkExecutionScheduleList",
Query.query(
Criteria.where("partitionId").is(scheduleToRemove.getPartitionId())
.and("frequency").is(scheduleToRemove.getFrequency())
.and("serviceToCall").is(scheduleToRemove.getServiceToCall())
.and("scheduleBeginDate").is(scheduleToRemove.getScheduleBeginDate())
.and("scheduleEndDate").is(scheduleToRemove.getScheduleEndDate())
.and("trustParameters").is(scheduleToRemove.getTrustParameters())
).getQueryObject()
);
mongoTemplate.updateMulti(
query,
update,
Constants.MONGODB_CONTEXT_COLLECTION_NAME
);
}
for (KraftwerkExecutionScheduleV2 scheduleToRemove : deletedV2) {
Update update = new Update().pull(
"kraftwerkExecutionScheduleV2List",
Query.query(
Criteria.where("scheduleUuid")
.is(scheduleToRemove.getScheduleUuid())
).getQueryObject()
);
mongoTemplate.updateMulti(
query,
update,
Constants.MONGODB_CONTEXT_COLLECTION_NAME
);
}
return new DeletedExpiredSchedules(deletedV1, deletedV2);
}
@Override
public List<DataProcessingContextDocument> findAllByReview(boolean withReview) {
return dataProcessingContextMongoDBRepository.findAll()
.stream().filter(doc ->
doc.isWithReview() == withReview
).toList();
}
}