]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/RelatedPropertyModification.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / RelatedPropertyModification.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.synchronization.graph;
13
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.CommentMetadata;
18 import org.simantics.db.common.utils.NameUtils;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.diagram.synchronization.ModificationAdapter;
21
22 /**
23  * @author Tuukka Lehtonen
24  */
25 public class RelatedPropertyModification extends ModificationAdapter {
26     private final Resource subject;
27     private final Resource relation;
28     private final Resource propertyRelation;
29     private final Resource type;
30     private final Object value;
31     private final Binding binding;
32
33     public RelatedPropertyModification(Resource subject, Resource relation, Resource propertyRelation, Resource propertyType, Object value, Binding binding) {
34         super(LOW_PRIORITY);
35         this.subject = subject;
36         this.relation = relation;
37         this.propertyRelation = propertyRelation;
38         this.type = propertyType;
39         this.value = value;
40         this.binding = binding;
41     }
42
43     @Override
44     public void perform(WriteGraph g) throws DatabaseException {
45         Resource propertyOwner = g.getPossibleObject(subject, relation);
46         if (propertyOwner != null) {
47                 Object existing = g.getPossibleRelatedValue(propertyOwner, propertyRelation, binding);
48                 if(!value.equals(existing)) {
49                 g.markUndoPoint();
50                         DiagramGraphUtil.setRelatedValue(g, propertyOwner, propertyRelation, type, value, binding);
51                         // Add comment to change set.
52                         CommentMetadata cm = g.getMetadata(CommentMetadata.class);
53                         g.addMetadata(cm.add("Changed property " + NameUtils.getSafeLabel(g, propertyRelation) + " in " + NameUtils.getSafeName(g, propertyOwner)));
54                 }
55         }
56     }
57     
58 }