-
Notifications
You must be signed in to change notification settings - Fork 3
/
DataElementService.groovy
667 lines (565 loc) · 30.6 KB
/
DataElementService.groovy
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
/*
* Copyright 2020-2023 University of Oxford and Health and Social Care Information Centre, also known as NHS Digital
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*/
package uk.ac.ox.softeng.maurodatamapper.datamodel.item
import uk.ac.ox.softeng.maurodatamapper.api.exception.ApiInternalException
import uk.ac.ox.softeng.maurodatamapper.core.container.Classifier
import uk.ac.ox.softeng.maurodatamapper.core.facet.SemanticLink
import uk.ac.ox.softeng.maurodatamapper.core.facet.SemanticLinkType
import uk.ac.ox.softeng.maurodatamapper.core.model.CatalogueItem
import uk.ac.ox.softeng.maurodatamapper.core.model.Model
import uk.ac.ox.softeng.maurodatamapper.core.model.ModelItemService
import uk.ac.ox.softeng.maurodatamapper.core.path.PathService
import uk.ac.ox.softeng.maurodatamapper.core.rest.transport.merge.FieldPatchData
import uk.ac.ox.softeng.maurodatamapper.core.rest.transport.model.CopyInformation
import uk.ac.ox.softeng.maurodatamapper.core.similarity.SimilarityPair
import uk.ac.ox.softeng.maurodatamapper.datamodel.DataModel
import uk.ac.ox.softeng.maurodatamapper.datamodel.DataModelService
import uk.ac.ox.softeng.maurodatamapper.datamodel.facet.SummaryMetadata
import uk.ac.ox.softeng.maurodatamapper.datamodel.facet.SummaryMetadataService
import uk.ac.ox.softeng.maurodatamapper.datamodel.item.datatype.DataType
import uk.ac.ox.softeng.maurodatamapper.datamodel.item.datatype.DataTypeService
import uk.ac.ox.softeng.maurodatamapper.datamodel.similarity.DataElementSimilarityResult
import uk.ac.ox.softeng.maurodatamapper.datamodel.traits.service.SummaryMetadataAwareService
import uk.ac.ox.softeng.maurodatamapper.gorm.HQLPagedResultList
import uk.ac.ox.softeng.maurodatamapper.hibernate.search.engine.search.predicate.FilterFactory
import uk.ac.ox.softeng.maurodatamapper.hibernate.search.engine.search.predicate.IdPathSecureFilterFactory
import uk.ac.ox.softeng.maurodatamapper.lucene.queries.mlt.BoostedMoreLikeThisQuery
import uk.ac.ox.softeng.maurodatamapper.path.Path
import uk.ac.ox.softeng.maurodatamapper.security.User
import uk.ac.ox.softeng.maurodatamapper.security.UserSecurityPolicyManager
import uk.ac.ox.softeng.maurodatamapper.traits.domain.MdmDomain
import uk.ac.ox.softeng.maurodatamapper.util.Utils
import grails.gorm.transactions.Transactional
import groovy.util.logging.Slf4j
import org.apache.lucene.analysis.Analyzer
import org.hibernate.search.backend.lucene.LuceneBackend
import org.hibernate.search.backend.lucene.LuceneExtension
import org.hibernate.search.engine.search.predicate.dsl.BooleanPredicateClausesStep
import org.hibernate.search.engine.search.query.SearchResult
import org.hibernate.search.mapper.orm.Search
import org.hibernate.search.mapper.orm.mapping.SearchMapping
import org.hibernate.search.mapper.orm.session.SearchSession
import java.util.function.BiFunction
import static org.grails.orm.hibernate.cfg.GrailsHibernateUtil.ARGUMENT_SORT
@Slf4j
@Transactional
class DataElementService extends ModelItemService<DataElement> implements SummaryMetadataAwareService {
DataClassService dataClassService
DataTypeService dataTypeService
SummaryMetadataService summaryMetadataService
DataModelService dataModelService
PathService pathService
@Override
DataElement get(Serializable id) {
DataElement.get(id)
}
Long count() {
DataElement.count()
}
@Override
List<DataElement> list(Map args) {
DataElement.list(args)
}
@Override
List<DataElement> getAll(Collection<UUID> ids) {
DataElement.getAll(ids).findAll()
}
@Override
void deleteAll(Collection<DataElement> dataElements) {
dataElements.each {delete(it)}
}
void delete(UUID id) {
delete(get(id), true)
}
void delete(DataElement dataElement, boolean flush = false) {
if (!dataElement) return
dataElement.breadcrumbTree.removeFromParent()
dataElement.dataType = null
dataElement.dataClass?.removeFromDataElements(dataElement)
dataElement.delete(flush: flush)
}
@Override
void deleteAllByModelIds(Set<UUID> dataModelIds) {
List<UUID> dataElementIds = DataElement.by().where {
dataClass {
inList('dataModel.id', dataModelIds)
}
}.id().list() as List<UUID>
if (dataElementIds) {
List<List<UUID>> batches = Utils.partition(dataElementIds, BATCH_SIZE)
batches.each { ids ->
log.trace('Removing facets for {} DataElements', ids.size())
deleteAllFacetsByMultiFacetAwareIds(ids,
'delete from datamodel.join_dataelement_to_facet where dataelement_id in :ids')
log.trace('Removing {} DataElements', ids.size())
sessionFactory.currentSession
.createSQLQuery('DELETE FROM datamodel.data_element WHERE id IN :ids')
.setParameter('ids', ids)
.executeUpdate()
}
}
log.trace('DataElements removed')
}
DataElement validate(DataElement dataElement) {
dataElement.validate()
dataElement
}
@Override
void deleteAllFacetDataByMultiFacetAwareIds(List<UUID> catalogueItemIds) {
super.deleteAllFacetDataByMultiFacetAwareIds(catalogueItemIds)
summaryMetadataService.deleteAllByMultiFacetAwareItemIds(catalogueItemIds)
}
@Override
DataElement save(Map args, DataElement dataElement) {
if (!dataElement.dataType.ident()) {
dataTypeService.save(dataElement.dataType)
}
super.save(args, dataElement)
}
@Override
DataElement checkFacetsAfterImportingCatalogueItem(DataElement catalogueItem) {
super.checkFacetsAfterImportingCatalogueItem(catalogueItem)
if (catalogueItem.summaryMetadata) {
catalogueItem.summaryMetadata.each {sm ->
sm.multiFacetAwareItemId = catalogueItem.id
sm.createdBy = sm.createdBy ?: catalogueItem.createdBy
sm.summaryMetadataReports.each {smr ->
smr.createdBy = catalogueItem.createdBy
}
}
}
catalogueItem
}
@Override
DataElement findByIdJoinClassifiers(UUID id) {
DataElement.findById(id, [fetch: [classifiers: 'join']])
}
@Override
void removeAllFromClassifier(Classifier classifier) {
DataElement.byClassifierId(classifier.id).list().each {
it.removeFromClassifiers(classifier)
}
}
@Override
List<DataElement> findAllByClassifier(Classifier classifier) {
DataElement.byClassifierId(classifier.id).list()
}
@Override
List<DataElement> findAllReadableByClassifier(UserSecurityPolicyManager userSecurityPolicyManager, Classifier classifier) {
findAllByClassifier(classifier).findAll {userSecurityPolicyManager.userCanReadSecuredResourceId(DataModel, it.model.id)}
}
@Override
Boolean shouldPerformSearchForTreeTypeCatalogueItems(String domainType) {
domainType == DataElement.simpleName
}
@Override
List<DataElement> findAllReadableTreeTypeCatalogueItemsBySearchTermAndDomain(UserSecurityPolicyManager userSecurityPolicyManager,
String searchTerm, String domainType) {
List<UUID> readableIds = userSecurityPolicyManager.listReadableSecuredResourceIds(DataModel)
if (!readableIds) return []
List<DataElement> results = []
if (shouldPerformSearchForTreeTypeCatalogueItems(domainType)) {
log.debug('Performing hs label search')
long start = System.currentTimeMillis()
results =
DataElement
.labelHibernateSearch(DataElement, searchTerm, readableIds.toList(), dataModelService.getAllReadablePaths(readableIds))
.results
log.debug("Search took: ${Utils.getTimeString(System.currentTimeMillis() - start)}. Found ${results.size()}")
}
results
}
void matchUpDataTypes(DataModel dataModel, Collection<DataElement> dataElements) {
if (dataElements) {
log.debug('Matching up {} DataElements to a possible {} DataTypes', dataElements.size(), dataModel.dataTypes.size())
def grouped = dataElements.groupBy {it.dataType.label}.sort {a, b ->
def res = a.value.size() <=> b.value.size()
if (res == 0) res = a.key <=> b.key
res
}
log.debug('Grouped {} DataElements by DataType label', grouped.size())
grouped.each {label, elements ->
log.trace('Matching {} elements to DataType label {}', elements.size(), label)
DataType dataType = dataModel.findDataTypeByLabel(label)
if (!dataType) {
log.debug('No DataType for {} in DataModel, using first DataElement DataType as base', label)
DataElement dataElement = elements.first()
dataType = dataElement.dataType
dataType.createdBy = dataElement.createdBy
dataModel.addToDataTypes(dataType)
}
elements.each {dataType.addToDataElements(it)}
}
}
}
DataElement findByDataClassIdAndId(Serializable dataClassId, Serializable id) {
DataElement.byDataClassIdAndId(dataClassId, id).find()
}
DataElement findByDataTypeIdAndId(Serializable dataTypeId, Serializable id) {
DataElement.byDataTypeIdAndId(dataTypeId, id).find()
}
List<DataElement> findAllByDataClassId(Serializable dataClassId, Map pagination = [:]) {
findAllByDataClassId(dataClassId, pagination, pagination)
}
List<DataElement> findAllByDataClassId(Serializable dataClassId, Map filter, Map pagination) {
DataElement.withFilter(DataElement.byDataClassId(dataClassId), filter).list(pagination)
}
List<DataElement> findAllByDataClassIdIncludingImported(UUID dataClassId, Map filters, Map pagination) {
Map<String, Object> queryParams = [dataClassId: dataClassId]
queryParams.putAll(extractFiltersAsHQLParameters(filters, 'dataType'))
String baseQuery = applyHQLFilters('''
FROM DataElement de
LEFT JOIN de.importingDataClasses idc
INNER JOIN de.dataType dt
WHERE (de.dataClass.id = :dataClassId OR idc.id = :dataClassId)''', 'de', filters)
// Cannot sort DEs including imported using idx
String sortedQuery = applyHQLSort(baseQuery, 'de', pagination[ARGUMENT_SORT] ?: ['label': 'asc'], pagination, true)
new HQLPagedResultList<DataElement>(DataElement)
.list("SELECT DISTINCT de ${sortedQuery}".toString())
.count("SELECT COUNT(DISTINCT de.id) ${baseQuery}".toString())
.queryParams(queryParams)
.paginate(pagination)
.postProcess {
it.dataType = proxyHandler.unwrapIfProxy(it.dataType)
it.trackChanges() // unwrapping the proxy changes the object and therefore is detected as a "change" this call undos this change as its not actually one
}
}
String applyHQLFilters(String originalQuery, String ciQueryPrefix, Map filters) {
StringBuilder filteredQuery = new StringBuilder(super.applyHQLFilters(originalQuery, ciQueryPrefix, filters))
if (filters.dataType) filteredQuery.append '\nAND lower(dt.label) LIKE lower(:dataType)'
filteredQuery.toString()
}
List<DataElement> findAllByDataClassIdJoinDataType(Serializable dataClassId) {
DataElement.byDataClassId(dataClassId).join('dataType').sort('label').list()
}
List<DataElement> findAllByDataTypeId(Serializable dataTypeId, Map pagination = [:]) {
DataElement.withFilter(DataElement.byDataTypeId(dataTypeId), pagination).list(pagination)
}
List<DataElement> findAllByDataType(DataType dataType) {
DataElement.byDataType(dataType).list()
}
List<DataElement> findAllByDataClass(DataClass dataClass) {
DataElement.byDataClass(dataClass).list()
}
List<DataElement> findAllByImportingDataClassId(UUID dataClassId) {
DataElement.byImportingDataClassId(dataClassId).list()
}
List<DataElement> findAllByImportingDataClassIds(List<UUID> dataClassIds) {
if (!dataClassIds) return []
DataElement.byImportingDataClassIdInList(dataClassIds).list()
}
List<DataElement> findAllByDataModelId(Serializable dataModelId, Map pagination = [:]) {
DataElement.byDataModelId(dataModelId).list(pagination)
}
List<DataElement> findAllByDataModelIdAndLabelIlike(Serializable dataModelId, String labelSearch, Map pagination = [:]) {
DataElement.byDataModelIdAndLabelIlike(dataModelId, labelSearch).list(pagination)
}
Number countByDataClassId(Serializable dataClassId) {
DataElement.byDataClassId(dataClassId).count()
}
Number countByDataTypeId(Serializable dataTypeId) {
DataElement.byDataTypeId(dataTypeId).count()
}
Number countByDataModelId(Serializable dataModelId) {
DataElement.byDataModelId(dataModelId).count()
}
DataElement findByDataClassPathAndLabel(DataModel dataModel, List<String> dataClassPath, String label) {
DataClass dataClass = dataClassService.findDataClassByPath(dataModel, dataClassPath)
dataClass ? findByDataClassIdAndLabel(dataClass.id, label) : null
}
DataElement findByDataClassIdAndLabel(Serializable dataClassId, String label) {
DataElement.byDataClassIdAndLabel(dataClassId, label).get()
}
DataElement findByDataModelIdAndId(Serializable dataModelId, Serializable id) {
DataElement.byDataModelIdAndId(dataModelId, id).get()
}
DataElement findOrCreateDataElementForDataClass(DataClass parentClass, String label, String description, User createdBy,
DataType dataType,
Integer minMultiplicity = 0, Integer maxMultiplicity = 1) {
String cleanLabel = label.trim()
DataElement dataElement = parentClass.findDataElement(cleanLabel)
if (!dataElement) {
dataElement = new DataElement(label: cleanLabel, description: description, createdBy: createdBy.emailAddress,
minMultiplicity: minMultiplicity,
maxMultiplicity: maxMultiplicity)
if (!dataType.label) dataType.setLabel("$cleanLabel-dataType")
dataType.addToDataElements(dataElement)
parentClass.addToDataElements(dataElement)
}
if (dataElement.dataType.label != dataType.label) {
return findOrCreateDataElementForDataClass(parentClass, "${cleanLabel}.1", description, createdBy, dataType, minMultiplicity,
maxMultiplicity)
}
dataElement
}
Path buildDataElementPath(DataElement dataElement) {
DataClass parent = dataElement.dataClass
List<DataClass> parents = []
while (parent) {
parents << parent
parent = parent.parentDataClass
}
List<MdmDomain> pathObjects = []
pathObjects << dataElement.model
pathObjects.addAll(parents.reverse())
pathObjects << dataElement
Path.from(pathObjects)
}
@Override
DataElement copy(Model copiedDataModel, DataElement original, CatalogueItem parentDataClass,
UserSecurityPolicyManager userSecurityPolicyManager) {
DataElement copy = copyDataElement(copiedDataModel as DataModel, original, userSecurityPolicyManager.user, userSecurityPolicyManager)
if (parentDataClass) {
(parentDataClass as DataClass).addToDataElements(copy)
}
copy
}
DataElement copyDataElement(DataModel copiedDataModel, DataElement original, User copier,
UserSecurityPolicyManager userSecurityPolicyManager, boolean copySummaryMetadata = false,
CopyInformation copyInformation = new CopyInformation()) {
DataElement copy = new DataElement(minMultiplicity: original.minMultiplicity,
maxMultiplicity: original.maxMultiplicity)
copy = copyModelItemInformation(original, copy, copier, userSecurityPolicyManager, copySummaryMetadata, copyInformation)
setCatalogueItemRefinesCatalogueItem(copy, original, copier)
DataType dataType = copiedDataModel.findDataTypeByLabel(original.dataType.label)
// If theres no DataType then copy the original's DataType into the DataModel
if (!dataType) {
dataType = dataTypeService.copyDataType(copiedDataModel, original.dataType, copier,
userSecurityPolicyManager)
}
dataType.addToDataElements(copy)
copy
}
DataElement copyModelItemInformation(DataElement original,
DataElement copy,
User copier,
UserSecurityPolicyManager userSecurityPolicyManager,
boolean copySummaryMetadata,
CopyInformation copyInformation) {
copy = super.copyModelItemInformation(original, copy, copier, userSecurityPolicyManager, copyInformation)
if (copySummaryMetadata) {
copySummaryMetadataFromOriginal(original, copy, copier, copyInformation)
}
copy
}
@Override
DataElement copyModelItemInformation(DataElement original,
DataElement copy,
User copier,
UserSecurityPolicyManager userSecurityPolicyManager,
CopyInformation copyInformation) {
copyModelItemInformation(original, copy, copier, userSecurityPolicyManager, false, copyInformation)
}
DataElementSimilarityResult findAllSimilarDataElementsInDataModel(DataModel dataModelToSearch, DataElement dataElementToCompare, maxResults = 5) {
SearchSession searchSession = Search.session(sessionFactory.currentSession)
SearchMapping searchMapping = Search.mapping(sessionFactory)
Analyzer wordDelimiter = searchMapping.backend().unwrap(LuceneBackend).analyzer('wordDelimiter').get()
Analyzer standard = searchMapping.backend().unwrap(LuceneBackend).analyzer('standard').get()
String[] fields = ['label', 'dataClass.label', 'dataType.label', 'description'] as String[]
List<BoostedMoreLikeThisQuery> moreLikeThisQueries = [
new BoostedMoreLikeThisQuery(wordDelimiter, 'label', dataElementToCompare.label, fields)
.boostedTo(2f)
.withMinWordLength(2)
.withMinDocFrequency(2),
new BoostedMoreLikeThisQuery(wordDelimiter, 'dataClass.label', dataElementToCompare.dataClass.label, fields)
.boostedTo(1f)
.withMinWordLength(2)
.withMinDocFrequency(2),
new BoostedMoreLikeThisQuery(wordDelimiter, 'dataType.label', dataElementToCompare.dataType.label, fields)
.boostedTo(1f)
.withMinWordLength(2)
.withMinDocFrequency(2),
]
if (dataElementToCompare.description)
moreLikeThisQueries.add(new BoostedMoreLikeThisQuery(standard, 'description', dataElementToCompare.description, fields)
.boostedTo(1f)
.withMinWordLength(2)
)
SearchResult<SimilarityPair<DataElement>> searchResult = searchSession.search(DataElement)
.extension(LuceneExtension.get())
.select {pf ->
pf.composite(new BiFunction<DataElement, Float, SimilarityPair<DataElement>>() {
@Override
SimilarityPair<DataElement> apply(DataElement dataElement, Float score) {
if (!dataElement) throw new ApiInternalException('DES', 'No DataElement passed to apply function in findAllSimilarDataElementsInDataModel')
dataElement.dataClass = proxyHandler.unwrapIfProxy(dataElement.dataClass)
dataElement.dataType = proxyHandler.unwrapIfProxy(dataElement.dataType)
dataElement.dataClass.dataModel = proxyHandler.unwrapIfProxy(dataElement.dataClass.dataModel)
new SimilarityPair<DataElement>(dataElement, score)
}
}, pf.entity(), pf.score())
}
.where {lsf ->
BooleanPredicateClausesStep boolStep = lsf
.bool()
.filter(IdPathSecureFilterFactory.createFilter(lsf, [dataModelToSearch.id], [dataModelToSearch.path]))
.filter(FilterFactory.mustNot(lsf, lsf.id().matching(dataElementToCompare.id)))
moreLikeThisQueries.each {mlt ->
boolStep.should(lsf.bool().must(lsf.fromLuceneQuery(mlt)).boost(mlt.boost))
}
boolStep
}
.fetch(maxResults)
DataElementSimilarityResult similarityResult = new DataElementSimilarityResult(dataElementToCompare, searchResult)
log.debug('Found {} similar results in {}', similarityResult.totalSimilar(), Utils.durationToString(similarityResult.took()))
similarityResult
}
DataElement findDataElementWithSameLabelTree(DataModel dataModel, DataElement original) {
DataClass parent = dataClassService.findSameLabelTree(dataModel, original.dataClass)
parent.findDataElement(original.label)
}
void addDataElementIsFromDataElements(DataElement dataElement, Collection<DataElement> fromDataElements, User user) {
addDataElementsAreFromDataElements([dataElement], fromDataElements, user)
}
void addDataElementsAreFromDataElement(Collection<DataElement> dataElements, DataElement fromDataElement, User user) {
addDataElementsAreFromDataElements(dataElements, [fromDataElement], user)
}
void addDataElementsAreFromDataElements(Collection<DataElement> dataElements, Collection<DataElement> fromDataElements, User user) {
if (!dataElements || !fromDataElements) throw new ApiInternalException('DESXX', 'No DataElements or FromDataElements exist to create links')
List<SemanticLink> alreadyExistingLinks =
semanticLinkService.findAllBySourceMultiFacetAwareItemIdInListAndTargetMultiFacetAwareItemIdInListAndLinkType(
dataElements*.id, fromDataElements*.id, SemanticLinkType.IS_FROM)
dataElements.each {de ->
fromDataElements.each {fde ->
// If no link already exists then add a new one
if (!alreadyExistingLinks.any {it.multiFacetAwareItemId == de.id && it.targetMultiFacetAwareItemId == fde.id}) {
setDataElementIsFromDataElement(de, fde, user)
}
}
}
}
void removeDataElementIsFromDataElements(DataElement dataElement, Collection<DataElement> fromDataElements) {
removeDataElementsAreFromDataElements([dataElement], fromDataElements)
}
void removeDataElementsAreFromDataElement(Collection<DataElement> dataElements, DataElement fromDataElement) {
removeDataElementsAreFromDataElements(dataElements, [fromDataElement])
}
void removeDataElementsAreFromDataElements(Collection<DataElement> dataElements, Collection<DataElement> fromDataElements) {
if (!dataElements || !fromDataElements) throw new ApiInternalException('DESXX', 'No DataElements or FromDataElements exist to remove links')
List<SemanticLink> links = semanticLinkService.findAllBySourceMultiFacetAwareItemIdInListAndTargetMultiFacetAwareItemIdInListAndLinkType(
dataElements*.id, fromDataElements*.id, SemanticLinkType.IS_FROM)
semanticLinkService.deleteAll(links)
}
void setDataElementIsFromDataElement(DataElement source, DataElement target, User user) {
source.addToSemanticLinks(linkType: SemanticLinkType.IS_FROM, createdBy: user.getEmailAddress(), targetMultiFacetAwareItem: target)
}
/**
* Find a DataElement which is labeled with label and which belongs to dataClass.
* @param dataClass The DataClass which is the parent of the DataElement being sought
* @param label The label of the DataElement being sought
*/
DataElement findDataElement(DataClass dataClass, String label) {
dataClass.dataElements.find {it.label == label.trim()}
}
/**
* Find a DataElement which is labeled with label and whose parent is parentCatalogueItem. The parentCatalogueItem
* can be a DataClass.
* @param parentCatalogueItem The DataClass which is the parent of the DataElement being sought
* @param label The label of the DataElement being sought
*/
@Override
DataElement findByParentIdAndLabel(UUID parentId, String label) {
findByDataClassIdAndLabel(parentId, label)
}
@Override
List<DataElement> findAllByMetadataNamespaceAndKey(String namespace, String key, Map pagination) {
DataElement.byMetadataNamespaceAndKey(namespace, key).list(pagination)
}
@Override
List<DataElement> findAllByMetadataNamespace(String namespace, Map pagination) {
DataElement.byMetadataNamespace(namespace).list(pagination)
}
@Override
void propagateContentsInformation(DataElement catalogueItem, DataElement previousVersionCatalogueItem) {
previousVersionCatalogueItem.summaryMetadata.each {previousSummaryMetadata ->
if (catalogueItem.summaryMetadata.any {it.label == previousSummaryMetadata.label}) return
SummaryMetadata summaryMetadata = new SummaryMetadata(label: previousSummaryMetadata.label,
description: previousSummaryMetadata.description,
summaryMetadataType: previousSummaryMetadata.summaryMetadataType)
previousSummaryMetadata.summaryMetadataReports.each {previousSummaryMetadataReport ->
summaryMetadata.addToSummaryMetadataReports(reportDate: previousSummaryMetadataReport.reportDate,
reportValue: previousSummaryMetadataReport.reportValue,
createdBy: previousSummaryMetadataReport.createdBy
)
}
catalogueItem.addToSummaryMetadata(summaryMetadata)
}
}
@Override
CopyInformation cacheFacetInformationForCopy(List<UUID> originalIds, CopyInformation copyInformation = null) {
CopyInformation cachedInformation = super.cacheFacetInformationForCopy(originalIds, copyInformation)
cacheSummaryMetadataInformationForCopy(originalIds, cachedInformation)
}
@Override
void preBatchSaveHandling(List<DataElement> modelItems) {
// Fix HS issue around non-session loaded DT.
// This may have an adverse effect on saving, which will need to be tested
dataTypeService.getAll(modelItems.collect {it.dataType.id})
}
/**
* Special handler to apply a modification patch to a DataType.
* In the diff we set a mergeField called dataTypePath. In this method we use that dataTypePath to find the
* relevant DataType.
* 1. Find the data type in source
* 2. Use the path of that data type to find the same data type in target
* 3. Set the data type of the target data element to be that data type
*
* Note that this leads to a merge conflict in in the following scenario (as highlighted by test MD05 in DataModelFunctionalSpec):
* 1. Common ancestor model has dataType1 and dataType2. dataElement has a dataType of dataType1.
* 2. Create source and main branches from the common ancestor
* 3. On source branch, change the dataElement to have a dataType of dataType2
* 4. Merge the source into main.
* 5. Make some more other changes to the source branch
* 6. Get the mergeDiff for source into main again.
* The mergeDiff looks like
*{* "fieldName": "dataTypePath",
* "path": "dm:Functional Test DataModel 1$source|dc:existingClass|de:existingDataElement@dataTypePath",
* "sourceValue": "dm:Functional Test DataModel 1$source|dt:existingDataType2",
* "targetValue": "dm:Functional Test DataModel 1$main|dt:existingDataType2",
* "commonAncestorValue": "dm:Functional Test DataModel 1$1.0.0|dt:existingDataType1",
* "isMergeConflict": true,
* "type": "modification"
*}* because both source and main have a different data type to the one in common ancestor.
* @param modificationPatch
* @param targetDomain
* @param fieldName
* @return
*/
@Override
boolean handlesModificationPatchOfField(FieldPatchData modificationPatch, MdmDomain targetBeingPatched, DataElement targetDomain, String fieldName) {
if (fieldName == 'dataTypePath') {
// This is the dataType that has been changed on the source
DataType sourceDataType = pathService.findResourceByPath(Path.from(modificationPatch.sourceValue))
if (!sourceDataType) {
throw new ApiInternalException('DES01', "Cannot find DataType with path ${modificationPatch.sourceValue}")
}
// We need the equivalent (i.e. matches by path) dataType on the target
DataType targetDataType = pathService.findResourceByPathFromRootResource(targetDomain.dataClass.dataModel, sourceDataType.getPath()
.getChildPath())
if (targetDataType) {
targetDomain.dataType = targetDataType
return true
} else {
throw new ApiInternalException(
'DES02',
"Cannot find DataType with path ${sourceDataType.getPath().getChildPath()} on target DataModel ${targetDomain.dataClass.dataModel.id}")
}
}
false
}
}