1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.layer0.migration;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\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
31 public class OrderedSetToListMigrationStep implements MigrationStep {
\r
33 final ArrayList<OrderedSetToListMapping> rules;
\r
35 private class OrderedSetToListMapping {
\r
36 public Resource entityType, orderedSet, orderedSetRelation, list, listRelation;
\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
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
62 public void applyTo(IProgressMonitor monitor, Session session, MigrationState state) throws DatabaseException {
\r
63 final Resource resource = MigrationUtils.getResource(monitor, session, state);
\r
65 if(resource != null) {
\r
66 session.syncRequest(new WriteRequest() {
\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