1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io;
|
11
|
|
12
|
import java.io.File;
|
13
|
import eu.etaxonomy.cdm.common.URI;
|
14
|
import java.net.URISyntaxException;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
import org.springframework.transaction.TransactionStatus;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
23
|
import eu.etaxonomy.cdm.app.wp6.palmae.config.PalmaeProtologueImportConfigurator;
|
24
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
25
|
import eu.etaxonomy.cdm.io.common.DefaultImportState;
|
26
|
import eu.etaxonomy.cdm.model.media.ExternalLinkType;
|
27
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
28
|
|
29
|
/**
|
30
|
* @author a.mueller
|
31
|
* @since 29.07.2008
|
32
|
*/
|
33
|
@Component
|
34
|
public class ProtologueImport
|
35
|
extends CdmImportBase<PalmaeProtologueImportConfigurator, DefaultImportState<PalmaeProtologueImportConfigurator>>{
|
36
|
|
37
|
private static final long serialVersionUID = 4580327331805229644L;
|
38
|
private static final Logger logger = Logger.getLogger(ProtologueImport.class);
|
39
|
|
40
|
private String pluralString = "protologues";
|
41
|
private static int modCount = 200;
|
42
|
|
43
|
public ProtologueImport(){
|
44
|
super();
|
45
|
}
|
46
|
|
47
|
@Override
|
48
|
public void doInvoke(DefaultImportState<PalmaeProtologueImportConfigurator> state){
|
49
|
logger.info("start make Protologues from files ...");
|
50
|
|
51
|
Set<TaxonName> nameStore = new HashSet<>();
|
52
|
|
53
|
PalmaeProtologueImportConfigurator config = state.getConfig();
|
54
|
File source = config.getSource();
|
55
|
TaxonName name;
|
56
|
TransactionStatus txStatus = startTransaction(false);
|
57
|
int count = 0;
|
58
|
if (source.isDirectory()){
|
59
|
for (File file : source.listFiles() ){
|
60
|
if (file.isFile()){
|
61
|
doCount(count++, modCount, pluralString);
|
62
|
name = importFile(file, state);
|
63
|
storeName(nameStore, name, state);
|
64
|
}
|
65
|
}
|
66
|
}else{
|
67
|
if (source.isFile()){
|
68
|
name = importFile(source, state);
|
69
|
storeName(nameStore, name, state);
|
70
|
}
|
71
|
}
|
72
|
getNameService().save(nameStore);
|
73
|
commitTransaction(txStatus);
|
74
|
logger.info("end make Protologues from files ...");
|
75
|
return;
|
76
|
}
|
77
|
|
78
|
private void storeName(Set<TaxonName> nameStore, TaxonName name, DefaultImportState<PalmaeProtologueImportConfigurator> state){
|
79
|
if (name != null){
|
80
|
nameStore.add(name);
|
81
|
return;
|
82
|
}else{
|
83
|
state.setUnsuccessfull();
|
84
|
return;
|
85
|
}
|
86
|
}
|
87
|
|
88
|
private TaxonName importFile(File file, DefaultImportState<PalmaeProtologueImportConfigurator> state){
|
89
|
String originalSourceId = file.getName();
|
90
|
originalSourceId =originalSourceId.replace("_P.pdf", "");
|
91
|
originalSourceId =originalSourceId.replace("_tc_", "_tn_");
|
92
|
String namespace = state.getConfig().getOriginalSourceTaxonNamespace();
|
93
|
|
94
|
|
95
|
//for testing only
|
96
|
TaxonName taxonName = getTaxonName(originalSourceId, namespace);
|
97
|
if (taxonName == null){
|
98
|
logger.warn("Name not found for " + originalSourceId);
|
99
|
return null;
|
100
|
}
|
101
|
|
102
|
try{
|
103
|
String urlStringPdf = state.getConfig().getUrlString() + file.getName();
|
104
|
URI uri = new URI(urlStringPdf);
|
105
|
taxonName.addProtologue(uri, null, ExternalLinkType.File);
|
106
|
}catch(NullPointerException e){
|
107
|
logger.warn("MediaUrl and/or MediaPath not set. Could not get protologue.");
|
108
|
return null;
|
109
|
} catch (URISyntaxException e) {
|
110
|
logger.warn("URISyntaxException when reading URI. Could not get protologue.");
|
111
|
return null;
|
112
|
}
|
113
|
return null;
|
114
|
}
|
115
|
|
116
|
private TaxonName getTaxonName(String originalSourceId, String namespace){
|
117
|
TaxonName result;
|
118
|
ICommonService commonService = getCommonService();
|
119
|
|
120
|
result = commonService.getSourcedObjectByIdInSource(TaxonName.class, originalSourceId , namespace);
|
121
|
if (result == null){
|
122
|
logger.warn("Taxon (id: " + originalSourceId + ", namespace: " + namespace + ") could not be found");
|
123
|
}
|
124
|
return result;
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public boolean doCheck(@SuppressWarnings("rawtypes") DefaultImportState state){
|
129
|
return true;
|
130
|
}
|
131
|
|
132
|
@Override
|
133
|
protected boolean isIgnore(DefaultImportState state){
|
134
|
return false; // ! state.getConfig();
|
135
|
}
|
136
|
|
137
|
protected void doCount(int count, int modCount, String pluralString){
|
138
|
if ((count % modCount ) == 0 && count!= 0 ){ logger.info(pluralString + " handled: " + (count));}
|
139
|
}
|
140
|
}
|