]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/utils/OntologicalRequirementEnforceRequest.java
Fixed changes made in commit 3a10ce85.
[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.adapters.ChangeHistoryUpdated;
15 import org.simantics.modeling.adapters.ChangeInformation;
16
17 /**
18  * @author Antti Villberg
19  * @author Tuukka Lehtonen
20  */
21 public class OntologicalRequirementEnforceRequest extends WriteRequest {
22
23         private Collection<Resource> creates;
24         private Collection<Resource> modis;
25         private Collection<Resource> ids;
26         private String author;
27         private long time;
28
29         public OntologicalRequirementEnforceRequest(Collection<Resource> creates, Collection<Resource> modis, Collection<Resource> ids) {
30                 this(creates,
31                                 modis,
32                                 ids,
33                                 System.getProperty("user.name", ""),
34                                 System.currentTimeMillis());
35         }
36
37         public OntologicalRequirementEnforceRequest(Collection<Resource> creates, Collection<Resource> modis, Collection<Resource> ids, String author, long time) {
38                 this.creates = creates;
39                 this.modis = modis;
40                 this.ids = ids;
41                 this.author = author;
42                 this.time = time;
43         }
44
45         @Override
46         public void perform(WriteGraph graph) throws DatabaseException {
47                 update(graph, creates, modis, ids, true, author, time, true);
48         }
49
50         public static void update(
51                         WriteGraph graph,
52                         Collection<Resource> creates,
53                         Collection<Resource> modis,
54                         Collection<Resource> ids,
55                         boolean addComment,
56                         boolean disableDependencyIndexing) throws DatabaseException
57         {
58                 update(graph,
59                                 creates,
60                                 modis,
61                                 ids,
62                                 addComment,
63                                 System.getProperty("user.name", ""),
64                                 System.currentTimeMillis(),
65                                 disableDependencyIndexing);
66
67         }
68
69         public static void update(
70                         WriteGraph graph,
71                         Collection<Resource> creates,
72                         Collection<Resource> modis,
73                         Collection<Resource> ids,
74                         boolean addComment,
75                         String author,
76                         long time,
77                         boolean disableDependencyIndexing) throws DatabaseException
78         {
79                 if (disableDependencyIndexing)
80                         Layer0Utils.setDependenciesIndexingDisabled(graph, true);
81                 
82                 ModelingResources MOD = ModelingResources.getInstance(graph);
83                 Layer0 L0 = Layer0.getInstance(graph);
84
85                 if (!creates.isEmpty()) {
86                         ChangeInformation info = new ChangeInformation();
87                         info.createdAt = time;
88                         info.createdBy = author;
89                         info.modifiedAt = time;
90                         info.modifiedBy = author;
91
92                         for (Resource c : creates) {
93                                 CommonDBUtils.selectClusterSet(graph, c);
94                                 graph.claimLiteral(c, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
95                         }
96                 }
97
98                 for (Resource m : modis) {
99                         ChangeInformation info = graph.getPossibleRelatedValue(m, MOD.changeInformation, ChangeInformation.BINDING);
100                         if (info == null) {
101                                 // Should not be possible but lets handle this anyway
102                                 info = new ChangeInformation();
103                                 info.createdAt = time;
104                                 info.createdBy = author;
105                         }
106                         info.modifiedAt = time;
107                         info.modifiedBy = author;
108                         CommonDBUtils.selectClusterSet(graph, m);
109                         graph.claimLiteral(m, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
110                 }
111                 
112                 for (Resource r : ids) {
113                         if (!graph.hasStatement(r, L0.identifier)) {
114                                 CommonDBUtils.selectClusterSet(graph, r);
115                                 Layer0Utils.claimNewIdentifier(graph, r, true);
116                         }
117                 }
118
119                 graph.addMetadata( graph.getMetadata(CommentMetadata.class).add("Updated change information") );
120                 
121                 graph.addMetadata( graph.getMetadata(ChangeHistoryUpdated.class) );
122                 
123         }
124
125 }