Project

General

Profile

Download (37.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.wp6.palmae;
11

    
12
import java.io.FileInputStream;
13
import java.io.FileNotFoundException;
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.net.URI;
17
import java.net.URISyntaxException;
18
import java.util.ArrayList;
19
import java.util.HashSet;
20
import java.util.Iterator;
21
import java.util.List;
22
import java.util.Set;
23
import java.util.UUID;
24

    
25
import org.apache.log4j.Logger;
26
import org.apache.poi.hssf.usermodel.HSSFCell;
27
import org.apache.poi.hssf.usermodel.HSSFRow;
28
import org.apache.poi.hssf.usermodel.HSSFSheet;
29
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
30
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
31

    
32
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
33
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
34
import eu.etaxonomy.cdm.api.service.IDescriptionService;
35
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
36
import eu.etaxonomy.cdm.api.service.IReferenceService;
37
import eu.etaxonomy.cdm.api.service.ITaxonService;
38
import eu.etaxonomy.cdm.api.service.ITermService;
39
import eu.etaxonomy.cdm.api.service.IVocabularyService;
40
import eu.etaxonomy.cdm.api.service.pager.Pager;
41
import eu.etaxonomy.cdm.common.AccountStore;
42
import eu.etaxonomy.cdm.database.CdmDataSource;
43
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
44
import eu.etaxonomy.cdm.database.ICdmDataSource;
45
import eu.etaxonomy.cdm.model.agent.Team;
46
import eu.etaxonomy.cdm.model.common.CdmBase;
47
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
48
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
49
import eu.etaxonomy.cdm.model.common.Language;
50
import eu.etaxonomy.cdm.model.common.LanguageString;
51
import eu.etaxonomy.cdm.model.common.Marker;
52
import eu.etaxonomy.cdm.model.common.MarkerType;
53
import eu.etaxonomy.cdm.model.common.TermVocabulary;
54
import eu.etaxonomy.cdm.model.common.TimePeriod;
55
import eu.etaxonomy.cdm.model.description.CategoricalData;
56
import eu.etaxonomy.cdm.model.description.Feature;
57
import eu.etaxonomy.cdm.model.description.FeatureNode;
58
import eu.etaxonomy.cdm.model.description.FeatureTree;
59
import eu.etaxonomy.cdm.model.description.Modifier;
60
import eu.etaxonomy.cdm.model.description.State;
61
import eu.etaxonomy.cdm.model.description.StateData;
62
import eu.etaxonomy.cdm.model.description.TaxonDescription;
63
import eu.etaxonomy.cdm.model.description.TextData;
64
import eu.etaxonomy.cdm.model.reference.Reference;
65
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
66
import eu.etaxonomy.cdm.model.taxon.Synonym;
67
import eu.etaxonomy.cdm.model.taxon.Taxon;
68
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
69

    
70
public class UseImport {
71
	public static final Logger logger = Logger.getLogger(UseImport.class);
72

    
73
	public static ICdmDataSource dataSource() {
74
		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
75
		String cdmServer = "localhost";
76
		String cdmDB = "palm_use_cdm_db";
77
		String cdmUserName = "root";
78
		String cdmPWD = "root";
79
		return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, cdmPWD);
80
	}
81

    
82
	private static ICdmDataSource makeDestination(DatabaseTypeEnum dbType,
83
			String cdmServer, String cdmDB, int port, String cdmUserName,
84
			String pwd) {
85
		// establish connection
86
		pwd = AccountStore.readOrStorePassword(cdmServer, cdmDB, cdmUserName,
87
				pwd);
88
		ICdmDataSource destination;
89
		if (dbType.equals(DatabaseTypeEnum.MySQL)) {
90
			destination = CdmDataSource.NewMySqlInstance(cdmServer, cdmDB,
91
					port, cdmUserName, pwd, null);
92
		} else if (dbType.equals(DatabaseTypeEnum.PostgreSQL)) {
93
			destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB,
94
					port, cdmUserName, pwd, null);
95
		} else {
96
			// TODO others
97
			throw new RuntimeException("Unsupported DatabaseType");
98
		}
99
		return destination;
100
	}
101

    
102
	public boolean importFromExcelSS(String xlsPath) {
103
		boolean success = true;
104
		
105
		CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
106
		ConversationHolder conversation = applicationController.NewConversation();
107
		conversation.startTransaction();
108
		
109
		ITaxonService service = applicationController.getTaxonService();
110
		ITermService termService = applicationController.getTermService();
111
		IDescriptionService descService = applicationController.getDescriptionService();
112
		IReferenceService referenceService = applicationController.getReferenceService();
113
		InputStream inputStream = null;
114
		
115
		try {
116
			inputStream = new FileInputStream(xlsPath);
117

    
118
		} catch (FileNotFoundException e) {
119
			success = false;
120
			System.out.println("File not found in the specified path.");
121
			e.printStackTrace();
122
		}
123

    
124
		POIFSFileSystem fileSystem = null;
125
		
126
		try {
127
			fileSystem = new POIFSFileSystem(inputStream);
128

    
129
			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
130
			HSSFSheet sheet = workBook.getSheetAt(0);
131
			Iterator rows = sheet.rowIterator();
132
			// Iterator rows = sheet.rowIterator();
133
			ArrayList<ArrayList<String>> lstUpdates = new ArrayList<ArrayList<String>>();
134
			Set<Integer> lstTaxonIDs = new HashSet<Integer>();
135
			//Set<Integer> lstTaxonIDs;
136
		
137

    
138
			while (rows.hasNext()) {
139

    
140
				HSSFRow row = (HSSFRow) rows.next();
141
				System.out.println("Row No.: " + row.getRowNum());
142
				Iterator cells = row.cellIterator();
143
				ArrayList<String> lstTaxon = new ArrayList<String>();
144
				while (cells.hasNext()) {
145
					HSSFCell cell = (HSSFCell) cells.next();
146
				
147
					int intCellType = cell.getCellType();
148
					switch (intCellType) {
149
						case 0:
150
							int cellValue = (int) cell.getNumericCellValue(); 
151
							lstTaxon.add(Integer.toString(cellValue));
152
							break;
153
						case 1:
154
							lstTaxon.add(cell.getStringCellValue());
155
							break;
156
					}
157
				}
158
				lstUpdates.add(lstTaxon);
159
				lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));	
160
			}
161
			
162
			List<TaxonBase> taxa = service.findTaxaByID(lstTaxonIDs);
163
			for(TaxonBase idTaxa : taxa) {
164
				//System.out.println(idTaxa.getUuid().toString());
165
				System.out.println(idTaxa.getName());
166
			}
167
			
168
			
169
			MarkerType useMarkerType = (MarkerType) termService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
170
			Marker useMarker = Marker.NewInstance(useMarkerType, true);
171
			for (ArrayList<String> lstUpdate : lstUpdates) {
172
				System.out.println("-----------------------------------------------: " + lstUpdate.get(1));
173
				String idTaxonToUpdate = lstUpdate.get(1);
174
				TaxonDescription newUseDescription = TaxonDescription.NewInstance();
175
				newUseDescription.addMarker(useMarker);
176
				newUseDescription.setTitleCache(lstUpdate.get(2));
177
				Reference citation = ReferenceFactory.newGeneric();
178
				Team authorTeam = Team.NewInstance();
179
				authorTeam.setTitleCache(lstUpdate.get(3));
180
				citation.setAuthorTeam(authorTeam);
181
				citation.setTitle(lstUpdate.get(4));
182
				
183
				//citation.
184
				TimePeriod year = TimePeriod.NewInstance(Integer.parseInt(lstUpdate.get(5)));
185
				citation.setDatePublished(year);
186
				citation.setTitleCache(lstUpdate.get(6));
187
				//citation.
188
				for(TaxonBase taxon : taxa) {
189
					String taxonUUID = taxon.getUuid().toString(); 
190
					//System.out.println(idTaxonToUpdate + "|" + taxonUUID);
191
					if(idTaxonToUpdate.equals(taxonUUID)) {
192
						logger.info("Processing Taxn " + taxon.getTitleCache() + " with UUID: " + taxon.getUuid());
193
						if(taxon.isInstanceOf(Synonym.class)) {
194
							Taxon bestCandidate = null;
195
							Synonym synonym = CdmBase.deproxy(taxon, Synonym.class);
196
							Set<Taxon> acceptetdCandidates = synonym.getAcceptedTaxa();
197
							if(!acceptetdCandidates.isEmpty()){
198
								bestCandidate = acceptetdCandidates.iterator().next();
199
								if(acceptetdCandidates.size() == 1){
200
									logger.info(acceptetdCandidates.size() + " Accepted taxa found for synonym " + taxon.getTitleCache() + ", using first one: " + bestCandidate.getTitleCache());
201
									Set<TaxonDescription> taxonDescriptions = bestCandidate.getDescriptions();
202
									if(!taxonDescriptions.isEmpty()) {
203
										TaxonDescription firstDescription = taxonDescriptions.iterator().next();
204
										//newUseSummary.addSource(null, null, citation, null);
205
										//firstDescription.addElement(newUseSummary);
206
									}
207
									else {
208
										logger.warn("No description container for: " + bestCandidate.getName());
209
										
210
									}
211
								} else {
212
									logger.info("using accepted Taxon " +  bestCandidate.getTitleCache() + "for synonym " + taxon.getTitleCache());
213
									//List<DescriptionElementBase> descriptionElements = descService.getDescriptionElementsForTaxon((Taxon) bestCandidate, null, null, null, null, null);
214
									Set<TaxonDescription> taxonDescriptions = bestCandidate.getDescriptions();
215
									if(!taxonDescriptions.isEmpty()) {
216
										TaxonDescription firstDescription = taxonDescriptions.iterator().next();
217
										//newUseSummary.addSource(null, null, citation, null);
218
										//firstDescription.addElement(newUseSummary);
219
									}
220
									else {
221
										logger.warn("No description container for: " + bestCandidate.getName());
222
										
223
									}
224
								}
225
							}
226
						}
227
						else {
228
							Taxon taxonAccepted = (Taxon) taxon;
229
							/*Set<TaxonDescription> taxonDescriptions = taxonAccepted.getDescriptions();
230
							if(!taxonDescriptions.isEmpty()) {
231
								TaxonDescription firstDescription = taxonDescriptions.iterator().next();
232
								//newUseSummary.addSource(null, null, citation, null);
233
								//firstDescription.addElement(newUseSummary);
234
							}
235
							else {
236
								logger.warn("No description container for: " + taxonAccepted.getName());
237
							}*/
238
							taxonAccepted.addDescription(newUseDescription);
239
							service.saveOrUpdate(taxonAccepted);
240
							
241
						}
242
					}
243
				}
244
				
245
			}
246
			conversation.commit(false);
247
			
248
		} catch (IOException e) {
249
			success = false;
250
			e.printStackTrace();
251
		}
252

    
253
		return success;
254

    
255
	}
256
	
257
	private boolean loadUses() {
258
		boolean success = true;
259
		//String xslUseSummaryPathString = "C://workspace//Matched_UseSummary_referenceIdTaxEd_TaxonName.xls";
260
		//String xslUseSummaryPathString = "C://workspace//testUseSummaries.xls";
261
		
262
		
263
		String xslUseSummaryPathString = "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//Matched_UseSummary_referenceIdTaxEd_TaxonName.xls";
264
		
265
		//String xslUseRecordPathString = "C://workspace//UseRecordTerms_UseSummaryId.xls";
266
		//String xslUseRecordPathString = "C://workspace//testUseRecords.xls";
267
		//String xslUseRecordPathString = "C://workspace//test_useRecord.xls";
268
		String xslUseRecordPathString = "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//UseRecordTerms_UseSummaryId.xls";
269
		
270
		InputStream inputStream = null;
271
		
272
		
273
		CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
274
		ConversationHolder conversation = applicationController.NewConversation();
275
		conversation.startTransaction();
276
		
277
		ITaxonService taxonService = applicationController.getTaxonService();
278
		ITermService termService = applicationController.getTermService();
279
		IDescriptionService descService = applicationController.getDescriptionService();
280
		IReferenceService referenceService = applicationController.getReferenceService();
281
			
282
		
283
		ArrayList<ArrayList<String>> lstUseSummaries = loadSpreadsheet(xslUseSummaryPathString);
284
		ArrayList<ArrayList<String>> lstUseRecords = loadSpreadsheet(xslUseRecordPathString);
285
		
286
		MarkerType useMarkerType = (MarkerType) termService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
287
		Feature featureUseRecord = (Feature) termService.find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
288
		Feature featureUseSummary = (Feature) termService.find(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
289
		Pager<DefinedTermBase>  notAvailModPager = (Pager<DefinedTermBase> ) termService.findByTitle(Modifier.class, "N/A", null, null, null, null, null, null);
290
		Pager<DefinedTermBase>  notAvailStatePager = (Pager<DefinedTermBase> ) termService.findByTitle(State.class, "N/A", null, null, null, null, null, null);
291
		Modifier notAvailMod = (Modifier) notAvailModPager.getRecords().get(0);
292
		State notAvailState = (State) notAvailStatePager.getRecords().get(0);
293
		
294
		int i = 0;
295
		int j = 0;
296
		try {
297
			for (ArrayList<String> lstUseSummary : lstUseSummaries) {
298
				i++;
299
				String idTaxonToUpdate = lstUseSummary.get(3);
300
				TaxonBase taxon = taxonService.find(UUID.fromString(idTaxonToUpdate));
301
				if (taxon != null) {
302
					TaxonDescription newUseDescription = TaxonDescription.NewInstance();
303
					Marker useMarker = Marker.NewInstance(useMarkerType, true);
304
					newUseDescription.addMarker(useMarker);
305
					Reference useReference = null;
306
					Pager<Reference> reference = referenceService.findByTitle(Reference.class, lstUseSummary.get(5), null, null, null, null, null, null);
307
					if(reference.getCount() == 0) {
308
						System.out.println("Reference title: " + lstUseSummary.get(5) + " not found.");
309
					} else if(reference.getCount() > 0 ) {
310
						useReference = reference.getRecords().get(0);
311
					}
312
					IdentifiableSource source =IdentifiableSource.NewInstance(useReference, null);
313
					source.setOriginalNameString(taxon.getName().toString());
314
					newUseDescription.addSource(source);
315
					TextData useSummary = TextData.NewInstance(featureUseSummary);
316
					LanguageString languageString = LanguageString.NewInstance(lstUseSummary.get(1), Language.ENGLISH());
317
					useSummary.putText(languageString);
318
					descService.saveDescriptionElement(useSummary);
319
					newUseDescription.addElement(useSummary);
320
					for (ArrayList<String> lstUseRecord : lstUseRecords) {
321
						j++;
322
						//System.out.println("Processing UseSummary#: " + i + " ID:" + lstUseSummary.get(0) + "UseRecord: " + lstUseRecord.get(1));
323
						if(lstUseSummary.get(0).equals(lstUseRecord.get(0))) {
324
							CategoricalData useRecord = CategoricalData.NewInstance();
325
							useRecord.setFeature(featureUseRecord);
326
							String modifyingText = "";
327
							if(lstUseRecord.get(3) != null && lstUseRecord.get(3).length() > 0) {
328
								Pager<DefinedTermBase> useCategoryPager = termService.findByTitle(State.class, lstUseRecord.get(3), null, null, null, null, null, null);
329
								State useCategory = null;
330
								if(useCategoryPager.getCount() > 0) {
331
									useCategory = (State) useCategoryPager.getRecords().get(0);
332
								} else {
333
									useCategory = notAvailState;
334
								}
335
								StateData stateCatData = StateData.NewInstance(useCategory);
336
								stateCatData.setState(useCategory);
337
								stateCatData.putModifyingText(Language.ENGLISH(), "Use Category");
338
								modifyingText += useCategory.toString() + ";";
339
								useRecord.addState(stateCatData);
340
								
341
								 
342
								//useRecord.addState(stateData);
343
							} else {
344
								State useCategory = notAvailState;
345
								StateData stateCatData = StateData.NewInstance(useCategory);
346
								stateCatData.setState(useCategory);
347
								stateCatData.putModifyingText(Language.ENGLISH(), "Use Category");
348
								modifyingText += useCategory.toString() + ";";
349
								useRecord.addState(stateCatData);
350
								
351
							}
352
							
353
							if(lstUseRecord.get(4) != null && lstUseRecord.get(4).length() > 0) {
354
								Pager<DefinedTermBase> useSubCategoryPager = termService.findByTitle(State.class, lstUseRecord.get(4), null, null, null, null, null, null);
355
								State useSubCategory = null;
356
								if(useSubCategoryPager.getCount() > 0) {
357
									useSubCategory = (State) useSubCategoryPager.getRecords().get(0);
358
								
359
								} else {
360
									useSubCategory = notAvailState;
361
								}
362
								StateData stateSubCatData = StateData.NewInstance(useSubCategory);
363
								stateSubCatData.setState(useSubCategory);
364
								stateSubCatData.putModifyingText(Language.ENGLISH(), "Use SubCategory");
365
								modifyingText += useSubCategory.toString() + ";";
366
								useRecord.addState(stateSubCatData);
367
								
368
							}
369
							else {
370
								State useSubCategory = notAvailState;
371
								StateData stateSubCatData = StateData.NewInstance(useSubCategory);
372
								stateSubCatData.setState(useSubCategory);
373
								stateSubCatData.putModifyingText(Language.ENGLISH(), "Use SubCategory");
374
								modifyingText += useSubCategory.toString() + ";";
375
								useRecord.addState(stateSubCatData);
376
								
377
							}
378
							if(lstUseRecord.get(5) != null && lstUseRecord.get(5).length() > 0) {
379
								Pager<DefinedTermBase> countryPager = termService.findByTitle(Modifier.class, lstUseRecord.get(5), null, null, null, null, null, null);
380
								Modifier country = null;
381
								if(countryPager.getCount() > 0) {
382
									country = (Modifier) countryPager.getRecords().get(0);
383
								} else {
384
									country = notAvailMod;
385
								}
386
								modifyingText += country.toString() + ";";
387
								useRecord.addModifier(country);
388
							} else {
389
								Modifier country = notAvailMod;
390
								modifyingText += country.toString() + ";";
391
								useRecord.addModifier(country);
392
							}
393
							
394
							if(lstUseRecord.get(6) != null && lstUseRecord.get(6).length() > 0) {
395
								Pager<DefinedTermBase> plantPartPager = termService.findByTitle(Modifier.class, lstUseRecord.get(6), null, null, null, null, null, null);
396
								Modifier plantPart = null;
397
								if(plantPartPager.getCount() > 0) {
398
									plantPart = (Modifier) plantPartPager.getRecords().get(0);
399
								} else {
400
									plantPart = notAvailMod;
401
								}
402
								modifyingText += plantPart.toString() + ";";
403
								useRecord.addModifier(plantPart);
404
							}else {
405
								Modifier plantPart = notAvailMod;
406
								modifyingText += plantPart.toString() + ";";
407
								useRecord.addModifier(plantPart);
408
							}
409
							if(lstUseRecord.get(7) != null && lstUseRecord.get(7).length() > 0) {
410
								Pager<DefinedTermBase> humanGroupPager = termService.findByTitle(Modifier.class, lstUseRecord.get(7), null, null, null, null, null, null);
411
								Modifier humanGroup = null;
412
								if(humanGroupPager.getCount() > 0) {
413
									humanGroup = (Modifier) humanGroupPager.getRecords().get(0);
414
								} else {
415
									humanGroup = notAvailMod;
416
								}
417
								modifyingText += humanGroup.toString() + ";";
418
								useRecord.addModifier(humanGroup);
419
							} else {
420
								Modifier humanGroup = notAvailMod;
421
								modifyingText += humanGroup.toString() + ";";
422
								useRecord.addModifier(humanGroup);
423
							}
424
							if(lstUseRecord.get(8) != null && lstUseRecord.get(8).length() > 0) {
425
								Pager<DefinedTermBase> ethnicGroupPager = termService.findByTitle(Modifier.class, lstUseRecord.get(8), null, null, null, null, null, null);
426
								Modifier ethnicGroup = null;
427
								if(ethnicGroupPager.getCount() > 0) {
428
									ethnicGroup = (Modifier) ethnicGroupPager.getRecords().get(0);
429
									modifyingText += ethnicGroup.toString() + ";";
430
								} else {
431
									ethnicGroup = notAvailMod;
432
								}
433
								useRecord.addModifier(ethnicGroup);
434
							}
435
							else {
436
								Modifier ethnicGroup = notAvailMod;
437
								modifyingText += ethnicGroup.toString() + ";";
438
								useRecord.addModifier(ethnicGroup);
439
							}
440
							useRecord.putModifyingText(Language.ENGLISH(), modifyingText);
441
							descService.saveDescriptionElement(useRecord);
442
							newUseDescription.addElement(useRecord);
443
						}
444
					}
445
					
446
					
447
					
448
					if (taxon.isInstanceOf(Synonym.class)){
449
						Taxon bestCandidate = null;
450
						Synonym synonym = CdmBase.deproxy(taxon, Synonym.class);
451
						Set<Taxon> acceptetdCandidates = synonym.getAcceptedTaxa();
452
						if(!acceptetdCandidates.isEmpty()){
453
							bestCandidate = acceptetdCandidates.iterator().next();
454
							if(acceptetdCandidates.size() == 1){
455
								logger.info(acceptetdCandidates.size() + " Accepted taxa found for synonym " + taxon.getTitleCache() + ", using first one: " + bestCandidate.getTitleCache());
456
								bestCandidate.addDescription(newUseDescription);
457
								taxonService.saveOrUpdate(bestCandidate);
458
								conversation.commit();
459
							}
460
							else {
461
								logger.info("using accepted Taxon " +  bestCandidate.getTitleCache() + "for synonym " + taxon.getTitleCache());
462
								bestCandidate.addDescription(newUseDescription);
463
								taxonService.saveOrUpdate(bestCandidate);
464
								conversation.commit();
465
							}
466
						}
467
					} else {
468
						Taxon taxonAccepted = (Taxon) taxon;
469
						taxonAccepted.addDescription(newUseDescription);
470
						taxonService.saveOrUpdate(taxonAccepted);
471
						conversation.commit();
472
					}
473
				}
474
				else {
475
					System.out.println("Processing UseSummary#: " + i + " ID:" + lstUseSummary.get(0));
476
				}
477
			}
478
			
479
			conversation.close();
480
			applicationController.close();
481
		
482
		} catch (Exception e) {
483
			success = false;
484
			e.printStackTrace();
485
		}
486
		return success;
487
		
488
	}
489
	
490
	//Completed and tested!
491
	private boolean loadTerms() {
492
		boolean success = true;
493
		
494
		//String xslPathString = "C://workspace//terms.xls";
495
		String xslPathString = "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//terms.xls";
496
		
497
		CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
498
		ConversationHolder conversation = applicationController.NewConversation();
499
		conversation.startTransaction();
500
		
501
		ITaxonService service = applicationController.getTaxonService();
502
		ITermService termService = applicationController.getTermService();
503
		IVocabularyService vocabularyService = applicationController.getVocabularyService();
504
		IReferenceService referenceService = applicationController.getReferenceService();	
505
		
506
		TermVocabulary<State> stateVocabulary =  (TermVocabulary<State>) vocabularyService.find(UUID.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931")); 
507
		TermVocabulary<Modifier> countryVocabulary = (TermVocabulary<Modifier>) vocabularyService.find(UUID.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));  
508
		TermVocabulary<Modifier> plantPartVocabulary = (TermVocabulary<Modifier>) vocabularyService.find(UUID.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));  
509
		TermVocabulary<Modifier> humanGroupVocabulary =  (TermVocabulary<Modifier>) vocabularyService.find(UUID.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
510
		
511
		IDescriptionService descService = applicationController.getDescriptionService();
512
		InputStream inputStream = null;
513
		
514
		try {
515
			inputStream = new FileInputStream(xslPathString);
516

    
517
		} catch (FileNotFoundException e) {
518
			success = false;
519
			System.out.println("File not found in the specified path.");
520
			e.printStackTrace();
521
		}
522

    
523
		POIFSFileSystem fileSystem = null;
524
		
525
		try {
526
			fileSystem = new POIFSFileSystem(inputStream);
527

    
528
			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
529
			HSSFSheet sheet = workBook.getSheetAt(0);
530
			Iterator rows = sheet.rowIterator();
531

    
532
			ArrayList<ArrayList<String>> lstUpdates = new ArrayList<ArrayList<String>>();
533
		
534
			while (rows.hasNext()) {
535

    
536
				HSSFRow row = (HSSFRow) rows.next();
537
				System.out.println("Row No.: " + row.getRowNum());
538
				Iterator cells = row.cellIterator();
539
				ArrayList<String> lstTerms = new ArrayList<String>();
540
				while (cells.hasNext()) {
541
					HSSFCell cell = (HSSFCell) cells.next();
542
				
543
					int intCellType = cell.getCellType();
544
					switch (intCellType) {
545
						case 0:
546
							int cellValue = (int) cell.getNumericCellValue(); 
547
							lstTerms.add(Integer.toString(cellValue));
548
							break;
549
						case 1:
550
							lstTerms.add(cell.getStringCellValue());
551
							break;
552
					}
553
				}
554
				lstUpdates.add(lstTerms);
555
				//lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));	
556
			}
557
			for (ArrayList<String> lstUpdate : lstUpdates) {
558
				int termType = Integer.parseInt(lstUpdate.get(0)); 
559
				switch (termType) {
560
				//Case 0 = UseCategory
561
				case 0:
562
					Pager<State> useCategoryPager = termService.findByRepresentationText(lstUpdate.get(1), State.class, null, null);
563
					State useCategory = null;
564
					State useSubCat = null;
565
					if (useCategoryPager.getCount()>0) {
566
						useCategory = useCategoryPager.getRecords().get(0);
567
					}
568
					if(useCategory == null) {
569
						useCategory = State.NewInstance(lstUpdate.get(1), lstUpdate.get(1), null);
570
					}
571
					//State useCategory = (State) termService.
572
					if(lstUpdate.size() > 2) {
573
						useSubCat = State.NewInstance(lstUpdate.get(2), lstUpdate.get(2), null);
574
						useCategory.addIncludes(useSubCat);
575
					}
576
					stateVocabulary.addTerm(useCategory);
577
					vocabularyService.saveOrUpdate(stateVocabulary);
578
					conversation.commit(true);
579
					break;
580
				
581
				//case 1: = HumanGroup
582
				case 1:
583
					Pager<Modifier> humanGroupPager = termService.findByRepresentationText(lstUpdate.get(1), Modifier.class, null, null);
584
					
585
					Modifier humanGroup = null;
586
					Modifier ethnicGroup = null;
587
					if(humanGroupPager.getCount()>0) {
588
						humanGroup = humanGroupPager.getRecords().get(0);
589
					}
590
					
591
					if(humanGroup == null) {
592
						humanGroup = Modifier.NewInstance(lstUpdate.get(1), lstUpdate.get(1), null);
593
					}
594
					
595
					if(lstUpdate.size() >2) {
596
						ethnicGroup = Modifier.NewInstance(lstUpdate.get(2), lstUpdate.get(2), null);
597
						humanGroup.addIncludes(ethnicGroup);
598
					}
599
					humanGroupVocabulary.addTerm(humanGroup);
600
					vocabularyService.saveOrUpdate(humanGroupVocabulary);
601
					conversation.commit(true);
602
					break;
603
				
604
				//case 2: = Country
605
				case 2:
606
					Pager<Modifier> countryPager = termService.findByRepresentationText(lstUpdate.get(1), Modifier.class, null, null);
607
					Modifier country = null;
608
					
609
					if(countryPager.getCount()>0) {
610
						country = countryPager.getRecords().get(0);
611
					}
612
					
613
					if(country == null) {
614
						country = Modifier.NewInstance(lstUpdate.get(1), lstUpdate.get(1), null);
615
						countryVocabulary.addTerm(country);
616
						vocabularyService.saveOrUpdate(countryVocabulary);
617
					}
618
					conversation.commit(true);
619
					break;
620
				
621
				//case 3: //plantPart
622
				case 3:
623
					Pager<Modifier> plantPartPager = termService.findByRepresentationText(lstUpdate.get(1), Modifier.class, null, null);
624
					Modifier plantPart = null;
625
					
626
					if(plantPartPager.getCount()>0) {
627
						plantPart = plantPartPager.getRecords().get(0);
628
					}
629
					
630
					if(plantPart == null) {
631
						plantPart = Modifier.NewInstance(lstUpdate.get(1), lstUpdate.get(1), null);
632
						plantPartVocabulary.addTerm(plantPart);
633
						vocabularyService.saveOrUpdate(plantPartVocabulary);
634
					}
635
					conversation.commit(true);
636
					break;
637
					
638
				}
639
			}
640
			conversation.close();	
641
			applicationController.close();
642
			
643
		} catch (IOException e) {
644
			success = false;
645
			e.printStackTrace();
646
		}
647
		return success;
648
		
649
	}
650
	
651
	private ArrayList<ArrayList<String>> loadSpreadsheet(String xslPathString) {
652
		ArrayList<ArrayList<String>> lstUpdates = new ArrayList<ArrayList<String>>();
653
		InputStream inputStream = null;
654
		
655
		try {
656
			inputStream = new FileInputStream(xslPathString);
657

    
658
		} catch (FileNotFoundException e) {
659
			System.out.println("File not found in the specified path.");
660
			e.printStackTrace();
661
		}
662
		
663
		POIFSFileSystem fileSystem = null;
664
		
665
		try {
666
			fileSystem = new POIFSFileSystem(inputStream);
667

    
668
			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
669
			HSSFSheet sheet = workBook.getSheetAt(0);
670
			Iterator rows = sheet.rowIterator();
671
			// Iterator rows = sheet.rowIterator();
672
			//Set<Integer> lstTaxonIDs;
673
		
674

    
675
			while (rows.hasNext()) {
676

    
677
				HSSFRow row = (HSSFRow) rows.next();
678
				System.out.println("Row No.: " + row.getRowNum());
679
				Iterator cells = row.cellIterator();
680
				ArrayList<String> lstTerms = new ArrayList<String>();
681
				while (cells.hasNext()) {
682
					HSSFCell cell = (HSSFCell) cells.next();
683
				
684
					int intCellType = cell.getCellType();
685
					switch (intCellType) {
686
						case 0:
687
							int cellValue = (int) cell.getNumericCellValue(); 
688
							lstTerms.add(Integer.toString(cellValue));
689
							break;
690
						case 1:
691
							lstTerms.add(cell.getStringCellValue());
692
							break;
693
					}
694
				}
695
				lstUpdates.add(lstTerms);
696
				//lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));	
697
			}
698
		} catch (IOException e) {
699
			e.printStackTrace();
700
		}
701
		return lstUpdates;
702
	}
703
	
704
	
705
	private boolean setupNecessaryItems() {
706
		boolean success = false;
707
		CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
708
		ConversationHolder conversation = applicationController.NewConversation();
709
		
710
		
711
		ITaxonService service = applicationController.getTaxonService();
712
		ITermService termService = applicationController.getTermService();
713
		IVocabularyService vocabularyService = applicationController.getVocabularyService();
714
		IFeatureTreeService featureTreeService = applicationController.getFeatureTreeService();
715
		
716
		MarkerType existingMarkertype = (MarkerType)termService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
717
		Feature featureUseRecord = (Feature) termService.find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
718
		Feature featureUseSummary = (Feature) termService.find(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
719
		TermVocabulary<State> stateVocabulary =  (TermVocabulary<State>) vocabularyService.find(UUID.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931")); 
720
		TermVocabulary<Modifier> countryVocabulary = (TermVocabulary<Modifier>) vocabularyService.find(UUID.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));  
721
		TermVocabulary<Modifier> plantPartVocabulary = (TermVocabulary<Modifier>) vocabularyService.find(UUID.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));  
722
		TermVocabulary<Modifier> humanGroupVocabulary =  (TermVocabulary<Modifier>) vocabularyService.find(UUID.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
723
		Pager<DefinedTermBase>  notAvailModPager = (Pager<DefinedTermBase> ) termService.findByTitle(Modifier.class, "N/A", null, null, null, null, null, null);
724
		Pager<DefinedTermBase>  notAvailStatePager = (Pager<DefinedTermBase> ) termService.findByTitle(State.class, "N/A", null, null, null, null, null, null);
725
		
726
		conversation.startTransaction();
727
		if (existingMarkertype == null) {
728
			existingMarkertype = MarkerType.NewInstance("use", "use", null);
729
			existingMarkertype.setUuid( UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
730
			TermVocabulary<MarkerType> markerTypeVocabulary = (TermVocabulary<MarkerType>)vocabularyService.find((UUID.fromString("19dffff7-e142-429c-a420-5d28e4ebe305")));
731
			markerTypeVocabulary.addTerm(existingMarkertype);
732
			vocabularyService.saveOrUpdate(markerTypeVocabulary);
733
			conversation.commit(true);
734
		}
735
		if (stateVocabulary == null) {
736
			
737
			URI termSourceUri = null;
738
			try {
739
				termSourceUri = new URI("eu.etaxonomy.cdm.model.description.State");
740
			} catch (URISyntaxException e) {
741
				e.printStackTrace();
742
			}
743
			stateVocabulary = TermVocabulary.NewInstance("Use Category", "Use Category", null, termSourceUri);
744
			stateVocabulary.setUuid(UUID.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
745
			vocabularyService.saveOrUpdate(stateVocabulary);
746
			conversation.commit(true);
747
		}
748
		if (countryVocabulary == null) {
749
			URI termSourceUri = null;
750
			try {
751
				termSourceUri = new URI("eu.etaxonomy.cdm.model.description.Modifier");
752
			} catch (URISyntaxException e) {
753
				e.printStackTrace();
754
			}
755
			countryVocabulary = TermVocabulary.NewInstance("Country", "Country", null, termSourceUri);
756
			countryVocabulary.setUuid(UUID.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
757
			
758
			vocabularyService.saveOrUpdate(countryVocabulary);
759
			conversation.commit(true);
760
		}
761
		if (plantPartVocabulary == null) {
762
			URI termSourceUri = null;
763
			try {
764
				termSourceUri = new URI("eu.etaxonomy.cdm.model.description.Modifier");
765
			} catch (URISyntaxException e) {
766
				e.printStackTrace();
767
			}
768
			plantPartVocabulary = TermVocabulary.NewInstance("Plant Part", "Plant Part", null, termSourceUri);
769
			plantPartVocabulary.setUuid(UUID.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
770
			vocabularyService.saveOrUpdate(plantPartVocabulary);
771
			conversation.commit(true);
772
		}
773
		if (humanGroupVocabulary == null) {
774
			URI termSourceUri = null;
775
			try {
776
				termSourceUri = new URI("eu.etaxonomy.cdm.model.description.Modifier");
777
			} catch (URISyntaxException e) {
778
				e.printStackTrace();
779
			}
780
			humanGroupVocabulary = TermVocabulary.NewInstance("Human Group", "Human Group", null, termSourceUri);
781
			humanGroupVocabulary.setUuid(UUID.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
782
			vocabularyService.saveOrUpdate(humanGroupVocabulary);
783
			conversation.commit(true);
784
		}
785
		if(featureUseRecord == null|| featureUseSummary == null) {
786
			TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)vocabularyService.find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
787
			FeatureTree palmWebFeatureTree = featureTreeService.find(UUID.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
788
			//List<FeatureTree> featureTrees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
789
			
790
			if (featureUseRecord == null ) {
791
				featureUseRecord = Feature.NewInstance("Use Record", "Use Record", null);
792
				featureUseRecord.setUuid(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
793
				featureUseRecord.isSupportsCategoricalData();
794
				featureUseRecord.setSupportsCategoricalData(true);
795
				featureVocabulary.addTerm(featureUseRecord);
796
				FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseRecord);
797
				palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
798
			}
799
			if (featureUseSummary == null) {
800
				featureUseSummary = Feature.NewInstance("Use", "Use", null);
801
				featureUseSummary.setUuid(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
802
				featureUseSummary.isSupportsTextData();
803
				featureUseSummary.setSupportsTextData(true);
804
				//TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)CdmStore.getService(IVocabularyService.class).find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
805
				featureVocabulary.addTerm(featureUseSummary);
806
				FeatureNode useSumFeatureNode = FeatureNode.NewInstance(featureUseSummary);
807
				palmWebFeatureTree.getRoot().addChild(useSumFeatureNode);
808
			}
809
			
810
			vocabularyService.saveOrUpdate(featureVocabulary);
811
			featureTreeService.saveOrUpdate(palmWebFeatureTree);
812
			conversation.commit(true);
813
			
814
		}
815
		if(notAvailModPager.getCount() == 0) {
816
			Modifier notAvailMod = Modifier.NewInstance("N/A", "N/A", null);
817
			termService.saveOrUpdate(notAvailMod);
818
			conversation.commit(true);
819
		}
820
		
821
		if(notAvailStatePager.getCount() == 0) {
822
			State notAvailState = State.NewInstance("N/A", "N/A", null);
823
			termService.saveOrUpdate(notAvailState);
824
			conversation.commit(true);
825
		}
826
		/*if(featureUseRecord == null) {
827
			featureUseRecord = Feature.NewInstance("Use Record", "Use Record", null);
828
			featureUseRecord.setUuid(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
829
			featureUseRecord.isSupportsCategoricalData();
830
			featureUseRecord.setSupportsCategoricalData(true);
831
			//TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)vocabularyService.find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
832
			featureVocabulary.addTerm(featureUseRecord);
833
			FeatureTree palmWebFeatureTree = featureTreeService.find(UUID.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
834
			FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseRecord);
835
			palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
836
			vocabularyService.saveOrUpdate(featureVocabulary);
837
			featureTreeService.saveOrUpdate(palmWebFeatureTree);
838
			conversation.commit(true);
839
			
840
		}
841
		if(featureUseSummary == null) {
842
			featureUseSummary = Feature.NewInstance("Use Summary", "Use Summary", null);
843
			featureUseSummary.setUuid(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
844
			featureUseSummary.isSupportsTextData();
845
			featureUseSummary.setSupportsTextData(true);
846
			//TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)vocabularyService.find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
847
			featureVocabulary.addTerm(featureUseSummary);
848
			FeatureTree palmWebFeatureTree = featureTreeService.find(UUID.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
849
			FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseSummary);
850
			palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
851
			vocabularyService.saveOrUpdate(featureVocabulary);
852
			featureTreeService.saveOrUpdate(palmWebFeatureTree);
853
			conversation.commit(true);
854
		}*/
855
		
856
		conversation.close();
857
		applicationController.close();
858
		
859
		return success;
860
	}
861

    
862
	public static void main(String[] args) {
863
		UseImport uiImport = new UseImport();
864
		// String xlsPath = ".//toload.xlsx";
865
		//String xlsPath = "C://workspace//CDM Trunk//UseImport//src//main//java//eu//etaxonomy//cdm//toLoad2.xls";
866
		
867
		uiImport.setupNecessaryItems();
868
		uiImport.loadTerms();
869
		uiImport.loadUses();
870
		//String xlsPath = "C://workspace//toLoad3.xls";
871
		//uiImport.importFromExcelSS(xlsPath);
872

    
873
	}
874
}
(7-7/7)