This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
DictionaryDAOImpl.java
1047 lines (919 loc) · 32.6 KB
/
DictionaryDAOImpl.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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* #%L
* Alfresco Data model classes
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.dictionary;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.service.cmr.dictionary.AspectDefinition;
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ClassDefinition;
import org.alfresco.service.cmr.dictionary.ConstraintDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryException;
import org.alfresco.service.cmr.dictionary.ModelDefinition;
import org.alfresco.service.cmr.dictionary.NamespaceDefinition;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
/**
* Default implementation of the Dictionary.
*
* @author David Caruana, janv, sglover
*
*/
// TODO deal with destroy of core dictionary registry i.e. do we remove all
// tenant dictionary registries too?
public class DictionaryDAOImpl implements DictionaryDAO, NamespaceDAO,
ApplicationListener<ApplicationEvent>
{
// Tenant Service
private TenantService tenantService;
// used to reset the cache
private ThreadLocal<Map<String, DictionaryRegistry>> dictionaryRegistryThreadLocal = new ThreadLocal<Map<String, DictionaryRegistry>>();
// Internal cache (clusterable)
private CompiledModelsCache dictionaryRegistryCache;
// Static list of registered dictionary listeners
private List<DictionaryListener> dictionaryListeners = new ArrayList<DictionaryListener>();
private ReadWriteLock dictionaryListenersLock = new ReentrantReadWriteLock();
// Logger
private static Log logger = LogFactory.getLog(DictionaryDAO.class);
private String defaultAnalyserResourceBundleName;
private ClassLoader resourceClassLoader;
// inject dependencies
public void setTenantService(TenantService tenantService)
{
this.tenantService = tenantService;
}
public void setDictionaryRegistryCache(
CompiledModelsCache dictionaryRegistryCache)
{
this.dictionaryRegistryCache = dictionaryRegistryCache;
}
@Override
public String getDefaultAnalyserResourceBundleName()
{
return defaultAnalyserResourceBundleName;
}
public void setDefaultAnalyserResourceBundleName(
String defaultAnalyserResourceBundleName)
{
this.defaultAnalyserResourceBundleName = defaultAnalyserResourceBundleName;
}
/**
* Construct
*
*/
public DictionaryDAOImpl()
{
}
/**
* Register listener with the Dictionary
* <p>
* This method is <b>deprecated</b>, use {@link #registerListener(DictionaryListener dictionaryListener)} instead.
* </p>
* @param dictionaryListener
*/
@Override
@Deprecated
public void register(DictionaryListener dictionaryListener)
{
registerListener(dictionaryListener);
}
/**
* Register with the Dictionary
*/
@Override
public void registerListener(DictionaryListener dictionaryListener)
{
this.dictionaryListenersLock.writeLock().lock();
try
{
if (!dictionaryListeners.contains(dictionaryListener))
{
dictionaryListeners.add(dictionaryListener);
}
}
finally
{
this.dictionaryListenersLock.writeLock().unlock();
}
}
@Override
public List<DictionaryListener> getDictionaryListeners()
{
// need to hold read lock here
this.dictionaryListenersLock.readLock().lock();
try
{
return new ArrayList<DictionaryListener>(dictionaryListeners);
}
finally
{
this.dictionaryListenersLock.readLock().unlock();
}
}
private Map<String, DictionaryRegistry> getThreadLocal()
{
Map<String, DictionaryRegistry> map = dictionaryRegistryThreadLocal
.get();
if (map == null)
{
map = new HashMap<String, DictionaryRegistry>();
dictionaryRegistryThreadLocal.set(map);
}
return map;
}
private DictionaryRegistry createCoreDictionaryRegistry()
{
DictionaryRegistry dictionaryRegistry = new CoreDictionaryRegistryImpl(
this);
return dictionaryRegistry;
}
private DictionaryRegistry createTenantDictionaryRegistry(
final String tenant)
{
DictionaryRegistry result = AuthenticationUtil.runAs(
new RunAsWork<DictionaryRegistry>()
{
public DictionaryRegistry doWork()
{
DictionaryRegistry dictionaryRegistry = new TenantDictionaryRegistryImpl(
DictionaryDAOImpl.this, tenant);
return dictionaryRegistry;
}
}, tenantService.getDomainUser(
AuthenticationUtil.getSystemUserName(), tenant));
return result;
}
@Override
public void init()
{
String tenant = tenantService.getCurrentUserDomain();
dictionaryRegistryCache.forceInChangesForThisUncommittedTransaction(tenant);
if (logger.isDebugEnabled())
{
logger.debug("Triggered immediate reload of dictionary for tenant " + tenant);
}
}
@Override
public void destroy()
{
String tenant = tenantService.getCurrentUserDomain();
// TODO Should be reworked when ACE-2001 will be implemented
dictionaryRegistryCache.remove(tenant);
if (logger.isDebugEnabled())
{
logger.debug("Dictionary destroyed for tenant " + tenant);
}
}
@Override
public void reset()
{
String tenant = tenantService.getCurrentUserDomain();
if (logger.isDebugEnabled())
{
logger.debug("Resetting dictionary for tenant " + tenant);
}
destroy();
// Ensure that we have a dictionary available right now
getDictionaryRegistry(tenant);
if (logger.isDebugEnabled())
{
logger.debug("Dictionary reset complete for tenant " + tenant);
}
}
@Override
public QName putModel(M2Model model)
{
// the core registry is not yet initialised so put it in the core registry
QName ret = putModelImpl(model, true);
return ret;
}
@Override
public QName putModelIgnoringConstraints(M2Model model)
{
return putModelImpl(model, false);
}
private QName putModelImpl(M2Model model, boolean enableConstraintClassLoading)
{
// Compile model definition
CompiledModel compiledModel = model.compile(this, this,
enableConstraintClassLoading);
QName modelName = compiledModel.getModelDefinition().getName();
getTenantDictionaryRegistry().putModel(compiledModel);
if (logger.isTraceEnabled())
{
logger.trace("Registered core model: "
+ modelName.toPrefixString(this));
for (M2Namespace namespace : model.getNamespaces())
{
logger.trace("Registered core namespace: '"
+ namespace.getUri() + "' (prefix '"
+ namespace.getPrefix() + "')");
}
}
return modelName;
}
/**
* @see org.alfresco.repo.dictionary.DictionaryDAO#removeModel(org.alfresco.service.namespace.QName)
*/
public void removeModel(QName modelName)
{
getTenantDictionaryRegistry().removeModel(modelName);
}
private DictionaryRegistry getTenantDictionaryRegistry()
{
String tenantDomain = tenantService.getCurrentUserDomain();
return getDictionaryRegistry(tenantDomain);
}
/**
* @param modelName
* the model name
* @return the compiled model of the given name
*/
public CompiledModel getCompiledModel(QName modelName)
{
return getTenantDictionaryRegistry().getModel(modelName);
}
@Override
public DataTypeDefinition getDataType(QName typeName)
{
DataTypeDefinition dataTypeDef = null;
if (typeName != null)
{
dataTypeDef = getTenantDictionaryRegistry().getDataType(typeName);
}
return dataTypeDef;
}
@SuppressWarnings("rawtypes")
@Override
public DataTypeDefinition getDataType(Class javaClass)
{
return getTenantDictionaryRegistry().getDataType(javaClass);
}
@Override
public Collection<DataTypeDefinition> getDataTypes(QName modelName)
{
CompiledModel model = getCompiledModel(modelName);
return model.getDataTypes();
}
@Override
public TypeDefinition getType(QName typeName)
{
TypeDefinition typeDef = null;
if (typeName != null)
{
typeDef = getTenantDictionaryRegistry().getType(typeName);
}
return typeDef;
}
@Override
public Collection<QName> getSubTypes(QName superType, boolean follow)
{
// note: could be optimised further, if compiled into the model
// Get all types (with parent type) for all models
Map<QName, QName> allTypesAndParents = new HashMap<QName, QName>(); // name,
// parent
for (CompiledModel model : getCompiledModels(true).values())
{
for (TypeDefinition type : model.getTypes())
{
allTypesAndParents.put(type.getName(), type.getParentName());
}
}
// Get sub types
HashSet<QName> subTypes = new HashSet<QName>();
for (QName type : allTypesAndParents.keySet())
{
if (follow)
{
// all sub types
QName current = type;
while ((current != null) && !current.equals(superType))
{
current = allTypesAndParents.get(current); // get parent
}
if (current != null)
{
subTypes.add(type);
}
}
else
{
// immediate sub types only
QName typesSuperType = allTypesAndParents.get(type);
if (typesSuperType != null && typesSuperType.equals(superType))
{
subTypes.add(type);
}
}
}
return subTypes;
}
@Override
public AspectDefinition getAspect(QName aspectName)
{
AspectDefinition aspectDef = null;
if (aspectName != null)
{
aspectDef = getTenantDictionaryRegistry().getAspect(aspectName);
}
return aspectDef;
}
@Override
public Collection<QName> getSubAspects(QName superAspect, boolean follow)
{
// note: could be optimised further, if compiled into the model
// Get all aspects (with parent aspect) for all models
Map<QName, QName> allAspectsAndParents = new HashMap<QName, QName>(); // name,
// parent
for (CompiledModel model : getCompiledModels(true).values())
{
for (AspectDefinition aspect : model.getAspects())
{
allAspectsAndParents.put(aspect.getName(),
aspect.getParentName());
}
}
// Get sub aspects
HashSet<QName> subAspects = new HashSet<QName>();
for (QName aspect : allAspectsAndParents.keySet())
{
if (follow)
{
// all sub aspects
QName current = aspect;
while ((current != null) && !current.equals(superAspect))
{
current = allAspectsAndParents.get(current); // get parent
}
if (current != null)
{
subAspects.add(aspect);
}
}
else
{
// immediate sub aspects only
QName typesSuperAspect = allAspectsAndParents.get(aspect);
if (typesSuperAspect != null
&& typesSuperAspect.equals(superAspect))
{
subAspects.add(aspect);
}
}
}
return subAspects;
}
@Override
public ClassDefinition getClass(QName className)
{
ClassDefinition classDef = null;
if (className != null)
{
classDef = getTenantDictionaryRegistry().getClass(className);
}
return classDef;
}
@Override
public PropertyDefinition getProperty(QName propertyName)
{
PropertyDefinition propertyDef = null;
if (propertyName != null)
{
propertyDef = getTenantDictionaryRegistry().getProperty(
propertyName);
}
return propertyDef;
}
@Override
public ConstraintDefinition getConstraint(QName constraintQName)
{
ConstraintDefinition constraintDef = null;
if (constraintQName != null)
{
constraintDef = getTenantDictionaryRegistry().getConstraint(
constraintQName);
}
return constraintDef;
}
@Override
public AssociationDefinition getAssociation(QName assocName)
{
return getTenantDictionaryRegistry().getAssociation(assocName);
}
public Collection<AssociationDefinition> getAssociations(QName modelName)
{
CompiledModel model = getCompiledModel(modelName);
return model.getAssociations();
}
public Collection<QName> getModels(boolean includeInherited)
{
// get all models - including inherited models, if applicable
return getCompiledModels(includeInherited).keySet();
}
@Override
public Collection<QName> getModels()
{
// get all models - including inherited models, if applicable
return getModels(true);
}
public Collection<QName> getTypes(boolean includeInherited)
{
return getTenantDictionaryRegistry().getTypes(includeInherited);
}
public Collection<QName> getAssociations(boolean includeInherited)
{
return getTenantDictionaryRegistry().getAssociations(includeInherited);
}
public Collection<QName> getAspects(boolean includeInherited)
{
return getTenantDictionaryRegistry().getAspects(includeInherited);
}
// MT-specific
public boolean isModelInherited(QName modelName)
{
return getTenantDictionaryRegistry().isModelInherited(modelName);
}
private Map<QName, CompiledModel> getCompiledModels(boolean includeInherited)
{
return getTenantDictionaryRegistry().getCompiledModels(includeInherited);
}
@Override
public ModelDefinition getModel(QName name)
{
CompiledModel model = getCompiledModel(name);
return model.getModelDefinition();
}
@Override
public Collection<TypeDefinition> getTypes(QName modelName)
{
CompiledModel model = getCompiledModel(modelName);
return model.getTypes();
}
@Override
public Collection<AspectDefinition> getAspects(QName modelName)
{
CompiledModel model = getCompiledModel(modelName);
return model.getAspects();
}
@Override
public TypeDefinition getAnonymousType(QName type, Collection<QName> aspects)
{
TypeDefinition typeDef = getType(type);
if (typeDef == null)
{
throw new DictionaryException(
"d_dictionary.model.err.type_not_found", type);
}
Collection<AspectDefinition> aspectDefs = new ArrayList<AspectDefinition>();
if (aspects != null)
{
for (QName aspect : aspects)
{
AspectDefinition aspectDef = getAspect(aspect);
if (aspectDef == null)
{
throw new DictionaryException(
"d_dictionary.model.err.aspect_not_found", aspect);
}
aspectDefs.add(aspectDef);
}
}
return new M2AnonymousTypeDefinition(typeDef, aspectDefs);
}
@Override
public Collection<PropertyDefinition> getProperties(QName modelName)
{
CompiledModel model = getCompiledModel(modelName);
return model.getProperties();
}
@Override
public Collection<PropertyDefinition> getProperties(QName modelName,
QName dataType)
{
HashSet<PropertyDefinition> properties = new HashSet<PropertyDefinition>();
Collection<PropertyDefinition> props = getProperties(modelName);
for (PropertyDefinition prop : props)
{
if ((dataType == null)
|| prop.getDataType().getName().equals(dataType))
{
properties.add(prop);
}
}
return properties;
}
@Override
public Collection<PropertyDefinition> getPropertiesOfDataType(QName dataType)
{
Collection<PropertyDefinition> properties = new HashSet<PropertyDefinition>();
Collection<QName> modelNames = getModels();
for (QName modelName : modelNames)
{
properties.addAll(getProperties(modelName, dataType));
}
return properties;
}
@Override
public Collection<NamespaceDefinition> getNamespaces(QName modelName)
{
CompiledModel model = getCompiledModel(modelName);
ModelDefinition modelDef = model.getModelDefinition();
return modelDef.getNamespaces();
}
@Override
public Collection<ConstraintDefinition> getConstraints(QName modelName)
{
return getConstraints(modelName, false);
}
public Collection<ConstraintDefinition> getConstraints(QName modelName,
boolean referenceableDefsOnly)
{
CompiledModel model = getCompiledModel(modelName);
if (referenceableDefsOnly)
{
return getReferenceableConstraintDefs(model);
}
else
{
return model.getConstraints();
}
}
private Collection<ConstraintDefinition> getReferenceableConstraintDefs(
CompiledModel model)
{
Collection<ConstraintDefinition> conDefs = model.getConstraints();
Collection<PropertyDefinition> propDefs = model.getProperties();
for (PropertyDefinition propDef : propDefs)
{
for (ConstraintDefinition conDef : propDef.getConstraints())
{
conDefs.remove(conDef);
}
}
return conDefs;
}
// re-entrant (eg. via reset)
@Override
public DictionaryRegistry getDictionaryRegistry(String tenantDomain)
{
DictionaryRegistry dictionaryRegistry = null;
if (tenantDomain == null)
{
throw new AlfrescoRuntimeException("Tenant must be set");
}
// check threadlocal first - return if set
dictionaryRegistry = getThreadLocal().get(tenantDomain);
if (dictionaryRegistry == null)
{
dictionaryRegistry = dictionaryRegistryCache.get(tenantDomain);
}
return dictionaryRegistry;
}
/**
* For cache use only.
*
* @param tenantDomain String
* @return constructed DictionaryRegistry
*/
public DictionaryRegistry initDictionaryRegistry(final String tenantDomain)
{
return AuthenticationUtil.runAs(
new RunAsWork<DictionaryRegistry>()
{
public DictionaryRegistry doWork()
{
DictionaryRegistry dictionaryRegistry = null;
if (tenantDomain.equals(TenantService.DEFAULT_DOMAIN))
{
dictionaryRegistry = createCoreDictionaryRegistry();
}
else
{
dictionaryRegistry = createTenantDictionaryRegistry(tenantDomain);
}
getThreadLocal().put(tenantDomain, dictionaryRegistry);
dictionaryRegistry.init();
getThreadLocal().remove(tenantDomain);
return dictionaryRegistry;
}
},
tenantService.getDomainUser(
AuthenticationUtil.getSystemUserName(), tenantDomain));
}
/**
* Return diffs between input model and model in the Dictionary.
*
* If the input model does not exist in the Dictionary then no diffs will be
* returned.
*
* @param model M2Model
* @return model diffs (if any)
*/
public List<M2ModelDiff> diffModel(M2Model model)
{
return diffModel(model, true);
}
public List<M2ModelDiff> diffModelIgnoringConstraints(M2Model model)
{
return diffModel(model, false);
}
/**
* Return diffs between input model and model in the Dictionary.
*
* If the input model does not exist in the Dictionary then no diffs will be
* returned.
*
* @param model M2Model
* @param enableConstraintClassLoading boolean
* @return model diffs (if any)
*/
public List<M2ModelDiff> diffModel(M2Model model,
boolean enableConstraintClassLoading)
{
// Compile model definition
CompiledModel compiledModel = model.compile(this, this,
enableConstraintClassLoading);
QName modelName = compiledModel.getModelDefinition().getName();
CompiledModel previousVersion = null;
try
{
previousVersion = getCompiledModel(modelName);
}
catch (DictionaryException e)
{
// ignore missing model, there's no need to warn about this.
logger.debug(e);
}
if (previousVersion == null)
{
return new ArrayList<M2ModelDiff>(0);
}
else
{
return diffModel(previousVersion, compiledModel);
}
}
/**
* Return diffs between two compiled models.
*
* note: - checks classes (types & aspects) for incremental updates - checks
* properties for incremental updates, but does not include the diffs -
* checks assocs & child assocs for incremental updates, but does not
* include the diffs - incremental updates include changes in
* title/description, property default value, etc - ignores changes in model
* definition except name (ie. title, description, author, published date,
* version are treated as an incremental update)
*
* TODO - imports - namespace - datatypes - constraints (including property
* constraints - references and inline)
*
* @param previousVersion CompiledModel
* @param model CompiledModel
* @return model diffs (if any)
*/
/* package */List<M2ModelDiff> diffModel(CompiledModel previousVersion,
CompiledModel model)
{
List<M2ModelDiff> M2ModelDiffs = new ArrayList<M2ModelDiff>();
if (previousVersion != null)
{
Collection<TypeDefinition> previousTypes = previousVersion
.getTypes();
Collection<AspectDefinition> previousAspects = previousVersion
.getAspects();
Collection<ConstraintDefinition> previousConDefs = getReferenceableConstraintDefs(previousVersion);
Collection<NamespaceDefinition> previousImportedNamespaces = previousVersion.getModelDefinition().getImportedNamespaces();
if (model == null)
{
// delete model
for (TypeDefinition previousType : previousTypes)
{
M2ModelDiffs.add(new M2ModelDiff(previousType.getName(),
M2ModelDiff.TYPE_TYPE, M2ModelDiff.DIFF_DELETED));
}
for (AspectDefinition previousAspect : previousAspects)
{
M2ModelDiffs.add(new M2ModelDiff(previousAspect.getName(),
M2ModelDiff.TYPE_ASPECT, M2ModelDiff.DIFF_DELETED));
}
for (ConstraintDefinition previousConDef : previousConDefs)
{
M2ModelDiffs.add(new M2ModelDiff(previousConDef.getName(),
M2ModelDiff.TYPE_CONSTRAINT,
M2ModelDiff.DIFF_DELETED));
}
}
else
{
// update model
Collection<TypeDefinition> types = model.getTypes();
Collection<AspectDefinition> aspects = model.getAspects();
Collection<ConstraintDefinition> conDefs = getReferenceableConstraintDefs(model);
Collection<NamespaceDefinition> importedNamespaces = model.getModelDefinition().getImportedNamespaces();
if (previousTypes.size() != 0)
{
M2ModelDiffs.addAll(M2ClassDefinition.diffClassLists(
new ArrayList<ClassDefinition>(previousTypes),
new ArrayList<ClassDefinition>(types),
M2ModelDiff.TYPE_TYPE));
}
else
{
for (TypeDefinition type : types)
{
M2ModelDiffs
.add(new M2ModelDiff(type.getName(),
M2ModelDiff.TYPE_TYPE,
M2ModelDiff.DIFF_CREATED));
}
}
if (previousAspects.size() != 0)
{
M2ModelDiffs.addAll(M2ClassDefinition.diffClassLists(
new ArrayList<ClassDefinition>(previousAspects),
new ArrayList<ClassDefinition>(aspects),
M2ModelDiff.TYPE_ASPECT));
}
else
{
for (AspectDefinition aspect : aspects)
{
M2ModelDiffs.add(new M2ModelDiff(aspect.getName(),
M2ModelDiff.TYPE_ASPECT,
M2ModelDiff.DIFF_CREATED));
}
}
if (previousConDefs.size() != 0)
{
M2ModelDiffs
.addAll(M2ConstraintDefinition
.diffConstraintLists(
new ArrayList<ConstraintDefinition>(
previousConDefs),
new ArrayList<ConstraintDefinition>(
conDefs)));
}
else
{
for (ConstraintDefinition conDef : conDefs)
{
M2ModelDiffs.add(new M2ModelDiff(conDef.getName(),
M2ModelDiff.TYPE_CONSTRAINT,
M2ModelDiff.DIFF_CREATED));
}
}
if (previousImportedNamespaces.size() != 0)
{
M2ModelDiffs
.addAll(M2NamespaceDefinition
.diffNamespaceDefinitionLists(
new ArrayList<NamespaceDefinition>(
previousImportedNamespaces),
new ArrayList<NamespaceDefinition>(
importedNamespaces)));
}
else
{
for(NamespaceDefinition namespaceDefinition: importedNamespaces)
{
M2ModelDiffs.add(new M2ModelDiff(namespaceDefinition.getModel().getName(),
namespaceDefinition,
M2ModelDiff.TYPE_NAMESPACE,
M2ModelDiff.DIFF_CREATED));
}
}
}
}
else
{
if (model != null)
{
// new model
Collection<TypeDefinition> types = model.getTypes();
Collection<AspectDefinition> aspects = model.getAspects();
for (TypeDefinition type : types)
{
M2ModelDiffs.add(new M2ModelDiff(type.getName(),
M2ModelDiff.TYPE_TYPE, M2ModelDiff.DIFF_CREATED));
}
for (AspectDefinition aspect : aspects)
{
M2ModelDiffs.add(new M2ModelDiff(aspect.getName(),
M2ModelDiff.TYPE_ASPECT, M2ModelDiff.DIFF_CREATED));
}
}
else
{
// nothing to diff
}
}
return M2ModelDiffs;
}
@Override
public ClassLoader getResourceClassLoader()
{
return resourceClassLoader;
}
@Override
public void setResourceClassLoader(ClassLoader resourceClassLoader)
{
this.resourceClassLoader = resourceClassLoader;
}
@Override
public String getNamespaceURI(String prefix)
{
return getTenantDictionaryRegistry().getNamespaceURI(prefix);
}
@Override
public Collection<String> getPrefixes(String URI)
{
return getTenantDictionaryRegistry().getPrefixes(URI);
}
@Override
public void addURI(String uri)
{
getTenantDictionaryRegistry().addURI(uri);
}
@Override
public Collection<String> getPrefixes()