1
|
package eu.dnetlib.enabling.is.lookup;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.List;
|
7
|
|
8
|
import javax.jws.WebService;
|
9
|
import javax.xml.parsers.ParserConfigurationException;
|
10
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
11
|
import javax.xml.xpath.XPathExpressionException;
|
12
|
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.dom4j.Document;
|
16
|
import org.dom4j.Element;
|
17
|
import org.dom4j.io.SAXReader;
|
18
|
import org.springframework.beans.factory.annotation.Required;
|
19
|
import org.xml.sax.SAXException;
|
20
|
|
21
|
import com.sun.xml.messaging.saaj.util.Base64;
|
22
|
|
23
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
24
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
25
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
26
|
import eu.dnetlib.enabling.is.store.rmi.ISStoreException;
|
27
|
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;
|
28
|
import eu.dnetlib.enabling.tools.AbstractBaseService;
|
29
|
import eu.dnetlib.enabling.tools.CompatResourceIdentifierResolverImpl;
|
30
|
import eu.dnetlib.enabling.tools.OpaqueResource;
|
31
|
import eu.dnetlib.enabling.tools.ResourceIdentifierResolver;
|
32
|
import eu.dnetlib.enabling.tools.ResourceType;
|
33
|
import eu.dnetlib.enabling.tools.StringOpaqueResource;
|
34
|
import eu.dnetlib.enabling.tools.XQueryUtils;
|
35
|
|
36
|
/**
|
37
|
* ISLookUpService implementation.
|
38
|
*
|
39
|
* @author marko
|
40
|
* @author michele
|
41
|
*
|
42
|
*/
|
43
|
@WebService(targetNamespace = "http://services.dnetlib.eu/")
|
44
|
public class ISLookUpServiceImpl extends AbstractBaseService implements ISLookUpService { // NOPMD
|
45
|
|
46
|
/**
|
47
|
* error message when the given collection cannot be fetched.
|
48
|
*/
|
49
|
private static final String COLLECTION_ERROR = "cannot get collection";
|
50
|
|
51
|
/**
|
52
|
* base xmldb directory.
|
53
|
*/
|
54
|
private static final String DB_BASE_DIR = "/db/DRIVER";
|
55
|
|
56
|
/**
|
57
|
* error message when the profile is not present in the db.
|
58
|
*/
|
59
|
private static final String PROFILE_NOT_FOUND = "Profile not found";
|
60
|
|
61
|
private ISStoreService isStore;
|
62
|
|
63
|
// NOPMD by marko on 11/24/08
|
64
|
// 5:02 PM
|
65
|
/**
|
66
|
* logger.
|
67
|
*/
|
68
|
public static final Log log = LogFactory.getLog(ISLookUpServiceImpl.class); // NOPMD by marko on 11/24/08 5:02 PM
|
69
|
|
70
|
/**
|
71
|
* resource identifier resolver. Resolves identifiers to xmldb file and collection names.
|
72
|
*/
|
73
|
private ResourceIdentifierResolver resIdManager = new CompatResourceIdentifierResolverImpl();
|
74
|
|
75
|
/**
|
76
|
* xquery utils. Used to obtain the xmldb collection mapping for resources.
|
77
|
*/
|
78
|
private XQueryUtils xqueryUtils;
|
79
|
|
80
|
/**
|
81
|
* {@inheritDoc}
|
82
|
*
|
83
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#flushCachedResultSets()
|
84
|
*/
|
85
|
@Override
|
86
|
public Boolean flushCachedResultSets() {
|
87
|
// TODO Auto-generated method stub
|
88
|
return false;
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* {@inheritDoc}
|
93
|
*
|
94
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#retrieveCollection(java.lang.String)
|
95
|
*/
|
96
|
@Override
|
97
|
public String retrieveCollection(final String profId) throws ISLookUpException {
|
98
|
try {
|
99
|
String profile = this.getResourceProfile(profId);
|
100
|
final List<String> list = quickSearchProfile("for $x in collection('/db/DRIVER/CollectionDSResources') where $x//FATHER/@id = '" + profId
|
101
|
+ "' return $x//RESOURCE_IDENTIFIER/@value/string()");
|
102
|
|
103
|
if (!list.isEmpty()) {
|
104
|
final SAXReader reader = new SAXReader();
|
105
|
final Document doc = reader.read(new StringReader(profile));
|
106
|
final Element childrenNode = (Element) doc.selectSingleNode("//CHILDREN"); // NOPMD
|
107
|
for (final String idC : list) {
|
108
|
childrenNode.addElement("CHILD").addAttribute("id", idC);
|
109
|
}
|
110
|
profile = doc.asXML();
|
111
|
}
|
112
|
|
113
|
return profile;
|
114
|
} catch (final Exception e) {
|
115
|
throw new ISLookUpException(e);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
/**
|
120
|
* {@inheritDoc}
|
121
|
*
|
122
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getCollection(java.lang.String, java.lang.String)
|
123
|
*/
|
124
|
@Override
|
125
|
public String getCollection(final String profId, final String format) throws ISLookUpException {
|
126
|
OpaqueResource prof;
|
127
|
try {
|
128
|
prof = new StringOpaqueResource(profId);
|
129
|
|
130
|
final StringBuilder query = new StringBuilder();
|
131
|
if (format != null && format.equals("ui")) {
|
132
|
query.append("doc('");
|
133
|
query.append(xqueryUtils.getCollectionAbsPath(prof));
|
134
|
query.append('/');
|
135
|
query.append(prof.getResourceId().split("_")[0]);
|
136
|
query.append("')");
|
137
|
} else {
|
138
|
query.append("let $x := doc('");
|
139
|
query.append(xqueryUtils.getCollectionAbsPath(prof));
|
140
|
query.append('/');
|
141
|
query.append(prof.getResourceId().split("_")[0]);
|
142
|
query.append("') return <COLLECTION name='{$x//NAME}' id='{$x//RESOURCE_IDENTIFIER/@value/string()}'>");
|
143
|
query.append("<STATUS private='{$x//PRIVATE}' visible='{$x//VISIBLE}' container='{$x//CONTAINER}'");
|
144
|
query.append("count_docs='{$x//COUNT_DOCS/@number/string()}' last_update='{$x//COUNT_DOCS/@last_update/string()}' />");
|
145
|
query.append("{$x//IMAGE_URL}{$x//DESCRIPTION}{$x//OWNER}{$x//FATHER}{$x//SUBJECT}{$x//CHILDREN}{$x//MEMBERSHIP_CONDITION}{$x//RETRIEVAL_CONDITION}</COLLECTION>");
|
146
|
}
|
147
|
|
148
|
return isStore.getXMLbyQuery(query.toString());
|
149
|
} catch (final ISStoreException e) {
|
150
|
throw new ISLookUpException(e);
|
151
|
} catch (final XPathExpressionException e) {
|
152
|
throw new ISLookUpException(COLLECTION_ERROR, e);
|
153
|
} catch (final SAXException e) {
|
154
|
throw new ISLookUpException(COLLECTION_ERROR, e);
|
155
|
} catch (final IOException e) {
|
156
|
throw new ISLookUpException(COLLECTION_ERROR, e);
|
157
|
} catch (final ParserConfigurationException e) {
|
158
|
throw new ISLookUpException(COLLECTION_ERROR, e);
|
159
|
}
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* {@inheritDoc}
|
164
|
*
|
165
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getResourceProfile(java.lang.String)
|
166
|
*/
|
167
|
@Override
|
168
|
public String getResourceProfile(final String profId) throws ISLookUpException {
|
169
|
if (profId == null || profId.isEmpty()) { throw new ISLookUpException("Invalid null profile ID: " + profId); }
|
170
|
|
171
|
try {
|
172
|
final String res = isStore.getXML(getFileNameForId(profId),
|
173
|
xqueryUtils.getRootCollection() + getFileCollForId(profId));
|
174
|
if (res == null) { throw new ISLookUpDocumentNotFoundException("document " + profId + " not found"); }
|
175
|
return res;
|
176
|
} catch (final ISStoreException e) {
|
177
|
throw new ISLookUpException(e);
|
178
|
}
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* obtain the xmldb file name from the profile identifier.
|
183
|
*
|
184
|
* @param profId
|
185
|
* profile id
|
186
|
* @return xml db file name
|
187
|
*/
|
188
|
String getFileNameForId(final String profId) {
|
189
|
return resIdManager.getFileName(profId);
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* obtain the xmldb collection name from the profile identifier.
|
194
|
*
|
195
|
* @param profId
|
196
|
* profile id
|
197
|
* @return plaintext xmldb collection name
|
198
|
*/
|
199
|
String getFileCollForId(final String profId) {
|
200
|
return resIdManager.getCollectionName(profId);
|
201
|
}
|
202
|
|
203
|
/**
|
204
|
* {@inheritDoc}
|
205
|
*
|
206
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getResourceProfileByQuery(java.lang.String)
|
207
|
*/
|
208
|
@Override
|
209
|
public String getResourceProfileByQuery(final String xquery) throws ISLookUpException {
|
210
|
String resource;
|
211
|
try {
|
212
|
resource = isStore.getXMLbyQuery(xquery);
|
213
|
if (resource == null || resource.isEmpty()) { throw new ISLookUpDocumentNotFoundException(PROFILE_NOT_FOUND); }
|
214
|
return resource;
|
215
|
} catch (final ISStoreException e) {
|
216
|
throw new ISLookUpException(e);
|
217
|
}
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* {@inheritDoc}
|
222
|
*
|
223
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getResourceQoSParams(java.lang.String)
|
224
|
*/
|
225
|
@Override
|
226
|
public String getResourceQoSParams(final String profId) throws ISLookUpException {
|
227
|
final String query = "for $x in collection('/db/DRIVER/ServiceResources') where $x//RESOURCE_IDENTIFIER/@value = '" + profId + "' return $x//QOS";
|
228
|
return getResourceProfileByQuery(query);
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* {@inheritDoc}
|
233
|
*
|
234
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#getResourceTypeSchema(java.lang.String)
|
235
|
*/
|
236
|
@Override
|
237
|
public String getResourceTypeSchema(final String resourceType) throws ISLookUpException, ISLookUpDocumentNotFoundException {
|
238
|
if (resourceType == null || resourceType.isEmpty()) { throw new ISLookUpException("Invalid resourceType"); }
|
239
|
|
240
|
try {
|
241
|
final String resource = isStore.getXML(resourceType, xqueryUtils.getRootCollection() + ResourceType.RESOURCE_TYPES);
|
242
|
if (resource == null || resource.isEmpty()) { throw new ISLookUpDocumentNotFoundException(PROFILE_NOT_FOUND); }
|
243
|
return resource;
|
244
|
} catch (final ISStoreException e) {
|
245
|
throw new ISLookUpException(e);
|
246
|
}
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* {@inheritDoc}
|
251
|
*
|
252
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listCollections(java.lang.String, java.lang.String, java.lang.String)
|
253
|
*/
|
254
|
@Override
|
255
|
public List<String> listCollections(final String format, final String idfatherParam, final String owner) throws ISLookUpException {
|
256
|
String idfather = idfatherParam; // PMD
|
257
|
if (idfather == null || idfather.length() == 0) {
|
258
|
idfather = "InfoSpace";
|
259
|
}
|
260
|
final String fileColl = xqueryUtils.getRootCollection() + "CollectionDSResources/CollectionDSResourceType";
|
261
|
|
262
|
final StringBuilder query = new StringBuilder();
|
263
|
query.append("for $x in collection('" + fileColl + "') where $x//FATHER[@id='" + idfather + "'] "); // NOPMD
|
264
|
if (owner != null && owner.length() > 0) {
|
265
|
query.append("and $x//OWNER[@id='" + owner + "'] ");
|
266
|
}
|
267
|
if (format != null && format.equals("short")) {
|
268
|
query.append("return $x//RESOURCE_IDENTIFIER/@value/string()");
|
269
|
} else {
|
270
|
query.append("return $x");
|
271
|
}
|
272
|
|
273
|
return quickSearchProfile(query.toString());
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* {@inheritDoc}
|
278
|
*
|
279
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listDHNIDs()
|
280
|
*/
|
281
|
@Override
|
282
|
public List<String> listDHNIDs() throws ISLookUpException {
|
283
|
final String fileColl = DB_BASE_DIR + "/InfrastructureResources/DRIVERHostingNodeDSResourceType";
|
284
|
|
285
|
final ArrayList<String> ret = new ArrayList<String>();
|
286
|
try {
|
287
|
for (final String i : isStore.getFileNames(fileColl)) {
|
288
|
ret.add(i + ":" + fileColl);
|
289
|
}
|
290
|
return ret;
|
291
|
} catch (final ISStoreException e) {
|
292
|
throw new ISLookUpException(e);
|
293
|
}
|
294
|
}
|
295
|
|
296
|
/**
|
297
|
* {@inheritDoc}
|
298
|
*
|
299
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listResourceTypes()
|
300
|
*/
|
301
|
@Override
|
302
|
public List<String> listResourceTypes() throws ISLookUpException {
|
303
|
try {
|
304
|
return isStore.getFileNames(DB_BASE_DIR + "/" + ResourceType.RESOURCE_TYPES);
|
305
|
} catch (final ISStoreException e) {
|
306
|
throw new ISLookUpException(e);
|
307
|
}
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* {@inheritDoc}
|
312
|
*
|
313
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listServiceIDs(java.lang.String)
|
314
|
*/
|
315
|
@Override
|
316
|
public List<String> listServiceIDs(final String serviceType) throws ISLookUpException {
|
317
|
final String fileColl = "ServiceResources/" + serviceType;
|
318
|
final String encodedColl = "_" + new String(Base64.encode(fileColl.getBytes())); // NOPMD
|
319
|
|
320
|
final List<String> ret = new ArrayList<String>();
|
321
|
try {
|
322
|
for (final String i : isStore.getFileNames(DB_BASE_DIR + "/" + fileColl)) {
|
323
|
ret.add(i + encodedColl);
|
324
|
}
|
325
|
return ret;
|
326
|
} catch (final ISStoreException e) {
|
327
|
throw new ISLookUpException(e);
|
328
|
}
|
329
|
}
|
330
|
|
331
|
/**
|
332
|
* {@inheritDoc}
|
333
|
*
|
334
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#listServiceTypes()
|
335
|
*/
|
336
|
@Override
|
337
|
public List<String> listServiceTypes() throws ISLookUpException {
|
338
|
final List<String> ret = new ArrayList<String>();
|
339
|
try {
|
340
|
for (final String i : isStore.getFileColls()) {
|
341
|
if (i.startsWith("ServiceResources/")) {
|
342
|
ret.add(i.substring("ServiceResources/".length()));
|
343
|
}
|
344
|
}
|
345
|
return ret;
|
346
|
} catch (final ISStoreException e) {
|
347
|
throw new ISLookUpException(e);
|
348
|
}
|
349
|
}
|
350
|
|
351
|
/**
|
352
|
* {@inheritDoc}
|
353
|
*
|
354
|
* @see eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService#quickSearchProfile(java.lang.String)
|
355
|
*/
|
356
|
@Override
|
357
|
public List<String> quickSearchProfile(final String xquery) throws ISLookUpException {
|
358
|
try {
|
359
|
return isStore.quickSearchXML(xquery);
|
360
|
} catch (final ISStoreException e) {
|
361
|
throw new ISLookUpException(e);
|
362
|
}
|
363
|
}
|
364
|
|
365
|
public ResourceIdentifierResolver getResIdManager() {
|
366
|
return resIdManager;
|
367
|
}
|
368
|
|
369
|
public void setResIdManager(final ResourceIdentifierResolver resIdManager) {
|
370
|
this.resIdManager = resIdManager;
|
371
|
}
|
372
|
|
373
|
@Required
|
374
|
public void setXqueryUtils(final XQueryUtils xqueryUtils) {
|
375
|
this.xqueryUtils = xqueryUtils;
|
376
|
}
|
377
|
|
378
|
public XQueryUtils getXqueryUtils() {
|
379
|
return xqueryUtils;
|
380
|
}
|
381
|
|
382
|
public ISStoreService getIsStore() {
|
383
|
return isStore;
|
384
|
}
|
385
|
|
386
|
@Required
|
387
|
public void setIsStore(final ISStoreService isStore) {
|
388
|
this.isStore = isStore;
|
389
|
}
|
390
|
|
391
|
}
|