Project

General

Profile

1
package eu.dnetlib.rmi.data;
2

    
3
import java.time.LocalDate;
4
import java.time.ZoneId;
5
import java.time.temporal.ChronoUnit;
6
import java.util.List;
7

    
8
import com.google.gson.Gson;
9
import eu.dnetlib.rmi.data.DownloadItem.OpenAccessValues;
10

    
11
// TODO: Auto-generated Javadoc
12

    
13
/**
14
 * The Class AbstractDownloadPlugin.
15
 */
16
public abstract class AbstractDownloadPlugin {
17

    
18
	public static final int DEFAULT_TIMEOUT = 5000;
19

    
20
	/**
21
	 * The regular expression.
22
	 */
23
	protected List<String> regularExpression;
24

    
25
	/**
26
	 * Check open access.
27
	 *
28
	 * @param input the input
29
	 * @return the download item
30
	 */
31
	public DownloadItem checkOpenAccess(final DownloadItem input) {
32
		if (input != null) {
33
			OpenAccessValues openAccess = OpenAccessValues.valueOf(input.getOpenAccess());
34
			switch (openAccess) {
35
			case OPEN:
36
				return input;
37
			case CLOSED:
38
			case RESTRICTED:
39
			case UNKNOWN:
40
				return null;
41
			case EMBARGO:
42
				if (input.getEmbargoDate() == null) return null;
43
				LocalDate embargoDate = input.getEmbargoDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
44
				LocalDate today = LocalDate.now();
45
				final long between = ChronoUnit.DAYS.between(embargoDate, today);
46
				if (between <= 0) return input;
47
				return null;
48
			}
49
		}
50
		return null;
51
	}
52

    
53
	public DownloadItem filterByRegexp(final DownloadItem input) {
54
		if (this.regularExpression != null && this.regularExpression.size() > 0) {
55
			final String baseURLs = input.getUrl();
56
			final List<String> urlsList = new Gson().fromJson(baseURLs, List.class);
57

    
58
			for (final String baseURL : urlsList) {
59
				for (final String regExp : regularExpression) {
60
					if (baseURL.matches(regExp))
61
						return input;
62
				}
63
			}
64
			return null;
65
		} else return input;
66
	}
67

    
68
	protected boolean checkUrlsNotNull(DownloadItem input, List<String> urls) {
69
		for (String s : urls) {
70
			String newURL = extractURL(s);
71
			if (newURL != null) {
72
				input.setOriginalUrl(s);
73
				input.setUrl(newURL);
74
				return true;
75
			} else {
76
				input.setOriginalUrl(null);
77
				input.setUrl(null);
78
			}
79
		}
80
		return false;
81
	}
82

    
83
	public abstract String extractURL(final String baseURL) throws DownloadPluginException;
84

    
85
	/**
86
	 * Gets the regular expression.
87
	 *
88
	 * @return the regular expression
89
	 */
90
	public List<String> getRegularExpression() {
91
		return regularExpression;
92
	}
93

    
94
	/**
95
	 * Sets the regular expression.
96
	 *
97
	 * @param regularExpression the new regular expression
98
	 */
99
	public void setRegularExpression(final List<String> regularExpression) {
100
		this.regularExpression = regularExpression;
101
	}
102

    
103
}
(1-1/35)