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