1
|
package eu.dnetlib.download.plugin;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileFilter;
|
5
|
import java.util.*;
|
6
|
|
7
|
import org.apache.commons.lang3.StringUtils;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
|
11
|
/**
|
12
|
* The Class PathRetreiver.
|
13
|
*/
|
14
|
public class PathRetreiver {
|
15
|
|
16
|
private static final Log log = LogFactory.getLog(PathRetreiver.class);
|
17
|
|
18
|
/** The instance. */
|
19
|
private static PathRetreiver instance;
|
20
|
|
21
|
/**
|
22
|
* Gets the single instance of PathRetreiver.
|
23
|
*
|
24
|
* @param base_path
|
25
|
* the base_path
|
26
|
* @return single instance of PathRetreiver
|
27
|
*/
|
28
|
public static PathRetreiver getInstance(final String base_path) {
|
29
|
if (instance == null) {
|
30
|
instance = new PathRetreiver();
|
31
|
instance.setBase_path(base_path);
|
32
|
}
|
33
|
return instance;
|
34
|
}
|
35
|
|
36
|
/** The base_path. */
|
37
|
private String base_path;
|
38
|
|
39
|
/** The values. */
|
40
|
private TreeMap<Integer, InfoPath> values;
|
41
|
|
42
|
/**
|
43
|
* Bootstrap.
|
44
|
*/
|
45
|
private void bootstrap() {
|
46
|
values = new TreeMap<>();
|
47
|
File basePath = new File(this.base_path);
|
48
|
File[] selectedFiles = basePath.listFiles(pathname -> pathname.isDirectory());
|
49
|
|
50
|
for (File f : selectedFiles) {
|
51
|
String lower = StringUtils.substringAfter(StringUtils.substringBefore(f.getName(), "_"), "PMC");
|
52
|
String upper = StringUtils.substringAfter(StringUtils.substringAfter(f.getName(), "_"), "PMC");
|
53
|
String path = f.getPath();
|
54
|
InfoPath i = new InfoPath();
|
55
|
i.setLower(Integer.parseInt(lower));
|
56
|
i.setUpper(Integer.parseInt(upper));
|
57
|
i.setPath(path);
|
58
|
values.put(i.getLower(), i);
|
59
|
}
|
60
|
|
61
|
|
62
|
if (log.isDebugEnabled()) {
|
63
|
for (InfoPath p : values.values()) {
|
64
|
log.debug(String.format("%s -- %s : %s", p.getLower(), p.getUpper(), p.getPath()));
|
65
|
}
|
66
|
}
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Gets the path for pmcid.
|
71
|
*
|
72
|
* @param pmcID
|
73
|
* the pmc id
|
74
|
* @return the path for pmcid
|
75
|
*/
|
76
|
public String getPathForPMCID(final int pmcID) {
|
77
|
if (values == null) {
|
78
|
bootstrap();
|
79
|
}
|
80
|
|
81
|
Map.Entry<Integer, InfoPath> infoPath = values.floorEntry(pmcID);
|
82
|
if (infoPath != null) {
|
83
|
|
84
|
final String currentPath = infoPath.getValue().getPath() + "/" + pmcID + ".xml";
|
85
|
final File f = new File(currentPath);
|
86
|
log.debug(String.format("try to search in path %s", currentPath));
|
87
|
String s = null;
|
88
|
if (f.exists()) {
|
89
|
s = f.getPath();
|
90
|
log.debug(String.format("found in %s", s));
|
91
|
} else {
|
92
|
log.debug(String.format("not found in %s", s));
|
93
|
}
|
94
|
return s;
|
95
|
}
|
96
|
log.debug(String.format("PMC with ID: %s not found", pmcID));
|
97
|
return null;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Sets the base_path.
|
102
|
*
|
103
|
* @param base_path
|
104
|
* the new base_path
|
105
|
*/
|
106
|
public void setBase_path(final String base_path) {
|
107
|
this.base_path = base_path;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Gets the base_path.
|
112
|
*
|
113
|
* @return the base_path
|
114
|
*/
|
115
|
public String getBase_path() {
|
116
|
return this.base_path;
|
117
|
}
|
118
|
}
|