]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/issue/StandardIssue.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / issue / StandardIssue.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.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;
23
24 public class StandardIssue implements Issue {
25
26         public final Resource type;
27         public final Resource[] contexts;
28         public final int hash;
29
30         public StandardIssue(Resource type, Resource ... contexts) {
31                 this.type = type;
32                 this.contexts = contexts;
33                 this.hash = makeHashCode();
34         }
35
36         public StandardIssue(Resource type, Collection<Resource> contexts) {
37                 this.type = type;
38                 this.contexts = contexts.toArray(new Resource[contexts.size()]);
39                 this.hash = makeHashCode();
40         }
41
42         public void writeAdditionalContext(WriteGraph graph, Resource issue) throws DatabaseException {
43
44                 IssueResource IR = IssueResource.getInstance(graph);
45
46                 // The main context
47                 graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, contexts[0]);
48                 // A possible parent
49                 Layer0 L0 = Layer0.getInstance(graph);
50                 Resource parent = graph.getPossibleObject(contexts[0], L0.PartOf);
51                 if(parent != null) {
52                         graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, parent);
53                 }
54
55         }
56
57         @Override
58         public Resource write(WriteGraph graph, Resource source) throws DatabaseException {
59
60                 Layer0 L0 = Layer0.getInstance(graph);
61                 IssueResource IR = IssueResource.getInstance(graph);
62
63                 Resource model = graph.syncRequest(new IndexRoot(source), TransientCacheListener.<Resource>instance());
64
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);
68
69                 graph.claim(issue, IR.Issue_HasContexts, IR.Issue_HasContexts_Inverse, ListUtils.create(graph, L0.List, contexts));
70
71                 writeAdditionalContext(graph, issue);
72
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);
78                 
79                 return issue;
80
81         }
82
83         @Override
84         public String toString() {
85                 return Arrays.toString(contexts);
86         }
87
88         @Override
89         public Resource getType() {
90                 return type;
91         }
92
93         @Override
94         public Object getMainContext() {
95                 return contexts[0];
96         }
97
98         @Override
99         public Set<Resource> getContexts() {
100                 HashSet<Resource> result = new HashSet<Resource>();
101                 for(Resource context : contexts) result.add(context);
102                 return result;
103         }
104
105         private int makeHashCode() {
106                 return type.hashCode() ^ Arrays.hashCode(contexts);
107         }
108
109         @Override
110         public int hashCode() {
111                 return hash;
112         }
113
114         @Override
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;
121                 return true;
122         }
123
124 }