This repository has been archived by the owner on Oct 27, 2020. It is now read-only.
/
NodeDAOImpl.java
1911 lines (1712 loc) · 69.1 KB
/
NodeDAOImpl.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 Repository
* %%
* 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.domain.node.ibatis;
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.Set;
import java.util.SortedSet;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.ibatis.IdsEntity;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.domain.node.AbstractNodeDAOImpl;
import org.alfresco.repo.domain.node.ChildAssocEntity;
import org.alfresco.repo.domain.node.ChildPropertyEntity;
import org.alfresco.repo.domain.node.Node;
import org.alfresco.repo.domain.node.NodeAspectsEntity;
import org.alfresco.repo.domain.node.NodeAssocEntity;
import org.alfresco.repo.domain.node.NodeEntity;
import org.alfresco.repo.domain.node.NodeExistsException;
import org.alfresco.repo.domain.node.NodeIdAndAclId;
import org.alfresco.repo.domain.node.NodePropertyEntity;
import org.alfresco.repo.domain.node.NodePropertyKey;
import org.alfresco.repo.domain.node.NodePropertyValue;
import org.alfresco.repo.domain.node.NodeUpdateEntity;
import org.alfresco.repo.domain.node.NodeVersionKey;
import org.alfresco.repo.domain.node.PrimaryChildrenAclUpdateEntity;
import org.alfresco.repo.domain.node.ServerEntity;
import org.alfresco.repo.domain.node.StoreEntity;
import org.alfresco.repo.domain.node.Transaction;
import org.alfresco.repo.domain.node.TransactionEntity;
import org.alfresco.repo.domain.node.TransactionQueryEntity;
import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.apache.ibatis.executor.result.DefaultResultContext;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.util.Assert;
/**
* iBatis-specific extension of the Node abstract DAO
*
* @author Derek Hulley
* @since 3.4
*/
public class NodeDAOImpl extends AbstractNodeDAOImpl
{
private static final String SELECT_SERVER_BY_IPADDRESS = "alfresco.node.select_ServerByIpAddress";
private static final String INSERT_SERVER = "alfresco.node.insert.insert_Server";
private static final String INSERT_TRANSACTION = "alfresco.node.insert.insert_Transaction";
private static final String UPDATE_TRANSACTION_COMMIT_TIME = "alfresco.node.update_TransactionCommitTime";
private static final String DELETE_TRANSACTION_BY_ID = "alfresco.node.delete_TransactionById";
private static final String INSERT_STORE = "alfresco.node.insert.insert_Store";
private static final String UPDATE_STORE_ROOT = "alfresco.node.update_StoreRoot";
private static final String UPDATE_STORE = "alfresco.node.update_Store";
private static final String UPDATE_NODES_IN_STORE = "alfresco.node.update_NodesInStore";
private static final String SELECT_STORE_ALL = "alfresco.node.select_StoreAll";
private static final String SELECT_STORE_BY_REF = "alfresco.node.select_StoreByRef";
private static final String SELECT_STORE_ROOT_NODE_BY_REF = "alfresco.node.select_StoreRootNodeByRef";
private static final String INSERT_NODE = "alfresco.node.insert.insert_Node";
private static final String UPDATE_NODE = "alfresco.node.update_Node";
private static final String UPDATE_NODE_BULK_TOUCH = "alfresco.node.update_NodeBulkTouch";
private static final String DELETE_NODE_BY_ID = "alfresco.node.delete_NodeById";
private static final String DELETE_NODES_BY_TXN_COMMIT_TIME = "alfresco.node.delete.delete_NodesByTxnCommitTime";
private static final String DELETE_NODE_PROPS_BY_TXN_COMMIT_TIME = "alfresco.node.delete.delete_NodePropsByTxnCommitTime";
private static final String SELECT_NODE_BY_ID = "alfresco.node.select_NodeById";
private static final String SELECT_NODE_BY_NODEREF = "alfresco.node.select_NodeByNodeRef";
private static final String SELECT_NODES_BY_UUIDS = "alfresco.node.select_NodesByUuids";
private static final String SELECT_NODES_BY_IDS = "alfresco.node.select_NodesByIds";
private static final String SELECT_NODE_PROPERTIES = "alfresco.node.select_NodeProperties";
private static final String SELECT_PROPERTIES_BY_TYPES = "alfresco.node.select_PropertiesByTypes";
private static final String SELECT_PROPERTIES_BY_ACTUAL_TYPE = "alfresco.node.select_PropertiesByActualType";
private static final String SELECT_NODE_ASPECTS = "alfresco.node.select_NodeAspects";
private static final String INSERT_NODE_PROPERTY = "alfresco.node.insert_NodeProperty";
private static final String UPDATE_PRIMARY_CHILDREN_SHARED_ACL = "alfresco.node.update.update_PrimaryChildrenSharedAcl";
private static final String INSERT_NODE_ASPECT = "alfresco.node.insert_NodeAspect";
private static final String DELETE_NODE_ASPECTS = "alfresco.node.delete_NodeAspects";
private static final String DELETE_NODE_PROPERTIES = "alfresco.node.delete_NodeProperties";
private static final String SELECT_NODE_MIN_ID = "alfresco.node.select_NodeMinId";
private static final String SELECT_NODE_MAX_ID = "alfresco.node.select_NodeMaxId";
private static final String SELECT_NODE_INTERVAL_BY_TYPE = "alfresco.node.select_MinMaxNodeIdForNodeType";
private static final String SELECT_NODES_WITH_ASPECT_IDS = "alfresco.node.select_NodesWithAspectIds";
private static final String INSERT_NODE_ASSOC = "alfresco.node.insert.insert_NodeAssoc";
private static final String UPDATE_NODE_ASSOC = "alfresco.node.update_NodeAssoc";
private static final String DELETE_NODE_ASSOC = "alfresco.node.delete_NodeAssoc";
private static final String DELETE_NODE_ASSOCS = "alfresco.node.delete_NodeAssocs";
private static final String SELECT_NODE_ASSOCS = "alfresco.node.select_NodeAssocs";
private static final String SELECT_NODE_ASSOCS_BY_SOURCE_AND_PROPERTY_VALUE = "alfresco.node.select_NodeAssocsBySourceAndPropertyValue";
private static final String SELECT_NODE_ASSOCS_BY_TARGET = "alfresco.node.select_NodeAssocsByTarget";
private static final String SELECT_NODE_ASSOC_BY_ID = "alfresco.node.select_NodeAssocById";
private static final String SELECT_NODE_ASSOCS_MAX_INDEX = "alfresco.node.select_NodeAssocsMaxId";
private static final String SELECT_CHILD_NODE_IDS = "alfresco.node.select.children.select_ChildNodeIds_Limited";
private static final String SELECT_NODE_PRIMARY_CHILD_ACLS = "alfresco.node.select_NodePrimaryChildAcls";
private static final String INSERT_CHILD_ASSOC = "alfresco.node.insert.insert_ChildAssoc";
private static final String DELETE_CHILD_ASSOCS = "alfresco.node.delete_ChildAssocs";
private static final String UPDATE_CHILD_ASSOCS_INDEX = "alfresco.node.update_ChildAssocsIndex";
private static final String UPDATE_CHILD_ASSOC_UNIQUE_NAME = "alfresco.node.update_ChildAssocUniqueName";
private static final String SELECT_CHILD_ASSOC_BY_ID = "alfresco.node.select_ChildAssocById";
private static final String COUNT_CHILD_ASSOC_BY_PARENT_ID = "alfresco.node.count_ChildAssocByParentId";
private static final String SELECT_CHILD_ASSOCS_BY_PROPERTY_VALUE = "alfresco.node.select_ChildAssocsByPropertyValue";
private static final String SELECT_CHILD_ASSOCS_OF_PARENT = "alfresco.node.select_ChildAssocsOfParent";
private static final String SELECT_CHILD_ASSOCS_OF_PARENT_LIMITED = "alfresco.node.select.children.select_ChildAssocsOfParent_Limited";
private static final String SELECT_CHILD_ASSOC_OF_PARENT_BY_NAME = "alfresco.node.select_ChildAssocOfParentByName";
private static final String SELECT_CHILD_ASSOCS_OF_PARENT_WITHOUT_PARENT_ASSOCS_OF_TYPE =
"alfresco.node.select_ChildAssocsOfParentWithoutParentAssocsOfType";
private static final String SELECT_CHILD_ASSOCS_OF_PARENT_WITHOUT_NODE_ASSOCS_OF_TYPE =
"alfresco.node.select_ChildAssocsOfParentWithoutNodeAssocsOfType";
private static final String SELECT_PARENT_ASSOCS_OF_CHILD = "alfresco.node.select_ParentAssocsOfChild";
private static final String UPDATE_PARENT_ASSOCS_OF_CHILD = "alfresco.node.update_ParentAssocsOfChild";
private static final String DELETE_SUBSCRIPTIONS = "alfresco.node.delete_NodeSubscriptions";
private static final String UPDATE_MOVE_PARENT_ASSOCS = "alfresco.node.update_MoveParentAssocs";
private static final String UPDATE_MOVE_CHILD_ASSOCS = "alfresco.node.update_MoveChildAssocs";
private static final String UPDATE_MOVE_SOURCE_ASSOCS = "alfresco.node.update_MoveSourceAssocs";
private static final String UPDATE_MOVE_TARGET_ASSOCS = "alfresco.node.update_MoveTargetAssocs";
private static final String UPDATE_MOVE_PROPERTIES = "alfresco.node.update_MoveProperties";
private static final String UPDATE_MOVE_ASPECTS = "alfresco.node.update_MoveAspects";
private static final String SELECT_TXN_LAST = "alfresco.node.select_TxnLast";
private static final String SELECT_TXN_NODES = "alfresco.node.select_TxnNodes";
private static final String SELECT_TXNS = "alfresco.node.select_Txns";
private static final String SELECT_TXN_COUNT = "alfresco.node.select_TxnCount";
private static final String SELECT_TXNS_UNUSED = "alfresco.node.select_TxnsUnused";
private static final String DELETE_TXNS_UNUSED = "alfresco.node.delete_Txns_Unused";
private static final String SELECT_TXN_MIN_COMMIT_TIME = "alfresco.node.select_TxnMinCommitTime";
private static final String SELECT_TXN_MAX_COMMIT_TIME = "alfresco.node.select_TxnMaxCommitTime";
private static final String SELECT_TXN_MIN_COMMIT_TIME_FOR_NODE_TYPE = "alfresco.node.select_TxnMinCommitTimeForNodeType";
private static final String SELECT_TXN_MIN_ID = "alfresco.node.select_TxnMinId";
private static final String SELECT_TXN_MAX_ID = "alfresco.node.select_TxnMaxId";
private static final String SELECT_TXN_UNUSED_MIN_COMMIT_TIME = "alfresco.node.select_TxnMinUnusedCommitTime";
private static final String SELECT_ONE_TXNS_BY_COMMIT_TIME_DESC = "alfresco.node.select_OneTxnsByCommitTimeDescending";
protected QNameDAO qnameDAO;
protected DictionaryService dictionaryService;
private SqlSessionTemplate template;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate)
{
this.template = sqlSessionTemplate;
}
@Override
public void setQnameDAO(QNameDAO qnameDAO)
{
this.qnameDAO = qnameDAO;
super.setQnameDAO(qnameDAO);
}
@Override
public void setDictionaryService(DictionaryService dictionaryService)
{
this.dictionaryService = dictionaryService;
super.setDictionaryService(dictionaryService);
}
public void startBatch()
{
// TODO
/*
try
{
template.getSqlMapClient().startBatch();
}
catch (SQLException e)
{
throw new RuntimeException("Failed to start DAO batch.", e);
}
*/
}
public void executeBatch()
{
// TODO
/*
try
{
template.getSqlMapClient().executeBatch();
}
catch (SQLException e)
{
throw new RuntimeException("Failed to start DAO batch.", e);
}
*/
}
@Override
protected ServerEntity selectServer(String ipAddress)
{
ServerEntity entity = new ServerEntity();
entity.setIpAddress(ipAddress);
// Potentially more results if there is a case issue (unlikely)
List<ServerEntity> results = template.selectList(SELECT_SERVER_BY_IPADDRESS, entity);
for (ServerEntity serverEntity : results)
{
// Take the first one that matches regardless of case
if (serverEntity.getIpAddress().equalsIgnoreCase(ipAddress))
{
return serverEntity;
}
}
// There was no match
return null;
}
@Override
protected Long insertServer(String ipAddress)
{
ServerEntity entity = new ServerEntity();
entity.setVersion(1L);
entity.setIpAddress(ipAddress);
template.insert(INSERT_SERVER, entity);
return entity.getId();
}
@Override
protected Long insertTransaction(Long serverId, String changeTxnId, Long commitTimeMs)
{
ServerEntity server = new ServerEntity();
server.setId(serverId);
TransactionEntity transaction = new TransactionEntity();
transaction.setServer(server);
transaction.setVersion(1L);
transaction.setChangeTxnId(changeTxnId);
transaction.setCommitTimeMs(commitTimeMs);
template.insert(INSERT_TRANSACTION, transaction);
return transaction.getId();
}
@Override
protected int updateTransaction(Long txnId, Long commitTimeMs)
{
TransactionEntity transaction = new TransactionEntity();
transaction.setId(txnId);
transaction.setCommitTimeMs(commitTimeMs);
return template.update(UPDATE_TRANSACTION_COMMIT_TIME, transaction);
}
@Override
protected int deleteTransaction(Long txnId)
{
TransactionEntity transaction = new TransactionEntity();
transaction.setId(txnId);
return template.delete(DELETE_TRANSACTION_BY_ID, transaction);
}
@Override
protected List<StoreEntity> selectAllStores()
{
return template.selectList(SELECT_STORE_ALL);
}
@Override
protected StoreEntity selectStore(StoreRef storeRef)
{
StoreEntity store = new StoreEntity();
store.setProtocol(storeRef.getProtocol());
store.setIdentifier(storeRef.getIdentifier());
return template.selectOne(SELECT_STORE_BY_REF, store);
}
@Override
protected NodeEntity selectStoreRootNode(StoreRef storeRef)
{
StoreEntity store = new StoreEntity();
store.setProtocol(storeRef.getProtocol());
store.setIdentifier(storeRef.getIdentifier());
return template.selectOne(SELECT_STORE_ROOT_NODE_BY_REF, store);
}
@Override
protected Long insertStore(StoreEntity store)
{
store.setVersion(1L);
template.insert(INSERT_STORE, store);
return store.getId();
}
@Override
protected int updateStoreRoot(StoreEntity store)
{
return template.update(UPDATE_STORE_ROOT, store);
}
@Override
protected int updateStore(StoreEntity store)
{
return template.update(UPDATE_STORE, store);
}
@Override
protected int updateNodesInStore(Long txnId, Long storeId)
{
IdsEntity ids = new IdsEntity();
ids.setIdOne(txnId);
ids.setIdTwo(storeId);
return template.update(UPDATE_NODES_IN_STORE, ids);
}
@Override
protected Long insertNode(NodeEntity node)
{
node.setVersion(1L);
template.insert(INSERT_NODE, node);
return node.getId();
}
@Override
protected int updateNode(NodeUpdateEntity nodeUpdate)
{
// Increment the version
nodeUpdate.incrementVersion();
return template.update(UPDATE_NODE, nodeUpdate);
}
@Override
protected int updateNodes(Long txnId, List<Long> nodeIds)
{
if (nodeIds.size() == 0)
{
return 0;
}
IdsEntity ids = new IdsEntity();
ids.setIdOne(txnId);
ids.setIds(nodeIds);
return template.update(UPDATE_NODE_BULK_TOUCH, ids);
}
@Override
public Long getMinNodeId()
{
return (Long) template.selectOne(SELECT_NODE_MIN_ID);
}
@Override
public Long getMaxNodeId()
{
return (Long) template.selectOne(SELECT_NODE_MAX_ID);
}
@Override
@SuppressWarnings("rawtypes")
public Pair<Long, Long> getNodeIdsIntervalForType(QName type, Long startTxnTime, Long endTxnTime)
{
final Pair<Long, Long> intervalPair = new Pair<Long, Long>(LONG_ZERO, LONG_ZERO);
Pair<Long, QName> typePair = qnameDAO.getQName(type);
if (typePair == null)
{
// Return default
return intervalPair;
}
TransactionQueryEntity txnQuery = new TransactionQueryEntity();
txnQuery.setTypeQNameId(typePair.getFirst());
txnQuery.setMinCommitTime(startTxnTime);
txnQuery.setMaxCommitTime(endTxnTime);
ResultHandler resultHandler = new ResultHandler()
{
@SuppressWarnings("unchecked")
public void handleResult(ResultContext context)
{
Map<Long, Long> result = (Map<Long, Long>) context.getResultObject();
if (result != null)
{
intervalPair.setFirst(result.get("minId"));
intervalPair.setSecond(result.get("maxId"));
}
}
};
template.select(SELECT_NODE_INTERVAL_BY_TYPE, txnQuery, resultHandler);
return intervalPair;
}
@Override
protected void updatePrimaryChildrenSharedAclId(
Long txnId,
Long primaryParentNodeId,
Long optionalOldSharedAlcIdInAdditionToNull,
Long newSharedAlcId)
{
PrimaryChildrenAclUpdateEntity primaryChildrenAclUpdateEntity = new PrimaryChildrenAclUpdateEntity();
primaryChildrenAclUpdateEntity.setTxnId(txnId);
primaryChildrenAclUpdateEntity.setPrimaryParentNodeId(primaryParentNodeId);
primaryChildrenAclUpdateEntity.setOptionalOldSharedAclIdInAdditionToNull(optionalOldSharedAlcIdInAdditionToNull);
primaryChildrenAclUpdateEntity.setNewSharedAclId(newSharedAlcId);
template.update(UPDATE_PRIMARY_CHILDREN_SHARED_ACL, primaryChildrenAclUpdateEntity);
}
@Override
protected int deleteNodeById(Long nodeId)
{
NodeEntity node = new NodeEntity();
node.setId(nodeId);
return template.delete(DELETE_NODE_BY_ID, node);
}
@Override
protected int deleteNodesByCommitTime(long fromTxnCommitTimeMs, long toTxnCommitTimeMs)
{
// Get the deleted nodes
Pair<Long, QName> deletedTypePair = qnameDAO.getQName(ContentModel.TYPE_DELETED);
if (deletedTypePair == null)
{
// Nothing to do
return 0;
}
TransactionQueryEntity query = new TransactionQueryEntity();
query.setTypeQNameId(deletedTypePair.getFirst());
query.setMinCommitTime(fromTxnCommitTimeMs);
query.setMaxCommitTime(toTxnCommitTimeMs);
// TODO: Fix ALF-16030 Use ON DELETE CASCADE for node aspects and properties
// First clean up properties
template.delete(DELETE_NODE_PROPS_BY_TXN_COMMIT_TIME, query);
// Finally remove the nodes
return template.delete(DELETE_NODES_BY_TXN_COMMIT_TIME, query);
}
@Override
protected NodeEntity selectNodeById(Long id)
{
NodeEntity node = new NodeEntity();
node.setId(id);
return template.selectOne(SELECT_NODE_BY_ID, node);
}
@Override
protected NodeEntity selectNodeByNodeRef(NodeRef nodeRef)
{
StoreEntity store = new StoreEntity();
StoreRef storeRef = nodeRef.getStoreRef();
store.setProtocol(storeRef.getProtocol());
store.setIdentifier(storeRef.getIdentifier());
NodeEntity node = new NodeEntity();
// Store
node.setStore(store);
// UUID
String uuid = nodeRef.getId();
if (uuid.length() > 36)
{
return null; // Avoid DB2 query failure if someone passes in a made-up UUID
}
node.setUuid(uuid);
return template.selectOne(SELECT_NODE_BY_NODEREF, node);
}
@Override
protected List<Node> selectNodesByUuids(Long storeId, SortedSet<String> uuids)
{
NodeBatchLoadEntity nodeBatchLoadEntity = new NodeBatchLoadEntity();
// Store ID
nodeBatchLoadEntity.setStoreId(storeId);
// UUID
nodeBatchLoadEntity.setUuids(new ArrayList<String>(uuids));
return template.selectList(SELECT_NODES_BY_UUIDS, nodeBatchLoadEntity);
}
@Override
protected List<Node> selectNodesByIds(SortedSet<Long> ids)
{
NodeBatchLoadEntity nodeBatchLoadEntity = new NodeBatchLoadEntity();
// IDs
nodeBatchLoadEntity.setIds(new ArrayList<Long>(ids));
return template.selectList(SELECT_NODES_BY_IDS, nodeBatchLoadEntity);
}
/**
* Pull out the key-value pairs from the rows
*/
private Map<NodeVersionKey, Map<NodePropertyKey, NodePropertyValue>> makePersistentPropertiesMap(List<NodePropertyEntity> rows)
{
Map<NodeVersionKey, Map<NodePropertyKey, NodePropertyValue>> results = new HashMap<NodeVersionKey, Map<NodePropertyKey, NodePropertyValue>>(3);
for (NodePropertyEntity row : rows)
{
Long nodeId = row.getNodeId();
Long nodeVersion = row.getNodeVersion();
if (nodeId == null || nodeVersion == null)
{
throw new RuntimeException("Expect results with a Node and Version: " + row);
}
NodeVersionKey nodeTxnKey = new NodeVersionKey(nodeId, nodeVersion);
Map<NodePropertyKey, NodePropertyValue> props = results.get(nodeTxnKey);
if (props == null)
{
props = new HashMap<NodePropertyKey, NodePropertyValue>(17);
results.put(nodeTxnKey, props);
}
props.put(row.getKey(), row.getValue());
}
// Done
return results;
}
/**
* Convert key-value pairs into rows
*/
private List<NodePropertyEntity> makePersistentRows(Long nodeId, Map<NodePropertyKey, NodePropertyValue> map)
{
List<NodePropertyEntity> rows = new ArrayList<NodePropertyEntity>(map.size());
for (Map.Entry<NodePropertyKey, NodePropertyValue> entry : map.entrySet())
{
NodePropertyEntity row = new NodePropertyEntity();
row.setNodeId(nodeId);
row.setKey(entry.getKey());
row.setValue(entry.getValue());
rows.add(row);
}
// Done
return rows;
}
@Override
protected Map<NodeVersionKey, Map<NodePropertyKey, NodePropertyValue>> selectNodeProperties(Set<Long> nodeIds)
{
if (nodeIds.size() == 0)
{
return Collections.emptyMap();
}
NodePropertyEntity prop = new NodePropertyEntity();
prop.setNodeIds(new ArrayList<Long>(nodeIds));
List<NodePropertyEntity> rows = template.selectList(SELECT_NODE_PROPERTIES, prop);
return makePersistentPropertiesMap(rows);
}
@Override
protected Map<NodeVersionKey, Map<NodePropertyKey, NodePropertyValue>> selectNodeProperties(Long nodeId)
{
return selectNodeProperties(nodeId, Collections.<Long>emptySet());
}
@Override
protected Map<NodeVersionKey, Map<NodePropertyKey, NodePropertyValue>> selectNodeProperties(Long nodeId, Set<Long> qnameIds)
{
NodePropertyEntity prop = new NodePropertyEntity();
// Node
prop.setNodeId(nodeId);
// QName(s)
switch(qnameIds.size())
{
case 0:
// Ignore
break;
case 1:
prop.setKey(new NodePropertyKey());
prop.getKey().setQnameId(qnameIds.iterator().next());
break;
default:
prop.setQnameIds(new ArrayList<Long>(qnameIds));
}
List<NodePropertyEntity> rows = template.selectList(SELECT_NODE_PROPERTIES, prop);
return makePersistentPropertiesMap(rows);
}
@Override
@SuppressWarnings("rawtypes")
public List<NodePropertyEntity> selectNodePropertiesByTypes(Set<QName> qnames)
{
final List<NodePropertyEntity> properties = new ArrayList<NodePropertyEntity>();
// qnames of properties that are encrypted
Set<Long> qnameIds = qnameDAO.convertQNamesToIds(qnames, false);
if(qnameIds.size() > 0)
{
IdsEntity param = new IdsEntity();
param.setIds(new ArrayList<Long>(qnameIds));
// TODO - use a callback approach
template.select(SELECT_PROPERTIES_BY_TYPES, param, new ResultHandler()
{
@Override
public void handleResult(ResultContext context)
{
properties.add((NodePropertyEntity)context.getResultObject());
}
});
}
return properties;
}
@SuppressWarnings("rawtypes")
@Override
public List<NodePropertyEntity> selectNodePropertiesByDataType(QName dataType, long minNodeId, long maxNodeId)
{
int typeOrdinal = NodePropertyValue.convertToTypeOrdinal(dataType);
IdsEntity ids = new IdsEntity();
ids.setIdOne((long)typeOrdinal);
ids.setIdTwo(minNodeId);
ids.setIdThree(maxNodeId);
final List<NodePropertyEntity> properties = new ArrayList<NodePropertyEntity>();
template.select(SELECT_PROPERTIES_BY_ACTUAL_TYPE, ids, new ResultHandler()
{
@Override
public void handleResult(ResultContext context)
{
properties.add((NodePropertyEntity)context.getResultObject());
}
});
return properties;
}
@Override
protected int deleteNodeProperties(Long nodeId, Set<Long> qnameIds)
{
NodePropertyEntity prop = new NodePropertyEntity();
// Node
prop.setNodeId(nodeId);
// QNames
if (qnameIds != null)
{
if (qnameIds.isEmpty())
{
return 0; // Nothing to do
}
prop.setQnameIds(new ArrayList<Long>(qnameIds));
}
return template.delete(DELETE_NODE_PROPERTIES, prop);
}
@Override
protected int deleteNodeProperties(Long nodeId, List<NodePropertyKey> propKeys)
{
Assert.notNull(nodeId, "Must have 'nodeId'");
Assert.notNull(nodeId, "Must have 'propKeys'");
if (propKeys.size() == 0)
{
return 0;
}
NodePropertyEntity prop = new NodePropertyEntity();
// Node
prop.setNodeId(nodeId);
startBatch();
int count = 0;
try
{
for (NodePropertyKey propKey : propKeys)
{
prop.setKey(propKey);
count += template.delete(DELETE_NODE_PROPERTIES, prop);
}
}
finally
{
executeBatch();
}
return count;
}
@Override
protected void insertNodeProperties(Long nodeId, Map<NodePropertyKey, NodePropertyValue> persistableProps)
{
if (persistableProps.isEmpty())
{
return;
}
List<NodePropertyEntity> rows = makePersistentRows(nodeId, persistableProps);
startBatch();
try
{
for (NodePropertyEntity row : rows)
{
template.insert(INSERT_NODE_PROPERTY, row);
}
}
finally
{
executeBatch();
}
}
@Override
protected Map<NodeVersionKey, Set<QName>> selectNodeAspects(Set<Long> nodeIds)
{
if (nodeIds.size() == 0)
{
return Collections.emptyMap();
}
NodeAspectsEntity aspects = new NodeAspectsEntity();
aspects.setNodeIds(new ArrayList<Long>(nodeIds));
List<NodeAspectsEntity> rows = template.selectList(SELECT_NODE_ASPECTS, aspects);
Map<NodeVersionKey, Set<QName>> results = new HashMap<NodeVersionKey, Set<QName>>(rows.size()*2);
for (NodeAspectsEntity nodeAspectsEntity : rows)
{
Long nodeId = nodeAspectsEntity.getNodeId();
Long nodeVersion = nodeAspectsEntity.getNodeVersion();
NodeVersionKey nodeVersionKey = new NodeVersionKey(nodeId, nodeVersion);
if (results.containsKey(nodeVersionKey))
{
throw new IllegalStateException("Found existing key while querying for node aspects: " + nodeIds);
}
Set<Long> aspectIds = new HashSet<Long>(nodeAspectsEntity.getAspectQNameIds());
Set<QName> aspectQNames = qnameDAO.convertIdsToQNames(aspectIds);
results.put(nodeVersionKey, aspectQNames);
}
return results;
}
@Override
protected void insertNodeAspect(Long nodeId, Long qnameId)
{
Map<String, Long> aspectParameters = new HashMap<String, Long>(5);
aspectParameters.put("nodeId", nodeId);
aspectParameters.put("qnameId", qnameId);
template.insert(INSERT_NODE_ASPECT, aspectParameters);
}
@Override
protected int deleteNodeAspects(Long nodeId, Set<Long> qnameIds)
{
NodeAspectsEntity nodeAspects = new NodeAspectsEntity();
nodeAspects.setNodeId(nodeId);
if (qnameIds != null && !qnameIds.isEmpty())
{
nodeAspects.setAspectQNameIds(new ArrayList<Long>(qnameIds)); // Null means all
}
return template.delete(DELETE_NODE_ASPECTS, nodeAspects);
}
@Override
protected void selectNodesWithAspects(
List<Long> qnameIds,
Long minNodeId, Long maxNodeId,
final NodeRefQueryCallback resultsCallback)
{
@SuppressWarnings("rawtypes")
ResultHandler resultHandler = new ResultHandler()
{
public void handleResult(ResultContext context)
{
NodeEntity entity = (NodeEntity) context.getResultObject();
Pair<Long, NodeRef> nodePair = new Pair<Long, NodeRef>(entity.getId(), entity.getNodeRef());
resultsCallback.handle(nodePair);
}
};
IdsEntity parameters = new IdsEntity();
parameters.setIdOne(minNodeId);
parameters.setIdTwo(maxNodeId);
parameters.setIds(qnameIds);
template.select(SELECT_NODES_WITH_ASPECT_IDS, parameters, resultHandler);
}
@Override
protected Long insertNodeAssoc(Long sourceNodeId, Long targetNodeId, Long assocTypeQNameId, int assocIndex)
{
NodeAssocEntity assoc = new NodeAssocEntity();
assoc.setVersion(1L);
assoc.setTypeQNameId(assocTypeQNameId);
// Source
NodeEntity sourceNode = new NodeEntity();
sourceNode.setId(sourceNodeId);
assoc.setSourceNode(sourceNode);
// Target
NodeEntity targetNode = new NodeEntity();
targetNode.setId(targetNodeId);
assoc.setTargetNode(targetNode);
// Index
assoc.setAssocIndex(assocIndex);
template.insert(INSERT_NODE_ASSOC, assoc);
return assoc.getId();
}
@Override
protected int updateNodeAssoc(Long id, int assocIndex)
{
NodeAssocEntity assoc = new NodeAssocEntity();
assoc.setId(id);
assoc.setAssocIndex(assocIndex);
return template.update(UPDATE_NODE_ASSOC, assoc);
}
@Override
protected int deleteNodeAssoc(Long sourceNodeId, Long targetNodeId, Long assocTypeQNameId)
{
NodeAssocEntity assoc = new NodeAssocEntity();
assoc.setTypeQNameId(assocTypeQNameId);
// Source
NodeEntity sourceNode = new NodeEntity();
sourceNode.setId(sourceNodeId);
assoc.setSourceNode(sourceNode);
// Target
NodeEntity targetNode = new NodeEntity();
targetNode.setId(targetNodeId);
assoc.setTargetNode(targetNode);
return template.delete(DELETE_NODE_ASSOC, assoc);
}
@Override
protected int deleteNodeAssocs(List<Long> ids)
{
IdsEntity param = new IdsEntity();
param.setIds(ids);
return template.delete(DELETE_NODE_ASSOCS, param);
}
@Override
protected List<NodeAssocEntity> selectNodeAssocs(Long nodeId)
{
// Node
NodeEntity node = new NodeEntity();
node.setId(nodeId);
return template.selectList(SELECT_NODE_ASSOCS, node);
}
@Override
protected List<NodeAssocEntity> selectNodeAssocsBySource(Long sourceNodeId, Long typeQNameId)
{
return selectNodeAssocsBySourceAndPropertyValue(sourceNodeId, typeQNameId, null, null);
}
@Override
protected List<NodeAssocEntity> selectNodeAssocsBySourceAndPropertyValue(Long sourceNodeId, Long typeQNameId, Long propertyQNameId, NodePropertyValue nodeValue)
{
NodeAssocEntity assoc = new NodeAssocEntity();
// Source
NodeEntity sourceNode = new NodeEntity();
sourceNode.setId(sourceNodeId);
assoc.setSourceNode(sourceNode);
// Type
assoc.setTypeQNameId(typeQNameId);
// Property
assoc.setPropertyQNameId(propertyQNameId);
assoc.setPropertyValue(nodeValue);
return template.selectList(SELECT_NODE_ASSOCS_BY_SOURCE_AND_PROPERTY_VALUE, assoc);
}
@Override
protected List<NodeAssocEntity> selectNodeAssocsByTarget(Long targetNodeId, Long typeQNameId)
{
NodeAssocEntity assoc = new NodeAssocEntity();
// Target
NodeEntity targetNode = new NodeEntity();
targetNode.setId(targetNodeId);
assoc.setTargetNode(targetNode);
// Type
assoc.setTypeQNameId(typeQNameId);
return template.selectList(SELECT_NODE_ASSOCS_BY_TARGET, assoc);
}
@Override
protected NodeAssocEntity selectNodeAssocById(Long assocId)
{
NodeAssocEntity assoc = new NodeAssocEntity();
assoc.setId(assocId);
return template.selectOne(SELECT_NODE_ASSOC_BY_ID, assoc);
}
@Override
protected int selectNodeAssocMaxIndex(Long sourceNodeId, Long assocTypeQNameId)
{
NodeAssocEntity assoc = new NodeAssocEntity();
// Source
NodeEntity sourceNode = new NodeEntity();
sourceNode.setId(sourceNodeId);
assoc.setSourceNode(sourceNode);
// Assoc
assoc.setTypeQNameId(assocTypeQNameId);
Integer maxIndex = template.selectOne(SELECT_NODE_ASSOCS_MAX_INDEX, assoc);
return maxIndex == null ? 0 : maxIndex.intValue();
}
@Override
protected Long insertChildAssoc(ChildAssocEntity assoc)
{
assoc.setVersion(1L);
template.insert(INSERT_CHILD_ASSOC, assoc);
return assoc.getId();
}
@Override
protected int deleteChildAssocs(List<Long> ids)
{
IdsEntity idsEntity = new IdsEntity();
// IDs
idsEntity.setIds(ids);
return template.delete(DELETE_CHILD_ASSOCS, idsEntity);
}
@Override
protected int updateChildAssocIndex(
Long parentNodeId,
Long childNodeId,
QName assocTypeQName,
QName assocQName,
int index)
{
ChildAssocEntity assoc = new ChildAssocEntity();
// Parent
NodeEntity parentNode = new NodeEntity();
parentNode.setId(parentNodeId);
assoc.setParentNode(parentNode);
// Child
NodeEntity childNode = new NodeEntity();
childNode.setId(childNodeId);
assoc.setChildNode(childNode);
// Type QName
assoc.setTypeQNameAll(qnameDAO, assocTypeQName, true);
// QName
assoc.setQNameAll(qnameDAO, assocQName, true);
// Index
assoc.setAssocIndex(index);
return template.update(UPDATE_CHILD_ASSOCS_INDEX, assoc);
}
@Override
protected int updateChildAssocUniqueName(Long assocId, String name)
{
ChildAssocEntity assoc = new ChildAssocEntity();
assoc.setId(assocId);
// Name
assoc.setChildNodeNameAll(null, null, name);
return template.update(UPDATE_CHILD_ASSOC_UNIQUE_NAME, assoc);
}
@Override
protected ChildAssocEntity selectChildAssoc(Long assocId)
{
ChildAssocEntity assoc = new ChildAssocEntity();
assoc.setId(assocId);
return template.selectOne(SELECT_CHILD_ASSOC_BY_ID, assoc);
}
@Override
protected List<ChildAssocEntity> selectChildNodeIds(
Long nodeId,
Boolean isPrimary,
Long minChildNodeIdInclusive,
int maxResults)
{
ChildAssocEntity assoc = new ChildAssocEntity();
NodeEntity parentNode = new NodeEntity();
parentNode.setId(nodeId);
NodeEntity childNode = new NodeEntity();
childNode.setId(minChildNodeIdInclusive);