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 java.net.MalformedURLException;
|
14
|
import java.net.URI;
|
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.common.CdmUtils;
|
25
|
import eu.etaxonomy.cdm.io.common.CdmIoBase;
|
26
|
import eu.etaxonomy.cdm.io.common.DefaultImportState;
|
27
|
import eu.etaxonomy.cdm.model.description.Feature;
|
28
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
29
|
import eu.etaxonomy.cdm.model.description.TextData;
|
30
|
import eu.etaxonomy.cdm.model.media.Media;
|
31
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
32
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
34
|
|
35
|
|
36
|
/**
|
37
|
* @author a.mueller
|
38
|
* @created 29.07.2008
|
39
|
* @version 1.0
|
40
|
*/
|
41
|
@Component
|
42
|
public class ProtologueImport extends CdmIoBase<DefaultImportState<PalmaeProtologueImportConfigurator>> {
|
43
|
private static final Logger logger = Logger.getLogger(ProtologueImport.class);
|
44
|
|
45
|
private String pluralString = "protologues";
|
46
|
private static int modCount = 200;
|
47
|
|
48
|
public ProtologueImport(){
|
49
|
super();
|
50
|
}
|
51
|
|
52
|
public void doInvoke(DefaultImportState<PalmaeProtologueImportConfigurator> state){
|
53
|
logger.info("start make Protologues from files ...");
|
54
|
|
55
|
Set<TaxonNameBase> nameStore = new HashSet<TaxonNameBase>();
|
56
|
|
57
|
PalmaeProtologueImportConfigurator config = state.getConfig();
|
58
|
File source = config.getSource();
|
59
|
TaxonNameBase name;
|
60
|
TransactionStatus txStatus = startTransaction(false);
|
61
|
int count = 0;
|
62
|
if (source.isDirectory()){
|
63
|
for (File file : source.listFiles() ){
|
64
|
if (file.isFile()){
|
65
|
doCount(count++, modCount, pluralString);
|
66
|
name = importFile(file, state);
|
67
|
storeName(nameStore, name, state);
|
68
|
}
|
69
|
}
|
70
|
}else{
|
71
|
if (source.isFile()){
|
72
|
name = importFile(source, state);
|
73
|
storeName(nameStore, name, state);
|
74
|
}
|
75
|
}
|
76
|
getNameService().save(nameStore);
|
77
|
commitTransaction(txStatus);
|
78
|
logger.info("end make Protologues from files ...");
|
79
|
return;
|
80
|
}
|
81
|
|
82
|
private void storeName(Set<TaxonNameBase> nameStore, TaxonNameBase name, DefaultImportState<PalmaeProtologueImportConfigurator> state){
|
83
|
if (name != null){
|
84
|
nameStore.add(name);
|
85
|
return;
|
86
|
}else{
|
87
|
state.setUnsuccessfull();
|
88
|
return;
|
89
|
}
|
90
|
}
|
91
|
|
92
|
private TaxonNameBase importFile(File file, DefaultImportState<PalmaeProtologueImportConfigurator> state){
|
93
|
String originalSourceId = file.getName();
|
94
|
originalSourceId =originalSourceId.replace("_P.pdf", "");
|
95
|
originalSourceId =originalSourceId.replace("_tc_", "_tn_");
|
96
|
String namespace = state.getConfig().getOriginalSourceTaxonNamespace();
|
97
|
|
98
|
|
99
|
//for testing only
|
100
|
TaxonNameBase taxonName = getTaxonName(originalSourceId, namespace);
|
101
|
if (taxonName == null){
|
102
|
logger.warn("Name not found for " + originalSourceId);
|
103
|
return null;
|
104
|
}
|
105
|
|
106
|
// TaxonNameDescription nameDescription = null;
|
107
|
// if (taxonName.getDescriptions().size() > 0){
|
108
|
// nameDescription = (TaxonNameDescription)taxonName.getDescriptions().iterator().next();
|
109
|
// }else{
|
110
|
// nameDescription = new TaxonNameDescription();
|
111
|
// }
|
112
|
try{
|
113
|
Media media = getMedia(state, file);
|
114
|
if (media.getRepresentations().size() > 0){
|
115
|
TaxonNameDescription description = getNameDescription(taxonName);
|
116
|
TextData protolog = TextData.NewInstance(Feature.PROTOLOGUE());
|
117
|
protolog.addMedia(media);
|
118
|
description.addElement(protolog);
|
119
|
return taxonName;
|
120
|
}
|
121
|
|
122
|
}catch(NullPointerException e){
|
123
|
logger.warn("MediaUrl and/or MediaPath not set. Could not get protologue.");
|
124
|
return null;
|
125
|
}
|
126
|
return null;
|
127
|
|
128
|
}
|
129
|
|
130
|
private TaxonNameDescription getNameDescription(TaxonNameBase taxonName) {
|
131
|
TaxonNameDescription result;
|
132
|
if (taxonName.getDescriptions().size()> 0){
|
133
|
result = (TaxonNameDescription)taxonName.getDescriptions().iterator().next();
|
134
|
}else{
|
135
|
result = TaxonNameDescription.NewInstance();
|
136
|
taxonName.addDescription(result);
|
137
|
}
|
138
|
|
139
|
return result;
|
140
|
}
|
141
|
|
142
|
private Media getMedia(DefaultImportState<PalmaeProtologueImportConfigurator> state, File file){
|
143
|
try {
|
144
|
//File file = (File)state.getConfig().getSource();
|
145
|
String url = file.toURI().toURL().toString();
|
146
|
String mimeTypePdf = "application/pdf";
|
147
|
String suffixPdf = "pdf";
|
148
|
String urlStringPdf = state.getConfig().getUrlString() + file.getName();
|
149
|
URI uri = CdmUtils.string2Uri(urlStringPdf);
|
150
|
Integer size = null;
|
151
|
|
152
|
if (file.exists()){
|
153
|
Media media = Media.NewInstance();
|
154
|
|
155
|
MediaRepresentation representationPdf = MediaRepresentation.NewInstance(mimeTypePdf, suffixPdf);
|
156
|
representationPdf.addRepresentationPart(MediaRepresentationPart.NewInstance(uri, size));
|
157
|
media.addRepresentation(representationPdf);
|
158
|
return media;
|
159
|
}else{
|
160
|
return null;
|
161
|
}
|
162
|
} catch (MalformedURLException e) {
|
163
|
logger.error(e.getMessage());
|
164
|
return null;
|
165
|
}
|
166
|
|
167
|
}
|
168
|
|
169
|
private TaxonNameBase getTaxonName(String originalSourceId, String namespace){
|
170
|
TaxonNameBase result;
|
171
|
ICommonService commonService = getCommonService();
|
172
|
|
173
|
result = (TaxonNameBase)commonService.getSourcedObjectByIdInSource(TaxonNameBase.class, originalSourceId , namespace);
|
174
|
if (result == null){
|
175
|
logger.warn("Taxon (id: " + originalSourceId + ", namespace: " + namespace + ") could not be found");
|
176
|
}
|
177
|
return result;
|
178
|
}
|
179
|
|
180
|
|
181
|
public boolean doCheck(DefaultImportState state){
|
182
|
boolean result = true;
|
183
|
return result;
|
184
|
}
|
185
|
|
186
|
/* (non-Javadoc)
|
187
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
188
|
*/
|
189
|
protected boolean isIgnore(DefaultImportState state){
|
190
|
return false; // ! state.getConfig();
|
191
|
}
|
192
|
|
193
|
protected void doCount(int count, int modCount, String pluralString){
|
194
|
if ((count % modCount ) == 0 && count!= 0 ){ logger.info(pluralString + " handled: " + (count));}
|
195
|
}
|
196
|
|
197
|
|
198
|
}
|