Project

General

Profile

Download (16.8 KB) Statistics
| Branch: | Tag: | 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.abcdImport;
11

    
12
import java.io.FileInputStream;
13
import java.util.ArrayList;
14
import java.util.Hashtable;
15
import java.util.List;
16
import java.util.ListIterator;
17

    
18
import org.apache.log4j.Logger;
19
import org.apache.poi.hssf.usermodel.HSSFCell;
20
import org.apache.poi.hssf.usermodel.HSSFRow;
21
import org.apache.poi.hssf.usermodel.HSSFSheet;
22
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
23
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
24
import org.springframework.transaction.TransactionStatus;
25

    
26
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
27
import eu.etaxonomy.cdm.api.service.pager.Pager;
28
import eu.etaxonomy.cdm.app.common.CdmDestinations;
29
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
30
import eu.etaxonomy.cdm.database.DbSchemaValidation;
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.common.init.TermNotFoundException;
37
import eu.etaxonomy.cdm.model.location.NamedArea;
38
import eu.etaxonomy.cdm.model.location.Point;
39
import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
40
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
41
import eu.etaxonomy.cdm.model.occurrence.Collection;
42
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
43
import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
44
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
45
import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
46
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
47
import eu.etaxonomy.cdm.model.occurrence.LivingBeing;
48
import eu.etaxonomy.cdm.model.occurrence.Observation;
49
import eu.etaxonomy.cdm.model.occurrence.Specimen;
50
import eu.etaxonomy.cdm.model.reference.IDatabase;
51
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
52
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
53
import eu.etaxonomy.cdm.model.taxon.Taxon;
54
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
55

    
56

    
57

    
58

    
59
/**
60
 * @author PK
61
 * @created 19.09.2008
62
 * @version 1.0
63
 */
64
public class SynthesysCacheActivator {
65
	private static final Logger logger = Logger.getLogger(SynthesysCacheActivator.class);
66

    
67
	protected String fullScientificNameString = null;
68
	protected String institutionCode = null;
69
	protected String collectionCode = null;
70
	protected String unitID = null;
71
	protected String recordBasis = null;
72
	protected String accessionNumber = null;
73
	protected String collectorsNumber = null;
74
	protected String fieldNumber = null;
75
	protected Double longitude = null;
76
	protected Double latitude = null;
77
	protected String locality = null;
78
	protected String country = null;
79
	protected String isocountry = null;
80
	protected ArrayList<String> gatheringAgentList = new ArrayList<String>();
81
	protected ArrayList<String> identificationList = new ArrayList<String>();
82

    
83
	static DbSchemaValidation hbm2dll = DbSchemaValidation.UPDATE;
84

    
85
	protected HSSFWorkbook hssfworkbook = null;
86

    
87

    
88

    
89
	private ArrayList<Hashtable<String, String>> parseXLS() {
90
		String filename = "/home/patricia/Desktop/CDMtabular9c04a474e2_23_09_08.xls";
91
//		String filename = "/home/patricia/Desktop/synthesys.xls";
92
		ArrayList<Hashtable<String, String>> units = new ArrayList<Hashtable<String,String>>();
93
		
94
		try {
95
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));
96
			HSSFWorkbook wb = new HSSFWorkbook(fs);
97
			HSSFSheet sheet = wb.getSheetAt(0);
98
			HSSFRow row;
99
			HSSFCell cell;
100

    
101
			int rows; // No of rows
102
			rows = sheet.getPhysicalNumberOfRows();
103

    
104
			int cols = 0; // No of columns
105
			int tmp = 0;
106

    
107
			// This trick ensures that we get the data properly even if it doesn't start from first few rows
108
			for(int i = 0; i < 10 || i < rows; i++) {
109
				row = sheet.getRow(i);
110
				if(row != null) {
111
					tmp = sheet.getRow(i).getPhysicalNumberOfCells();
112
					if(tmp > cols) cols = tmp;
113
				}
114
			}
115

    
116
			
117
			Hashtable<String, String> headers = null;
118
			ArrayList<String> columns = new ArrayList<String>();
119
			row = sheet.getRow(0);
120
			for (int c =0; c<cols; c++){
121
				cell = row.getCell(c);
122
				columns.add(cell.toString());
123
			}
124
			for(int r = 1; r < rows; r++) {
125
				row = sheet.getRow(r);
126
				headers = new Hashtable<String, String>();
127
				if(row != null) {
128
					for(int c = 0; c < cols; c++) {
129
						cell = row.getCell((short)c);
130
						if(cell != null) {
131
							headers.put(columns.get(c),cell.toString());
132
						}
133
					}
134
				}
135
				units.add(headers);
136
			}
137
			System.out.println("units: "+units);
138

    
139
			
140

    
141

    
142
		} catch(Exception ioe) {
143
			ioe.printStackTrace();
144
		}
145
		return units;
146
	}
147

    
148

    
149
	public void saveUnit(Hashtable<String,String> unit){
150
		String author = unit.get("author");
151
		author=author.replaceAll("None","");
152
		String taxonName = unit.get("taxonName");
153
		taxonName = taxonName.replaceAll("None", "");
154

    
155
		try {
156
			this.institutionCode = unit.get("institution").replaceAll("None", null);
157
		} catch (Exception e) {
158
		}
159

    
160
		try {this.collectionCode = unit.get("collection").replaceAll("None", null);
161
		} catch (Exception e) {
162
		}
163
		try {this.unitID = unit.get("unitID").replaceAll("None", null);
164
		} catch (Exception e) {
165
		}
166
		try {this.recordBasis = unit.get("recordBasis").replaceAll("None", null);
167
		} catch (Exception e) {
168
		}
169
		try {this.accessionNumber = null;
170
		} catch (Exception e) {
171
		}
172
		try {this.locality = unit.get("locality").replaceAll("None", null);
173
		} catch (Exception e) {
174
		}
175
		try {this.longitude = Double.valueOf(unit.get("longitude"));
176
		} catch (Exception e) {
177
		}
178
		try {this.latitude = Double.valueOf(unit.get("latitude"));
179
		} catch (Exception e) {
180
		}
181
		try {this.country = unit.get("country").replaceAll("None", null);
182
		} catch (Exception e) {
183
		}
184
		try {this.isocountry = unit.get("isoCountry").replaceAll("None", null);
185
		} catch (Exception e) {
186
		}
187
		try {this.fieldNumber = unit.get("field number").replaceAll("None", null);
188
		} catch (Exception e) {
189
		}
190
		try {this.collectorsNumber = unit.get("collector number").replaceAll("None", null);
191
		} catch (Exception e) {
192
		}
193
		try {String coll =unit.get("collector");
194
		coll=coll.replaceAll("None", null);
195
		this.gatheringAgentList.add(coll);
196
		} catch (Exception e) {
197
		}
198
		try {this.identificationList.add(taxonName+" "+author);
199
		} catch (Exception e) {System.out.println(e);
200
		}
201
	}
202

    
203
	@SuppressWarnings("unchecked")
204
	public boolean invoke(){
205
		boolean result = true;
206
		boolean withCdm = true;
207
		CdmApplicationController app = null;
208
		TransactionStatus tx = null;
209

    
210

    
211
		try {
212
			app = CdmApplicationController.NewInstance(CdmDestinations.cdm_test_patricia(), hbm2dll);
213
		} catch (DataSourceNotFoundException e1) {
214
			e1.printStackTrace();
215
			System.out.println("DataSourceNotFoundException "+e1);
216
		} catch (TermNotFoundException e1) {
217
			e1.printStackTrace();
218
			System.out.println("TermNotFoundException " +e1);
219
		}
220
		tx = app.startTransaction();
221
		try {
222
			ReferenceFactory refFactory = ReferenceFactory.newInstance();
223
			ReferenceBase sec = refFactory.newDatabase();
224
			sec.setTitleCache("SYNTHESYS CACHE DATA", true);
225

    
226
			/**
227
			 * SPECIMEN OR OBSERVATION OR LIVING
228
			 */
229
			DerivedUnitBase derivedThing = null;
230
			//create specimen
231
			if (this.recordBasis != null){
232
				if (this.recordBasis.toLowerCase().startsWith("s")) {//specimen
233
					derivedThing = Specimen.NewInstance();				
234
				}
235
				else if (this.recordBasis.toLowerCase().startsWith("o")) {//observation
236
					derivedThing = Observation.NewInstance();				
237
				}
238
				else if (this.recordBasis.toLowerCase().startsWith("l")) {//living -> fossil, herbarium sheet....???
239
					derivedThing = LivingBeing.NewInstance();
240
				}
241
			}
242
			if (derivedThing == null) derivedThing = Observation.NewInstance();
243

    
244
			TaxonNameBase taxonName = null;
245
			Taxon taxon = null;
246
			DeterminationEvent determinationEvent = null;
247
			List<TaxonNameBase> names = null;
248
			NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
249
			String scientificName="";
250
			boolean preferredFlag=false;
251
			System.out.println(this.identificationList);
252
			for (int i = 0; i < this.identificationList.size(); i++) {
253
				this.fullScientificNameString = this.identificationList.get(i);
254
				this.fullScientificNameString = this.fullScientificNameString.replaceAll(" et ", " & ");
255
				if (this.fullScientificNameString.indexOf("_preferred_") != -1){
256
					scientificName = this.fullScientificNameString.split("_preferred_")[0];
257
					String pTmp = this.fullScientificNameString.split("_preferred_")[1];
258
					if (pTmp == "1" || pTmp.toLowerCase().indexOf("true") != -1)
259
						preferredFlag=true;
260
					else
261
						preferredFlag=false;
262
				}
263
				else scientificName = this.fullScientificNameString;
264

    
265
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICZN(),null);
266
//				if (taxonName.hasProblem()){
267
//				System.out.println("pb ICZN");
268
//				taxonName  = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICBN(),null);
269
//				if (taxonName.hasProblem()){
270
//				System.out.println("pb ICBN");
271
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICNB(), null);
272
//				if (taxonName.hasProblem()){
273
//				System.out.println("pb ICNB");
274
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICNCP(), null);
275
//				if (taxonName.hasProblem()){
276
//				System.out.println("pb ICNCP");
277
//				}
278
//				}
279
//				}				
280
//				}
281
				taxonName = nvnpi.parseFullName(scientificName);
282
				if (withCdm){
283
					names = app.getNameService().getNamesByName(scientificName);
284
					if (names.size() == 0){
285
						System.out.println("Name not found: " + scientificName);
286
					}else{
287
						if (names.size() > 1){
288
							System.out.println("More then 1 name found: " + scientificName);
289
						}
290
						System.out.println("Name found");
291
						taxonName = names.get(0);
292
					}
293
				}
294

    
295
				
296
//				tx = app.startTransaction();
297
				app.getNameService().saveOrUpdate(taxonName);
298
				taxon = Taxon.NewInstance(taxonName, sec); //TODO use real reference for sec
299
//				app.commitTransaction(tx);
300

    
301

    
302
				determinationEvent = DeterminationEvent.NewInstance();
303
				determinationEvent.setTaxon(taxon);
304
				determinationEvent.setPreferredFlag(preferredFlag);
305
				derivedThing.addDetermination(determinationEvent);
306
			}
307

    
308

    
309
			//set catalogue number (unitID)
310
			derivedThing.setCatalogNumber(this.unitID);
311
			derivedThing.setAccessionNumber(this.accessionNumber);
312
			derivedThing.setCollectorsNumber(this.collectorsNumber);
313

    
314

    
315
			/**
316
			 * INSTITUTION & COLLECTION
317
			 */
318
			//manage institution
319
			Institution institution;
320
			List<Institution> institutions;
321
			try{
322
				System.out.println(this.institutionCode);
323
				institutions= app.getAgentService().searchInstitutionByCode(this.institutionCode);
324
			}catch(Exception e){
325
				System.out.println("BLI "+e);
326
				institutions=new ArrayList<Institution>();
327
			}
328
			if (institutions.size() ==0){
329
				System.out.println("Institution (agent) unknown");
330
				//create institution
331
				institution = Institution.NewInstance();
332
				institution.setCode(this.institutionCode);				
333
			}
334
			else{
335
				System.out.println("Institution (agent) already in the db");
336
				institution = institutions.get(0);
337
			}
338

    
339
			//manage collection
340
			Collection collection = Collection.NewInstance();
341
			List<Collection> collections;
342
			try{
343
				collections = app.getCollectionService().searchByCode(this.collectionCode);
344
			}catch(Exception e){
345
				System.out.println("BLA"+e);
346
				collections=new ArrayList<Collection>();
347
			}
348
			if (collections.size() ==0){
349
				System.out.println("Collection not found "+this.collectionCode);
350
				//create new collection
351
				collection.setCode(this.collectionCode);
352
				collection.setCodeStandard("GBIF");
353
				collection.setInstitute(institution);
354
			}
355
			else{
356
				boolean collectionFound=false;
357
				for (int i=0; i<collections.size(); i++){
358
					collection = collections.get(i);
359
					try {
360
						if (collection.getInstitute().getCode().equalsIgnoreCase(institution.getCode())){ 
361
							//found a collection with the same code and the same institution
362
							collectionFound=true;
363
						}
364
					} catch (NullPointerException e) {}
365
				}
366
				System.out.println("a trouvé la collection avec la meme institution? "+collectionFound);
367
				if (!collectionFound){ //need to add a new collection with the pre-configured institution
368
					collection.setCode(this.collectionCode);
369
					collection.setCodeStandard("GBIF");
370
					collection.setInstitute(institution);
371
				}
372

    
373
			}
374
			System.out.println("collection inserted");
375
			//link specimen & collection
376
			derivedThing.setCollection(collection);
377

    
378
			/**
379
			 * GATHERING EVENT
380
			 */
381
			//create gathering event
382
			GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
383
			//add locality
384
			Language language = Language.DEFAULT();
385
			LanguageString loc = LanguageString.NewInstance(this.locality,language);
386
			gatheringEvent.setLocality(loc);
387

    
388
			//create coordinates point
389
			Point coordinates = Point.NewInstance();
390
			//add coordinates
391
			coordinates.setLatitude(this.latitude);
392
			coordinates.setLongitude(this.longitude);
393
			gatheringEvent.setExactLocation(coordinates);
394

    
395
			NamedArea area = NamedArea.NewInstance();
396
			
397

    
398
			WaterbodyOrCountry country = null;
399
//			System.out.println("isocountry "+this.isocountry);
400
			if (this.isocountry != null)
401
				country = app.getOccurrenceService().getCountryByIso(this.isocountry);
402
			
403
//			System.out.println(country.getLabel());
404
//			Set<Continent> cont = country.getContinents();
405
//			
406
//			System.out.println(cont.size());
407
//			Iterator<Continent> iter = cont.iterator();
408
//			while (iter.hasNext())
409
//				System.out.println(iter.next().toString());
410
			
411
			if (country != null){
412
				area.addWaterbodyOrCountry(country);
413
				System.out.println("country not null!");
414
			}
415
//			else{
416
//				if (this.country != null){
417
//					List<WaterbodyOrCountry>countries = app.getOccurrenceService().getWaterbodyOrCountryByName(this.country);
418
//					if (countries.size() >0)
419
//						area.addWaterbodyOrCountry(countries.get(0));
420
//					else
421
//						System.out.println("NO COUNTRY");//TODO need to add a new country!
422
//				}
423
//			}
424
//			app.getTermService().saveTerm(area);
425
			gatheringEvent.addCollectingArea(area);
426

    
427
			//create collector
428
			AgentBase collector;
429
			ListIterator<String> collectors = this.gatheringAgentList.listIterator();
430
			//add the collectors
431
			String collName;
432
			while (collectors.hasNext()){
433
				collName = collectors.next();
434
				/*check if the collector does already exist*/
435
				try{
436
					Pager<AgentBase> col = app.getAgentService().findByTitle(null, collName, null, null, null, null, null, null);
437
					collector=col.getRecords().get(0);
438
					System.out.println("a trouve l'agent");
439
				}catch (Exception e) {
440
					collector = Person.NewInstance();
441
					collector.setTitleCache(collName, true);
442
				}
443
				gatheringEvent.setCollector(collector);
444
			}
445

    
446
			//create field/observation
447
			FieldObservation fieldObservation = FieldObservation.NewInstance();
448
			//add fieldNumber
449
			fieldObservation.setFieldNumber(this.fieldNumber);
450

    
451
			//join gatheringEvent to fieldObservation
452
			fieldObservation.setGatheringEvent(gatheringEvent);
453

    
454

    
455
//			//link fieldObservation and specimen
456
			DerivationEvent derivationEvent = DerivationEvent.NewInstance();
457
			derivationEvent.addOriginal(fieldObservation);
458
			derivedThing.addDerivationEvent(derivationEvent);
459
//			derivationEvent.addDerivative(derivedThing);
460

    
461
			/**
462
			 * SAVE AND STORE DATA
463
			 */			
464
			//save the specimen data
465
			//	app.getOccurrenceService().saveSpecimenOrObservationBase(fieldObservation);
466
			try {
467
//				tx = app.startTransaction();
468
				app.getTermService().saveOrUpdate(area);//save it sooner
469
				app.getOccurrenceService().saveOrUpdate(derivedThing);
470
//				app.commitTransaction(tx);
471
//				app.close();
472
			} catch (Exception e) {
473
				// TODO Auto-generated catch block
474
				System.out.println("PATATE "+e);
475
			}
476

    
477

    
478
			logger.info("saved new specimen ...");
479

    
480

    
481

    
482
		} catch (Exception e) {
483
			logger.warn("Error when reading record!!");
484
			e.printStackTrace();
485
			result = false;
486
		}
487
//		
488
		app.commitTransaction(tx);
489
		System.out.println("commit done");
490
		app.close();
491
		
492
		return result;
493
	}
494

    
495

    
496

    
497
	private DeterminationEvent getDetermination(Taxon taxon, String actor){
498
		logger.info("Create determination event");
499
		DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
500
		determinationEvent.setTaxon(taxon);
501
		Person person = Person.NewTitledInstance(actor);
502
		determinationEvent.setActor(person);
503
		return determinationEvent;
504
	}
505

    
506

    
507

    
508
	/**
509
	 * @param args
510
	 */
511
	public static void main(String[] args) {
512
		logger.info("main method");
513
		SynthesysCacheActivator abcdAct = new SynthesysCacheActivator();
514
		ArrayList<Hashtable<String,String>> units = abcdAct.parseXLS();
515
		Hashtable<String,String> unit=null;
516
		for (int i=0; i<units.size();i++){
517
			unit = units.get(i);
518
			System.out.println(unit);
519
			abcdAct.saveUnit(unit);//and then invoke
520
			abcdAct.invoke();
521
			
522
		}
523
	}
524

    
525

    
526

    
527
}
(2-2/2)