]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/issue/AnonymousIssue.java
Export also dynamic issues to issue CSV export file
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / issue / AnonymousIssue.java
1 package org.simantics.db.common.issue;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.Arrays;
6 import java.util.Calendar;
7 import java.util.Collection;
8 import java.util.HashSet;
9 import java.util.Set;
10 import java.util.UUID;
11
12 import org.simantics.databoard.Bindings;
13 import org.simantics.db.Issue;
14 import org.simantics.db.Resource;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
17 import org.simantics.db.common.request.IndexRoot;
18 import org.simantics.db.common.utils.ListUtils;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.issues.ontology.IssueResource;
21 import org.simantics.layer0.Layer0;
22
23 public class AnonymousIssue implements Issue {
24
25         public final Resource type;
26         public final Resource[] contexts;
27
28         public AnonymousIssue(Resource type, Resource ... contexts) {
29                 this.type = type;
30                 this.contexts = contexts;
31         }
32
33         public AnonymousIssue(Resource type, Collection<Resource> contexts) {
34                 this.type = type;
35                 this.contexts = contexts.toArray(new Resource[contexts.size()]);
36         }
37
38         public void writeAdditionalContext(WriteGraph graph, Resource issue) throws DatabaseException {
39
40                 IssueResource IR = IssueResource.getInstance(graph);
41
42                 // The main context
43                 graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, contexts[0]);
44                 // A possible parent
45                 Layer0 L0 = Layer0.getInstance(graph);
46                 Resource parent = graph.getPossibleObject(contexts[0], L0.PartOf);
47                 if(parent != null) {
48                         graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, parent);
49                 }
50
51         }
52
53         @Override
54         public Resource write(WriteGraph graph, Resource source) throws DatabaseException {
55
56                 Layer0 L0 = Layer0.getInstance(graph);
57                 IssueResource IR = IssueResource.getInstance(graph);
58
59                 Resource model = graph.syncRequest(new IndexRoot(source), TransientCacheListener.<Resource>instance());
60
61                 Resource issue = graph.newResource();
62                 graph.claim(issue, L0.InstanceOf, null, type);
63                 graph.addLiteral(issue, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
64
65                 graph.claim(issue, IR.Issue_HasContexts, IR.Issue_HasContexts_Inverse, ListUtils.create(graph, L0.List, contexts));
66
67                 writeAdditionalContext(graph, issue);
68
69                 DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
70                 String created = format.format(Calendar.getInstance().getTime());
71                 graph.addLiteral(issue, IR.Issue_creationTime, IR.Issue_creationTime_Inverse, L0.String, created, Bindings.STRING);
72                 graph.claim(source, IR.IssueSource_Manages, IR.IssueSource_Manages_Inverse, issue);
73                 graph.claim(model, L0.ConsistsOf, L0.PartOf, issue);
74                 
75                 return issue;
76
77         }
78
79         @Override
80         public String toString() {
81                 return Arrays.toString(contexts);
82         }
83
84         @Override
85         public Resource getType() {
86                 return type;
87         }
88
89         @Override
90         public Object getMainContext() {
91                 return contexts[0];
92         }
93
94         @Override
95         public Set<Resource> getContexts() {
96                 HashSet<Resource> result = new HashSet<Resource>();
97                 for(Resource context : contexts) result.add(context);
98                 return result;
99         }
100
101 }