1
|
package eu.dnetlib.data.mdstore.plugins.objects;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.net.URI;
|
5
|
import java.util.regex.Matcher;
|
6
|
import java.util.regex.Pattern;
|
7
|
|
8
|
import javax.xml.bind.annotation.XmlAccessType;
|
9
|
import javax.xml.bind.annotation.XmlAccessorType;
|
10
|
import javax.xml.bind.annotation.XmlElement;
|
11
|
|
12
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.dom4j.Document;
|
16
|
import org.dom4j.DocumentException;
|
17
|
import org.dom4j.io.SAXReader;
|
18
|
|
19
|
import eu.dnetlib.data.utils.HttpFetcher;
|
20
|
|
21
|
@XmlAccessorType(XmlAccessType.FIELD)
|
22
|
public class Project {
|
23
|
|
24
|
@XmlElement(name = "infoId")
|
25
|
private String infoId;
|
26
|
@XmlElement(name = "openaireId")
|
27
|
private String openaireId;
|
28
|
@XmlElement(name = "code")
|
29
|
private String code;
|
30
|
@XmlElement(name = "name")
|
31
|
private String name;
|
32
|
@XmlElement(name = "acronym")
|
33
|
private String acronym;
|
34
|
@XmlElement(name = "funder")
|
35
|
private String funder;
|
36
|
@XmlElement(name = "program")
|
37
|
private String program;
|
38
|
@XmlElement(name = "jurisdiction")
|
39
|
private String jurisdiction;
|
40
|
|
41
|
private static final Log log = LogFactory.getLog(Project.class);
|
42
|
|
43
|
private static final String PROJECT_REGEX = "info:eu-repo\\/grantAgreement\\/(.*)\\/(.*)\\/(.*)\\/(.*)\\/(.*)\\/(.*)";
|
44
|
|
45
|
public static Project newInstance(final URI url) {
|
46
|
|
47
|
try {
|
48
|
final SAXReader reader = new SAXReader();
|
49
|
final String s = HttpFetcher.fetch(url);
|
50
|
final Document doc = reader.read(new StringReader(s));
|
51
|
|
52
|
final String openaireId = doc.valueOf("//*[local-name()='objIdentifier']");
|
53
|
|
54
|
if (StringUtils.isBlank(openaireId)) {
|
55
|
log.warn("Project not found using OpenAIRE API, url: " + url);
|
56
|
return null;
|
57
|
}
|
58
|
|
59
|
final String code = doc.valueOf("//code");
|
60
|
final String name = doc.valueOf("//title");
|
61
|
final String acronym = doc.valueOf("//acronym");
|
62
|
final String funder = doc.valueOf("//funder/shortname");
|
63
|
final String program = doc.valueOf("//funding_level_0/name");
|
64
|
final String jurisdiction = doc.valueOf("//funder/jurisdiction");
|
65
|
|
66
|
return new Project(openaireId, code, name, acronym, funder, program, jurisdiction);
|
67
|
} catch (final DocumentException e) {
|
68
|
log.error("Error parsing document from url " + url);
|
69
|
throw new RuntimeException(e);
|
70
|
}
|
71
|
|
72
|
}
|
73
|
|
74
|
public static Project newInstance(final String infoId) {
|
75
|
final Pattern pattern = Pattern.compile(PROJECT_REGEX);
|
76
|
final Matcher matcher = pattern.matcher(infoId);
|
77
|
if (matcher.find()) {
|
78
|
final String openaireId = "";
|
79
|
final String funder = matcher.group(1).replaceAll("%2F", "/");
|
80
|
final String program = matcher.group(2).replaceAll("%2F", "/");
|
81
|
final String code = matcher.group(3).replaceAll("%2F", "/");
|
82
|
final String jurisdiction = matcher.group(4).replaceAll("%2F", "/");
|
83
|
final String name = StringUtils.defaultIfBlank(matcher.group(5).replaceAll("%2F", "/"), funder + "/" + program + "/" + code);
|
84
|
final String acronym = StringUtils.defaultIfBlank(matcher.group(6), name).replaceAll("%2F", "/");
|
85
|
|
86
|
if (StringUtils.isNotEmpty(code) && StringUtils.isNotEmpty(program)
|
87
|
&& StringUtils.isNotEmpty(funder)) {
|
88
|
return new Project(openaireId, code, name, acronym, funder, program, jurisdiction);
|
89
|
}
|
90
|
}
|
91
|
|
92
|
log.warn("Invalid project ID: " + infoId);
|
93
|
return null;
|
94
|
|
95
|
}
|
96
|
|
97
|
public static boolean isValid(final String infoId) {
|
98
|
return Project.newInstance(infoId) != null;
|
99
|
}
|
100
|
|
101
|
public Project() {}
|
102
|
|
103
|
public Project(final String openaireId, final String code, final String name, final String acronym, final String funder, final String program,
|
104
|
final String jurisdiction) {
|
105
|
this.openaireId = openaireId;
|
106
|
this.code = code;
|
107
|
this.name = name;
|
108
|
this.acronym = acronym;
|
109
|
this.funder = funder;
|
110
|
this.program = program;
|
111
|
this.jurisdiction = jurisdiction;
|
112
|
}
|
113
|
|
114
|
public String getOpenaireId() {
|
115
|
return openaireId;
|
116
|
}
|
117
|
|
118
|
public String getCode() {
|
119
|
return code;
|
120
|
}
|
121
|
|
122
|
public String getName() {
|
123
|
return name;
|
124
|
}
|
125
|
|
126
|
public String getAcronym() {
|
127
|
return acronym;
|
128
|
}
|
129
|
|
130
|
public String getFunder() {
|
131
|
return funder;
|
132
|
}
|
133
|
|
134
|
public String getProgram() {
|
135
|
return program;
|
136
|
}
|
137
|
|
138
|
public String getJurisdiction() {
|
139
|
return jurisdiction;
|
140
|
}
|
141
|
|
142
|
public boolean match(final String infoId) {
|
143
|
final Project p = Project.newInstance(infoId);
|
144
|
return ((p != null) && funder.equals(p.funder) && program.equals(p.program) && code.equals(p.code));
|
145
|
}
|
146
|
|
147
|
@Override
|
148
|
public String toString() {
|
149
|
return "Project [openaireId=" + openaireId + ", code=" + code + ", name=" + name + ", acronym=" + acronym + ", funder=" + funder + ", program="
|
150
|
+ program + ", jurisdiction=" + jurisdiction + "]";
|
151
|
}
|
152
|
|
153
|
@Override
|
154
|
public int hashCode() {
|
155
|
final int prime = 31;
|
156
|
int result = 1;
|
157
|
result = (prime * result) + ((code == null) ? 0 : code.hashCode());
|
158
|
result = (prime * result) + ((funder == null) ? 0 : funder.hashCode());
|
159
|
result = (prime * result) + ((program == null) ? 0 : program.hashCode());
|
160
|
return result;
|
161
|
}
|
162
|
|
163
|
@Override
|
164
|
public boolean equals(final Object obj) {
|
165
|
if (this == obj) { return true; }
|
166
|
if (obj == null) { return false; }
|
167
|
if (getClass() != obj.getClass()) { return false; }
|
168
|
final Project other = (Project) obj;
|
169
|
if (code == null) {
|
170
|
if (other.code != null) { return false; }
|
171
|
} else if (!code.equals(other.code)) { return false; }
|
172
|
if (funder == null) {
|
173
|
if (other.funder != null) { return false; }
|
174
|
} else if (!funder.equals(other.funder)) { return false; }
|
175
|
if (program == null) {
|
176
|
if (other.program != null) { return false; }
|
177
|
} else if (!program.equals(other.program)) { return false; }
|
178
|
return true;
|
179
|
}
|
180
|
|
181
|
public String getInfoId() {
|
182
|
return infoId;
|
183
|
}
|
184
|
|
185
|
public void setInfoId(final String infoId) {
|
186
|
this.infoId = infoId;
|
187
|
}
|
188
|
|
189
|
public void setOpenaireId(final String openaireId) {
|
190
|
this.openaireId = openaireId;
|
191
|
}
|
192
|
|
193
|
public void setCode(final String code) {
|
194
|
this.code = code;
|
195
|
}
|
196
|
|
197
|
public void setName(final String name) {
|
198
|
this.name = name;
|
199
|
}
|
200
|
|
201
|
public void setAcronym(final String acronym) {
|
202
|
this.acronym = acronym;
|
203
|
}
|
204
|
|
205
|
public void setFunder(final String funder) {
|
206
|
this.funder = funder;
|
207
|
}
|
208
|
|
209
|
public void setProgram(final String program) {
|
210
|
this.program = program;
|
211
|
}
|
212
|
|
213
|
public void setJurisdiction(final String jurisdiction) {
|
214
|
this.jurisdiction = jurisdiction;
|
215
|
}
|
216
|
}
|