-
Notifications
You must be signed in to change notification settings - Fork 477
/
CtlServiceImpl.java
627 lines (582 loc) · 27.2 KB
/
CtlServiceImpl.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
/*
* 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.apache.commons.lang.StringUtils.isBlank;
import static org.kaaproject.kaa.common.dto.ctl.CTLSchemaScopeDto.SYSTEM;
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.impl.DaoUtil.getStringFromFile;
import static org.kaaproject.kaa.server.common.dao.service.Validator.validateObject;
import static org.kaaproject.kaa.server.common.dao.service.Validator.validateSqlId;
import static org.kaaproject.kaa.server.common.dao.service.Validator.validateString;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang.Validate;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.kaaproject.kaa.common.avro.GenericAvroConverter;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaMetaInfoDto;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaScopeDto;
import org.kaaproject.kaa.common.dto.file.FileData;
import org.kaaproject.kaa.server.common.core.algorithms.generation.ConfigurationGenerationException;
import org.kaaproject.kaa.server.common.core.algorithms.generation.DefaultRecordGenerationAlgorithm;
import org.kaaproject.kaa.server.common.core.algorithms.generation.DefaultRecordGenerationAlgorithmImpl;
import org.kaaproject.kaa.server.common.core.configuration.RawData;
import org.kaaproject.kaa.server.common.core.configuration.RawDataFactory;
import org.kaaproject.kaa.server.common.core.schema.RawSchema;
import org.kaaproject.kaa.server.common.dao.CtlService;
import org.kaaproject.kaa.server.common.dao.exception.DatabaseProcessingException;
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.CtlSchemaMetaInfoDao;
import org.kaaproject.kaa.server.common.dao.impl.DaoUtil;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchemaMetaInfo;
import org.kaaproject.kaa.server.common.dao.model.sql.CtlSchema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Service
@Transactional
public class CtlServiceImpl implements CtlService {
private static final String JSON = "application/json";
private static final String ZIP = "application/zip";
private static final String VERSION = "version";
private static final String FQN = "fqn";
private static final Logger LOG = LoggerFactory.getLogger(CtlServiceImpl.class);
private static final String DEPENDENCIES = "dependencies";
private static final String DEFAULT_SYSTEM_EMPTY_SCHEMA_FILE = "/default_system_empty_schema"
+ ".avsc";
/**
* A template for naming exported CTL schemas.
*
* @see #shallowExport(CTLSchemaDto)
* @see #flatExport(CTLSchemaDto)
*/
private static final String CTL_EXPORT_TEMPLATE = "{0}.v{1}.avsc";
/**
* The name of the archive to put exported CTL schemas into.
*
* @see #deepExport(CTLSchemaDto)
*/
private static final String CTL_EXPORT_ZIP_NAME = "schemas.zip";
/**
* Used to format CTL schema body.
*/
private static final ObjectMapper FORMATTER = new ObjectMapper();
private final LockOptions lockOptions = new LockOptions(LockMode.PESSIMISTIC_WRITE);
@Autowired
private CtlSchemaDao<CtlSchema> ctlSchemaDao;
@Autowired
private CtlSchemaMetaInfoDao<CTLSchemaMetaInfo> ctlSchemaMetaInfoDao;
@Override
public CTLSchemaDto getOrCreateEmptySystemSchema(String createdUsername) {
CTLSchemaDto ctlSchema = findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(
DEFAULT_SYSTEM_EMPTY_SCHEMA_FQN, DEFAULT_SYSTEM_EMPTY_SCHEMA_VERSION, null, null);
if (ctlSchema == null) {
ctlSchema = new CTLSchemaDto();
CTLSchemaMetaInfoDto metaInfo = new CTLSchemaMetaInfoDto(DEFAULT_SYSTEM_EMPTY_SCHEMA_FQN);
ctlSchema.setMetaInfo(metaInfo);
ctlSchema.setVersion(DEFAULT_SYSTEM_EMPTY_SCHEMA_VERSION);
ctlSchema.setCreatedUsername(createdUsername);
ctlSchema.setDependencySet(new HashSet<CTLSchemaDto>());
String body = getStringFromFile(DEFAULT_SYSTEM_EMPTY_SCHEMA_FILE, CtlServiceImpl.class);
if (!body.isEmpty()) {
ctlSchema.setBody(body);
} else {
throw new RuntimeException("Can't read default system schema."); // NOSONAR
}
ctlSchema = saveCtlSchema(ctlSchema);
}
return ctlSchema;
}
@Override
public CTLSchemaDto saveCtlSchema(CTLSchemaDto unSavedSchema) {
validateCtlSchemaObject(unSavedSchema);
if (unSavedSchema.getDefaultRecord() == null) {
unSavedSchema = generateDefaultRecord(unSavedSchema);
} else {
validateDefaultRecord(unSavedSchema);
}
if (isBlank(unSavedSchema.getId())) {
CTLSchemaMetaInfoDto metaInfo = unSavedSchema.getMetaInfo();
CTLSchemaDto dto;
synchronized (this) {
List<CTLSchemaMetaInfo> existingFqns = ctlSchemaMetaInfoDao.findExistingFqns(metaInfo
.getFqn(), metaInfo.getTenantId(), metaInfo.getApplicationId());
if (existingFqns != null && !existingFqns.isEmpty()) {
throw new DatabaseProcessingException("Can't save common type due to an FQN conflict.");
}
metaInfo.setId(null);
CTLSchemaMetaInfo uniqueMetaInfo = ctlSchemaMetaInfoDao.save(
new CTLSchemaMetaInfo(metaInfo));
ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(uniqueMetaInfo);
CtlSchema ctlSchema = new CtlSchema(unSavedSchema);
ctlSchema.setMetaInfo(uniqueMetaInfo);
ctlSchema.setCreatedTime(System.currentTimeMillis());
ctlSchemaMetaInfoDao.refresh(uniqueMetaInfo);
try {
dto = getDto(ctlSchemaDao.save(ctlSchema, true));
} catch (DataIntegrityViolationException ex) {
throw new DatabaseProcessingException("Can't save common type: such FQN and version "
+ "already exist.");
} catch (Exception ex) {
throw new DatabaseProcessingException(ex);
}
}
return dto;
} else {
return updateCtlSchema(unSavedSchema);
}
}
private void validateDefaultRecord(CTLSchemaDto unSavedSchema) {
try {
String schemaBody = flatExportAsString(unSavedSchema);
GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(schemaBody);
converter.decodeJson(unSavedSchema.getDefaultRecord());
} catch (IOException | RuntimeException ex) {
LOG.error("Invalid default record for CTL schema with body: {}", unSavedSchema.getBody(), ex);
throw new RuntimeException("An unexpected exception occured: "
+ ex.toString());
}
}
private CTLSchemaDto generateDefaultRecord(CTLSchemaDto unSavedSchema) {
try {
String schemaBody = flatExportAsString(unSavedSchema);
LOG.debug("Generating default record for flat schema: {}", schemaBody);
RawSchema dataSchema = new RawSchema(schemaBody);
DefaultRecordGenerationAlgorithm<RawData> dataProcessor = new
DefaultRecordGenerationAlgorithmImpl<RawSchema, RawData>(
dataSchema, new RawDataFactory());
unSavedSchema.setDefaultRecord(dataProcessor.getRootData().getRawData());
return unSavedSchema;
} catch (StackOverflowError ex) {
LOG.error("Failed to generate default record. An endless recursion is detected. CTL schema "
+ "body: {}", unSavedSchema.getBody(), ex);
throw new RuntimeException("Unable to generate default record. An endless recursion is "
+ "detected! "
+ "Please check non-optional references to nested types.");
} catch (ConfigurationGenerationException | IOException | RuntimeException ex) {
LOG.error("Failed to generate default record for CTL schema with body: {}", unSavedSchema
.getBody(), ex);
throw new RuntimeException("An unexpected exception occured: "
+ ex.toString());
}
}
@Override
public CTLSchemaDto updateCtlSchema(CTLSchemaDto ctlSchema) {
validateCtlSchemaObject(ctlSchema);
LOG.debug("Update ctl schema with id [{}]", ctlSchema.getId());
CtlSchema schema = ctlSchemaDao.findById(ctlSchema.getId());
if (schema != null) {
synchronized (this) {
if (ctlSchema.getVersion()
!= schema.getVersion()) {
throw new DatabaseProcessingException("Can't change version of existing common type "
+ "version.");
}
CTLSchemaMetaInfo metaInfo = schema.getMetaInfo();
if (!ctlSchema.getMetaInfo().equals(metaInfo.toDto())) {
throw new DatabaseProcessingException("Can't update scope of existing common type "
+ "version within update procedure.");
}
ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(metaInfo);
schema.update(ctlSchema);
return DaoUtil.getDto(ctlSchemaDao.save(schema, true));
}
} else {
throw new DatabaseProcessingException("Can't find common type version by id.");
}
}
@Override
public CTLSchemaMetaInfoDto updateCtlSchemaMetaInfoScope(CTLSchemaMetaInfoDto ctlSchemaMetaInfo) {
validateObject(ctlSchemaMetaInfo, "Incorrect ctl schema meta info object");
LOG.debug("Update ctl schema meta info scope with id [{}]", ctlSchemaMetaInfo.getId());
CTLSchemaMetaInfo schemaMetaInfo = ctlSchemaMetaInfoDao.findById(ctlSchemaMetaInfo.getId());
if (schemaMetaInfo
!= null) {
synchronized (this) {
ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(schemaMetaInfo);
if (checkScopeUpdate(ctlSchemaMetaInfo, schemaMetaInfo.toDto())) {
List<CTLSchemaMetaInfo> others = ctlSchemaMetaInfoDao.findOthersByFqnAndTenantId(
ctlSchemaMetaInfo.getFqn(), ctlSchemaMetaInfo.getTenantId(), ctlSchemaMetaInfo
.getId());
if (others != null && !others.isEmpty()) {
throw new DatabaseProcessingException("Can't update scope of the common type due to "
+ "an FQN conflict.");
}
schemaMetaInfo = ctlSchemaMetaInfoDao.updateScope(
new CTLSchemaMetaInfo(ctlSchemaMetaInfo));
}
return DaoUtil.getDto(schemaMetaInfo);
}
} else {
throw new DatabaseProcessingException("Can't find common type by id.");
}
}
@Override
public List<CTLSchemaMetaInfoDto> findSiblingsByFqnTenantIdAndApplicationId(String fqn, String
tenantId, String applicationId) {
if (isBlank(fqn)) {
throw new IncorrectParameterException("Incorrect parameters for ctl schema request.");
}
LOG.debug("Find sibling ctl schemas by fqn {}, tenant id {} and application id {}", fqn,
tenantId, applicationId);
return convertDtoList(ctlSchemaMetaInfoDao.findSiblingsByFqnTenantIdAndApplicationId(fqn,
tenantId, applicationId));
}
private boolean checkScopeUpdate(CTLSchemaMetaInfoDto newSchemaMetaInfo, CTLSchemaMetaInfoDto
prevSchemaMetaInfo) {
if (!newSchemaMetaInfo.equals(prevSchemaMetaInfo)) {
if (isBlank(newSchemaMetaInfo.getFqn())) {
throw new DatabaseProcessingException("FQN can't be empty.");
}
if (!newSchemaMetaInfo.getFqn().equals(prevSchemaMetaInfo.getFqn())) {
throw new DatabaseProcessingException("Can't change FQN of the existing common type.");
}
CTLSchemaScopeDto newScope = newSchemaMetaInfo.getScope();
CTLSchemaScopeDto prevScope = prevSchemaMetaInfo.getScope();
if (newScope
!= prevScope) {
if (SYSTEM.equals(newScope)) {
throw new DatabaseProcessingException("Can't update scope to system.");
}
if (newScope.getLevel()
> prevScope.getLevel()) {
throw new DatabaseProcessingException("Can't update scope to lower.");
}
}
if (!isBlank(newSchemaMetaInfo.getTenantId())
&& !newSchemaMetaInfo.getTenantId().equals(prevSchemaMetaInfo.getTenantId())) {
throw new DatabaseProcessingException("Can't change tenant reference for the existing "
+ "common type.");
}
if (!isBlank(newSchemaMetaInfo.getApplicationId())
&& !newSchemaMetaInfo.getApplicationId().equals(prevSchemaMetaInfo.getApplicationId())) {
throw new DatabaseProcessingException("Can't change application reference for the "
+ "existing common type.");
}
return true;
}
return false;
}
@Override
public void removeCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(String fqn, Integer version,
String tenantId, String
applicationId) {
if (isBlank(fqn) || version == null) {
throw new IncorrectParameterException("Incorrect parameters for ctl schema request.");
}
LOG.debug("Remove ctl schema by fqn {} version {}, tenant id {} and application id {}", fqn,
version, tenantId, applicationId);
CtlSchema ctlSchema = ctlSchemaDao.findByFqnAndVerAndTenantIdAndApplicationId(fqn, version,
tenantId, applicationId);
if (ctlSchema
!= null) {
List<CtlSchema> dependsList = ctlSchemaDao.findDependentSchemas(ctlSchema.getStringId());
if (dependsList.isEmpty()) {
synchronized (this) {
CTLSchemaMetaInfo metaInfo = ctlSchema.getMetaInfo();
ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(metaInfo);
try {
ctlSchemaDao.removeById(ctlSchema.getStringId());
List<CtlSchema> schemas = ctlSchemaDao.findAllByMetaInfoId(metaInfo.getStringId());
if (schemas == null || schemas.isEmpty()) {
ctlSchemaMetaInfoDao.removeById(metaInfo.getStringId());
}
} catch (DataIntegrityViolationException ex) {
throw new DatabaseProcessingException("Common type version can't be deleted because "
+ "it is referenced by system modules.");
}
}
} else {
throw new DatabaseProcessingException("Common type version can't be deleted because it is"
+ " referenced by other types.");
}
}
}
@Override
public CTLSchemaDto findCtlSchemaById(String schemaId) {
validateSqlId(schemaId, "Incorrect schema id for ctl request "
+ schemaId);
LOG.debug("Find ctl schema by id [{}]", schemaId);
return DaoUtil.getDto(ctlSchemaDao.findById(schemaId));
}
@Override
public CTLSchemaDto findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(String fqn, Integer
version, String tenantId, String applicationId) {
if (isBlank(fqn) || version == null) {
throw new IncorrectParameterException("Incorrect parameters for ctl schema request.");
}
LOG.debug("Find ctl schema by fqn {} version {}, tenant id {} and application id {}", fqn,
version, tenantId, applicationId);
return DaoUtil.getDto(ctlSchemaDao.findByFqnAndVerAndTenantIdAndApplicationId(fqn, version,
tenantId, applicationId));
}
@Override
public CTLSchemaDto findByMetaInfoIdAndVer(String metaInfoId, Integer version) {
if (isBlank(metaInfoId) || version == null) {
throw new IncorrectParameterException("Incorrect parameters for ctl schema request.");
}
LOG.debug("Find ctl schema by meta info id {} and version {}", metaInfoId, version);
return DaoUtil.getDto(ctlSchemaDao.findByMetaInfoIdAndVer(metaInfoId, version));
}
@Override
public CTLSchemaDto findAnyCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(String fqn,
Integer version,
String tenantId,
String applicationId) {
if (isBlank(fqn) || version == null) {
throw new IncorrectParameterException("Incorrect parameters for ctl schema request.");
}
LOG.debug("Find any ctl schema by fqn {} version {}, tenant id {} and application id {}",
fqn, version, tenantId, applicationId);
return DaoUtil.getDto(ctlSchemaDao.findAnyByFqnAndVerAndTenantIdAndApplicationId(fqn,
version, tenantId, applicationId));
}
@Override
public List<CTLSchemaDto> findSystemCtlSchemas() {
LOG.debug("Find system ctl schemas");
return convertDtoList(ctlSchemaDao.findSystemSchemas());
}
@Override
public List<CTLSchemaMetaInfoDto> findSystemCtlSchemasMetaInfo() {
LOG.debug("Find system ctl schemas");
return getMetaInfoFromCtlSchema(ctlSchemaDao.findSystemSchemas());
}
@Override
public List<CTLSchemaMetaInfoDto> findAvailableCtlSchemasMetaInfoForTenant(String tenantId) {
LOG.debug("Find system and tenant scopes ctl schemas by tenant id {}", tenantId);
return getMetaInfoFromCtlSchema(ctlSchemaDao.findAvailableSchemasForTenant(tenantId));
}
@Override
public List<CTLSchemaMetaInfoDto> findAvailableCtlSchemasMetaInfoForApplication(
String tenantId, String applicationId) {
LOG.debug("Find system, tenant and application scopes ctl schemas by application id {}",
applicationId);
return getMetaInfoFromCtlSchema(ctlSchemaDao.findAvailableSchemasForApplication(tenantId,
applicationId));
}
@Override
public CTLSchemaDto findLatestCtlSchemaByFqnAndTenantIdAndApplicationId(String fqn, String
tenantId, String applicationId) {
validateString(fqn, "Incorrect fqn for ctl schema request.");
LOG.debug("Find latest ctl schema by fqn {}, tenantId {} and applicationId {}", fqn,
tenantId, applicationId);
return DaoUtil.getDto(ctlSchemaDao.findLatestByFqnAndTenantIdAndApplicationId(fqn, tenantId,
applicationId));
}
@Override
public CTLSchemaDto findLatestByMetaInfoId(String metaInfoId) {
validateString(metaInfoId, "Incorrect meta info id for ctl schema request.");
LOG.debug("Find latest ctl schema by meta info id {}", metaInfoId);
return DaoUtil.getDto(ctlSchemaDao.findLatestByMetaInfoId(metaInfoId));
}
@Override
public List<CTLSchemaDto> findAllCtlSchemasByFqnAndTenantIdAndApplicationId(String fqn, String
tenantId, String applicationId) {
validateString(fqn, "Incorrect fqn for ctl schema request.");
LOG.debug("Find all ctl schemas by fqn {}, tenantId {} and applicationId {}", fqn, tenantId,
applicationId);
return convertDtoList(ctlSchemaDao.findAllByFqnAndTenantIdAndApplicationId(fqn, tenantId,
applicationId));
}
@Override
public List<CTLSchemaDto> findCtlSchemas() {
LOG.debug("Find all ctl schemas");
return convertDtoList(ctlSchemaDao.find());
}
@Override
public List<CTLSchemaDto> findCtlSchemaDependents(String schemaId) {
validateSqlId(schemaId, "Incorrect schema id for ctl schema request.");
LOG.debug("Find dependents schemas for schema with id [{}]", schemaId);
List<CTLSchemaDto> list = Collections.emptyList();
CtlSchema schemaDto = ctlSchemaDao.findById(schemaId);
if (schemaDto
!= null) {
list = convertDtoList(ctlSchemaDao.findDependentSchemas(schemaDto.getStringId()));
}
return list;
}
@Override
public List<CTLSchemaDto> findCtlSchemaDependents(String fqn, Integer version, String tenantId,
String applicationId) {
if (isBlank(fqn) || version == null) {
throw new IncorrectParameterException("Incorrect parameters for ctl schema request.");
}
LOG.debug("Find dependents schemas for schema with fqn {} version {}, tenantId {} and "
+ "applicationId ()", fqn, version, tenantId, applicationId);
List<CTLSchemaDto> schemas = Collections.emptyList();
CtlSchema schema = ctlSchemaDao.findByFqnAndVerAndTenantIdAndApplicationId(fqn, version,
tenantId, applicationId);
if (schema
!= null) {
schemas = convertDtoList(ctlSchemaDao.findDependentSchemas(schema.getStringId()));
}
return schemas;
}
private void validateCtlSchemaObject(CTLSchemaDto ctlSchema) {
validateObject(ctlSchema, "Incorrect ctl schema object");
CTLSchemaMetaInfoDto metaInfo = ctlSchema.getMetaInfo();
if (metaInfo == null) {
throw new RuntimeException("Incorrect ctl schema object. CTLSchemaMetaInfoDto is mandatory "
+ "information.");
} else {
if (isBlank(metaInfo.getFqn()) || ctlSchema.getVersion() == null) {
throw new RuntimeException("Incorrect CTL schema, please add correct version and fqn.");
}
}
}
private List<CTLSchemaMetaInfoDto> getMetaInfoFromCtlSchema(List<CtlSchema> schemas) {
Map<String, CTLSchemaMetaInfoDto> metaInfoMap = new HashMap<>();
if (!schemas.isEmpty()) {
for (CtlSchema schema : schemas) {
String metaInfoId = schema.getMetaInfo().getStringId();
CTLSchemaMetaInfoDto metaInfoDto = metaInfoMap.get(metaInfoId);
if (metaInfoDto == null) {
metaInfoDto = getDto(schema.getMetaInfo());
metaInfoMap.put(metaInfoId, metaInfoDto);
}
List<Integer> versions = metaInfoDto.getVersions();
if (versions == null) {
versions = new ArrayList<>();
metaInfoDto.setVersions(versions);
}
versions.add(schema.getVersion());
}
}
List<CTLSchemaMetaInfoDto> result = new ArrayList<>(metaInfoMap.values());
return result;
}
@Override
public FileData shallowExport(CTLSchemaDto schema) {
try {
FileData result = new FileData();
result.setContentType(JSON);
result.setFileName(MessageFormat.format(CTL_EXPORT_TEMPLATE, schema.getMetaInfo().getFqn(),
schema.getVersion()));
// Format schema body
Object json = FORMATTER.readValue(schema.getBody(), Object.class);
result.setFileData(FORMATTER.writerWithDefaultPrettyPrinter().writeValueAsString(json)
.getBytes());
return result;
} catch (Exception cause) {
throw new RuntimeException("An unexpected exception occured: " + cause.toString());
}
}
@Override
public Schema flatExportAsSchema(CTLSchemaDto schema) {
try {
return this.parseDependencies(schema, new Schema.Parser());
} catch (Exception cause) {
LOG.error("Unable to export CTL schema as flat: {}", schema, cause);
throw new RuntimeException("An unexpected exception occured: " + cause.toString());
}
}
@Override
public String flatExportAsString(CTLSchemaDto schema) {
return flatExportAsSchema(schema).toString();
}
@Override
public FileData flatExport(CTLSchemaDto schema) {
try {
FileData result = new FileData();
result.setContentType(JSON);
result.setFileName(MessageFormat.format(CTL_EXPORT_TEMPLATE, schema.getMetaInfo().getFqn(),
schema.getVersion()));
// Get schema body
String body = flatExportAsString(schema);
// Format schema body
Object json = FORMATTER.readValue(body, Object.class);
result.setFileData(FORMATTER.writerWithDefaultPrettyPrinter().writeValueAsString(json)
.getBytes());
return result;
} catch (Exception cause) {
throw new RuntimeException("An unexpected exception occured: "
+ cause.toString());
}
}
@Override
public FileData deepExport(CTLSchemaDto schema) {
try {
ByteArrayOutputStream content = new ByteArrayOutputStream();
ZipOutputStream out = new ZipOutputStream(content);
List<FileData> files = this.recursiveShallowExport(new ArrayList<FileData>(), schema);
for (FileData file : files) {
out.putNextEntry(new ZipEntry(file.getFileName()));
out.write(file.getFileData());
out.closeEntry();
}
out.close();
FileData result = new FileData();
result.setContentType(ZIP);
result.setFileName(CTL_EXPORT_ZIP_NAME);
result.setFileData(content.toByteArray());
return result;
} catch (Exception cause) {
throw new RuntimeException("An unexpected exception occured: " + cause.toString());
}
}
private Schema parseDependencies(CTLSchemaDto schema, final Schema.Parser parser) throws
Exception {
if (schema.getDependencySet() != null) {
for (CTLSchemaDto dependency : schema.getDependencySet()) {
this.parseDependencies(dependency, parser);
}
}
ObjectNode object = new ObjectMapper().readValue(schema.getBody(), ObjectNode.class);
object.remove(DEPENDENCIES);
String body = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object);
return parser.parse(body);
}
private List<FileData> recursiveShallowExport(List<FileData> files, CTLSchemaDto parent) throws
Exception {
files.add(this.shallowExport(parent));
ObjectNode object = new ObjectMapper().readValue(parent.getBody(), ObjectNode.class);
ArrayNode dependencies = (ArrayNode) object.get(DEPENDENCIES);
if (dependencies != null) {
for (JsonNode node : dependencies) {
ObjectNode dependency = (ObjectNode) node;
String fqn = dependency.get(FQN).getTextValue();
Integer version = dependency.get(VERSION).getIntValue();
CTLSchemaDto child = this.findAnyCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(
fqn, version, parent.getMetaInfo().getTenantId(),
parent.getMetaInfo().getApplicationId());
Validate.notNull(child, MessageFormat.format("The dependency [{0}] was not found!", fqn));
this.recursiveShallowExport(files, child);
}
}
return files;
}
}