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