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