Project

General

Profile

1
package eu.dnetlib.download.plugin;
2

    
3
import java.io.File;
4
import java.io.FileFilter;
5
import java.util.ArrayList;
6
import java.util.Collections;
7
import java.util.Comparator;
8
import java.util.List;
9

    
10
import org.apache.commons.lang3.StringUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13

    
14
// TODO: Auto-generated Javadoc
15
/**
16
 * The Class PathRetreiver.
17
 */
18
public class PathRetreiver {
19

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

    
22
	/** The instance. */
23
	private static PathRetreiver instance;
24

    
25
	/**
26
	 * Gets the single instance of PathRetreiver.
27
	 *
28
	 * @param base_path
29
	 *            the base_path
30
	 * @return single instance of PathRetreiver
31
	 */
32
	public static PathRetreiver getInstance(final String base_path) {
33
		if (instance == null) {
34
			instance = new PathRetreiver();
35
			instance.setBase_path(base_path);
36
		}
37
		return instance;
38
	}
39

    
40
	/** The base_path. */
41
	private String base_path;
42

    
43
	/** The values. */
44
	private List<InfoPath> values;
45

    
46
	/**
47
	 * Bootstrap.
48
	 */
49
	private void bootstrap() {
50
		values = new ArrayList<InfoPath>();
51
		File basePath = new File(this.base_path);
52
		File[] selectedFiles = basePath.listFiles(new FileFilter() {
53

    
54
			@Override
55
			public boolean accept(final File pathname) {
56
				return pathname.isDirectory();
57
			}
58
		});
59

    
60
		for (File f : selectedFiles) {
61
			String lower = StringUtils.substringAfter(StringUtils.substringBefore(f.getName(), "_"), "PMC");
62
			String upper = StringUtils.substringAfter(StringUtils.substringAfter(f.getName(), "_"), "PMC");
63
			String path = f.getPath();
64
			InfoPath i = new InfoPath();
65
			i.setLower(Integer.parseInt(lower));
66
			i.setUpper(Integer.parseInt(upper));
67
			i.setPath(path);
68
			values.add(i);
69
		}
70

    
71
		Collections.sort(values, new Comparator<InfoPath>() {
72

    
73
			@Override
74
			public int compare(final InfoPath o1, final InfoPath o2) {
75
				if (o1.getLower() < o2.getLower()) return -1;
76
				else if (o1.getLower() < o2.getLower()) return 0;
77
				else return 1;
78
			}
79
		});
80
		if (log.isDebugEnabled()) {
81
			for (InfoPath p : values) {
82
				log.debug(String.format("%s -- %s : %s", p.getLower(), p.getUpper(), p.getPath()));
83
			}
84
		}
85

    
86
	}
87

    
88
	/**
89
	 * Gets the path for pmcid.
90
	 *
91
	 * @param pmcID
92
	 *            the pmc id
93
	 * @return the path for pmcid
94
	 */
95
	public String getPathForPMCID(final int pmcID) {
96
		if (values == null) {
97
			bootstrap();
98
		}
99
		for (int i = 0; i < values.size(); i++) {
100
			if (pmcID < values.get(i).getLower()) {
101
				if (i == 0) return null;
102
				String currentPath = values.get(i - 1).getPath() + "/" + pmcID + ".xml";
103
				File f = new File(currentPath);
104
				log.debug(String.format("try to search in path %s", currentPath));
105
				String s = null;
106
				if (f.exists()) {
107
					s = f.getPath();
108
					log.debug(String.format("found in %s", s));
109
				} else {
110
					log.debug(String.format("not found in %s", s));
111
				}
112
				return s;
113

    
114
			}
115
		}
116
		log.debug(String.format("PMC with ID: %s not found", pmcID));
117
		return null;
118
	}
119

    
120
	/**
121
	 * Sets the base_path.
122
	 *
123
	 * @param base_path
124
	 *            the new base_path
125
	 */
126
	public void setBase_path(final String base_path) {
127
		this.base_path = base_path;
128
	}
129

    
130
	/**
131
	 * Gets the base_path.
132
	 *
133
	 * @return the base_path
134
	 */
135
	public String getBase_path() {
136
		return this.base_path;
137
	}
138
}
(12-12/12)