]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/utils/OntologicalRequirementEnforceRequest.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / utils / OntologicalRequirementEnforceRequest.java
1 package org.simantics.modeling.utils;
2
3 import java.util.Collection;
4
5 import org.simantics.db.Resource;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.common.CommentMetadata;
8 import org.simantics.db.common.request.WriteRequest;
9 import org.simantics.db.common.utils.CommonDBUtils;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.util.Layer0Utils;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.modeling.ModelingResources;
14 import org.simantics.modeling.ModelingUtils;
15 import org.simantics.modeling.adapters.ChangeHistoryUpdated;
16 import org.simantics.modeling.adapters.ChangeInformation;
17
18 /**
19  * @author Antti Villberg
20  * @author Tuukka Lehtonen
21  */
22 public class OntologicalRequirementEnforceRequest extends WriteRequest {
23
24         private static final String PROP_WRITE_CHANGED_TAGS = "org.simantics.modeling.writeChangedTags"; //$NON-NLS-1$
25
26         private Collection<Resource> creates;
27         private Collection<Resource> modis;
28         private Collection<Resource> ids;
29         private String author;
30         private long time;
31
32         private static String getAuthor() {
33                 return System.getProperty("user.name", ""); //$NON-NLS-1$ //$NON-NLS-2$
34         }
35
36         private static boolean writeChangedTags() {
37                 return !System.getProperty(PROP_WRITE_CHANGED_TAGS, "") //$NON-NLS-1$
38                                 .equalsIgnoreCase("false"); //$NON-NLS-1$
39         }
40
41         public OntologicalRequirementEnforceRequest(Collection<Resource> creates, Collection<Resource> modis, Collection<Resource> ids) {
42                 this(creates,
43                                 modis,
44                                 ids,
45                                 getAuthor(),
46                                 System.currentTimeMillis());
47         }
48
49         public OntologicalRequirementEnforceRequest(Collection<Resource> creates, Collection<Resource> modis, Collection<Resource> ids, String author, long time) {
50                 this.creates = creates;
51                 this.modis = modis;
52                 this.ids = ids;
53                 this.author = author;
54                 this.time = time;
55         }
56
57         @Override
58         public void perform(WriteGraph graph) throws DatabaseException {
59                 update(graph, creates, modis, ids, true, author, time, true, writeChangedTags());
60         }
61
62         public static void update(
63                         WriteGraph graph,
64                         Collection<Resource> creates,
65                         Collection<Resource> modis,
66                         Collection<Resource> ids,
67                         boolean addComment,
68                         boolean disableDependencyIndexing) throws DatabaseException
69         {
70                 update(graph,
71                                 creates,
72                                 modis,
73                                 ids,
74                                 addComment,
75                                 getAuthor(),
76                                 System.currentTimeMillis(),
77                                 disableDependencyIndexing);
78
79         }
80
81         public static void update(
82                         WriteGraph graph,
83                         Collection<Resource> creates,
84                         Collection<Resource> modis,
85                         Collection<Resource> ids,
86                         boolean addComment,
87                         String author,
88                         long time,
89                         boolean disableDependencyIndexing) throws DatabaseException
90         {
91                 update(graph, creates, modis, ids, addComment, author, time, disableDependencyIndexing, writeChangedTags());
92         }
93
94         public static void update(
95                         WriteGraph graph,
96                         Collection<Resource> creates,
97                         Collection<Resource> modis,
98                         Collection<Resource> ids,
99                         boolean addComment,
100                         String author,
101                         long time,
102                         boolean disableDependencyIndexing,
103                         boolean writeChangedTags) throws DatabaseException
104         {
105                 if (disableDependencyIndexing)
106                         Layer0Utils.setDependenciesIndexingDisabled(graph, true);
107
108                 ModelingResources MOD = ModelingResources.getInstance(graph);
109                 Layer0 L0 = Layer0.getInstance(graph);
110
111                 if (!creates.isEmpty()) {
112                         ChangeInformation info = new ChangeInformation();
113                         info.createdAt = time;
114                         info.createdBy = author;
115                         info.modifiedAt = time;
116                         info.modifiedBy = author;
117
118                         for (Resource c : creates) {
119                                 CommonDBUtils.selectClusterSet(graph, c);
120                                 graph.claimLiteral(c, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
121                         }
122                         if (writeChangedTags)
123                                 ModelingUtils.markChanged(graph, creates);
124                 }
125
126                 for (Resource m : modis) {
127                         ChangeInformation info = graph.getPossibleRelatedValue(m, MOD.changeInformation, ChangeInformation.BINDING);
128                         if (info == null) {
129                                 // Should not be possible but lets handle this anyway
130                                 info = new ChangeInformation();
131                                 info.createdAt = time;
132                                 info.createdBy = author;
133                         }
134                         info.modifiedAt = time;
135                         info.modifiedBy = author;
136                         CommonDBUtils.selectClusterSet(graph, m);
137                         graph.claimLiteral(m, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
138                 }
139                 if (writeChangedTags)
140                         ModelingUtils.markChanged(graph, modis);
141
142                 for (Resource r : ids) {
143                         if (!graph.hasStatement(r, L0.identifier)) {
144                                 CommonDBUtils.selectClusterSet(graph, r);
145                                 Layer0Utils.claimNewIdentifier(graph, r, true);
146                         }
147                 }
148
149                 graph.addMetadata( graph.getMetadata(CommentMetadata.class).add("Updated change information") ); //$NON-NLS-1$
150                 graph.addMetadata( graph.getMetadata(ChangeHistoryUpdated.class) );
151         }
152
153 }