Project

General

Profile

Download (35.4 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.openxml4j.exceptions.InvalidFormatException;
27
import org.apache.poi.ss.usermodel.Cell;
28
import org.apache.poi.ss.usermodel.Row;
29
import org.apache.poi.ss.usermodel.Sheet;
30
import org.apache.poi.ss.usermodel.Workbook;
31
import org.apache.poi.ss.usermodel.WorkbookFactory;
32

    
33
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
34
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
35
import eu.etaxonomy.cdm.api.service.IDescriptionService;
36
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
37
import eu.etaxonomy.cdm.api.service.IReferenceService;
38
import eu.etaxonomy.cdm.api.service.ITaxonService;
39
import eu.etaxonomy.cdm.api.service.ITermService;
40
import eu.etaxonomy.cdm.api.service.IVocabularyService;
41
import eu.etaxonomy.cdm.api.service.pager.Pager;
42
import eu.etaxonomy.cdm.common.AccountStore;
43
import eu.etaxonomy.cdm.database.CdmDataSource;
44
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
45
import eu.etaxonomy.cdm.database.ICdmDataSource;
46
import eu.etaxonomy.cdm.model.agent.Team;
47
import eu.etaxonomy.cdm.model.common.CdmBase;
48
import eu.etaxonomy.cdm.model.common.DefinedTerm;
49
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
50
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
51
import eu.etaxonomy.cdm.model.common.Language;
52
import eu.etaxonomy.cdm.model.common.LanguageString;
53
import eu.etaxonomy.cdm.model.common.Marker;
54
import eu.etaxonomy.cdm.model.common.MarkerType;
55
import eu.etaxonomy.cdm.model.common.TermType;
56
import eu.etaxonomy.cdm.model.common.TermVocabulary;
57
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
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);
95
		} else if (dbType.equals(DatabaseTypeEnum.PostgreSQL)) {
96
			destination = CdmDataSource.NewPostgreSQLInstance(cdmServer, cdmDB,
97
					port, cdmUserName, pwd);
98
		} else {
99
			// TODO others
100
			throw new RuntimeException("Unsupported DatabaseType");
101
		}
102
		return destination;
103
	}
104

    
105
	public boolean importFromExcelSS(String xlsPath) throws InvalidFormatException {
106
		boolean success = true;
107

    
108
		CdmApplicationController applicationController = CdmApplicationController.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
			Workbook workBook = WorkbookFactory.create(inputStream);
134

    
135

    
136
			Sheet sheet = workBook.getSheetAt(0);
137
			Iterator<Row> rows = sheet.rowIterator();
138
			// Iterator rows = sheet.rowIterator();
139
			ArrayList<ArrayList<String>> lstUpdates = new ArrayList<ArrayList<String>>();
140
			Set<Integer> lstTaxonIDs = new HashSet<Integer>();
141
			//Set<Integer> lstTaxonIDs;
142

    
143

    
144
			while (rows.hasNext()) {
145

    
146
				Row row = rows.next();
147
				System.out.println("Row No.: " + row.getRowNum());
148
				Iterator<Cell> cells = row.cellIterator();
149
				ArrayList<String> lstTaxon = new ArrayList<String>();
150
				while (cells.hasNext()) {
151
					Cell cell = cells.next();
152

    
153
					int intCellType = cell.getCellType();
154
					switch (intCellType) {
155
						case 0:
156
							int cellValue = (int) cell.getNumericCellValue();
157
							lstTaxon.add(Integer.toString(cellValue));
158
							break;
159
						case 1:
160
							lstTaxon.add(cell.getStringCellValue());
161
							break;
162
					}
163
				}
164
				lstUpdates.add(lstTaxon);
165
				lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));
166
			}
167

    
168
			List<TaxonBase> taxa = service.findTaxaByID(lstTaxonIDs);
169
			for(TaxonBase idTaxa : taxa) {
170
				//System.out.println(idTaxa.getUuid().toString());
171
				System.out.println(idTaxa.getName());
172
			}
173

    
174

    
175
			MarkerType useMarkerType = (MarkerType) termService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
176
			Marker useMarker = Marker.NewInstance(useMarkerType, true);
177
			for (ArrayList<String> lstUpdate : lstUpdates) {
178
				System.out.println("-----------------------------------------------: " + lstUpdate.get(1));
179
				String idTaxonToUpdate = lstUpdate.get(1);
180
				TaxonDescription newUseDescription = TaxonDescription.NewInstance();
181
				newUseDescription.addMarker(useMarker);
182
				newUseDescription.setTitleCache(lstUpdate.get(2));
183
				Reference citation = ReferenceFactory.newGeneric();
184
				Team authorTeam = Team.NewInstance();
185
				authorTeam.setTitleCache(lstUpdate.get(3));
186
				citation.setAuthorship(authorTeam);
187
				citation.setTitle(lstUpdate.get(4));
188

    
189
				//citation.
190
				VerbatimTimePeriod year = VerbatimTimePeriod.NewVerbatimInstance(Integer.parseInt(lstUpdate.get(5)));
191
				citation.setDatePublished(year);
192
				citation.setTitleCache(lstUpdate.get(6), true);
193
				//citation.
194
				for(TaxonBase taxon : taxa) {
195
					String taxonUUID = taxon.getUuid().toString();
196
					//System.out.println(idTaxonToUpdate + "|" + taxonUUID);
197
					if(idTaxonToUpdate.equals(taxonUUID)) {
198
						logger.info("Processing Taxn " + taxon.getTitleCache() + " with UUID: " + taxon.getUuid());
199
						if(taxon.isInstanceOf(Synonym.class)) {
200
							Synonym synonym = CdmBase.deproxy(taxon, Synonym.class);
201
							Taxon acceptetdTaxon = synonym.getAcceptedTaxon();
202
							if(acceptetdTaxon != null){
203
								Set<TaxonDescription> taxonDescriptions = acceptetdTaxon.getDescriptions();
204
								if(!taxonDescriptions.isEmpty()) {
205
									TaxonDescription firstDescription = taxonDescriptions.iterator().next();
206
									//newUseSummary.addSource(null, null, citation, null);
207
									//firstDescription.addElement(newUseSummary);
208
								}
209
								else {
210
									logger.warn("No description container for: " + acceptetdTaxon.getName());
211
								}
212
							}
213
						}
214
						else {
215
							Taxon taxonAccepted = (Taxon) taxon;
216
							/*Set<TaxonDescription> taxonDescriptions = taxonAccepted.getDescriptions();
217
							if(!taxonDescriptions.isEmpty()) {
218
								TaxonDescription firstDescription = taxonDescriptions.iterator().next();
219
								//newUseSummary.addSource(null, null, citation, null);
220
								//firstDescription.addElement(newUseSummary);
221
							}
222
							else {
223
								logger.warn("No description container for: " + taxonAccepted.getName());
224
							}*/
225
							taxonAccepted.addDescription(newUseDescription);
226
							service.saveOrUpdate(taxonAccepted);
227

    
228
						}
229
					}
230
				}
231

    
232
			}
233
			conversation.commit(false);
234

    
235
		} catch (IOException e) {
236
			success = false;
237
			e.printStackTrace();
238
		}
239

    
240
		return success;
241

    
242
	}
243

    
244
	private boolean loadUses() throws InvalidFormatException {
245
		boolean success = true;
246
		//String xslUseSummaryPathString = "C://workspace//Matched_UseSummary_referenceIdTaxEd_TaxonName.xls";
247
		//String xslUseSummaryPathString = "C://workspace//testUseSummaries.xls";
248

    
249

    
250
		String xslUseSummaryPathString = "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//Matched_UseSummary_referenceIdTaxEd_TaxonName.xls";
251

    
252
		//String xslUseRecordPathString = "C://workspace//UseRecordTerms_UseSummaryId.xls";
253
		//String xslUseRecordPathString = "C://workspace//testUseRecords.xls";
254
		//String xslUseRecordPathString = "C://workspace//test_useRecord.xls";
255
		String xslUseRecordPathString = "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//UseRecordTerms_UseSummaryId.xls";
256

    
257
		InputStream inputStream = null;
258

    
259

    
260
		CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
261
		ConversationHolder conversation = applicationController.NewConversation();
262
		conversation.startTransaction();
263

    
264
		ITaxonService taxonService = applicationController.getTaxonService();
265
		ITermService termService = applicationController.getTermService();
266
		IDescriptionService descService = applicationController.getDescriptionService();
267
		IReferenceService referenceService = applicationController.getReferenceService();
268

    
269

    
270
		ArrayList<ArrayList<String>> lstUseSummaries = loadSpreadsheet(xslUseSummaryPathString);
271
		ArrayList<ArrayList<String>> lstUseRecords = loadSpreadsheet(xslUseRecordPathString);
272

    
273
		MarkerType useMarkerType = (MarkerType) termService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
274
		Feature featureUseRecord = (Feature) termService.find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
275
		Feature featureUseSummary = (Feature) termService.find(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
276
		Pager<DefinedTerm>  notAvailModPager = termService.findByTitle(DefinedTerm.class, "N/A", null, null, null, null, null, null);
277
		Pager<State>  notAvailStatePager = termService.findByTitle(State.class, "N/A", null, null, null, null, null, null);
278
		DefinedTerm notAvailMod = notAvailModPager.getRecords().get(0);
279
		State notAvailState = notAvailStatePager.getRecords().get(0);
280

    
281
		int i = 0;
282
		int j = 0;
283
		try {
284
			for (ArrayList<String> lstUseSummary : lstUseSummaries) {
285
				i++;
286
				String idTaxonToUpdate = lstUseSummary.get(3);
287
				TaxonBase<?> taxon = taxonService.find(UUID.fromString(idTaxonToUpdate));
288
				if (taxon != null) {
289
					TaxonDescription newUseDescription = TaxonDescription.NewInstance();
290
					Marker useMarker = Marker.NewInstance(useMarkerType, true);
291
					newUseDescription.addMarker(useMarker);
292
					Reference useReference = null;
293
					Pager<Reference> reference = referenceService.findByTitle(Reference.class, lstUseSummary.get(5), null, null, null, null, null, null);
294
					if(reference.getCount() == 0) {
295
						System.out.println("Reference title: " + lstUseSummary.get(5) + " not found.");
296
					} else if(reference.getCount() > 0 ) {
297
						useReference = reference.getRecords().get(0);
298
					}
299
					IdentifiableSource source =IdentifiableSource.NewPrimarySourceInstance(useReference, null);  //is type correct?
300
					source.setOriginalNameString(taxon.getName().toString());
301
					newUseDescription.addSource(source);
302
					TextData useSummary = TextData.NewInstance(featureUseSummary);
303
					LanguageString languageString = LanguageString.NewInstance(lstUseSummary.get(1), Language.ENGLISH());
304
					useSummary.putText(languageString);
305
					descService.saveDescriptionElement(useSummary);
306
					newUseDescription.addElement(useSummary);
307
					for (ArrayList<String> lstUseRecord : lstUseRecords) {
308
						j++;
309
						//System.out.println("Processing UseSummary#: " + i + " ID:" + lstUseSummary.get(0) + "UseRecord: " + lstUseRecord.get(1));
310
						if(lstUseSummary.get(0).equals(lstUseRecord.get(0))) {
311
							CategoricalData useRecord = CategoricalData.NewInstance();
312
							useRecord.setFeature(featureUseRecord);
313
							String modifyingText = "";
314
							if(lstUseRecord.get(3) != null && lstUseRecord.get(3).length() > 0) {
315
								Pager<State> useCategoryPager = termService.findByTitle(State.class, lstUseRecord.get(3), null, null, null, null, null, null);
316
								State useCategory = null;
317
								if(useCategoryPager.getCount() > 0) {
318
									useCategory = useCategoryPager.getRecords().get(0);
319
								} else {
320
									useCategory = notAvailState;
321
								}
322
								StateData stateCatData = StateData.NewInstance(useCategory);
323
								stateCatData.setState(useCategory);
324
								stateCatData.putModifyingText(Language.ENGLISH(), "Use Category");
325
								modifyingText += useCategory.toString() + ";";
326
								useRecord.addStateData(stateCatData);
327

    
328

    
329
								//useRecord.addState(stateData);
330
							} else {
331
								State useCategory = notAvailState;
332
								StateData stateCatData = StateData.NewInstance(useCategory);
333
								stateCatData.setState(useCategory);
334
								stateCatData.putModifyingText(Language.ENGLISH(), "Use Category");
335
								modifyingText += useCategory.toString() + ";";
336
								useRecord.addStateData(stateCatData);
337

    
338
							}
339

    
340
							if(lstUseRecord.get(4) != null && lstUseRecord.get(4).length() > 0) {
341
								Pager<State> useSubCategoryPager = termService.findByTitle(State.class, lstUseRecord.get(4), null, null, null, null, null, null);
342
								State useSubCategory = null;
343
								if(useSubCategoryPager.getCount() > 0) {
344
									useSubCategory = useSubCategoryPager.getRecords().get(0);
345

    
346
								} else {
347
									useSubCategory = notAvailState;
348
								}
349
								StateData stateSubCatData = StateData.NewInstance(useSubCategory);
350
								stateSubCatData.setState(useSubCategory);
351
								stateSubCatData.putModifyingText(Language.ENGLISH(), "Use SubCategory");
352
								modifyingText += useSubCategory.toString() + ";";
353
								useRecord.addStateData(stateSubCatData);
354

    
355
							}
356
							else {
357
								State useSubCategory = notAvailState;
358
								StateData stateSubCatData = StateData.NewInstance(useSubCategory);
359
								stateSubCatData.setState(useSubCategory);
360
								stateSubCatData.putModifyingText(Language.ENGLISH(), "Use SubCategory");
361
								modifyingText += useSubCategory.toString() + ";";
362
								useRecord.addStateData(stateSubCatData);
363

    
364
							}
365
							if(lstUseRecord.get(5) != null && lstUseRecord.get(5).length() > 0) {
366
								Pager<DefinedTerm> countryPager = termService.findByTitle(DefinedTerm.class, lstUseRecord.get(5), null, null, null, null, null, null);
367
								DefinedTerm country = null;
368
								if(countryPager.getCount() > 0) {
369
									country = countryPager.getRecords().get(0);
370
								} else {
371
									country = notAvailMod;
372
								}
373
								modifyingText += country.toString() + ";";
374
								useRecord.addModifier(country);
375
							} else {
376
								DefinedTerm country = notAvailMod;
377
								modifyingText += country.toString() + ";";
378
								useRecord.addModifier(country);
379
							}
380

    
381
							if(lstUseRecord.get(6) != null && lstUseRecord.get(6).length() > 0) {
382
								Pager<DefinedTerm> plantPartPager = termService.findByTitle(DefinedTerm.class, lstUseRecord.get(6), null, null, null, null, null, null);
383
								DefinedTerm plantPart = null;
384
								if(plantPartPager.getCount() > 0) {
385
									plantPart = plantPartPager.getRecords().get(0);
386
								} else {
387
									plantPart = notAvailMod;
388
								}
389
								modifyingText += plantPart.toString() + ";";
390
								useRecord.addModifier(plantPart);
391
							}else {
392
								DefinedTerm plantPart = notAvailMod;
393
								modifyingText += plantPart.toString() + ";";
394
								useRecord.addModifier(plantPart);
395
							}
396
							if(lstUseRecord.get(7) != null && lstUseRecord.get(7).length() > 0) {
397
								Pager<DefinedTerm> humanGroupPager = termService.findByTitle(DefinedTerm.class, lstUseRecord.get(7), null, null, null, null, null, null);
398
								DefinedTerm humanGroup = null;
399
								if(humanGroupPager.getCount() > 0) {
400
									humanGroup = humanGroupPager.getRecords().get(0);
401
								} else {
402
									humanGroup = notAvailMod;
403
								}
404
								modifyingText += humanGroup.toString() + ";";
405
								useRecord.addModifier(humanGroup);
406
							} else {
407
								DefinedTerm humanGroup = notAvailMod;
408
								modifyingText += humanGroup.toString() + ";";
409
								useRecord.addModifier(humanGroup);
410
							}
411
							if(lstUseRecord.get(8) != null && lstUseRecord.get(8).length() > 0) {
412
								Pager<DefinedTerm> ethnicGroupPager = termService.findByTitle(DefinedTerm.class, lstUseRecord.get(8), null, null, null, null, null, null);
413
								DefinedTerm ethnicGroup = null;
414
								if(ethnicGroupPager.getCount() > 0) {
415
									ethnicGroup = ethnicGroupPager.getRecords().get(0);
416
									modifyingText += ethnicGroup.toString() + ";";
417
								} else {
418
									ethnicGroup = notAvailMod;
419
								}
420
								useRecord.addModifier(ethnicGroup);
421
							}
422
							else {
423
								DefinedTerm ethnicGroup = notAvailMod;
424
								modifyingText += ethnicGroup.toString() + ";";
425
								useRecord.addModifier(ethnicGroup);
426
							}
427
							useRecord.putModifyingText(Language.ENGLISH(), modifyingText);
428
							descService.saveDescriptionElement(useRecord);
429
							newUseDescription.addElement(useRecord);
430
						}
431
					}
432

    
433

    
434

    
435
					if (taxon.isInstanceOf(Synonym.class)){
436
						Taxon bestCandidate = null;
437
						Synonym synonym = CdmBase.deproxy(taxon, Synonym.class);
438
						Taxon acceptedTaxon = synonym.getAcceptedTaxon();
439
						if(acceptedTaxon != null){
440
						    acceptedTaxon.addDescription(newUseDescription);
441
							taxonService.saveOrUpdate(bestCandidate);
442
							conversation.commit();
443
						}
444
					} else {
445
						Taxon taxonAccepted = (Taxon) taxon;
446
						taxonAccepted.addDescription(newUseDescription);
447
						taxonService.saveOrUpdate(taxonAccepted);
448
						conversation.commit();
449
					}
450
				}
451
				else {
452
					System.out.println("Processing UseSummary#: " + i + " ID:" + lstUseSummary.get(0));
453
				}
454
			}
455

    
456
			conversation.close();
457
			applicationController.close();
458

    
459
		} catch (Exception e) {
460
			success = false;
461
			e.printStackTrace();
462
		}
463
		return success;
464

    
465
	}
466

    
467
	//Completed and tested!
468
	private boolean loadTerms() throws InvalidFormatException {
469
		boolean success = true;
470

    
471
		//String xslPathString = "C://workspace//terms.xls";
472
		String xslPathString = "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//terms.xls";
473

    
474
		CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
475
		ConversationHolder conversation = applicationController.NewConversation();
476
		conversation.startTransaction();
477

    
478
		ITaxonService service = applicationController.getTaxonService();
479
		ITermService termService = applicationController.getTermService();
480
		IVocabularyService vocabularyService = applicationController.getVocabularyService();
481
		IReferenceService referenceService = applicationController.getReferenceService();
482

    
483
		TermVocabulary<State> stateVocabulary =  vocabularyService.find(UUID.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
484
		TermVocabulary<DefinedTermBase<?>> countryVocabulary = vocabularyService.find(UUID.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
485
		TermVocabulary<DefinedTerm> plantPartVocabulary = vocabularyService.find(UUID.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
486
		TermVocabulary<DefinedTerm> humanGroupVocabulary =  vocabularyService.find(UUID.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
487

    
488
		IDescriptionService descService = applicationController.getDescriptionService();
489
		InputStream inputStream = null;
490

    
491
		try {
492
			inputStream = new FileInputStream(xslPathString);
493

    
494
		} catch (FileNotFoundException e) {
495
			success = false;
496
			System.out.println("File not found in the specified path.");
497
			e.printStackTrace();
498
		}
499

    
500

    
501
		try {
502
//			POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
503
//			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
504

    
505
			Workbook workBook = WorkbookFactory.create(inputStream);
506

    
507

    
508
			Sheet sheet = workBook.getSheetAt(0);
509
			Iterator<Row> rows = sheet.rowIterator();
510

    
511
			ArrayList<ArrayList<String>> lstUpdates = new ArrayList<ArrayList<String>>();
512

    
513
			while (rows.hasNext()) {
514

    
515
				Row row = rows.next();
516
				System.out.println("Row No.: " + row.getRowNum());
517
				Iterator<Cell> cells = row.cellIterator();
518
				ArrayList<String> lstTerms = new ArrayList<String>();
519
				while (cells.hasNext()) {
520
					Cell cell = cells.next();
521

    
522
					int intCellType = cell.getCellType();
523
					switch (intCellType) {
524
						case 0:
525
							int cellValue = (int) cell.getNumericCellValue();
526
							lstTerms.add(Integer.toString(cellValue));
527
							break;
528
						case 1:
529
							lstTerms.add(cell.getStringCellValue());
530
							break;
531
					}
532
				}
533
				lstUpdates.add(lstTerms);
534
				//lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));
535
			}
536
			for (ArrayList<String> lstUpdate : lstUpdates) {
537
				int termType = Integer.parseInt(lstUpdate.get(0));
538
				switch (termType) {
539
				//Case 0 = UseCategory
540
				case 0:
541
					Pager<State> useCategoryPager = termService.findByRepresentationText(lstUpdate.get(1), State.class, null, null);
542
					State useCategory = null;
543
					State useSubCat = null;
544
					if (useCategoryPager.getCount()>0) {
545
						useCategory = useCategoryPager.getRecords().get(0);
546
					}
547
					if(useCategory == null) {
548
						useCategory = State.NewInstance(lstUpdate.get(1), lstUpdate.get(1), null);
549
					}
550
					//State useCategory = (State) termService.
551
					if(lstUpdate.size() > 2) {
552
						useSubCat = State.NewInstance(lstUpdate.get(2), lstUpdate.get(2), null);
553
						useCategory.addIncludes(useSubCat);
554
					}
555
					stateVocabulary.addTerm(useCategory);
556
					vocabularyService.saveOrUpdate(stateVocabulary);
557
					conversation.commit(true);
558
					break;
559

    
560
				//case 1: = HumanGroup
561
				case 1:
562
					Pager<DefinedTerm> humanGroupPager = termService.findByRepresentationText(lstUpdate.get(1), DefinedTerm.class, null, null);
563

    
564
					DefinedTerm humanGroup = null;
565
					DefinedTerm ethnicGroup = null;
566
					if(humanGroupPager.getCount()>0) {
567
						humanGroup = humanGroupPager.getRecords().get(0);
568
					}
569

    
570
					if(humanGroup == null) {
571
						humanGroup = DefinedTerm.NewModifierInstance(lstUpdate.get(1), lstUpdate.get(1), null);
572
					}
573

    
574
					if(lstUpdate.size() >2) {
575
						ethnicGroup = DefinedTerm.NewModifierInstance(lstUpdate.get(2), lstUpdate.get(2), null);
576
						humanGroup.addIncludes(ethnicGroup);
577
					}
578
					humanGroupVocabulary.addTerm(humanGroup);
579
					vocabularyService.saveOrUpdate(humanGroupVocabulary);
580
					conversation.commit(true);
581
					break;
582

    
583
				//case 2: = Country
584
				case 2:
585
					Pager<DefinedTerm> countryPager = termService.findByRepresentationText(lstUpdate.get(1), DefinedTerm.class, null, null);
586
					DefinedTermBase country = null;
587

    
588
					if(countryPager.getCount()>0) {
589
						country = countryPager.getRecords().get(0);
590
					}
591

    
592
					if(country == null) {
593
						country = NamedArea.NewInstance(lstUpdate.get(1), lstUpdate.get(1), null);
594
						countryVocabulary.addTerm(country);
595
						vocabularyService.saveOrUpdate(countryVocabulary);
596
					}
597
					conversation.commit(true);
598
					break;
599

    
600
				//case 3: //plantPart
601
				case 3:
602
					Pager<DefinedTerm> plantPartPager = termService.findByRepresentationText(lstUpdate.get(1), DefinedTerm.class, null, null);
603
					DefinedTerm plantPart = null;
604

    
605
					if(plantPartPager.getCount()>0) {
606
						plantPart = plantPartPager.getRecords().get(0);
607
					}
608

    
609
					if(plantPart == null) {
610
						plantPart = DefinedTerm.NewModifierInstance(lstUpdate.get(1), lstUpdate.get(1), null);
611
						plantPartVocabulary.addTerm(plantPart);
612
						vocabularyService.saveOrUpdate(plantPartVocabulary);
613
					}
614
					conversation.commit(true);
615
					break;
616

    
617
				}
618
			}
619
			conversation.close();
620
			applicationController.close();
621

    
622
		} catch (IOException e) {
623
			success = false;
624
			e.printStackTrace();
625
		}
626
		return success;
627

    
628
	}
629

    
630
	private ArrayList<ArrayList<String>> loadSpreadsheet(String xslPathString) throws InvalidFormatException {
631
		ArrayList<ArrayList<String>> lstUpdates = new ArrayList<ArrayList<String>>();
632
		InputStream inputStream = null;
633

    
634
		try {
635
			inputStream = new FileInputStream(xslPathString);
636

    
637
		} catch (FileNotFoundException e) {
638
			System.out.println("File not found in the specified path.");
639
			e.printStackTrace();
640
		}
641

    
642
		try {
643
//			POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
644
//			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
645

    
646
			Workbook workBook = WorkbookFactory.create(inputStream);
647

    
648

    
649
			Sheet sheet = workBook.getSheetAt(0);
650
			Iterator<Row> rows = sheet.rowIterator();
651
			// Iterator rows = sheet.rowIterator();
652
			//Set<Integer> lstTaxonIDs;
653

    
654

    
655
			while (rows.hasNext()) {
656

    
657
				Row row = rows.next();
658
				System.out.println("Row No.: " + row.getRowNum());
659
				Iterator<Cell> cells = row.cellIterator();
660
				ArrayList<String> lstTerms = new ArrayList<String>();
661
				while (cells.hasNext()) {
662
					Cell cell = cells.next();
663

    
664
					int intCellType = cell.getCellType();
665
					switch (intCellType) {
666
						case 0:
667
							int cellValue = (int) cell.getNumericCellValue();
668
							lstTerms.add(Integer.toString(cellValue));
669
							break;
670
						case 1:
671
							lstTerms.add(cell.getStringCellValue());
672
							break;
673
					}
674
				}
675
				lstUpdates.add(lstTerms);
676
				//lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));
677
			}
678
		} catch (IOException e) {
679
			e.printStackTrace();
680
		}
681
		return lstUpdates;
682
	}
683

    
684

    
685
	private boolean setupNecessaryItems() {
686
		boolean success = false;
687
		CdmApplicationController applicationController = CdmApplicationController.NewInstance(dataSource());
688
		ConversationHolder conversation = applicationController.NewConversation();
689

    
690

    
691
		ITaxonService service = applicationController.getTaxonService();
692
		ITermService termService = applicationController.getTermService();
693
		IVocabularyService vocabularyService = applicationController.getVocabularyService();
694
		IFeatureTreeService featureTreeService = applicationController.getFeatureTreeService();
695

    
696
		MarkerType existingMarkertype = (MarkerType)termService.find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
697
		Feature featureUseRecord = (Feature) termService.find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
698
		Feature featureUseSummary = (Feature) termService.find(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
699
		TermVocabulary<State> stateVocabulary =  vocabularyService.find(UUID.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
700
		TermVocabulary<DefinedTerm> countryVocabulary = vocabularyService.find(UUID.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
701
		TermVocabulary<DefinedTerm> plantPartVocabulary = vocabularyService.find(UUID.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
702
		TermVocabulary<DefinedTerm> humanGroupVocabulary =  vocabularyService.find(UUID.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
703
		Pager<DefinedTerm>  notAvailModPager = termService.findByTitle(DefinedTerm.class, "N/A", null, null, null, null, null, null);
704
		Pager<State>  notAvailStatePager = termService.findByTitle(State.class, "N/A", null, null, null, null, null, null);
705

    
706
		conversation.startTransaction();
707
		if (existingMarkertype == null) {
708
			existingMarkertype = MarkerType.NewInstance("use", "use", null);
709
			existingMarkertype.setUuid( UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
710
			TermVocabulary<MarkerType> markerTypeVocabulary = vocabularyService.find((UUID.fromString("19dffff7-e142-429c-a420-5d28e4ebe305")));
711
			markerTypeVocabulary.addTerm(existingMarkertype);
712
			vocabularyService.saveOrUpdate(markerTypeVocabulary);
713
			conversation.commit(true);
714
		}
715
		if (stateVocabulary == null) {
716

    
717
			URI termSourceUri = null;
718
			try {
719
				termSourceUri = new URI("eu.etaxonomy.cdm.model.description.State");
720
			} catch (URISyntaxException e) {
721
				e.printStackTrace();
722
			}
723
			stateVocabulary = TermVocabulary.NewInstance(TermType.State, "Use Category", "Use Category", null, termSourceUri);
724
			stateVocabulary.setUuid(UUID.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
725
			vocabularyService.saveOrUpdate(stateVocabulary);
726
			conversation.commit(true);
727
		}
728
		if (countryVocabulary == null) {
729
			URI termSourceUri = null;
730
			try {
731
				termSourceUri = new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
732
			} catch (URISyntaxException e) {
733
				e.printStackTrace();
734
			}
735
			countryVocabulary = TermVocabulary.NewInstance(TermType.NamedArea, "Country", "Country", null, termSourceUri);
736
			countryVocabulary.setUuid(UUID.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
737

    
738
			vocabularyService.saveOrUpdate(countryVocabulary);
739
			conversation.commit(true);
740
		}
741
		if (plantPartVocabulary == null) {
742
			URI termSourceUri = null;
743
			try {
744
				termSourceUri = new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
745
			} catch (URISyntaxException e) {
746
				e.printStackTrace();
747
			}
748
			plantPartVocabulary = TermVocabulary.NewInstance(TermType.Modifier, "Plant Part", "Plant Part", null, termSourceUri);
749
			plantPartVocabulary.setUuid(UUID.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
750
			vocabularyService.saveOrUpdate(plantPartVocabulary);
751
			conversation.commit(true);
752
		}
753
		if (humanGroupVocabulary == null) {
754
			URI termSourceUri = null;
755
			try {
756
				termSourceUri = new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
757
			} catch (URISyntaxException e) {
758
				e.printStackTrace();
759
			}
760
			humanGroupVocabulary = TermVocabulary.NewInstance(TermType.Modifier, "Human Group", "Human Group", null, termSourceUri);
761
			humanGroupVocabulary.setUuid(UUID.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
762
			vocabularyService.saveOrUpdate(humanGroupVocabulary);
763
			conversation.commit(true);
764
		}
765
		if(featureUseRecord == null|| featureUseSummary == null) {
766
			TermVocabulary<Feature> featureVocabulary = vocabularyService.find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
767
			FeatureTree palmWebFeatureTree = featureTreeService.find(UUID.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
768
			//List<FeatureTree> featureTrees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
769

    
770
			if (featureUseRecord == null ) {
771
				featureUseRecord = Feature.NewInstance("Use Record", "Use Record", null);
772
				featureUseRecord.setUuid(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
773
				featureUseRecord.isSupportsCategoricalData();
774
				featureUseRecord.setSupportsCategoricalData(true);
775
				featureVocabulary.addTerm(featureUseRecord);
776
				FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseRecord);
777
				palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
778
			}
779
			if (featureUseSummary == null) {
780
				featureUseSummary = Feature.NewInstance("Use", "Use", null);
781
				featureUseSummary.setUuid(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
782
				featureUseSummary.isSupportsTextData();
783
				featureUseSummary.setSupportsTextData(true);
784
				//TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)CdmStore.getService(IVocabularyService.class).find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
785
				featureVocabulary.addTerm(featureUseSummary);
786
				FeatureNode useSumFeatureNode = FeatureNode.NewInstance(featureUseSummary);
787
				palmWebFeatureTree.getRoot().addChild(useSumFeatureNode);
788
			}
789

    
790
			vocabularyService.saveOrUpdate(featureVocabulary);
791
			featureTreeService.saveOrUpdate(palmWebFeatureTree);
792
			conversation.commit(true);
793

    
794
		}
795
		if(notAvailModPager.getCount() == 0) {
796
			DefinedTerm notAvailMod = DefinedTerm.NewInstance(TermType.Modifier, "N/A", "N/A", null);
797
			termService.saveOrUpdate(notAvailMod);
798
			conversation.commit(true);
799
		}
800

    
801
		if(notAvailStatePager.getCount() == 0) {
802
			State notAvailState = State.NewInstance("N/A", "N/A", null);
803
			termService.saveOrUpdate(notAvailState);
804
			conversation.commit(true);
805
		}
806
		/*if(featureUseRecord == null) {
807
			featureUseRecord = Feature.NewInstance("Use Record", "Use Record", null);
808
			featureUseRecord.setUuid(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
809
			featureUseRecord.isSupportsCategoricalData();
810
			featureUseRecord.setSupportsCategoricalData(true);
811
			//TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)vocabularyService.find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
812
			featureVocabulary.addTerm(featureUseRecord);
813
			FeatureTree palmWebFeatureTree = featureTreeService.find(UUID.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
814
			FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseRecord);
815
			palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
816
			vocabularyService.saveOrUpdate(featureVocabulary);
817
			featureTreeService.saveOrUpdate(palmWebFeatureTree);
818
			conversation.commit(true);
819

    
820
		}
821
		if(featureUseSummary == null) {
822
			featureUseSummary = Feature.NewInstance("Use Summary", "Use Summary", null);
823
			featureUseSummary.setUuid(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
824
			featureUseSummary.isSupportsTextData();
825
			featureUseSummary.setSupportsTextData(true);
826
			//TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)vocabularyService.find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
827
			featureVocabulary.addTerm(featureUseSummary);
828
			FeatureTree palmWebFeatureTree = featureTreeService.find(UUID.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
829
			FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseSummary);
830
			palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
831
			vocabularyService.saveOrUpdate(featureVocabulary);
832
			featureTreeService.saveOrUpdate(palmWebFeatureTree);
833
			conversation.commit(true);
834
		}*/
835

    
836
		conversation.close();
837
		applicationController.close();
838

    
839
		return success;
840
	}
841

    
842
	public static void main(String[] args) {
843
		UseImport uiImport = new UseImport();
844
		// String xlsPath = ".//toload.xlsx";
845
		//String xlsPath = "C://workspace//CDM Trunk//UseImport//src//main//java//eu//etaxonomy//cdm//toLoad2.xls";
846

    
847
		uiImport.setupNecessaryItems();
848
		try {
849
			uiImport.loadTerms();
850
			uiImport.loadUses();
851
		} catch (InvalidFormatException e) {
852
			// TODO Auto-generated catch block
853
			e.printStackTrace();
854
		}
855
		//String xlsPath = "C://workspace//toLoad3.xls";
856
		//uiImport.importFromExcelSS(xlsPath);
857

    
858
	}
859
}
(7-7/8)