-
Notifications
You must be signed in to change notification settings - Fork 477
/
EventClassServiceImpl.java
249 lines (222 loc) · 12.5 KB
/
EventClassServiceImpl.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
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.common.dao.service;
import static org.kaaproject.kaa.server.common.dao.impl.DaoUtil.convertDtoList;
import static org.kaaproject.kaa.server.common.dao.impl.DaoUtil.getDto;
import static org.kaaproject.kaa.server.common.dao.service.Validator.isValidSqlId;
import static org.kaaproject.kaa.server.common.dao.service.Validator.isValidSqlObject;
import static org.kaaproject.kaa.server.common.dao.service.Validator.validateSqlId;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.kaaproject.avro.ui.shared.NamesValidator;
import org.kaaproject.kaa.common.dto.event.EventClassDto;
import org.kaaproject.kaa.common.dto.event.EventClassFamilyDto;
import org.kaaproject.kaa.common.dto.event.EventClassFamilyVersionDto;
import org.kaaproject.kaa.common.dto.event.EventClassType;
import org.kaaproject.kaa.server.common.dao.EventClassService;
import org.kaaproject.kaa.server.common.dao.exception.IncorrectParameterException;
import org.kaaproject.kaa.server.common.dao.impl.CTLSchemaDao;
import org.kaaproject.kaa.server.common.dao.impl.EventClassDao;
import org.kaaproject.kaa.server.common.dao.impl.EventClassFamilyDao;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClass;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamily;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamilyVersion;
import org.kaaproject.kaa.server.common.dao.schema.EventSchemaProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class EventClassServiceImpl implements EventClassService {
private static final Logger LOG = LoggerFactory.getLogger(EventClassServiceImpl.class);
@Autowired
private EventClassFamilyDao<EventClassFamily> eventClassFamilyDao;
@Autowired
private EventClassDao<EventClass> eventClassDao;
@Autowired
private EventSchemaProcessor eventSchemaProcessor;
@Autowired
private CTLSchemaDao<CTLSchema> ctlSchemaDao;
@Override
public List<EventClassFamilyDto> findEventClassFamiliesByTenantId(
String tenantId) {
List<EventClassFamilyDto> eventClassFamilies;
if (isValidSqlId(tenantId)) {
LOG.debug("Find event class families by tenant id [{}]", tenantId);
eventClassFamilies = convertDtoList(eventClassFamilyDao.findByTenantId(tenantId));
} else {
throw new IncorrectParameterException("Incorrect tenant id: " + tenantId);
}
return eventClassFamilies;
}
@Override
public EventClassFamilyDto findEventClassFamilyByTenantIdAndName(String tenantId, String name) {
if (isValidSqlId(tenantId)) {
LOG.debug("Find event class family by tenant id [{}] and name {}", tenantId, name);
return eventClassFamilyDao.findByTenantIdAndName(tenantId, name).toDto();
} else {
throw new IncorrectParameterException("Incorrect tenant id: " + tenantId);
}
}
@Override
public EventClassFamilyDto findEventClassFamilyById(String id) {
validateSqlId(id, "Event class family id is incorrect. Can't find event class family by id " + id);
return getDto(eventClassFamilyDao.findById(id));
}
@Override
public EventClassFamilyDto findEventClassFamilyByECFVersionId(String id) {
validateSqlId(id, "Event class family version id is incorrect. Can't find event class family by ECF version id " + id);
EventClassFamily eventClassFamily = eventClassFamilyDao.find().stream().
filter(ecf -> ecf.getSchemas().stream()
.filter(ecfv -> ecfv.getStringId().equals(id)).count() > 0)
.findFirst().get();
return getDto(eventClassFamily);
}
@Override
public List<EventClassFamilyVersionDto> findEventClassFamilyVersionsById(String id) {
validateSqlId(id, "Event class family id is incorrect. Can't find event class family by id " + id);
EventClassFamily ecf = eventClassFamilyDao.findById(id);
return convertDtoList(ecf.getSchemas());
}
@Override
public EventClassFamilyDto saveEventClassFamily(
EventClassFamilyDto eventClassFamilyDto) {
EventClassFamilyDto savedEventClassFamilyDto = null;
if (isValidSqlObject(eventClassFamilyDto)) {
if (eventClassFamilyDao.validateName(eventClassFamilyDto.getTenantId(), eventClassFamilyDto.getId(), eventClassFamilyDto.getName())) {
if (StringUtils.isBlank(eventClassFamilyDto.getId())) {
if (NamesValidator.validateNamespace(eventClassFamilyDto.getNamespace())) {
if (NamesValidator.validateClassName(eventClassFamilyDto.getClassName())) {
if (eventClassFamilyDao.validateClassName(eventClassFamilyDto.getTenantId(), eventClassFamilyDto.getId(), eventClassFamilyDto.getClassName())) {
eventClassFamilyDto.setCreatedTime(System.currentTimeMillis());
} else {
LOG.debug("Can't save event class family. Class name should be unique within the tenant.");
throw new IncorrectParameterException("Incorrect event class family. Class name should be unique within the tenant.");
}
} else {
LOG.debug("Can't save event class family. Class name [{}] is not valid.", eventClassFamilyDto.getClassName());
throw new IncorrectParameterException("Incorrect event class family. Class name is not valid. '" + eventClassFamilyDto.getClassName() + "' is not a valid identifier.");
}
} else {
LOG.debug("Can't save event class family. Namespace [{}] is not valid.", eventClassFamilyDto.getNamespace());
throw new IncorrectParameterException("Incorrect event class family. Namespace is not valid. '" + eventClassFamilyDto.getNamespace() + "' is not a valid identifier.");
}
}
savedEventClassFamilyDto = getDto(eventClassFamilyDao.save(new EventClassFamily(eventClassFamilyDto)));
} else {
LOG.debug("Can't save event class family. Name should be unique within the tenant.");
throw new IncorrectParameterException("Incorrect event class family. Name should be unique within the tenant.");
}
}
return savedEventClassFamilyDto;
}
@Override
public void addEventClassFamilyVersion(String eventClassFamilyId,
EventClassFamilyVersionDto eventClassFamilyVersion, String createdUsername) {
EventClassFamilyDto eventClassFamily = findEventClassFamilyById(eventClassFamilyId);
if (eventClassFamily != null) {
List<EventClassDto> records = eventClassFamilyVersion.getRecords();
List<String> fqns = new ArrayList<>(records.size());
for (EventClassDto eventClass : records) {
fqns.add(eventClass.getFqn());
}
if (validateEventClassFamilyFqns(eventClassFamily, fqns)) {
List<EventClassFamilyVersionDto> schemasDto = findEventClassFamilyVersionsById(eventClassFamilyId);
int version = 1;
if (schemasDto != null && !schemasDto.isEmpty()) {
Collections.sort(schemasDto, new Comparator<EventClassFamilyVersionDto>() {
@Override
public int compare(EventClassFamilyVersionDto o1,
EventClassFamilyVersionDto o2) {
return o1.getVersion() - o2.getVersion();
}
});
version = schemasDto.get(schemasDto.size()-1).getVersion()+1;
} else {
schemasDto = new ArrayList<>();
}
eventClassFamilyVersion.setVersion(version);
eventClassFamilyVersion.setCreatedTime(System.currentTimeMillis());
eventClassFamilyVersion.setCreatedUsername(createdUsername);
schemasDto.add(eventClassFamilyVersion);
EventClassFamily ecf = new EventClassFamily(eventClassFamily);
List<EventClassFamilyVersion> schemas = new ArrayList<>();
for (EventClassDto eventClass : records) {
setEventClassProperties(eventClassFamily, eventClass);
}
schemasDto.forEach(s -> schemas.add(new EventClassFamilyVersion(s)));
setBackreference(schemas);
ecf.setSchemas(schemas);
eventClassFamilyDao.save(ecf);
} else {
LOG.debug("Can't process event class family schema.");
throw new IncorrectParameterException("Incorrect event class family schema. FQNs should be unique within the tenant.");
}
} else {
LOG.debug("Can't find related event class family.");
throw new IncorrectParameterException("Event class family not found, id:" + eventClassFamilyId);
}
}
private void setEventClassProperties(EventClassFamilyDto eventClassFamilyDto, EventClassDto eventClass) {
eventClass.setTenantId(eventClassFamilyDto.getTenantId());
eventClass.setVersion(ctlSchemaDao.findById(eventClass.getCtlSchemaId()).getVersion());
}
private void setBackreference(List<EventClassFamilyVersion> ecfvList) {
ecfvList.forEach(ecfv -> ecfv.getRecords().forEach(ec -> ec.setEcfv(ecfv)));
}
private boolean validateEventClassFamilyFqns(EventClassFamilyDto eventClassFamily, List<String> fqns) {
return eventClassDao.validateFqns(eventClassFamily.getTenantId(), eventClassFamily.getId(), fqns);
}
@Override
public List<EventClassDto> findEventClassesByFamilyIdVersionAndType(String ecfId, int version, EventClassType type) {
List<EventClassDto> eventClasses;
if (isValidSqlId(ecfId)) {
LOG.debug("Find event classes by family id [{}] version [{}] and type [{}]", ecfId, version, type);
EventClassFamily ecf = eventClassFamilyDao.findById(ecfId);
EventClassFamilyVersion ecfv = ecf.getSchemas().stream().filter(s -> s.getVersion() == version).collect(Collectors.toList()).get(0);
eventClasses = convertDtoList(eventClassDao.findByEcfvIdVersionAndType(String.valueOf(ecfv.getId()), ecfv.getVersion(), type));
} else {
throw new IncorrectParameterException("Incorrect event class family id: " + ecfId);
}
return eventClasses;
}
@Override
public List<EventClassDto> findEventClassByTenantIdAndFQN(String tenantId, String fqn) {
if (isValidSqlId(tenantId)) {
LOG.debug("Find event class family by tenant id [{}] and fqn {}", tenantId, fqn);
return convertDtoList(eventClassDao.findByTenantIdAndFqn(tenantId, fqn));
} else {
throw new IncorrectParameterException("Incorrect tenant id: " + tenantId);
}
}
@Override
public EventClassDto findEventClassByTenantIdAndFQNAndVersion(String tenantId, String fqn, int version) {
if (isValidSqlId(tenantId)) {
LOG.debug("Find event class family by tenant id [{}] and fqn {}", tenantId, fqn);
return getDto(eventClassDao.findByTenantIdAndFqnAndVersion(tenantId, fqn, version));
} else {
throw new IncorrectParameterException("Incorrect tenant id: " + tenantId);
}
}
}