]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/migration/OrderedSetToListMigrationStep.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / migration / OrderedSetToListMigrationStep.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.migration;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Session;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.PossibleObjectWithType;
24 import org.simantics.db.common.request.WriteRequest;
25 import org.simantics.db.common.utils.ListUtils;
26 import org.simantics.db.common.utils.OrderedSetUtils;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.adapter.Instances;
29 import org.simantics.layer0.Layer0;
30
31 public class OrderedSetToListMigrationStep implements MigrationStep {
32     
33     final ArrayList<OrderedSetToListMapping> rules;
34     
35     private class OrderedSetToListMapping {
36         public Resource entityType, orderedSet, orderedSetRelation, list, listRelation;
37     }
38     
39     public OrderedSetToListMigrationStep(ReadGraph graph, Resource step) throws DatabaseException {
40         rules = new ArrayList<OrderedSetToListMapping>();
41         Layer0 L0 = Layer0.getInstance(graph);
42         for(Resource change : ListUtils.toList(graph, step)) {
43             String entityType = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_entityType, Bindings.STRING);
44             String orderedSet = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_orderedSetType, Bindings.STRING);
45             String orderedSetRelation = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_orderedSetRelation, Bindings.STRING);
46             String list = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_listType, Bindings.STRING);
47             String listRelation = graph.getPossibleRelatedValue(change, L0.OrderedSetToListMigrationStep_listRelation, Bindings.STRING);
48             
49             if(orderedSet != null && orderedSetRelation != null && list != null && listRelation != null) {
50                 OrderedSetToListMapping mapping = new OrderedSetToListMapping();
51                 mapping.entityType = graph.getResource(entityType);
52                 mapping.orderedSet = graph.getResource(orderedSet);
53                 mapping.orderedSetRelation = graph.getResource(orderedSetRelation);
54                 mapping.list = graph.getResource(list);
55                 mapping.listRelation = graph.getResource(listRelation);
56                 rules.add(mapping);
57             }
58         }
59     }
60
61     @Override
62     public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
63         final Resource resource = MigrationUtils.getResource(monitor, session, state);
64         
65         if(resource != null) {
66             session.syncRequest(new WriteRequest() {
67                 
68                 @Override
69                 public void perform(WriteGraph graph) throws DatabaseException {
70                     for(OrderedSetToListMapping mapping : rules) {
71                         Instances entityFinder = graph.adapt(mapping.entityType, Instances.class);
72                         for(Resource entity : entityFinder.find(graph, resource)) {
73                             Resource orderedSet = graph.syncRequest(new PossibleObjectWithType(entity, mapping.orderedSetRelation, mapping.orderedSet));
74                             if(orderedSet != null) {
75                                 Collection<Resource> list = OrderedSetUtils.toList(graph, orderedSet);
76                                 graph.deny(entity, mapping.orderedSetRelation, orderedSet);
77                                 graph.claim(entity, mapping.listRelation, ListUtils.create(graph, mapping.list, list));
78                             }
79                         }
80
81                     }                    
82                 }
83             });
84         }
85         
86     }
87
88
89 }