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