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.app.viennaImport;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
import java.util.List;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
21
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
22
|
import eu.etaxonomy.cdm.app.common.CdmDestinations;
|
23
|
import eu.etaxonomy.cdm.common.AccountStore;
|
24
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
25
|
import eu.etaxonomy.cdm.io.common.Source;
|
26
|
import eu.etaxonomy.cdm.model.agent.Person;
|
27
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
28
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
29
|
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
30
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
31
|
import eu.etaxonomy.cdm.model.media.Media;
|
32
|
import eu.etaxonomy.cdm.model.name.BotanicalName;
|
33
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
34
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
35
|
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
|
36
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
37
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
38
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
40
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
41
|
|
42
|
/**
|
43
|
* @author AM
|
44
|
* @created 08.05.2008
|
45
|
* @version 1.0
|
46
|
*/
|
47
|
public class ViennaActivator {
|
48
|
private static final Logger logger = Logger.getLogger(ViennaActivator.class);
|
49
|
|
50
|
static final Source berlinModelSource = ViennaActivator.VIENNA();
|
51
|
|
52
|
|
53
|
public static Source VIENNA(){
|
54
|
// Vienna Asteraceae
|
55
|
String dbms = "ODBC";
|
56
|
String strServer = "AsteraceaeViennaAccess";
|
57
|
String strDB = "AsteraceaeViennaAccess";
|
58
|
String userName = "webUser";
|
59
|
return makeSource(dbms, strServer, strDB, -1, userName, null);
|
60
|
}
|
61
|
|
62
|
public boolean invoke(){
|
63
|
boolean result = true;
|
64
|
boolean withCdm = false;
|
65
|
berlinModelSource.setQuery("SELECT * FROM vienna"); // WHERE ID1 <> 1
|
66
|
CdmApplicationController app = null;
|
67
|
|
68
|
|
69
|
try {
|
70
|
if (withCdm){
|
71
|
app = CdmApplicationController.NewInstance(CdmDestinations.localH2());
|
72
|
}else{
|
73
|
//app = CdmApplicationController.NewInstance(DbSchemaValidation.VALIDATE);
|
74
|
}
|
75
|
} catch (Exception e1) {
|
76
|
e1.printStackTrace();
|
77
|
result = false;
|
78
|
return result;
|
79
|
}
|
80
|
|
81
|
|
82
|
ResultSet rs = berlinModelSource.getResultSet();
|
83
|
try {
|
84
|
while (rs.next()){
|
85
|
String uriPath = "http://131.130.131.9/database/img/imgBrowser.php?ID=";
|
86
|
int id = rs.getInt("ID");
|
87
|
String strId = String.valueOf(id);
|
88
|
String catalogNumber = rs.getString("catalogueNumber");
|
89
|
String strTaxonName = rs.getString("TaxonName");
|
90
|
String annotation = rs.getString("Annotation");
|
91
|
String typeInformation = rs.getString("TypeInformation");
|
92
|
String typifiedBy = rs.getString("TypifiedBy");
|
93
|
String family = rs.getString("Family");
|
94
|
String strActor = rs.getString("Actor");
|
95
|
String timePeriod = rs.getString("TimePeriod");
|
96
|
String collectingArea = rs.getString("CollectingArea");
|
97
|
String locality = rs.getString("Locality");
|
98
|
String assigned = rs.getString("assigned");
|
99
|
String history = rs.getString("history");
|
100
|
|
101
|
if (! family.equals("Asteraceae")){
|
102
|
logger.warn("Family not Asteracea: ID= " + strId);
|
103
|
}
|
104
|
Reference<?> sec = ReferenceFactory.newDatabase();
|
105
|
sec.setTitleCache("Vienna Asteraceae Images", true);
|
106
|
|
107
|
TaxonNameBase<?,?> taxonName = (BotanicalName)NonViralNameParserImpl.NewInstance().parseFullName(strTaxonName);
|
108
|
if (withCdm){
|
109
|
List<TaxonNameBase> names = app.getNameService().getNamesByName(strTaxonName);
|
110
|
if (names.size() == 0){
|
111
|
logger.warn("Name not found: " + strTaxonName);
|
112
|
}else{
|
113
|
if (names.size() > 1){
|
114
|
logger.warn("More then 1 name found: " + strTaxonName);
|
115
|
}
|
116
|
taxonName = names.get(0);
|
117
|
}
|
118
|
}
|
119
|
Taxon taxon = Taxon.NewInstance(taxonName, sec);
|
120
|
|
121
|
logger.info("Create new specimen ...");
|
122
|
DerivedUnit specimenx = DerivedUnit.NewPreservedSpecimenInstance();
|
123
|
DerivedUnitFacade specimen = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
|
124
|
|
125
|
specimen.setCatalogNumber(catalogNumber);
|
126
|
specimen.setStoredUnder(taxonName); //??
|
127
|
//TODO
|
128
|
//specimen.setCollection(collection);
|
129
|
specimen.addAnnotation(Annotation.NewDefaultLanguageInstance(annotation));
|
130
|
specimen.addDetermination(getDetermination(taxon, strActor));
|
131
|
specimen.addDerivedUnitMedia(getMedia(uriPath, strId));
|
132
|
|
133
|
//Original ID
|
134
|
specimen.addSource(IdentifiableSource.NewDataImportInstance(strId));
|
135
|
|
136
|
|
137
|
}
|
138
|
} catch (SQLException e) {
|
139
|
logger.warn("Error when reading record!!");
|
140
|
e.printStackTrace();
|
141
|
result = false;
|
142
|
}
|
143
|
return result;
|
144
|
}
|
145
|
|
146
|
private Media getMedia(String uriPath, String id){
|
147
|
//"http://131.130.131.9/database/img/imgBrowser.php?ID=50599";
|
148
|
String uriString = uriPath + id;
|
149
|
if (CdmUtils.urlExists(uriString, false)){
|
150
|
String suffix = "jpg";
|
151
|
String mimeType = "image/jpg";
|
152
|
URI uri = null;
|
153
|
try {
|
154
|
uri = new URI(uriString);
|
155
|
} catch (URISyntaxException e) {
|
156
|
e.printStackTrace();
|
157
|
}
|
158
|
Media media = ImageFile.NewMediaInstance(null, null, uri, mimeType, suffix, null, null, null);
|
159
|
return media;
|
160
|
}else{
|
161
|
logger.warn("URI does not exist: " + uriString);
|
162
|
return null;
|
163
|
}
|
164
|
}
|
165
|
|
166
|
private DeterminationEvent getDetermination(Taxon taxon, String actor){
|
167
|
logger.info("Create determination event");
|
168
|
DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
|
169
|
determinationEvent.setTaxon(taxon);
|
170
|
Person person = Person.NewTitledInstance(actor);
|
171
|
determinationEvent.setActor(person);
|
172
|
return determinationEvent;
|
173
|
}
|
174
|
|
175
|
|
176
|
|
177
|
/**
|
178
|
* @param args
|
179
|
*/
|
180
|
public static void main(String[] args) {
|
181
|
ViennaActivator viennaAct = new ViennaActivator();
|
182
|
viennaAct.invoke();
|
183
|
}
|
184
|
|
185
|
|
186
|
/**
|
187
|
* Initialises source
|
188
|
* @return true, if connection established
|
189
|
*/
|
190
|
private static Source makeSource(String dbms, String strServer, String strDB, int port, String userName, String pwd ){
|
191
|
//establish connection
|
192
|
pwd = AccountStore.readOrStorePassword(dbms, strServer, userName, pwd);
|
193
|
Source source = new Source(dbms, strServer, strDB);
|
194
|
source.setPort(port);
|
195
|
source.setUserAndPwd(userName, pwd);
|
196
|
return source;
|
197
|
}
|
198
|
}
|