Project

General

Profile

1
package eu.dnetlib.parthenos.jrr;
2

    
3
import java.util.List;
4

    
5
import eu.dnetlib.parthenos.virtuoso.VirtuosoReadAPI;
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.stereotype.Component;
10

    
11
/**
12
 * Created by Alessia Bardi on 2019-06-26.
13
 *
14
 * @author Alessia Bardi
15
 */
16
@Component
17
public class JRRPublisherHelper {
18

    
19
	private static final Log log = LogFactory.getLog(JRRPublisherHelper.class);
20

    
21
	protected final static int LIMIT = 100;
22
	protected static final String ANY_TIME_QUERY_MS = "1800000"; //1800000 == 3 mins
23

    
24
	@Autowired
25
	private VirtuosoReadAPI virtuosoReadAPI;
26
	@Autowired
27
	private JRRPublisher jrrPublisher;
28

    
29

    
30
	public int publish(final String typeNamespace, final String typeName, final String apiId, final String datasourceName) {
31
		int offset = 0;
32
		boolean again;
33
		int countSuccess = 0;
34
		int countFailed = 0;
35
		do {
36
			//get list of subjects of the entityType in the apiURI graph
37
			List<String> subjects = virtuosoReadAPI.getSubjectsForApiWithType(apiId, typeNamespace, typeName, LIMIT, offset);
38
			//get the RDF
39
			for(String subject : subjects) {
40
				try {
41
					String fullTypeName = typeNamespace + typeName;
42
					String record = virtuosoReadAPI.getRDF(subject, fullTypeName, apiId, ANY_TIME_QUERY_MS);
43
					//request publishing on JRR (need to have the OAI header with the proper subject as objIdentifier
44
					jrrPublisher.register(record, subject, datasourceName);
45
					countSuccess++;
46
				}catch(Throwable t){
47
					log.warn(String.format("Cannot publish %s with URL %s", typeName, subject));
48
					log.warn(t.getMessage());
49
					countFailed++;
50
				}
51
			}
52
			again = subjects.size() == LIMIT;
53
			offset += LIMIT;
54
		} while(again);
55
		log.info(String.format("%s : published %d, failed %d", typeName, countSuccess, countFailed));
56
		return countSuccess;
57
	}
58

    
59
	public JRRPublisherHelper(){
60

    
61
	}
62

    
63
	public VirtuosoReadAPI getVirtuosoReadAPI() {
64
		return virtuosoReadAPI;
65
	}
66

    
67
	public void setVirtuosoReadAPI(final VirtuosoReadAPI virtuosoReadAPI) {
68
		this.virtuosoReadAPI = virtuosoReadAPI;
69
	}
70

    
71
	public JRRPublisher getJrrPublisher() {
72
		return jrrPublisher;
73
	}
74

    
75
	public void setJrrPublisher(final JRRPublisher jrrPublisher) {
76
		this.jrrPublisher = jrrPublisher;
77
	}
78
}
(2-2/4)