Project

General

Profile

Download (37.6 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.CdmApplicationDefaultController;
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.DefinedTerm;
48
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
49
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
50
import eu.etaxonomy.cdm.model.common.Language;
51
import eu.etaxonomy.cdm.model.common.LanguageString;
52
import eu.etaxonomy.cdm.model.common.Marker;
53
import eu.etaxonomy.cdm.model.common.MarkerType;
54
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
55
import eu.etaxonomy.cdm.model.common.TermType;
56
import eu.etaxonomy.cdm.model.common.TermVocabulary;
57
import eu.etaxonomy.cdm.model.common.TimePeriod;
58
import eu.etaxonomy.cdm.model.description.CategoricalData;
59
import eu.etaxonomy.cdm.model.description.Feature;
60
import eu.etaxonomy.cdm.model.description.FeatureNode;
61
import eu.etaxonomy.cdm.model.description.FeatureTree;
62
import eu.etaxonomy.cdm.model.description.State;
63
import eu.etaxonomy.cdm.model.description.StateData;
64
import eu.etaxonomy.cdm.model.description.TaxonDescription;
65
import eu.etaxonomy.cdm.model.description.TextData;
66
import eu.etaxonomy.cdm.model.location.NamedArea;
67
import eu.etaxonomy.cdm.model.reference.Reference;
68
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
69
import eu.etaxonomy.cdm.model.taxon.Synonym;
70
import eu.etaxonomy.cdm.model.taxon.Taxon;
71
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
72

    
73
public class UseImport {
74
	public static final Logger logger = Logger.getLogger(UseImport.class);
75

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

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

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

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

    
127
		POIFSFileSystem fileSystem = null;
128
		
129
		try {
130
			fileSystem = new POIFSFileSystem(inputStream);
131

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

    
141
			while (rows.hasNext()) {
142

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

    
256
		return success;
257

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

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

    
526
		POIFSFileSystem fileSystem = null;
527
		
528
		try {
529
			fileSystem = new POIFSFileSystem(inputStream);
530

    
531
			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
532
			HSSFSheet sheet = workBook.getSheetAt(0);
533
			Iterator rows = sheet.rowIterator();
534

    
535
			ArrayList<ArrayList<String>> lstUpdates = new ArrayList<ArrayList<String>>();
536
		
537
			while (rows.hasNext()) {
538

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

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

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

    
678
			while (rows.hasNext()) {
679

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

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

    
876
	}
877
}
(7-7/7)