1 package org.simantics.modeling.utils;
3 import java.util.Collection;
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;
19 * @author Antti Villberg
20 * @author Tuukka Lehtonen
22 public class OntologicalRequirementEnforceRequest extends WriteRequest {
24 private static final String PROP_WRITE_CHANGED_TAGS = "org.simantics.modeling.writeChangedTags"; //$NON-NLS-1$
26 private Collection<Resource> creates;
27 private Collection<Resource> modis;
28 private Collection<Resource> ids;
29 private String author;
32 private static String getAuthor() {
33 return System.getProperty("user.name", ""); //$NON-NLS-1$ //$NON-NLS-2$
36 private static boolean writeChangedTags() {
37 return !System.getProperty(PROP_WRITE_CHANGED_TAGS, "") //$NON-NLS-1$
38 .equalsIgnoreCase("false"); //$NON-NLS-1$
41 public OntologicalRequirementEnforceRequest(Collection<Resource> creates, Collection<Resource> modis, Collection<Resource> ids) {
46 System.currentTimeMillis());
49 public OntologicalRequirementEnforceRequest(Collection<Resource> creates, Collection<Resource> modis, Collection<Resource> ids, String author, long time) {
50 this.creates = creates;
58 public void perform(WriteGraph graph) throws DatabaseException {
59 update(graph, creates, modis, ids, true, author, time, true, writeChangedTags());
62 public static void update(
64 Collection<Resource> creates,
65 Collection<Resource> modis,
66 Collection<Resource> ids,
68 boolean disableDependencyIndexing) throws DatabaseException
76 System.currentTimeMillis(),
77 disableDependencyIndexing);
81 public static void update(
83 Collection<Resource> creates,
84 Collection<Resource> modis,
85 Collection<Resource> ids,
89 boolean disableDependencyIndexing) throws DatabaseException
91 update(graph, creates, modis, ids, addComment, author, time, disableDependencyIndexing, writeChangedTags());
94 public static void update(
96 Collection<Resource> creates,
97 Collection<Resource> modis,
98 Collection<Resource> ids,
102 boolean disableDependencyIndexing,
103 boolean writeChangedTags) throws DatabaseException
105 if (disableDependencyIndexing)
106 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
108 ModelingResources MOD = ModelingResources.getInstance(graph);
109 Layer0 L0 = Layer0.getInstance(graph);
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;
118 for (Resource c : creates) {
119 CommonDBUtils.selectClusterSet(graph, c);
120 graph.claimLiteral(c, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
122 if (writeChangedTags)
123 ModelingUtils.markChanged(graph, creates);
126 for (Resource m : modis) {
127 ChangeInformation info = graph.getPossibleRelatedValue(m, MOD.changeInformation, ChangeInformation.BINDING);
129 // Should not be possible but lets handle this anyway
130 info = new ChangeInformation();
131 info.createdAt = time;
132 info.createdBy = author;
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);
139 if (writeChangedTags)
140 ModelingUtils.markChanged(graph, modis);
142 for (Resource r : ids) {
143 if (!graph.hasStatement(r, L0.identifier)) {
144 CommonDBUtils.selectClusterSet(graph, r);
145 Layer0Utils.claimNewIdentifier(graph, r, true);
149 graph.addMetadata( graph.getMetadata(CommentMetadata.class).add("Updated change information") ); //$NON-NLS-1$
150 graph.addMetadata( graph.getMetadata(ChangeHistoryUpdated.class) );