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