Project

General

Profile

1
package eu.dnetlib.parthenos.publisher;
2

    
3
import eu.dnetlib.parthenos.registry.RegistryClient;
4
import eu.dnetlib.parthenos.registry.RegistryClientFactory;
5
import eu.dnetlib.parthenos.virtuoso.VirtuosoClient;
6
import eu.dnetlib.parthenos.virtuoso.VirtuosoClientFactory;
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Component;
11

    
12
/**
13
 * Created by Alessia Bardi on 11/08/2017.
14
 *
15
 * @author Alessia Bardi
16
 */
17
@Component
18
public class ParthenosPublisherHelper {
19

    
20
	private static final Log log = LogFactory.getLog(ParthenosPublisherHelper.class);
21

    
22
	public enum ParthenosTargets{
23
		ALL, SOLR, VIRTUOSO, JRR, OAI
24
	}
25

    
26
	@Autowired
27
	private VirtuosoClientFactory virtuosoClientFactory;
28
	@Autowired
29
	private RegistryClientFactory registryClientFactory;
30

    
31
	public void publish(final String record, final ParthenosTargets target) throws ParthenosPublisherException {
32
		switch(target){
33
		case ALL:
34
			publishVirtuoso(record);
35
			publishJRR(record);
36
			break;
37
		case VIRTUOSO:
38
			publishVirtuoso(record);
39
			break;
40
		case JRR:
41
			publishJRR(record);
42
			break;
43
		default: throw new ParthenosPublisherException("Target "+target+" not supported yet");
44
		}
45

    
46
	}
47

    
48
	public long unpublish(final String datasourceInterface, final ParthenosTargets target) throws ParthenosPublisherException {
49
		long res = 0;
50
		switch(target){
51
		case ALL:
52
			res = unpublishVirtuoso(datasourceInterface);
53
			break;
54
		case VIRTUOSO:
55
			res = unpublishVirtuoso(datasourceInterface);
56
			break;
57
		default: throw new ParthenosPublisherException("Target "+target+" not supported yet");
58
		}
59
		return res;
60
	}
61

    
62
	private void publishVirtuoso(final String record) throws ParthenosPublisherException {
63
		log.debug("Publishing on virtuoso");
64
		VirtuosoClient virtuosoClient = this.virtuosoClientFactory.getVirtuosoClient();
65
		virtuosoClient.feed(record);
66
	}
67

    
68
	private long unpublishVirtuoso(final String datasourceInterface) throws ParthenosPublisherException {
69
		log.debug("Unpublishing from virtuoso "+datasourceInterface);
70
		VirtuosoClient virtuosoClient = this.virtuosoClientFactory.getVirtuosoClient();
71
		return virtuosoClient.drop(datasourceInterface);
72
	}
73

    
74
	private void publishJRR(final String record) throws ParthenosPublisherException {
75
		log.debug("Publishing on Registry");
76
		RegistryClient registryClient = this.registryClientFactory.getRegistryClient();
77
		registryClient.register(record);
78
	}
79

    
80
	private int unpublishJRR(final String datasourceInterface) throws ParthenosPublisherException {
81
		log.debug("Unpublishing from registry "+datasourceInterface);
82
		RegistryClient registryClient = this.registryClientFactory.getRegistryClient();
83
		return registryClient.unregister(datasourceInterface);
84
	}
85

    
86

    
87
}
(3-3/4)