1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.tests.api.write.request;
14 import java.util.ArrayList;
15 import java.util.Collection;
17 import java.util.TreeMap;
19 import org.junit.Assert;
20 import org.junit.Test;
21 import org.simantics.databoard.Databoard;
22 import org.simantics.databoard.annotations.Arguments;
23 import org.simantics.databoard.annotations.Union;
24 import org.simantics.databoard.binding.Binding;
25 import org.simantics.databoard.serialization.Serializer;
26 import org.simantics.db.ChangeSetIdentifier;
27 import org.simantics.db.Metadata;
28 import org.simantics.db.Resource;
29 import org.simantics.db.Session;
30 import org.simantics.db.WriteGraph;
31 import org.simantics.db.common.request.WriteRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.exception.RuntimeDatabaseException;
34 import org.simantics.db.service.ManagementSupport;
35 import org.simantics.db.testing.base.ExistingDatabaseTest;
38 * @author Tuukka Lehtonen
40 public class WriteMetadataTest extends ExistingDatabaseTest {
41 boolean DEBUG = false;
43 public void test() throws Exception{
45 final Resource root = getSession().getRootLibrary();
47 Collection<ChangeSetIdentifier> ids = getSession().getService(ManagementSupport.class).getChangeSetIdentifiers(Long.MIN_VALUE, Long.MAX_VALUE);
49 for (ChangeSetIdentifier id : ids) {
50 System.out.println("CSID: " + id.getId());
51 System.out.println(" METADATA: " + id.getMetadata());
54 getSession().syncRequest(new WriteRequest() {
56 public void perform(WriteGraph graph) throws DatabaseException {
57 Resource r = graph.newResource();
58 graph.claim(root, L0.InstanceOf, L0.Library);
59 graph.claim(root, L0.ConsistsOf, r);
61 TreeMap<Resource, StructuralChanges.Change[]> map = new TreeMap<Resource, StructuralChanges.Change[]>();
62 map.put(r, new StructuralChanges.Change[] { new StructuralChanges.ComponentAddition(r) });
63 StructuralChanges ch = new StructuralChanges(map);
64 graph.addMetadata(ch);
67 int count = ids.size();
68 ids = getSession().getService(ManagementSupport.class).getChangeSetIdentifiers(Long.MIN_VALUE, Long.MAX_VALUE);
69 Assert.assertTrue("Metadata not updated.", count < ids.size());
71 for (ChangeSetIdentifier id : ids) {
72 System.out.println("CSID: " + id.getId());
73 System.out.println(" METADATA: " + id.getMetadata());
78 ids = getSession().getService(ManagementSupport.class).getChangeSetIdentifiers(Long.MIN_VALUE, Long.MAX_VALUE);
79 for (ChangeSetIdentifier id : ids) {
80 System.out.println("CSID: " + id.getId());
81 System.out.println(" METADATA: " + id.getMetadata());
88 class StructuralChanges implements Metadata {
90 public final @Arguments({Resource.class, Change[].class}) TreeMap<Resource, Change[]> modelChanges;
92 public StructuralChanges(TreeMap<Resource, Change[]> modelChanges) {
93 this.modelChanges = modelChanges;
96 public StructuralChanges(Map<Resource, ArrayList<Change>> _modelChanges) {
97 this(new TreeMap<Resource, Change[]>());
98 for(Map.Entry<Resource, ArrayList<Change>> entry : _modelChanges.entrySet()) {
99 ArrayList<Change> value = entry.getValue();
100 modelChanges.put(entry.getKey(), value.toArray(new Change[value.size()]));
104 public static enum ChangeType {
105 COMPONENT_ADDITION, COMPONENT_REMOVAL,
106 COMPONENT_MODIFICATION, CONNECTION_CHANGE
109 @Union({ComponentAddition.class,
110 ComponentRemoval.class,
111 ComponentModification.class,
112 ConnectionChange.class})
113 public static interface Change {
114 ChangeType getType();
117 public static class ComponentAddition implements Change {
118 public final Resource component;
119 public ComponentAddition(Resource component) {
120 this.component = component;
123 public ChangeType getType() {
124 return ChangeType.COMPONENT_ADDITION;
128 public static class ComponentRemoval implements Change {
129 public final Resource component;
130 public final Resource parent;
131 public ComponentRemoval(Resource component, Resource parent) {
132 this.component = component;
133 this.parent = parent;
136 public ChangeType getType() {
137 return ChangeType.COMPONENT_REMOVAL;
141 public static class ComponentModification implements Change {
142 public final Resource component;
143 public ComponentModification(Resource component) {
144 this.component = component;
147 public ChangeType getType() {
148 return ChangeType.COMPONENT_MODIFICATION;
152 public static class ConnectionChange implements Change {
153 public final Resource connection;
154 public ConnectionChange(Resource connection) {
155 this.connection = connection;
158 public ChangeType getType() {
159 return ChangeType.CONNECTION_CHANGE;
163 public Change[] get(Resource model) {
164 return modelChanges.get(model);
168 public byte[] serialise(Session session) {
170 Databoard databoard = session.getService( Databoard.class );
171 Binding binding = databoard.getBinding( StructuralChanges.class );
172 Serializer serializer = databoard.getSerializer( binding );
173 return serializer.serialize(this);
174 } catch (Exception e) {
176 if (e instanceof RuntimeDatabaseException)
177 throw (RuntimeDatabaseException)e;
179 throw new RuntimeDatabaseException("Serialization failed.", e);
183 public static StructuralChanges deserialise(Session session, byte[] input) {
185 Databoard databoard = session.getService( Databoard.class );
186 Binding binding = databoard.getBinding( StructuralChanges.class );
187 Serializer serializer = databoard.getSerializer( binding );
188 return (StructuralChanges) serializer.deserialize(input);
189 } catch (Exception e) {
191 if (e instanceof RuntimeDatabaseException)
192 throw (RuntimeDatabaseException)e;
194 throw new RuntimeDatabaseException("Deserialization failed.", e);