Layer0Utils.addL0Identifier to prevent possible differentiation of code
[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.datatypes.literal.GUID;
6 import org.simantics.db.Resource;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.common.CommentMetadata;
9 import org.simantics.db.common.request.WriteRequest;
10 import org.simantics.db.common.utils.CommonDBUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.util.Layer0Utils;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.modeling.ModelingResources;
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 Collection<Resource> creates;
25         private Collection<Resource> modis;
26         private Collection<Resource> ids;
27         private String author;
28         private long time;
29
30         public OntologicalRequirementEnforceRequest(Collection<Resource> creates, Collection<Resource> modis, Collection<Resource> ids) {
31                 this(creates,
32                                 modis,
33                                 ids,
34                                 System.getProperty("user.name", ""),
35                                 System.currentTimeMillis());
36         }
37
38         public OntologicalRequirementEnforceRequest(Collection<Resource> creates, Collection<Resource> modis, Collection<Resource> ids, String author, long time) {
39                 this.creates = creates;
40                 this.modis = modis;
41                 this.ids = ids;
42                 this.author = author;
43                 this.time = time;
44         }
45
46         @Override
47         public void perform(WriteGraph graph) throws DatabaseException {
48                 update(graph, creates, modis, ids, true, author, time, true);
49         }
50
51         public static void update(
52                         WriteGraph graph,
53                         Collection<Resource> creates,
54                         Collection<Resource> modis,
55                         Collection<Resource> ids,
56                         boolean addComment,
57                         boolean disableDependencyIndexing) throws DatabaseException
58         {
59                 update(graph,
60                                 creates,
61                                 modis,
62                                 ids,
63                                 addComment,
64                                 System.getProperty("user.name", ""),
65                                 System.currentTimeMillis(),
66                                 disableDependencyIndexing);
67
68         }
69
70         public static void update(
71                         WriteGraph graph,
72                         Collection<Resource> creates,
73                         Collection<Resource> modis,
74                         Collection<Resource> ids,
75                         boolean addComment,
76                         String author,
77                         long time,
78                         boolean disableDependencyIndexing) throws DatabaseException
79         {
80                 if (disableDependencyIndexing)
81                         Layer0Utils.setDependenciesIndexingDisabled(graph, true);
82                 
83                 ModelingResources MOD = ModelingResources.getInstance(graph);
84                 Layer0 L0 = Layer0.getInstance(graph);
85
86                 if (!creates.isEmpty()) {
87                         ChangeInformation info = new ChangeInformation();
88                         info.createdAt = time;
89                         info.createdBy = author;
90                         info.modifiedAt = time;
91                         info.modifiedBy = author;
92
93                         for (Resource c : creates) {
94                                 CommonDBUtils.selectClusterSet(graph, c);
95                                 graph.claimLiteral(c, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
96                         }
97                 }
98
99                 for (Resource m : modis) {
100                         ChangeInformation info = graph.getPossibleRelatedValue(m, MOD.changeInformation, ChangeInformation.BINDING);
101                         if (info == null) {
102                                 // Should not be possible but lets handle this anyway
103                                 info = new ChangeInformation();
104                                 info.createdAt = time;
105                                 info.createdBy = author;
106                         }
107                         info.modifiedAt = time;
108                         info.modifiedBy = author;
109                         CommonDBUtils.selectClusterSet(graph, m);
110                         graph.claimLiteral(m, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
111                 }
112                 
113                 for (Resource r : ids) {
114                         if (!graph.hasStatement(r, L0.identifier)) {
115                                 CommonDBUtils.selectClusterSet(graph, r);
116                                 Layer0Utils.addL0Identifier(graph, r);
117                         }
118                 }
119
120                 graph.addMetadata( graph.getMetadata(CommentMetadata.class).add("Updated change information") );
121                 
122                 graph.addMetadata( graph.getMetadata(ChangeHistoryUpdated.class) );
123                 
124         }
125
126 }