JcrRelationship.java
3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.jackrabbit.util.Text
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.socialgraph.impl;
import com.adobe.granite.socialgraph.Direction;
import com.adobe.granite.socialgraph.GraphNode;
import com.adobe.granite.socialgraph.Relationship;
import com.adobe.granite.socialgraph.impl.AbstractPropertyMap;
import com.adobe.granite.socialgraph.impl.JcrSocialGraph;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JcrRelationship
extends AbstractPropertyMap
implements Relationship {
private static final Logger log = LoggerFactory.getLogger(JcrRelationship.class);
private final JcrSocialGraph graph;
private final String startId;
private final String endId;
private final String type;
private final String signature;
public JcrRelationship(JcrSocialGraph graph, String path, String type, String startId, String endId) {
super(path);
this.graph = graph;
this.startId = startId;
this.endId = endId;
this.type = type;
this.signature = Text.escape((String)startId) + ":" + Text.escape((String)type) + ":" + Text.escape((String)endId);
}
protected Node getNode(boolean create) {
try {
Session s = this.graph.getSession();
if (s.nodeExists(this.path)) {
return s.getNode(this.path);
}
if (create) {
throw new IllegalArgumentException("relationship nodes must exist.");
}
}
catch (RepositoryException e) {
log.warn("error while reading relationship node", (Throwable)e);
}
return null;
}
public void delete() {
try {
Session s = this.graph.getSession();
if (s.nodeExists(this.path)) {
s.getNode(this.path).remove();
}
}
catch (RepositoryException e) {
log.warn("error while deleting relationship node", (Throwable)e);
}
}
public String signature() {
return this.signature;
}
public boolean isVirtual() {
return false;
}
public GraphNode getStartNode() {
return this.graph.getNode(this.startId);
}
public GraphNode getEndNode() {
return this.graph.getNode(this.endId);
}
public GraphNode getOtherNode(GraphNode node) {
String id = node.getId();
if (id.equals(this.startId)) {
return this.getEndNode();
}
if (id.equals(this.endId)) {
return this.getStartNode();
}
throw new IllegalArgumentException("Neither start- or end node given.");
}
public boolean isBidirectional() {
GraphNode start = this.getStartNode();
GraphNode end = this.getEndNode();
return end != null && start != null && end.getRelationship(Direction.OUTGOING, start, this.type) != null;
}
public String getType() {
return this.type;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("JcrRelationship");
sb.append("{path='").append(this.path).append('\'');
sb.append(", startId='").append(this.startId).append('\'');
sb.append(", endId='").append(this.endId).append('\'');
sb.append(", type=").append(this.type);
sb.append('}');
return sb.toString();
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Relationship)) {
return false;
}
Relationship that = (Relationship)o;
return this.signature.equals(that.signature());
}
public int hashCode() {
return this.signature.hashCode();
}
}