1 | package org.biohackathon.SPARQLBuilder.endpointMetadata;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Set;
|
---|
6 |
|
---|
7 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.CrawledMetadata;
|
---|
8 | import jp.riken.accc.db.sparqlBuilderMetadata.crawler.dataStructure.sparql.crawler.CrawledMetadataFileReader;
|
---|
9 |
|
---|
10 | public class MetadataManager {
|
---|
11 |
|
---|
12 | HashMap<String,CrawledMetadata> metadataTable = null;
|
---|
13 |
|
---|
14 | /*
|
---|
15 | public static void main(String[] args) throws Exception{
|
---|
16 | String dirStr = "c:\\temp\\crawl";
|
---|
17 | MetadataManager manager = new MetadataManager(dirStr);
|
---|
18 | String[] uris = manager.getURIList();
|
---|
19 | for(String uri: uris){
|
---|
20 | System.out.println(uri);
|
---|
21 | MetadataFile mFile = manager.getMetadataFile(uri);
|
---|
22 | System.out.println(mFile.getStartDateTime().getTime().toString());
|
---|
23 | System.out.println(mFile.getEndDateTime().getTime().toString());
|
---|
24 | }
|
---|
25 | }*/
|
---|
26 |
|
---|
27 | public CrawledMetadata[] getCrawlerMetadataList(){
|
---|
28 | return metadataTable.values().toArray(new CrawledMetadata[0]);
|
---|
29 | }
|
---|
30 | public CrawledMetadata getCrawledMetadata(String uri){
|
---|
31 | return metadataTable.get(uri);
|
---|
32 | }
|
---|
33 |
|
---|
34 | public String[] getURIList(){
|
---|
35 | Set<String> keySet = metadataTable.keySet();
|
---|
36 | return keySet.toArray(new String[0]);
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | public MetadataManager(String metadataDirStr) throws Exception {
|
---|
41 | init(metadataDirStr);
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | public void init(String metadataDirStr) throws Exception{
|
---|
46 | metadataTable = new HashMap<String,CrawledMetadata>();
|
---|
47 | File metadataDir = new File(metadataDirStr);
|
---|
48 | File[] files = null;
|
---|
49 | if( metadataDir.exists() ){
|
---|
50 | if( metadataDir.isFile()){
|
---|
51 | files = new File[]{metadataDir};
|
---|
52 | }else{
|
---|
53 | if( metadataDir.isDirectory()){
|
---|
54 | files = metadataDir.listFiles();
|
---|
55 | }else{
|
---|
56 | throw new Exception("Inernal File/Directory error: " + metadataDirStr);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }else{
|
---|
60 | throw new Exception("File/Directory not found: " + metadataDirStr);
|
---|
61 | }
|
---|
62 | for(File file: files){
|
---|
63 | CrawledMetadata crawledMetadata = null;
|
---|
64 | try{
|
---|
65 | crawledMetadata = CrawledMetadataFileReader.readFile(file.getCanonicalPath());
|
---|
66 | String uri = crawledMetadata.getEndpointURI();
|
---|
67 | if( uri != null ){
|
---|
68 | if( metadataTable.containsKey(uri)){
|
---|
69 | CrawledMetadata tempMF = metadataTable.get(uri);
|
---|
70 | if(tempMF.getDefaultDataset().getCrawlLog().crawlEndTime.before(crawledMetadata.getDefaultDataset().getCrawlLog().crawlEndTime) ){
|
---|
71 | metadataTable.remove(uri);
|
---|
72 | metadataTable.put(uri, crawledMetadata);
|
---|
73 | }
|
---|
74 | }else{
|
---|
75 | metadataTable.put(uri, crawledMetadata);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }catch(Exception ex){
|
---|
79 | // WARNING
|
---|
80 | System.out.println("Invalid metadata file: " + file.getAbsolutePath());
|
---|
81 | ex.printStackTrace();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|