Project

General

Profile

1
package gr.uoa.di.web.utils;
2

    
3
import eu.dnetlib.api.data.SearchServiceException;
4
import eu.dnetlib.domain.ResourceType;
5
import eu.dnetlib.domain.data.Repository;
6
import eu.dnetlib.domain.data.RepositorySearchCriteria;
7
import eu.dnetlib.domain.enabling.Notification;
8
import gr.uoa.di.driver.app.DriverServiceImpl;
9
import gr.uoa.di.driver.enabling.ISLookUp;
10
import gr.uoa.di.driver.enabling.ISLookUpException;
11
import gr.uoa.di.driver.enabling.issn.NotificationListener;
12
import gr.uoa.di.web.utils.search.SearchManager;
13
import org.apache.log4j.Logger;
14

    
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.concurrent.Executors;
19
import java.util.concurrent.ScheduledExecutorService;
20
import java.util.concurrent.TimeUnit;
21

    
22
public class DriverStatus implements NotificationListener {
23

    
24
	private long documentCount = 0;
25
	private int repositoryCount = 0;
26
	private int countryCount = 0;
27
	private int documentCountGranularity = 10000;
28
	private boolean enableUpdate = true;
29

    
30
	private SearchManager searchManager = null;
31
	private ISLookUp<Repository> repositoryLookUp = null;
32

    
33
	private DriverServiceImpl serviceImpl = null;
34
	
35
	private ScheduledExecutorService executor = null;
36
	
37
	private static Logger logger = Logger.getLogger(DriverStatus.class);
38

    
39
	public DriverStatus(long documentCount, int repositoryCount, int countryCount, boolean enableUpdate) {
40
		super();
41
		
42
		this.documentCount = documentCount;
43
		this.repositoryCount = repositoryCount;
44
		this.countryCount = countryCount;
45
		this.enableUpdate = enableUpdate;
46
	}
47

    
48
	public void init() {
49
	/*	serviceImpl.subscribe(ActionType.CREATE, ResourceType.INDEXDSRESOURCETYPE, this);
50
		serviceImpl.subscribe(ActionType.UPDATE, ResourceType.INDEXDSRESOURCETYPE, this);
51
		serviceImpl.subscribe(ActionType.DELETE, ResourceType.INDEXDSRESOURCETYPE, this);
52
		serviceImpl.subscribe(ActionType.CREATE, ResourceType.REPOSITORYSERVICERESOURCETYPE, this);
53
		serviceImpl.subscribe(ActionType.UPDATE, ResourceType.REPOSITORYSERVICERESOURCETYPE, this);
54
		serviceImpl.subscribe(ActionType.DELETE, ResourceType.REPOSITORYSERVICERESOURCETYPE, this);
55
	*/
56
		this.executor = Executors.newSingleThreadScheduledExecutor();
57
		this.executor.scheduleWithFixedDelay(new Updater(), 1, 30, TimeUnit.MINUTES);
58
	}
59
	
60
	public void destroy() {
61
		this.executor.shutdown();
62
	}
63

    
64
	private void updateStatus() {
65
		if (enableUpdate) {
66
			logger.debug("updating driver status...");
67
			try {
68
				int count = searchManager.search("*", 1, 1)
69
						.getNumberOfDocuments();
70

    
71
				List<Repository> repositories = repositoryLookUp.fetch(new RepositorySearchCriteria());
72
				Set<String> countries = new HashSet<String>();
73
				
74
				for (Repository repository : repositories) {
75
					countries.add(repository.getCountryName());
76
				}
77
				
78
				documentCount = count - count % documentCountGranularity;
79
				repositoryCount = repositories.size();
80
				countryCount = countries.size(); 
81
	
82
			} catch (SearchServiceException sse) {
83
				logger.error("Error updating document count", sse);
84
				
85
			} catch (ISLookUpException isle) {
86
				logger.error("Error updating repository and country count", isle);
87
			} catch (Exception e) {
88
				logger.error("Error updating driver status. Using previous or default data");
89
			}
90
		}
91
	}
92

    
93
	@Override
94
	public void processNotification(Notification notification) {
95
		if (notification.getResourceType().equals(ResourceType.INDEXDSRESOURCETYPE)
96
				|| notification.getResourceType().equals(ResourceType.REPOSITORYSERVICERESOURCETYPE)){
97
			
98
			logger.debug("Updating status");
99
			updateStatus();
100
		}
101
	}
102

    
103
	public SearchManager getSearchManager() {
104
		return searchManager;
105
	}
106

    
107
	public void setSearchManager(SearchManager searchManager) {
108
		this.searchManager = searchManager;
109
	}
110

    
111
	public ISLookUp<Repository> getRepositoryLookUp() {
112
		return repositoryLookUp;
113
	}
114

    
115
	public void setRepositoryLookUp(ISLookUp<Repository> repositoryLookUp) {
116
		this.repositoryLookUp = repositoryLookUp;
117
	}
118

    
119
	public long getDocumentCount() {
120
		return documentCount;
121
	}
122

    
123
	public int getRepositoryCount() {
124
		return repositoryCount;
125
	}
126

    
127
	public int getCountryCount() {
128
		return countryCount;
129
	}
130

    
131
	public DriverServiceImpl getServiceImpl() {
132
		return serviceImpl;
133
	}
134

    
135
	public void setServiceImpl(DriverServiceImpl serviceImpl) {
136
		this.serviceImpl = serviceImpl;
137
	}
138

    
139
	public int getDocumentCountGranularity() {
140
		return documentCountGranularity;
141
	}
142

    
143
	public void setDocumentCountGranularity(int documentCountGranularity) {
144
		this.documentCountGranularity = documentCountGranularity;
145
	}
146
	
147
	public void setEnableUpdate(boolean enableUpdate) {
148
		this.enableUpdate = enableUpdate;
149
	}
150

    
151
	class Updater implements Runnable {
152
		public void run() {
153
			try {
154
				updateStatus();
155
			} catch (Exception e) {
156
				logger.error("Error running update", e);
157
			}
158
		}
159
	}
160
}
(3-3/7)