-
Notifications
You must be signed in to change notification settings - Fork 249
/
JPARegistryStorage.java
784 lines (665 loc) · 29.7 KB
/
JPARegistryStorage.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
/*
* Copyright 2019 Red Hat
*
* 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 io.apicurio.registry.storage.impl.jpa;
import io.apicurio.registry.content.ContentCanonicalizer;
import io.apicurio.registry.content.ContentCanonicalizerFactory;
import io.apicurio.registry.content.ContentHandle;
import io.apicurio.registry.metrics.PersistenceExceptionLivenessApply;
import io.apicurio.registry.metrics.PersistenceTimeoutReadinessApply;
import io.apicurio.registry.storage.ArtifactAlreadyExistsException;
import io.apicurio.registry.storage.ArtifactMetaDataDto;
import io.apicurio.registry.storage.ArtifactNotFoundException;
import io.apicurio.registry.storage.ArtifactVersionMetaDataDto;
import io.apicurio.registry.storage.EditableArtifactMetaDataDto;
import io.apicurio.registry.storage.MetaDataKeys;
import io.apicurio.registry.storage.RegistryStorage;
import io.apicurio.registry.storage.RegistryStorageException;
import io.apicurio.registry.storage.RuleAlreadyExistsException;
import io.apicurio.registry.storage.RuleConfigurationDto;
import io.apicurio.registry.storage.RuleNotFoundException;
import io.apicurio.registry.storage.StoredArtifact;
import io.apicurio.registry.storage.VersionNotFoundException;
import io.apicurio.registry.storage.impl.jpa.entity.Artifact;
import io.apicurio.registry.storage.impl.jpa.entity.MetaData;
import io.apicurio.registry.storage.impl.jpa.entity.Rule;
import io.apicurio.registry.storage.impl.jpa.entity.RuleConfig;
import io.apicurio.registry.types.ArtifactType;
import io.apicurio.registry.types.RuleType;
import static java.util.Objects.requireNonNull;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.transaction.Transactional;
@ApplicationScoped
@PersistenceExceptionLivenessApply
@PersistenceTimeoutReadinessApply
public class JPARegistryStorage implements RegistryStorage {
// private static Logger log = LoggerFactory.getLogger(JPARegistryStorage.class);
@Inject
ContentCanonicalizerFactory ccFactory;
@Inject
EntityManager entityManager;
@Inject
JPAEntityMapper mapper;
private long _getNextArtifactVersion(String artifactId) {
requireNonNull(artifactId);
Long latest = entityManager.createQuery(
"SELECT MAX(a.version) FROM Artifact a " +
"WHERE a.artifactId = :artifact_id", Long.class)
.setParameter("artifact_id", artifactId)
.getSingleResult();
return latest != null ? latest + 1 : 1;
}
private List<MetaData> _getMetaData(String artifactId, Long version) {
requireNonNull(artifactId); // TODO split null version into separate method?
TypedQuery<MetaData> res = entityManager.createQuery(
"SELECT m FROM MetaData m " +
"WHERE m.artifactId = :artifact_id AND m.version " + (version == null ? "IS NULL" : "= :version"), MetaData.class)
.setParameter("artifact_id", artifactId);
if (version != null)
res.setParameter("version", version);
return res.getResultList();
}
private Rule _getRule(String artifactId, RuleType rule) {
requireNonNull(rule); // TODO split null artifactId into separate method?
try {
TypedQuery<Rule> res = entityManager.createQuery("SELECT r FROM Rule r " +
"WHERE r.artifactId " + (artifactId == null ? "IS NULL" : "= :artifact_id") + " AND r.name = :name", Rule.class)
.setParameter("name", rule);
if (artifactId != null) {
res.setParameter("artifact_id", artifactId);
}
return res.getSingleResult();
} catch (NoResultException ex) {
throw new RuleNotFoundException(rule, ex);
}
}
private List<RuleConfig> _getRuleConfig(Rule rule) {
requireNonNull(rule);
return entityManager.createQuery("SELECT rc FROM RuleConfig rc " +
"WHERE rc.rule.id = :rule_id", RuleConfig.class)
.setParameter("rule_id", rule.getId())
.getResultList();
}
private Artifact _getArtifact(String artifactId, long version) {
requireNonNull(artifactId);
try {
return entityManager.createQuery("SELECT a FROM Artifact a " +
"WHERE a.artifactId = :artifact_id AND a.version = :version", Artifact.class)
.setParameter("artifact_id", artifactId)
.setParameter("version", version)
.getSingleResult();
} catch (NoResultException ex) {
throw new VersionNotFoundException(artifactId, version, ex);
}
}
private Artifact _getArtifact(String artifactId) {
requireNonNull(artifactId);
try {
return entityManager.createQuery(
"SELECT a FROM Artifact a " +
"WHERE a.artifactId = :artifact_id " +
"ORDER BY a.version DESC ", Artifact.class)
.setParameter("artifact_id", artifactId)
.setMaxResults(1)
.getSingleResult();
} catch (NoResultException ex) {
throw new ArtifactNotFoundException(artifactId, ex);
}
}
private boolean _artifactExists(String artifactId) {
requireNonNull(artifactId);
return entityManager.createQuery("SELECT COUNT(a) FROM Artifact a " +
"WHERE a.artifactId = :artifact_id", Long.class)
.setParameter("artifact_id", artifactId)
.getSingleResult() != 0;
}
private void _ensureArtifactExists(String artifactId) {
if (!_artifactExists(artifactId))
throw new ArtifactNotFoundException(artifactId);
}
private boolean _artifactVersionExists(String artifactId, long version) {
requireNonNull(artifactId);
return entityManager.createQuery("SELECT COUNT(a) FROM Artifact a " +
"WHERE a.artifactId = :artifact_id AND a.version = :version", Long.class)
.setParameter("artifact_id", artifactId)
.setParameter("version", version)
.getSingleResult() != 0;
}
private void _ensureArtifactVersionExists(String artifactId, long version) {
if (!_artifactVersionExists(artifactId, version))
throw new VersionNotFoundException(artifactId, version);
}
private Artifact _getArtifact(long id) {
return entityManager.createQuery(
"SELECT a FROM Artifact a " +
"WHERE a.globalId = :global_id", Artifact.class)
.setParameter("global_id", id)
.getSingleResult();
}
// ========================================================================
@Override
@Transactional
public CompletionStage<ArtifactMetaDataDto> createArtifact(String artifactId, ArtifactType artifactType, ContentHandle content) throws ArtifactAlreadyExistsException, RegistryStorageException {
requireNonNull(artifactId);
requireNonNull(artifactType);
requireNonNull(content);
try {
long nextVersion = _getNextArtifactVersion(artifactId);
if (nextVersion != 1) {
throw new ArtifactAlreadyExistsException(artifactId);
}
Artifact artifact = Artifact.builder()
.artifactId(artifactId)
.version(nextVersion)
.content(content.bytes())
.build();
entityManager.persist(artifact);
new MetaDataMapperUpdater()
.update(MetaDataKeys.TYPE, artifactType.value())
.persistUpdate(entityManager, artifactId, nextVersion);
ArtifactMetaDataDto amdd = new MetaDataMapperUpdater()
.update(MetaDataKeys.TYPE, artifactType.value())
.persistUpdate(entityManager, artifactId, null)
.update(artifact)
.toArtifactMetaDataDto();
return CompletableFuture.completedFuture(amdd);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public SortedSet<Long> deleteArtifact(String artifactId) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
_ensureArtifactExists(artifactId);
List<Long> res1 = entityManager.createQuery(
"SELECT a.version FROM Artifact a " +
"WHERE a.artifactId = :artifact_id " +
"ORDER BY a.version DESC", Long.class)
.setParameter("artifact_id", artifactId)
.getResultList();
if (res1.size() == 0)
throw new ArtifactNotFoundException(artifactId);
entityManager.createQuery(
"DELETE FROM Artifact a " +
"WHERE a.artifactId = :artifact_id")
.setParameter("artifact_id", artifactId)
.executeUpdate();
// delete meta data
entityManager.createQuery(
"DELETE FROM MetaData m " +
"WHERE m.artifactId = :artifact_id")
.setParameter("artifact_id", artifactId)
.executeUpdate();
// delete rules
entityManager.createQuery(
"DELETE FROM Rule r " +
"WHERE r.artifactId = :artifact_id")
.setParameter("artifact_id", artifactId)
.executeUpdate();
return new TreeSet<>(res1);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public StoredArtifact getArtifact(String artifactId) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
Artifact artifact = _getArtifact(artifactId);
return mapper.toStoredArtifact(artifact);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public CompletionStage<ArtifactMetaDataDto> updateArtifact(String artifactId, ArtifactType artifactType, ContentHandle content) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
requireNonNull(artifactType);
requireNonNull(content);
_ensureArtifactExists(artifactId);
long nextVersion = _getNextArtifactVersion(artifactId);
Artifact artifact = Artifact.builder()
.artifactId(artifactId)
.version(nextVersion)
.content(content.bytes())
.build();
entityManager.persist(artifact);
new MetaDataMapperUpdater()
.update(MetaDataKeys.TYPE, artifactType.value())
.persistUpdate(entityManager, artifactId, nextVersion);
ArtifactMetaDataDto amdd = new MetaDataMapperUpdater()
.update(MetaDataKeys.TYPE, artifactType.value())
.persistUpdate(entityManager, artifactId, null)
.update(artifact)
.toArtifactMetaDataDto();
return CompletableFuture.completedFuture(amdd);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public Set<String> getArtifactIds() {
try {
List<String> res1 = entityManager.createQuery(
"SELECT a.artifactId FROM Artifact a", String.class)
.getResultList();
return new HashSet<>(res1);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
// =======================================================
@Override
@Transactional
public ArtifactMetaDataDto getArtifactMetaData(String artifactId) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
Artifact artifact = _getArtifact(artifactId);
return new MetaDataMapperUpdater(_getMetaData(artifactId, null))
.update(artifact)
.toArtifactMetaDataDto();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#getArtifactMetaData(java.lang.String, io.apicurio.registry.content.ContentHandle)
*/
@Override
public ArtifactMetaDataDto getArtifactMetaData(String artifactId, ContentHandle content) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
// Get the meta-data for the artifact
ArtifactMetaDataDto metaData = getArtifactMetaData(artifactId);
// Create a canonicalizer for the artifact based on its type, and then
// canonicalize the inbound content
ContentCanonicalizer canonicalizer = ccFactory.create(metaData.getType());
ContentHandle canonicalContent = canonicalizer.canonicalize(content);
byte[] canonicalBytes = canonicalContent.bytes();
Artifact artifact = null;
List<Artifact> list = entityManager.createQuery(
"SELECT a FROM Artifact a " +
"WHERE a.artifactId = :artifact_id " +
"ORDER BY a.version DESC ", Artifact.class)
.setParameter("artifact_id", artifactId)
.getResultList();
for (Artifact candidateArtifact : list) {
ContentHandle candidateContent = ContentHandle.create(candidateArtifact.getContent());
ContentHandle canonicalCandidateContent = canonicalizer.canonicalize(candidateContent);
byte[] candidateBytes = canonicalCandidateContent.bytes();
if (Arrays.equals(canonicalBytes, candidateBytes)) {
artifact = candidateArtifact;
}
}
if (artifact == null) {
throw new ArtifactNotFoundException(artifactId);
}
return new MetaDataMapperUpdater(_getMetaData(artifactId, null))
.update(artifact)
.toArtifactMetaDataDto();
} catch (Exception e) {
throw new RegistryStorageException(e);
}
}
@Override
public ArtifactMetaDataDto getArtifactMetaData(long id) throws ArtifactNotFoundException, RegistryStorageException {
try {
Artifact artifact = _getArtifact(id);
return new MetaDataMapperUpdater(_getMetaData(artifact.getArtifactId(), artifact.getVersion()))
.update(artifact)
.toArtifactMetaDataDto();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public void updateArtifactMetaData(String artifactId, EditableArtifactMetaDataDto metaData) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
requireNonNull(metaData);
_ensureArtifactExists(artifactId);
new MetaDataMapperUpdater(_getMetaData(artifactId, null))
.update(metaData)
.persistUpdate(entityManager, artifactId, null);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public List<RuleType> getArtifactRules(String artifactId) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
_ensureArtifactExists(artifactId);
return entityManager.createQuery("SELECT r.name FROM Rule r " +
"WHERE r.artifactId = :artifact_id", RuleType.class)
.setParameter("artifact_id", artifactId)
.getResultList();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#createArtifactRule(java.lang.String, io.apicurio.registry.types.RuleType, io.apicurio.registry.storage.RuleConfigurationDto)
*/
@Override
@Transactional
public void createArtifactRule(String artifactId, RuleType rule, RuleConfigurationDto config)
throws ArtifactNotFoundException, RuleAlreadyExistsException, RegistryStorageException {
try {
requireNonNull(artifactId);
requireNonNull(rule);
requireNonNull(config);
if (getArtifactRules(artifactId).contains(rule))
throw new RuleAlreadyExistsException(rule);
Rule ruleEntity = Rule.builder()
.artifactId(artifactId)
.name(rule)
.build();
entityManager.persist(ruleEntity);
new RuleConfigMapperUpdater()
.update(config)
.persistUpdate(entityManager, ruleEntity);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public void deleteArtifactRules(String artifactId) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
_ensureArtifactExists(artifactId);
entityManager.createQuery("DELETE FROM Rule r " +
"WHERE r.artifactId = :artifact_id")
.setParameter("artifact_id", artifactId)
.executeUpdate();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#getArtifactRule(java.lang.String, io.apicurio.registry.types.RuleType)
*/
@Transactional
@Override
public RuleConfigurationDto getArtifactRule(String artifactId, RuleType rule)
throws ArtifactNotFoundException, RuleNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
requireNonNull(rule);
Rule res1 = _getRule(artifactId, rule);
return new RuleConfigMapperUpdater(_getRuleConfig(res1))
.toRuleConfigurationDto();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#updateArtifactRule(java.lang.String, io.apicurio.registry.types.RuleType, io.apicurio.registry.storage.RuleConfigurationDto)
*/
@Override
@Transactional
public void updateArtifactRule(String artifactId, RuleType rule, RuleConfigurationDto config)
throws ArtifactNotFoundException, RuleNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
requireNonNull(rule);
requireNonNull(config);
Rule ruleEntity = _getRule(artifactId, rule);
new RuleConfigMapperUpdater(_getRuleConfig(ruleEntity))
.update(config)
.persistUpdate(entityManager, ruleEntity);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#deleteArtifactRule(java.lang.String, io.apicurio.registry.types.RuleType)
*/
@Override
@Transactional
public void deleteArtifactRule(String artifactId, RuleType rule)
throws ArtifactNotFoundException, RuleNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
requireNonNull(rule);
_ensureArtifactExists(artifactId);
int affected = entityManager.createQuery("DELETE FROM Rule r " +
"WHERE r.artifactId = :artifact_id AND r.name = :name")
.setParameter("artifact_id", artifactId)
.setParameter("name", rule)
.executeUpdate();
if (affected == 0) {
throw new RuleNotFoundException(rule);
}
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public SortedSet<Long> getArtifactVersions(String artifactId) throws ArtifactNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
_ensureArtifactExists(artifactId);
List<Long> res1 = entityManager.createQuery(
"SELECT a.version FROM Artifact a " +
"WHERE a.artifactId = :artifact_id " +
"ORDER BY a.version DESC", Long.class)
.setParameter("artifact_id", artifactId)
.getResultList();
return new TreeSet<>(res1);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public StoredArtifact getArtifactVersion(long id) throws ArtifactNotFoundException, VersionNotFoundException, RegistryStorageException {
try {
Artifact artifact = _getArtifact(id);
return mapper.toStoredArtifact(artifact);
} catch (NoResultException ex) {
throw new ArtifactNotFoundException(ex);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
public StoredArtifact getArtifactVersion(String artifactId, long version) throws ArtifactNotFoundException, VersionNotFoundException, RegistryStorageException {
requireNonNull(artifactId);
try {
_ensureArtifactVersionExists(artifactId, version);
return mapper.toStoredArtifact(_getArtifact(artifactId, version));
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public void deleteArtifactVersion(String artifactId, long version) throws ArtifactNotFoundException, VersionNotFoundException, RegistryStorageException {
requireNonNull(artifactId);
try {
// TODO ArtifactVersionNotFoundEx ?
int affected = entityManager.createQuery("DELETE FROM Artifact a " +
"WHERE a.artifactId = :artifact_id AND a.version = :version")
.setParameter("artifact_id", artifactId)
.setParameter("version", version)
.executeUpdate();
if (affected == 0)
throw new VersionNotFoundException(artifactId, version);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public ArtifactVersionMetaDataDto getArtifactVersionMetaData(String artifactId, long version) throws ArtifactNotFoundException, VersionNotFoundException, RegistryStorageException {
requireNonNull(artifactId);
try {
Artifact artifact = _getArtifact(artifactId, version);
return new MetaDataMapperUpdater(_getMetaData(artifactId, version))
.update(artifact)
.toArtifactVersionMetaDataDto();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public void updateArtifactVersionMetaData(String artifactId, long version, EditableArtifactMetaDataDto metaData) throws ArtifactNotFoundException, VersionNotFoundException, RegistryStorageException {
requireNonNull(artifactId);
requireNonNull(metaData);
try {
_ensureArtifactExists(artifactId);
new MetaDataMapperUpdater(_getMetaData(artifactId, version))
.update(metaData)
.persistUpdate(entityManager, artifactId, version);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public void deleteArtifactVersionMetaData(String artifactId, long version) throws ArtifactNotFoundException, VersionNotFoundException, RegistryStorageException {
try {
requireNonNull(artifactId);
_ensureArtifactExists(artifactId);
entityManager.createQuery("DELETE FROM MetaData md " +
"WHERE md.artifactId = :artifact_id AND md.version = :version")
.setParameter("artifact_id", artifactId)
.setParameter("version", version)
.executeUpdate();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public List<RuleType> getGlobalRules() throws RegistryStorageException {
try {
return entityManager.createQuery("SELECT r.name FROM Rule r " +
"WHERE r.artifactId IS NULL", RuleType.class)
.getResultList();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#createGlobalRule(io.apicurio.registry.types.RuleType, io.apicurio.registry.storage.RuleConfigurationDto)
*/
@Override
@Transactional
public void createGlobalRule(RuleType rule, RuleConfigurationDto config)
throws RuleAlreadyExistsException, RegistryStorageException {
requireNonNull(rule);
requireNonNull(config);
try {
if (getGlobalRules().contains(rule))
throw new RuleAlreadyExistsException(rule);
Rule ruleEntity = Rule.builder()
.artifactId(null)
.name(rule)
.build();
entityManager.persist(ruleEntity);
new RuleConfigMapperUpdater()
.update(config)
.persistUpdate(entityManager, ruleEntity);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
@Override
@Transactional
public void deleteGlobalRules() throws RegistryStorageException {
try {
entityManager.createQuery("DELETE FROM Rule r " +
"WHERE r.artifactId IS NULL")
.executeUpdate();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#getGlobalRule(io.apicurio.registry.types.RuleType)
*/
@Override
@Transactional
public RuleConfigurationDto getGlobalRule(RuleType rule)
throws RuleNotFoundException, RegistryStorageException {
requireNonNull(rule);
try {
Rule ruleEntity = _getRule(null, rule);
return new RuleConfigMapperUpdater(_getRuleConfig(ruleEntity))
.toRuleConfigurationDto();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#updateGlobalRule(io.apicurio.registry.types.RuleType, io.apicurio.registry.storage.RuleConfigurationDto)
*/
@Override
@Transactional
public void updateGlobalRule(RuleType rule, RuleConfigurationDto config)
throws RuleNotFoundException, RegistryStorageException {
requireNonNull(rule);
requireNonNull(config);
try {
Rule ruleEntity = _getRule(null, rule);
new RuleConfigMapperUpdater(_getRuleConfig(ruleEntity))
.update(config)
.persistUpdate(entityManager, ruleEntity)
.toRuleConfigurationDto();
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
/**
* @see io.apicurio.registry.storage.RegistryStorage#deleteGlobalRule(io.apicurio.registry.types.RuleType)
*/
@Override
@Transactional
public void deleteGlobalRule(RuleType rule) throws RuleNotFoundException, RegistryStorageException {
requireNonNull(rule);
try {
Rule ruleEntity = _getRule(null, rule);
entityManager.remove(ruleEntity);
} catch (PersistenceException ex) {
throw new RegistryStorageException(ex);
}
}
}