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 record = virtuosoReadAPI.getRDF(subject, typeName, apiId, ANY_TIME_QUERY_MS);
42
					jrrPublisher.register(record, subject, datasourceName);
43
					countSuccess++;
44
				}catch(Throwable t){
45
					log.error(String.format("Cannot publish %s with URL %s", typeName, subject), t);
46
					countFailed++;
47
				}
48
			}
49
			again = subjects.size() == LIMIT;
50
			offset += LIMIT;
51
		} while(again);
52
		log.info(String.format("%s : published %d, failed %d", typeName, countSuccess, countFailed));
53
		return countSuccess;
54
	}
55

    
56
	public JRRPublisherHelper(){
57

    
58
	}
59

    
60
	public VirtuosoReadAPI getVirtuosoReadAPI() {
61
		return virtuosoReadAPI;
62
	}
63

    
64
	public void setVirtuosoReadAPI(final VirtuosoReadAPI virtuosoReadAPI) {
65
		this.virtuosoReadAPI = virtuosoReadAPI;
66
	}
67

    
68
	public JRRPublisher getJrrPublisher() {
69
		return jrrPublisher;
70
	}
71

    
72
	public void setJrrPublisher(final JRRPublisher jrrPublisher) {
73
		this.jrrPublisher = jrrPublisher;
74
	}
75
}
(2-2/4)