Project

General

Profile

Download (15.1 KB) Statistics
| Branch: | Revision:
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.synthesysImport;
11

    
12
import java.io.FileNotFoundException;
13
import java.net.URI;
14
import java.net.URISyntaxException;
15
import java.util.ArrayList;
16
import java.util.HashMap;
17
import java.util.List;
18
import java.util.ListIterator;
19

    
20
import org.apache.log4j.Logger;
21
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
22
import org.springframework.transaction.TransactionStatus;
23

    
24
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
25
import eu.etaxonomy.cdm.api.service.pager.Pager;
26
import eu.etaxonomy.cdm.app.common.CdmDestinations;
27
import eu.etaxonomy.cdm.app.common.CdmImportSources;
28
import eu.etaxonomy.cdm.common.ExcelUtils;
29
import eu.etaxonomy.cdm.database.DbSchemaValidation;
30
import eu.etaxonomy.cdm.database.ICdmDataSource;
31
import eu.etaxonomy.cdm.model.agent.AgentBase;
32
import eu.etaxonomy.cdm.model.agent.Institution;
33
import eu.etaxonomy.cdm.model.agent.Person;
34
import eu.etaxonomy.cdm.model.common.Language;
35
import eu.etaxonomy.cdm.model.common.LanguageString;
36
import eu.etaxonomy.cdm.model.location.NamedArea;
37
import eu.etaxonomy.cdm.model.location.Point;
38
import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
39
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
40
import eu.etaxonomy.cdm.model.occurrence.Collection;
41
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
42
import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
43
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
44
import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
45
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
46
import eu.etaxonomy.cdm.model.occurrence.LivingBeing;
47
import eu.etaxonomy.cdm.model.occurrence.Observation;
48
import eu.etaxonomy.cdm.model.occurrence.Specimen;
49
import eu.etaxonomy.cdm.model.reference.Reference;
50
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
51
import eu.etaxonomy.cdm.model.taxon.Taxon;
52
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
53

    
54
/**
55
 * @author PK
56
 * @created 19.09.2008
57
 * @version 1.0
58
 */
59
public class SynthesysCacheActivator {
60
	private static final Logger logger = Logger.getLogger(SynthesysCacheActivator.class);
61

    
62
	protected String fullScientificNameString = null;
63
	protected String institutionCode = null;
64
	protected String collectionCode = null;
65
	protected String unitID = null;
66
	protected String recordBasis = null;
67
	protected String accessionNumber = null;
68
	protected String collectorsNumber = null;
69
	protected String fieldNumber = null;
70
	protected Double longitude = null;
71
	protected Double latitude = null;
72
	protected String locality = null;
73
	protected String country = null;
74
	protected String isocountry = null;
75
	protected ArrayList<String> gatheringAgentList = new ArrayList<String>();
76
	protected ArrayList<String> identificationList = new ArrayList<String>();
77

    
78
	static DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
79
	
80
	static ICdmDataSource desitination = CdmDestinations.localH2();
81
	
82
	protected HSSFWorkbook hssfworkbook = null;
83

    
84

    
85
	public void saveUnit(HashMap<String,String> unit){
86
		String author = unit.get("author");
87
		author=author.replaceAll("None","");
88
		String taxonName = unit.get("taxonName");
89
		taxonName = taxonName.replaceAll("None", "");
90

    
91
		try {
92
			this.institutionCode = unit.get("institution").replaceAll("None", null);
93
		} catch (Exception e) {
94
		}
95

    
96
		try {this.collectionCode = unit.get("collection").replaceAll("None", null);
97
		} catch (Exception e) {
98
		}
99
		try {this.unitID = unit.get("unitID").replaceAll("None", null);
100
		} catch (Exception e) {
101
		}
102
		try {this.recordBasis = unit.get("recordBasis").replaceAll("None", null);
103
		} catch (Exception e) {
104
		}
105
		try {this.accessionNumber = null;
106
		} catch (Exception e) {
107
		}
108
		try {this.locality = unit.get("locality").replaceAll("None", null);
109
		} catch (Exception e) {
110
		}
111
		try {this.longitude = Double.valueOf(unit.get("longitude"));
112
		} catch (Exception e) {
113
		}
114
		try {this.latitude = Double.valueOf(unit.get("latitude"));
115
		} catch (Exception e) {
116
		}
117
		try {this.country = unit.get("country").replaceAll("None", null);
118
		} catch (Exception e) {
119
		}
120
		try {this.isocountry = unit.get("isoCountry").replaceAll("None", null);
121
		} catch (Exception e) {
122
		}
123
		try {this.fieldNumber = unit.get("field number").replaceAll("None", null);
124
		} catch (Exception e) {
125
		}
126
		try {this.collectorsNumber = unit.get("collector number").replaceAll("None", null);
127
		} catch (Exception e) {
128
		}
129
		try {String coll =unit.get("collector");
130
			coll=coll.replaceAll("None", null);
131
			this.gatheringAgentList.add(coll);
132
		} catch (Exception e) {
133
		}
134
		try {this.identificationList.add(taxonName+" "+author);
135
		} catch (Exception e) {System.out.println(e);
136
		}
137
	}
138

    
139
	public boolean invoke(){
140
		boolean result = true;
141
		boolean withCdm = true;
142
		CdmApplicationController app = null;
143
		TransactionStatus tx = null;
144

    
145
		app = CdmApplicationController.NewInstance(desitination, hbm2dll);
146
		
147
		tx = app.startTransaction();
148
		try {
149
			ReferenceFactory refFactory = ReferenceFactory.newInstance();
150
			Reference sec = refFactory.newDatabase();
151
			sec.setTitleCache("SYNTHESYS CACHE DATA", true);
152

    
153
			/**
154
			 * SPECIMEN OR OBSERVATION OR LIVING
155
			 */
156
			DerivedUnitBase derivedThing = null;
157
			//create specimen
158
			if (this.recordBasis != null){
159
				if (this.recordBasis.toLowerCase().startsWith("s")) {//specimen
160
					derivedThing = Specimen.NewInstance();				
161
				}
162
				else if (this.recordBasis.toLowerCase().startsWith("o")) {//observation
163
					derivedThing = Observation.NewInstance();				
164
				}
165
				else if (this.recordBasis.toLowerCase().startsWith("l")) {//living -> fossil, herbarium sheet....???
166
					derivedThing = LivingBeing.NewInstance();
167
				}
168
			}
169
			if (derivedThing == null) derivedThing = Observation.NewInstance();
170

    
171
			TaxonNameBase taxonName = null;
172
			Taxon taxon = null;
173
			DeterminationEvent determinationEvent = null;
174
			List<TaxonNameBase> names = null;
175
			NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
176
			String scientificName="";
177
			boolean preferredFlag=false;
178
			System.out.println(this.identificationList);
179
			for (int i = 0; i < this.identificationList.size(); i++) {
180
				this.fullScientificNameString = this.identificationList.get(i);
181
				this.fullScientificNameString = this.fullScientificNameString.replaceAll(" et ", " & ");
182
				if (this.fullScientificNameString.indexOf("_preferred_") != -1){
183
					scientificName = this.fullScientificNameString.split("_preferred_")[0];
184
					String pTmp = this.fullScientificNameString.split("_preferred_")[1];
185
					if (pTmp == "1" || pTmp.toLowerCase().indexOf("true") != -1)
186
						preferredFlag=true;
187
					else
188
						preferredFlag=false;
189
				}
190
				else scientificName = this.fullScientificNameString;
191

    
192
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICZN(),null);
193
//				if (taxonName.hasProblem()){
194
//				System.out.println("pb ICZN");
195
//				taxonName  = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICBN(),null);
196
//				if (taxonName.hasProblem()){
197
//				System.out.println("pb ICBN");
198
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICNB(), null);
199
//				if (taxonName.hasProblem()){
200
//				System.out.println("pb ICNB");
201
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICNCP(), null);
202
//				if (taxonName.hasProblem()){
203
//				System.out.println("pb ICNCP");
204
//				}
205
//				}
206
//				}				
207
//				}
208
				taxonName = nvnpi.parseFullName(scientificName);
209
				if (withCdm){
210
					names = app.getNameService().getNamesByName(scientificName);
211
					if (names.size() == 0){
212
						System.out.println("Name not found: " + scientificName);
213
					}else{
214
						if (names.size() > 1){
215
							System.out.println("More then 1 name found: " + scientificName);
216
						}
217
						System.out.println("Name found");
218
						taxonName = names.get(0);
219
					}
220
				}
221

    
222
				
223
//				tx = app.startTransaction();
224
				app.getNameService().saveOrUpdate(taxonName);
225
				taxon = Taxon.NewInstance(taxonName, sec); //TODO use real reference for sec
226
//				app.commitTransaction(tx);
227

    
228

    
229
				determinationEvent = DeterminationEvent.NewInstance();
230
				determinationEvent.setTaxon(taxon);
231
				determinationEvent.setPreferredFlag(preferredFlag);
232
				derivedThing.addDetermination(determinationEvent);
233
			}
234

    
235

    
236
			//set catalogue number (unitID)
237
			derivedThing.setCatalogNumber(this.unitID);
238
			derivedThing.setAccessionNumber(this.accessionNumber);
239
			derivedThing.setCollectorsNumber(this.collectorsNumber);
240

    
241

    
242
			/**
243
			 * INSTITUTION & COLLECTION
244
			 */
245
			//manage institution
246
			Institution institution;
247
			List<Institution> institutions;
248
			try{
249
				System.out.println(this.institutionCode);
250
				institutions= app.getAgentService().searchInstitutionByCode(this.institutionCode);
251
			}catch(Exception e){
252
				System.out.println("BLI "+e);
253
				institutions=new ArrayList<Institution>();
254
			}
255
			if (institutions.size() ==0){
256
				System.out.println("Institution (agent) unknown");
257
				//create institution
258
				institution = Institution.NewInstance();
259
				institution.setCode(this.institutionCode);				
260
			}
261
			else{
262
				System.out.println("Institution (agent) already in the db");
263
				institution = institutions.get(0);
264
			}
265

    
266
			//manage collection
267
			Collection collection = Collection.NewInstance();
268
			List<Collection> collections;
269
			try{
270
				collections = app.getCollectionService().searchByCode(this.collectionCode);
271
				//.searchCollectionByCode(this.collectionCode);
272
			}catch(Exception e){
273
				System.out.println("BLA"+e);
274
				collections=new ArrayList<Collection>();
275
			}
276
			if (collections.size() ==0){
277
				System.out.println("Collection not found "+this.collectionCode);
278
				//create new collection
279
				collection.setCode(this.collectionCode);
280
				collection.setCodeStandard("GBIF");
281
				collection.setInstitute(institution);
282
			}
283
			else{
284
				boolean collectionFound=false;
285
				for (int i=0; i<collections.size(); i++){
286
					collection = collections.get(i);
287
					try {
288
						if (collection.getInstitute().getCode().equalsIgnoreCase(institution.getCode())){ 
289
							//found a collection with the same code and the same institution
290
							collectionFound=true;
291
						}
292
					} catch (NullPointerException e) {}
293
				}
294
				System.out.println("a trouvé la collection avec la meme institution? "+collectionFound);
295
				if (!collectionFound){ //need to add a new collection with the pre-configured institution
296
					collection.setCode(this.collectionCode);
297
					collection.setCodeStandard("GBIF");
298
					collection.setInstitute(institution);
299
				}
300

    
301
			}
302
			System.out.println("collection inserted");
303
			//link specimen & collection
304
			derivedThing.setCollection(collection);
305

    
306
			/**
307
			 * GATHERING EVENT
308
			 */
309
			//create gathering event
310
			GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
311
			//add locality
312
			Language language = Language.DEFAULT();
313
			LanguageString loc = LanguageString.NewInstance(this.locality,language);
314
			gatheringEvent.setLocality(loc);
315

    
316
			//create coordinates point
317
			Point coordinates = Point.NewInstance();
318
			//add coordinates
319
			coordinates.setLatitude(this.latitude);
320
			coordinates.setLongitude(this.longitude);
321
			gatheringEvent.setExactLocation(coordinates);
322

    
323
			NamedArea area = NamedArea.NewInstance();
324
			
325

    
326
			WaterbodyOrCountry country = null;
327
//			System.out.println("isocountry "+this.isocountry);
328
			if (this.isocountry != null)
329
				country = app.getOccurrenceService().getCountryByIso(this.isocountry);
330
			
331
//			System.out.println(country.getLabel());
332
//			Set<Continent> cont = country.getContinents();
333
//			
334
//			System.out.println(cont.size());
335
//			Iterator<Continent> iter = cont.iterator();
336
//			while (iter.hasNext())
337
//				System.out.println(iter.next().toString());
338
			
339
			if (country != null){
340
				area.addWaterbodyOrCountry(country);
341
				System.out.println("country not null!");
342
			}
343
//			else{
344
//				if (this.country != null){
345
//					List<WaterbodyOrCountry>countries = app.getOccurrenceService().getWaterbodyOrCountryByName(this.country);
346
//					if (countries.size() >0)
347
//						area.addWaterbodyOrCountry(countries.get(0));
348
//					else
349
//						System.out.println("NO COUNTRY");//TODO need to add a new country!
350
//				}
351
//			}
352
//			app.getTermService().saveTerm(area);
353
			gatheringEvent.addCollectingArea(area);
354

    
355
			//create collector
356
			AgentBase collector;
357
			ListIterator<String> collectors = this.gatheringAgentList.listIterator();
358
			//add the collectors
359
			String collName;
360
			while (collectors.hasNext()){
361
				collName = collectors.next();
362
				/*check if the collector does already exist*/
363
				try{
364
					Pager<AgentBase> col = app.getAgentService().findByTitle(null, collName, null, null, null, null, null, null);
365
					collector=col.getRecords().get(0);
366
					System.out.println("a trouve l'agent");
367
				}catch (Exception e) {
368
					collector = Person.NewInstance();
369
					collector.setTitleCache(collName, true);
370
				}
371
				gatheringEvent.setCollector(collector);
372
			}
373

    
374
			//create field/observation
375
			FieldObservation fieldObservation = FieldObservation.NewInstance();
376
			//add fieldNumber
377
			fieldObservation.setFieldNumber(this.fieldNumber);
378

    
379
			//join gatheringEvent to fieldObservation
380
			fieldObservation.setGatheringEvent(gatheringEvent);
381

    
382

    
383
//			//link fieldObservation and specimen
384
			DerivationEvent derivationEvent = DerivationEvent.NewInstance();
385
			derivationEvent.addOriginal(fieldObservation);
386
			derivedThing.addDerivationEvent(derivationEvent);
387
//			derivationEvent.addDerivative(derivedThing);
388

    
389
			/**
390
			 * SAVE AND STORE DATA
391
			 */			
392
			//save the specimen data
393
			//	app.getOccurrenceService().saveSpecimenOrObservationBase(fieldObservation);
394
			try {
395
//				tx = app.startTransaction();
396
				app.getTermService().saveOrUpdate(area);//save it sooner
397
				app.getOccurrenceService().saveOrUpdate(derivedThing);
398
//				app.commitTransaction(tx);
399
//				app.close();
400
			} catch (Exception e) {
401
				// TODO Auto-generated catch block
402
				System.out.println("PATATE "+e);
403
			}
404

    
405

    
406
			logger.info("saved new specimen ...");
407

    
408

    
409

    
410
		} catch (Exception e) {
411
			logger.warn("Error when reading record!!");
412
			e.printStackTrace();
413
			result = false;
414
		}
415
//		
416
		app.commitTransaction(tx);
417
		System.out.println("commit done");
418
		app.close();
419
		
420
		return result;
421
	}
422

    
423

    
424

    
425
	private DeterminationEvent getDetermination(Taxon taxon, String actor){
426
		logger.info("Create determination event");
427
		DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
428
		determinationEvent.setTaxon(taxon);
429
		Person person = Person.NewTitledInstance(actor);
430
		determinationEvent.setActor(person);
431
		return determinationEvent;
432
	}
433

    
434

    
435

    
436
	/**
437
	 * @param args
438
	 * @throws URISyntaxException 
439
	 */
440
	public static void main(String[] args) throws URISyntaxException {
441
		URI uri = CdmImportSources.SYNTHESYS_SPECIMEN();		
442
		logger.info("main method");
443
		SynthesysCacheActivator abcdAct = new SynthesysCacheActivator();
444
		
445
		ArrayList<HashMap<String, String>> units;
446
		try {
447
			units = ExcelUtils.parseXLS(uri);
448
		} catch (FileNotFoundException e) {
449
			logger.error("FileNotFound: " + uri);
450
			return;
451
		}
452
		HashMap<String,String> unit=null;
453
		for (int i=0; i<units.size();i++){
454
			unit = units.get(i);
455
			logger.info(unit);
456
			abcdAct.saveUnit(unit);//and then invoke
457
			abcdAct.invoke();
458
			
459
		}
460
	}
461

    
462

    
463

    
464
}
(2-2/3)