Project

General

Profile

« Previous | Next » 

Revision 861ebadd

Added by Patricia Kelbert about 11 years ago

copy main method for abcd and synthesys excel import testing

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/app/abcdImport/SpecimenImport.java
1 1
/**
2 2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy 
3
 * European Distributed Institute of Taxonomy
4 4
 * http://www.e-taxonomy.eu
5
 * 
5
 *
6 6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
 * See LICENSE.TXT at the top of this package for the full license terms.
8 8
 */
......
10 10
package eu.etaxonomy.cdm.app.abcdImport;
11 11
import java.io.File;
12 12
import java.net.URI;
13
import java.net.URISyntaxException;
14 13

  
15 14
import org.apache.log4j.Logger;
16 15

  
......
22 21
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
23 22

  
24 23
public class SpecimenImport {
25
@SuppressWarnings("unused")
26
private static Logger logger = Logger.getLogger(SpecimenImport.class);
27
	
28
	//database validation status (create, update, validate ...)
29
	static DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
30
//	final static String xmlSource = "/home/patricia/Desktop/multiABCD.xml";
31
	//final static String xmlSource = "D:\\_Tagungen\\2010-09 TDWG 2010\\Workshop\\data\\specimen\\Picris pauciflora\\B-W14632-000_B-W14632-010_B100097145_B100097146_B100326668_B180004364_B180017717_.xml";
32
	final static String xmlSource = "D:\\_Tagungen\\2010-09 TDWG 2010\\Workshop\\data\\specimen\\Cichorium endivia 1\\B-W14812-000_B-W14812-010_B-W14812-020_B-W14812-030_B100066574_B100066583_.xml";
33
	
34
	
35
	
36
	
37
	static final ICdmDataSource cdmDestination = CdmDestinations.cdm_local_dipera();
38
	//check - import
39
	static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
40
	
41
	/**
42
	 * @param args
43
	 */
44
	public static void main(String[] args) {
45
		URI source;
46
		try {
47
			URI uri = new File(xmlSource).toURI();
48
			source = new URI(uri.toString());
49
			System.out.println(source.toString());
50
			System.out.println("Start import from  ABCD Specimen data("+ source.toString() + ") ...");
51
			
52
			ICdmDataSource destination = cdmDestination;
53
			Abcd206ImportConfigurator specimenImportConfigurator = Abcd206ImportConfigurator.NewInstance(source,  destination);
54
			
55
			specimenImportConfigurator.setSourceSecId("specimen");
56
			specimenImportConfigurator.setCheck(check);
57
			specimenImportConfigurator.setDbSchemaValidation(hbm2dll);
58
			specimenImportConfigurator.setDoAutomaticParsing(true);
59
			specimenImportConfigurator.setReUseExistingMetadata(true);
60
			
61
			specimenImportConfigurator.setDoMatchTaxa(true);
62
			specimenImportConfigurator.setReUseTaxon(true);
63
			
64
			specimenImportConfigurator.setDoCreateIndividualsAssociations(true);
65
			
66
			specimenImportConfigurator.setSourceReference(null);
67
			specimenImportConfigurator.setTaxonReference(null);
68
			
69
			// invoke import
70
			CdmDefaultImport<Abcd206ImportConfigurator> specimenImport = new CdmDefaultImport<Abcd206ImportConfigurator>();
71
			//new Test().invoke(tcsImportConfigurator);
72
			specimenImport.invoke(specimenImportConfigurator);
73
			System.out.println("End import from SpecimenData ("+ source.toString() + ")...");
74
		} catch (URISyntaxException e) {
75
			e.printStackTrace();
76
		}
77
		
78
	}
24
    @SuppressWarnings("unused")
25
    private static Logger logger = Logger.getLogger(SpecimenImport.class);
26

  
27
    //database validation status (create, update, validate ...)
28
    static DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
29
    //  final static String xmlSource = "/home/patricia/Desktop/multiABCD.xml";
30
    //final static String xmlSource = "D:\\_Tagungen\\2010-09 TDWG 2010\\Workshop\\data\\specimen\\Picris pauciflora\\B-W14632-000_B-W14632-010_B100097145_B100097146_B100326668_B180004364_B180017717_.xml";
31
//    final static String xmlSource = "/home/pkelbert/workspace/proibiosphere/cdmlib-io/src/test/resources/eu/etaxonomy/cdm/io/specimen/abcd206/in/SpecimenImportConfiguratorTest-input.xml";
32

  
33
    final static String xmlSource = "/home/pkelbert/Bureau/CLD33-a.xml";
34

  
35

  
36
    static final ICdmDataSource cdmDestination = CdmDestinations.mon_cdm();
37
//    static final ICdmDataSource cdmDestination = CdmDestinations.makeDestination(DatabaseTypeEnum.H2, "localhost", "abcd_import_test", 8082, "root", "root");
38
    static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
39

  
40
    /**
41
     * @param args
42
     */
43
    public static void main(String[] args) {
44

  
45

  
46
        URI source;
47
        try {
48
   //         org.h2.tools.Server.createWebServer(new String[]{}).start();
49
            URI uri = new File(xmlSource).toURI();
50
            source = new URI(uri.toString());
51
            System.out.println(source.toString());
52
            System.out.println("Start import from  ABCD Specimen data("+ source.toString() + ") ...");
53

  
54
            ICdmDataSource destination = cdmDestination;
55
            Abcd206ImportConfigurator specimenImportConfigurator = Abcd206ImportConfigurator.NewInstance(source,  destination);
56

  
57
            specimenImportConfigurator.setClassificationName(specimenImportConfigurator.getSourceReferenceTitle());
58
            specimenImportConfigurator.setSourceSecId("specimen");
59
            specimenImportConfigurator.setCheck(check);
60
            specimenImportConfigurator.setDbSchemaValidation(hbm2dll);
61
            specimenImportConfigurator.setDoAutomaticParsing(true);
62
            specimenImportConfigurator.setReUseExistingMetadata(true);
63

  
64
            specimenImportConfigurator.setDoMatchTaxa(true);
65
            specimenImportConfigurator.setReUseTaxon(true);
66

  
67
            specimenImportConfigurator.setDoCreateIndividualsAssociations(true);
68

  
69
            specimenImportConfigurator.setSourceReference(null);
70
            specimenImportConfigurator.setTaxonReference(null);
71

  
72
            // invoke import
73
            CdmDefaultImport<Abcd206ImportConfigurator> specimenImport = new CdmDefaultImport<Abcd206ImportConfigurator>();
74
            //new Test().invoke(tcsImportConfigurator);
75
            specimenImport.invoke(specimenImportConfigurator);
76
            System.out.println("End import from SpecimenData ("+ source.toString() + ")...");
77
        } catch (Exception e) {
78
            e.printStackTrace();
79
        }
80

  
81
    }
79 82

  
80 83
}
app-import/src/main/java/eu/etaxonomy/cdm/app/abcdImport/SynthesysCacheActivator.java
1 1
/**
2 2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy 
3
 * European Distributed Institute of Taxonomy
4 4
 * http://www.e-taxonomy.eu
5
 * 
5
 *
6 6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
 * See LICENSE.TXT at the top of this package for the full license terms.
8 8
 */
9 9

  
10 10
package eu.etaxonomy.cdm.app.abcdImport;
11 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;
12
import java.io.File;
13
import java.net.URI;
14
import java.net.URISyntaxException;
17 15

  
18 16
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 17

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

  
53 25

  
54 26

  
......
59 31
 * @version 1.0
60 32
 */
61 33
public class SynthesysCacheActivator {
62
	private static final Logger logger = Logger.getLogger(SynthesysCacheActivator.class);
63

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

  
80
	static DbSchemaValidation hbm2dll = DbSchemaValidation.UPDATE;
81

  
82
	protected HSSFWorkbook hssfworkbook = null;
83

  
84

  
85

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

  
98
			int rows; // No of rows
99
			rows = sheet.getPhysicalNumberOfRows();
100

  
101
			int cols = 0; // No of columns
102
			int tmp = 0;
103

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

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

  
136
			
137

  
138

  
139
		} catch(Exception ioe) {
140
			ioe.printStackTrace();
141
		}
142
		return units;
143
	}
144

  
145

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

  
152
		try {
153
			this.institutionCode = unit.get("institution").replaceAll("None", null);
154
		} catch (Exception e) {
155
		}
156

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

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

  
207

  
208
		app = CdmApplicationController.NewInstance(CdmDestinations.localH2(), hbm2dll);
209
		
210
		tx = app.startTransaction();
211
		try {
212
			ReferenceFactory refFactory = ReferenceFactory.newInstance();
213
			Reference sec = refFactory.newDatabase();
214
			sec.setTitleCache("SYNTHESYS CACHE DATA", true);
215

  
216
			/**
217
			 * SPECIMEN OR OBSERVATION OR LIVING
218
			 */
219
			DerivedUnitBase derivedThing = null;
220
			//create specimen
221
			if (this.recordBasis != null){
222
				if (this.recordBasis.toLowerCase().startsWith("s")) {//specimen
223
					derivedThing = Specimen.NewInstance();				
224
				}
225
				else if (this.recordBasis.toLowerCase().startsWith("o")) {//observation
226
					derivedThing = Observation.NewInstance();				
227
				}
228
				else if (this.recordBasis.toLowerCase().startsWith("l")) {//living -> fossil, herbarium sheet....???
229
					derivedThing = LivingBeing.NewInstance();
230
				}
231
			}
232
			if (derivedThing == null) derivedThing = Observation.NewInstance();
233

  
234
			TaxonNameBase taxonName = null;
235
			Taxon taxon = null;
236
			DeterminationEvent determinationEvent = null;
237
			List<TaxonNameBase> names = null;
238
			NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
239
			String scientificName="";
240
			boolean preferredFlag=false;
241
			System.out.println(this.identificationList);
242
			for (int i = 0; i < this.identificationList.size(); i++) {
243
				this.fullScientificNameString = this.identificationList.get(i);
244
				this.fullScientificNameString = this.fullScientificNameString.replaceAll(" et ", " & ");
245
				if (this.fullScientificNameString.indexOf("_preferred_") != -1){
246
					scientificName = this.fullScientificNameString.split("_preferred_")[0];
247
					String pTmp = this.fullScientificNameString.split("_preferred_")[1];
248
					if (pTmp == "1" || pTmp.toLowerCase().indexOf("true") != -1)
249
						preferredFlag=true;
250
					else
251
						preferredFlag=false;
252
				}
253
				else scientificName = this.fullScientificNameString;
254

  
255
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICZN(),null);
256
//				if (taxonName.hasProblem()){
257
//				System.out.println("pb ICZN");
258
//				taxonName  = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICBN(),null);
259
//				if (taxonName.hasProblem()){
260
//				System.out.println("pb ICBN");
261
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICNB(), null);
262
//				if (taxonName.hasProblem()){
263
//				System.out.println("pb ICNB");
264
//				taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICNCP(), null);
265
//				if (taxonName.hasProblem()){
266
//				System.out.println("pb ICNCP");
267
//				}
268
//				}
269
//				}				
270
//				}
271
				taxonName = nvnpi.parseFullName(scientificName);
272
				if (withCdm){
273
					names = app.getNameService().getNamesByName(scientificName);
274
					if (names.size() == 0){
275
						System.out.println("Name not found: " + scientificName);
276
					}else{
277
						if (names.size() > 1){
278
							System.out.println("More then 1 name found: " + scientificName);
279
						}
280
						System.out.println("Name found");
281
						taxonName = names.get(0);
282
					}
283
				}
284

  
285
				
286
//				tx = app.startTransaction();
287
				app.getNameService().saveOrUpdate(taxonName);
288
				taxon = Taxon.NewInstance(taxonName, sec); //TODO use real reference for sec
289
//				app.commitTransaction(tx);
290

  
291

  
292
				determinationEvent = DeterminationEvent.NewInstance();
293
				determinationEvent.setTaxon(taxon);
294
				determinationEvent.setPreferredFlag(preferredFlag);
295
				derivedThing.addDetermination(determinationEvent);
296
			}
297

  
298

  
299
			//set catalogue number (unitID)
300
			derivedThing.setCatalogNumber(this.unitID);
301
			derivedThing.setAccessionNumber(this.accessionNumber);
302
			derivedThing.setCollectorsNumber(this.collectorsNumber);
303

  
304

  
305
			/**
306
			 * INSTITUTION & COLLECTION
307
			 */
308
			//manage institution
309
			Institution institution;
310
			List<Institution> institutions;
311
			try{
312
				System.out.println(this.institutionCode);
313
				institutions= app.getAgentService().searchInstitutionByCode(this.institutionCode);
314
			}catch(Exception e){
315
				System.out.println("BLI "+e);
316
				institutions=new ArrayList<Institution>();
317
			}
318
			if (institutions.size() ==0){
319
				System.out.println("Institution (agent) unknown");
320
				//create institution
321
				institution = Institution.NewInstance();
322
				institution.setCode(this.institutionCode);				
323
			}
324
			else{
325
				System.out.println("Institution (agent) already in the db");
326
				institution = institutions.get(0);
327
			}
328

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

  
363
			}
364
			System.out.println("collection inserted");
365
			//link specimen & collection
366
			derivedThing.setCollection(collection);
367

  
368
			/**
369
			 * GATHERING EVENT
370
			 */
371
			//create gathering event
372
			GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
373
			//add locality
374
			Language language = Language.DEFAULT();
375
			LanguageString loc = LanguageString.NewInstance(this.locality,language);
376
			gatheringEvent.setLocality(loc);
377

  
378
			//create coordinates point
379
			Point coordinates = Point.NewInstance();
380
			//add coordinates
381
			coordinates.setLatitude(this.latitude);
382
			coordinates.setLongitude(this.longitude);
383
			gatheringEvent.setExactLocation(coordinates);
384

  
385
			NamedArea area = NamedArea.NewInstance();
386
			
387

  
388
			WaterbodyOrCountry country = null;
389
//			System.out.println("isocountry "+this.isocountry);
390
			if (this.isocountry != null)
391
				country = app.getOccurrenceService().getCountryByIso(this.isocountry);
392
			
393
//			System.out.println(country.getLabel());
394
//			Set<Continent> cont = country.getContinents();
395
//			
396
//			System.out.println(cont.size());
397
//			Iterator<Continent> iter = cont.iterator();
398
//			while (iter.hasNext())
399
//				System.out.println(iter.next().toString());
400
			
401
			if (country != null){
402
				area.addWaterbodyOrCountry(country);
403
				System.out.println("country not null!");
404
			}
405
//			else{
406
//				if (this.country != null){
407
//					List<WaterbodyOrCountry>countries = app.getOccurrenceService().getWaterbodyOrCountryByName(this.country);
408
//					if (countries.size() >0)
409
//						area.addWaterbodyOrCountry(countries.get(0));
410
//					else
411
//						System.out.println("NO COUNTRY");//TODO need to add a new country!
412
//				}
413
//			}
414
//			app.getTermService().saveTerm(area);
415
			gatheringEvent.addCollectingArea(area);
416

  
417
			//create collector
418
			AgentBase collector;
419
			ListIterator<String> collectors = this.gatheringAgentList.listIterator();
420
			//add the collectors
421
			String collName;
422
			while (collectors.hasNext()){
423
				collName = collectors.next();
424
				/*check if the collector does already exist*/
425
				try{
426
					Pager<AgentBase> col = app.getAgentService().findByTitle(null, collName, null, null, null, null, null, null);
427
					collector=col.getRecords().get(0);
428
					System.out.println("a trouve l'agent");
429
				}catch (Exception e) {
430
					collector = Person.NewInstance();
431
					collector.setTitleCache(collName, true);
432
				}
433
				gatheringEvent.setCollector(collector);
434
			}
435

  
436
			//create field/observation
437
			FieldObservation fieldObservation = FieldObservation.NewInstance();
438
			//add fieldNumber
439
			fieldObservation.setFieldNumber(this.fieldNumber);
440

  
441
			//join gatheringEvent to fieldObservation
442
			fieldObservation.setGatheringEvent(gatheringEvent);
443

  
444

  
445
//			//link fieldObservation and specimen
446
			DerivationEvent derivationEvent = DerivationEvent.NewInstance();
447
			derivationEvent.addOriginal(fieldObservation);
448
			derivedThing.addDerivationEvent(derivationEvent);
449
//			derivationEvent.addDerivative(derivedThing);
450

  
451
			/**
452
			 * SAVE AND STORE DATA
453
			 */			
454
			//save the specimen data
455
			//	app.getOccurrenceService().saveSpecimenOrObservationBase(fieldObservation);
456
			try {
457
//				tx = app.startTransaction();
458
				app.getTermService().saveOrUpdate(area);//save it sooner
459
				app.getOccurrenceService().saveOrUpdate(derivedThing);
460
//				app.commitTransaction(tx);
461
//				app.close();
462
			} catch (Exception e) {
463
				// TODO Auto-generated catch block
464
				System.out.println("PATATE "+e);
465
			}
466

  
467

  
468
			logger.info("saved new specimen ...");
469

  
470

  
471

  
472
		} catch (Exception e) {
473
			logger.warn("Error when reading record!!");
474
			e.printStackTrace();
475
			result = false;
476
		}
477
//		
478
		app.commitTransaction(tx);
479
		System.out.println("commit done");
480
		app.close();
481
		
482
		return result;
483
	}
484

  
485

  
486

  
487
	private DeterminationEvent getDetermination(Taxon taxon, String actor){
488
		logger.info("Create determination event");
489
		DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
490
		determinationEvent.setTaxon(taxon);
491
		Person person = Person.NewTitledInstance(actor);
492
		determinationEvent.setActor(person);
493
		return determinationEvent;
494
	}
495

  
496

  
497

  
498
	/**
499
	 * @param args
500
	 */
501
	public static void main(String[] args) {
502
		logger.info("main method");
503
		SynthesysCacheActivator abcdAct = new SynthesysCacheActivator();
504
		ArrayList<Hashtable<String,String>> units = abcdAct.parseXLS();
505
		Hashtable<String,String> unit=null;
506
		for (int i=0; i<units.size();i++){
507
			unit = units.get(i);
508
			System.out.println(unit);
509
			abcdAct.saveUnit(unit);//and then invoke
510
			abcdAct.invoke();
511
			
512
		}
513
	}
514

  
34
    @SuppressWarnings("unused")
35
    private static Logger logger = Logger.getLogger(SynthesysCacheActivator.class);
36

  
37
    //database validation status (create, update, validate ...)
38

  
39
//    final static String xmlSource = "/home/pkelbert/workspace/proibiosphere/cdmlib-io/target/test-classes/eu/etaxonomy/cdm/io/specimen/excel/in/ExcelImportConfiguratorTest-input.xls";
40
    final static String xmlSource = "/home/pkelbert/Documents/Proibiosphere/ChenopodiumQuentin/GBIF data etc.xls";
41

  
42

  
43
//  static final ICdmDataSource cdmDestination = CdmDestinations.mon_cdm();
44
//    static DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
45
    static final ICdmDataSource cdmDestination = CdmDestinations.proibiosphere_local();
46
    static DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
47
    static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
48

  
49
    /**
50
     * @param args
51
     */
52
    public static void main(String[] args) {
53
        URI source;
54
        try {
55
            URI uri = new File(xmlSource).toURI();
56
            source = new URI(uri.toString());
57
            System.out.println(source.toString());
58
            System.out.println("Start import from  Excel Specimen data("+ source.toString() + ") ...");
59

  
60
            ICdmDataSource destination = cdmDestination;
61
            SpecimenSynthesysExcelImportConfigurator specimenImportConfigurator = SpecimenSynthesysExcelImportConfigurator.NewInstance(source,  destination);
62

  
63
            specimenImportConfigurator.setSourceSecId("specimen");
64
            specimenImportConfigurator.setCheck(check);
65
            specimenImportConfigurator.setDbSchemaValidation(hbm2dll);
66
            specimenImportConfigurator.setDoAutomaticParsing(true);
67
            specimenImportConfigurator.setReUseExistingMetadata(true);
68
            specimenImportConfigurator.setAskForDate(true);
69
            specimenImportConfigurator.setDefaultAuthor("L.");
70
            specimenImportConfigurator.setClassificationName("Chenopodium");
71

  
72
            specimenImportConfigurator.setReUseTaxon(true);
73

  
74
            specimenImportConfigurator.setSourceReference(null);
75
            specimenImportConfigurator.setTaxonReference(null);
76

  
77
            // invoke import
78
            CdmDefaultImport<SpecimenSynthesysExcelImportConfigurator> specimenImport = new CdmDefaultImport<SpecimenSynthesysExcelImportConfigurator>();
79
            //new Test().invoke(tcsImportConfigurator);
80
            specimenImport.invoke(specimenImportConfigurator);
81
            System.out.println("End import from SpecimenData ("+ source.toString() + ")...");
82
        } catch (URISyntaxException e) {
83
            e.printStackTrace();
84
        }
85

  
86
    }
515 87

  
516 88

  
517 89
}

Also available in: Unified diff