1
|
package eu.dnetlib.data.mapreduce.util;
|
2
|
|
3
|
import com.google.protobuf.Descriptors.FieldDescriptor;
|
4
|
import com.google.protobuf.GeneratedMessage;
|
5
|
import com.google.protobuf.Message.Builder;
|
6
|
import com.google.protobuf.MessageOrBuilder;
|
7
|
import com.google.protobuf.ProtocolMessageEnum;
|
8
|
|
9
|
import eu.dnetlib.data.proto.OafProtos.OafEntity;
|
10
|
import eu.dnetlib.data.proto.OafProtos.OafRel;
|
11
|
import eu.dnetlib.data.proto.RelMetadataProtos.RelMetadata;
|
12
|
import eu.dnetlib.data.proto.RelTypeProtos.RelType;
|
13
|
import eu.dnetlib.data.proto.RelTypeProtos.SubRelType;
|
14
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
15
|
|
16
|
public class OafRelDecoder {
|
17
|
|
18
|
private static final String SEPARATOR = "_";
|
19
|
|
20
|
private final OafRel oafRel;
|
21
|
|
22
|
public static OafRelDecoder decode(final OafRel oafRel) {
|
23
|
return new OafRelDecoder(oafRel);
|
24
|
}
|
25
|
|
26
|
private OafRelDecoder(final OafRel oafRel) {
|
27
|
this.oafRel = oafRel;
|
28
|
}
|
29
|
|
30
|
public RelType getRelType() {
|
31
|
return oafRel.getRelType();
|
32
|
}
|
33
|
|
34
|
public String relTypeName() {
|
35
|
return getRelType().toString();
|
36
|
}
|
37
|
|
38
|
public SubRelType getSubRelType() {
|
39
|
return oafRel.getSubRelType();
|
40
|
}
|
41
|
|
42
|
public String relSubTypeName() {
|
43
|
return getSubRelType().toString();
|
44
|
}
|
45
|
|
46
|
public String getCF() {
|
47
|
return OafRelDecoder.getCF(getRelType(), getSubRelType());
|
48
|
}
|
49
|
|
50
|
public String getCFQ() {
|
51
|
return OafRelDecoder.getCFQ(getRelType(), getSubRelType(), getRelClass());
|
52
|
}
|
53
|
|
54
|
public static String getCFQ(final RelType relType, final SubRelType subRelType, final ProtocolMessageEnum relClass) {
|
55
|
return OafRelDecoder.getCFQ(relType, subRelType, relClass.getValueDescriptor().getName());
|
56
|
}
|
57
|
|
58
|
public static String getCFQ(final RelType relType, final SubRelType subRelType, final String relClass) {
|
59
|
return OafRelDecoder.getCF(relType, subRelType) + SEPARATOR + relClass;
|
60
|
}
|
61
|
|
62
|
public static String getCF(final RelType relType, final SubRelType subRelType) {
|
63
|
return relType + SEPARATOR + subRelType;
|
64
|
}
|
65
|
|
66
|
public String getRelClass() {
|
67
|
return oafRel.getRelClass();
|
68
|
}
|
69
|
|
70
|
public RelDescriptor getRelDescriptor() {
|
71
|
return new RelDescriptor(getCFQ());
|
72
|
}
|
73
|
|
74
|
public GeneratedMessage getRel() {
|
75
|
|
76
|
FieldDescriptor fd = oafRel.getDescriptorForType().findFieldByName(relTypeName());
|
77
|
return (GeneratedMessage) oafRel.getField(fd);
|
78
|
}
|
79
|
|
80
|
public GeneratedMessage getSubRel() {
|
81
|
GeneratedMessage rel = getRel();
|
82
|
FieldDescriptor fd = rel.getDescriptorForType().findFieldByName(relSubTypeName());
|
83
|
return (GeneratedMessage) rel.getField(fd);
|
84
|
}
|
85
|
|
86
|
public RelMetadata getRelMetadata() {
|
87
|
GeneratedMessage rel = getSubRel();
|
88
|
FieldDescriptor fd = rel.getDescriptorForType().findFieldByName("relMetadata");
|
89
|
return fd != null ? (RelMetadata) rel.getField(fd) : null;
|
90
|
}
|
91
|
|
92
|
public OafRel.Builder setClassId(final String classid) {
|
93
|
RelMetadata.Builder relMetadataBuilder = RelMetadata.newBuilder(getRelMetadata());
|
94
|
relMetadataBuilder.getSemanticsBuilder().setClassid(classid).setClassname(classid);
|
95
|
|
96
|
OafRel.Builder builder = OafRel.newBuilder(oafRel);
|
97
|
|
98
|
FieldDescriptor fdRel = fd(oafRel, relTypeName());
|
99
|
Builder relBuilder = builder.newBuilderForField(fdRel);
|
100
|
|
101
|
FieldDescriptor fdSubRel = fd(relBuilder, relSubTypeName());
|
102
|
Builder subRelBuilder = relBuilder.newBuilderForField(fdSubRel).mergeFrom(getSubRel());
|
103
|
|
104
|
subRelBuilder.setField(fd(getSubRel(), "relMetadata"), relMetadataBuilder.build());
|
105
|
|
106
|
relBuilder.setField(fdSubRel, subRelBuilder.build());
|
107
|
builder.setField(fdRel, relBuilder.build());
|
108
|
|
109
|
return builder.setRelClass(classid);
|
110
|
}
|
111
|
|
112
|
public Type getTargetType(final Type sourceType) {
|
113
|
switch (getRelType()) {
|
114
|
case datasourceOrganization:
|
115
|
return sourceType.equals(Type.datasource) ? Type.organization : Type.datasource;
|
116
|
case organizationOrganization:
|
117
|
return Type.organization;
|
118
|
case projectOrganization:
|
119
|
return sourceType.equals(Type.project) ? Type.organization : Type.project;
|
120
|
case resultOrganization:
|
121
|
return sourceType.equals(Type.result) ? Type.organization : Type.result;
|
122
|
case resultProject:
|
123
|
return sourceType.equals(Type.result) ? Type.project : Type.result;
|
124
|
case resultResult:
|
125
|
return Type.result;
|
126
|
default:
|
127
|
throw new IllegalArgumentException("Unknown relationship type: " + relTypeName());
|
128
|
}
|
129
|
}
|
130
|
|
131
|
protected FieldDescriptor fd(final MessageOrBuilder mb, final int fieldNumber) {
|
132
|
return mb.getDescriptorForType().findFieldByNumber(fieldNumber);
|
133
|
}
|
134
|
|
135
|
protected FieldDescriptor fd(final MessageOrBuilder mb, final String fieldName) {
|
136
|
return mb.getDescriptorForType().findFieldByName(fieldName);
|
137
|
}
|
138
|
|
139
|
public String getCachedTargedId() {
|
140
|
|
141
|
if (!oafRel.hasCachedTarget()) return null;
|
142
|
|
143
|
final OafEntity entity = oafRel.getCachedTarget();
|
144
|
return OafEntityDecoder.decode(entity).getId();
|
145
|
}
|
146
|
|
147
|
public String getRelSourceId() {
|
148
|
return oafRel.getSource();
|
149
|
}
|
150
|
|
151
|
public String getRelTargetId() {
|
152
|
return oafRel.getTarget();
|
153
|
}
|
154
|
|
155
|
}
|