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.databoard.util.ObjectUtils;
14 import org.simantics.db.Issue;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
18 import org.simantics.db.common.request.IndexRoot;
19 import org.simantics.db.common.utils.ListUtils;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.issues.ontology.IssueResource;
22 import org.simantics.layer0.Layer0;
24 public class StandardIssue implements Issue {
26 public final Resource type;
27 public final Resource[] contexts;
28 public final int hash;
30 public StandardIssue(Resource type, Resource ... contexts) {
32 this.contexts = contexts;
33 this.hash = makeHashCode();
36 public StandardIssue(Resource type, Collection<Resource> contexts) {
38 this.contexts = contexts.toArray(new Resource[contexts.size()]);
39 this.hash = makeHashCode();
42 public void writeAdditionalContext(WriteGraph graph, Resource issue) throws DatabaseException {
44 IssueResource IR = IssueResource.getInstance(graph);
47 graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, contexts[0]);
49 Layer0 L0 = Layer0.getInstance(graph);
50 Resource parent = graph.getPossibleObject(contexts[0], L0.PartOf);
52 graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, parent);
58 public Resource write(WriteGraph graph, Resource source) throws DatabaseException {
60 Layer0 L0 = Layer0.getInstance(graph);
61 IssueResource IR = IssueResource.getInstance(graph);
63 Resource model = graph.syncRequest(new IndexRoot(source), TransientCacheListener.<Resource>instance());
65 Resource issue = graph.newResource();
66 graph.claim(issue, L0.InstanceOf, null, type);
67 graph.addLiteral(issue, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
69 graph.claim(issue, IR.Issue_HasContexts, IR.Issue_HasContexts_Inverse, ListUtils.create(graph, L0.List, contexts));
71 writeAdditionalContext(graph, issue);
73 DateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
74 String created = format.format(Calendar.getInstance().getTime());
75 graph.addLiteral(issue, IR.Issue_creationTime, IR.Issue_creationTime_Inverse, L0.String, created, Bindings.STRING);
76 graph.claim(source, IR.IssueSource_Manages, IR.IssueSource_Manages_Inverse, issue);
77 graph.claim(model, L0.ConsistsOf, L0.PartOf, issue);
84 public String toString() {
85 return Arrays.toString(contexts);
89 public Resource getType() {
94 public Object getMainContext() {
99 public Set<Resource> getContexts() {
100 HashSet<Resource> result = new HashSet<Resource>();
101 for(Resource context : contexts) result.add(context);
105 private int makeHashCode() {
106 return type.hashCode() ^ Arrays.hashCode(contexts);
110 public int hashCode() {
115 public boolean equals(Object obj) {
116 if(this == obj) return true;
117 if(!(obj instanceof StandardIssue)) return false;
118 StandardIssue other = (StandardIssue)obj;
119 if(!ObjectUtils.objectEquals(type, other.type)) return false;
120 if(!Arrays.equals(contexts, other.contexts)) return false;