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.diagram.synchronization.graph;
14 import java.util.Arrays;
16 import org.simantics.databoard.binding.Binding;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.CommentMetadata;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.diagram.synchronization.ModificationAdapter;
25 * @author Tuukka Lehtonen
27 public class PropertyModification extends ModificationAdapter {
28 private final Resource owner;
29 private final Resource relation;
30 private final Resource type;
31 private final Object value;
32 private final Binding binding;
34 public PropertyModification(Resource propertyOwner, Resource propertyRelation, Resource propertyType, Object value, Binding binding) {
36 this.owner = propertyOwner;
37 this.relation = propertyRelation;
38 this.type = propertyType;
40 this.binding = binding;
43 private final boolean arrayEquals(Object av1, Object av2) {
46 Class<?> c1 = av1.getClass().getComponentType();
47 Class<?> c2 = av2.getClass().getComponentType();
48 if (c2 == null || !c1.equals(c2))
50 boolean p1 = c1.isPrimitive();
51 boolean p2 = c2.isPrimitive();
55 return Arrays.equals((Object[]) av1, (Object[]) av2);
56 if (boolean.class.equals(c1))
57 return Arrays.equals((boolean[]) av1, (boolean[]) av2);
58 else if (byte.class.equals(c1))
59 return Arrays.equals((byte[]) av1, (byte[]) av2);
60 else if (int.class.equals(c1))
61 return Arrays.equals((int[]) av1, (int[]) av2);
62 else if (long.class.equals(c1))
63 return Arrays.equals((long[]) av1, (long[]) av2);
64 else if (float.class.equals(c1))
65 return Arrays.equals((float[]) av1, (float[]) av2);
66 else if (double.class.equals(c1))
67 return Arrays.equals((double[]) av1, (double[]) av2);
68 throw new RuntimeException("??? Contact application querySupport.");
71 private boolean sameValue(Object oldValue, Object newValue) {
72 if (newValue.getClass().isArray()) {
73 return arrayEquals(newValue, oldValue);
75 return newValue.equals(oldValue);
80 public void perform(WriteGraph g) throws DatabaseException {
82 Object existing = g.getPossibleRelatedValue(owner, relation, binding);
83 if(!sameValue(existing, value)) {
85 DiagramGraphUtil.setRelatedValue(g, owner, relation, type, value, binding);
86 // Add comment to change set.
87 CommentMetadata cm = g.getMetadata(CommentMetadata.class);
88 g.addMetadata(cm.add("Changed property " + NameUtils.getSafeName(g, relation)));