]> gerrit.simantics Code Review - simantics/platform.git/blob
3f8ffb15b94d81282f275b7edd7246d78c29e948
[simantics/platform.git] /
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 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                                 ModelingUtils.markChanged(graph, c);
97                         }
98                 }
99
100                 for (Resource m : modis) {
101                         ChangeInformation info = graph.getPossibleRelatedValue(m, MOD.changeInformation, ChangeInformation.BINDING);
102                         if (info == null) {
103                                 // Should not be possible but lets handle this anyway
104                                 info = new ChangeInformation();
105                                 info.createdAt = time;
106                                 info.createdBy = author;
107                         }
108                         info.modifiedAt = time;
109                         info.modifiedBy = author;
110                         CommonDBUtils.selectClusterSet(graph, m);
111                         graph.claimLiteral(m, MOD.changeInformation, MOD.changeInformation_Inverse, MOD.ChangeInformation, info, ChangeInformation.BINDING);
112                         ModelingUtils.markChanged(graph, m);
113                 }
114                 
115                 for (Resource r : ids) {
116                         if (!graph.hasStatement(r, L0.identifier)) {
117                                 CommonDBUtils.selectClusterSet(graph, r);
118                                 Layer0Utils.claimNewIdentifier(graph, r, true);
119                         }
120                 }
121
122                 graph.addMetadata( graph.getMetadata(CommentMetadata.class).add("Updated change information") );
123                 
124                 graph.addMetadata( graph.getMetadata(ChangeHistoryUpdated.class) );
125                 
126         }
127
128 }