1 package org.simantics.db.common.issue;
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;
10 import java.util.UUID;
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;
23 public class AnonymousIssue implements Issue {
25 public final Resource type;
26 public final Resource[] contexts;
28 public AnonymousIssue(Resource type, Resource ... contexts) {
30 this.contexts = contexts;
33 public AnonymousIssue(Resource type, Collection<Resource> contexts) {
35 this.contexts = contexts.toArray(new Resource[contexts.size()]);
38 public void writeAdditionalContext(WriteGraph graph, Resource issue) throws DatabaseException {
40 IssueResource IR = IssueResource.getInstance(graph);
43 graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, contexts[0]);
45 Layer0 L0 = Layer0.getInstance(graph);
46 Resource parent = graph.getPossibleObject(contexts[0], L0.PartOf);
48 graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, parent);
54 public Resource write(WriteGraph graph, Resource source) throws DatabaseException {
56 Layer0 L0 = Layer0.getInstance(graph);
57 IssueResource IR = IssueResource.getInstance(graph);
59 Resource model = graph.syncRequest(new IndexRoot(source), TransientCacheListener.<Resource>instance());
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);
65 graph.claim(issue, IR.Issue_HasContexts, IR.Issue_HasContexts_Inverse, ListUtils.create(graph, L0.List, contexts));
67 writeAdditionalContext(graph, issue);
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);
80 public String toString() {
81 return Arrays.toString(contexts);
85 public Resource getType() {
90 public Object getMainContext() {
95 public Set<Resource> getContexts() {
96 HashSet<Resource> result = new HashSet<Resource>();
97 for(Resource context : contexts) result.add(context);