Project

General

Profile

« Previous | Next » 

Revision f307edd3

Added by Andreas Müller over 10 years ago

moved UseImport to app-import..palmae

View differences:

.gitattributes
1 1
* text=auto !eol
2 2
UseImport/pom.xml -text
3
UseImport/src/main/java/eu/etaxonomy/cdm/UseImport.java -text
4 3
UseImport/src/main/resources/Matched_UseSummary_referenceIdTaxEd_TaxonName.xls -text
5 4
UseImport/src/main/resources/UseRecordTerms_UseSummaryId.xls -text
6 5
UseImport/src/main/resources/terms.xls -text
......
99 98
app-import/src/main/java/eu/etaxonomy/cdm/app/wp6/palmae/PalmaePostImportUpdater.java -text
100 99
app-import/src/main/java/eu/etaxonomy/cdm/app/wp6/palmae/PalmaeProtologueImportActivator.java -text
101 100
app-import/src/main/java/eu/etaxonomy/cdm/app/wp6/palmae/PalmaeTaxonXImportActivator.java -text
101
app-import/src/main/java/eu/etaxonomy/cdm/app/wp6/palmae/UseImport.java -text
102 102
app-import/src/main/java/eu/etaxonomy/cdm/app/wp6/palmae/config/PalmaeProtologueImportConfigurator.java -text
103 103
app-import/src/main/java/eu/etaxonomy/cdm/io/PalmaeImageImport.java -text
104 104
app-import/src/main/java/eu/etaxonomy/cdm/io/PalmaeProtologueImport.java -text
UseImport/src/main/java/eu/etaxonomy/cdm/UseImport.java
1
package eu.etaxonomy.cdm;
2

  
3
import java.io.FileInputStream;
4
import java.io.FileNotFoundException;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.net.URI;
8
import java.net.URISyntaxException;
9
import java.util.ArrayList;
10
import java.util.HashSet;
11
import java.util.Iterator;
12
import java.util.List;
13
import java.util.Set;
14
import java.util.UUID;
15

  
16
import org.apache.log4j.Logger;
17
import org.apache.poi.hssf.usermodel.HSSFCell;
18
import org.apache.poi.hssf.usermodel.HSSFRow;
19
import org.apache.poi.hssf.usermodel.HSSFSheet;
20
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
21
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
22

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

  
64
public class UseImport {
65
	public static final Logger logger = Logger.getLogger(UseImport.class);
66

  
67
	public void UseImport() {
68

  
69
	}
70

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

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

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

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

  
122
		POIFSFileSystem fileSystem = null;
123
		
124
		try {
125
			fileSystem = new POIFSFileSystem(inputStream);
126

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

  
136
			while (rows.hasNext()) {
137

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

  
251
		return success;
252

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

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

  
521
		POIFSFileSystem fileSystem = null;
522
		
523
		try {
524
			fileSystem = new POIFSFileSystem(inputStream);
525

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

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

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

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

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

  
673
			while (rows.hasNext()) {
674

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

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

  
871
	}
872
}
app-import/src/main/java/eu/etaxonomy/cdm/app/wp6/palmae/UseImport.java
1
package eu.etaxonomy.cdm;
2

  
3
import java.io.FileInputStream;
4
import java.io.FileNotFoundException;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.net.URI;
8
import java.net.URISyntaxException;
9
import java.util.ArrayList;
10
import java.util.HashSet;
11
import java.util.Iterator;
12
import java.util.List;
13
import java.util.Set;
14
import java.util.UUID;
15

  
16
import org.apache.log4j.Logger;
17
import org.apache.poi.hssf.usermodel.HSSFCell;
18
import org.apache.poi.hssf.usermodel.HSSFRow;
19
import org.apache.poi.hssf.usermodel.HSSFSheet;
20
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
21
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
22

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

  
64
public class UseImport {
65
	public static final Logger logger = Logger.getLogger(UseImport.class);
66

  
67
	public void UseImport() {
68

  
69
	}
70

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

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

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

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

  
122
		POIFSFileSystem fileSystem = null;
123
		
124
		try {
125
			fileSystem = new POIFSFileSystem(inputStream);
126

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

  
136
			while (rows.hasNext()) {
137

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

  
251
		return success;
252

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

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

  
521
		POIFSFileSystem fileSystem = null;
522
		
523
		try {
524
			fileSystem = new POIFSFileSystem(inputStream);
525

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

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

  
534
				HSSFRow row = (HSSFRow) rows.next();
535
				System.out.println("Row No.: " + row.getRowNum());
536
				Iterator cells = row.cellIterator();
537
				ArrayList<String> lstTerms = new ArrayList<String>();
538
				while (cells.hasNext()) {
539
					HSSFCell cell = (HSSFCell) cells.next();
540
				
541
					int intCellType = cell.getCellType();
542
					switch (intCellType) {
543
						case 0:
544
							int cellValue = (int) cell.getNumericCellValue(); 
545
							lstTerms.add(Integer.toString(cellValue));
546
							break;
547
						case 1:
548
							lstTerms.add(cell.getStringCellValue());
549
							break;
550
					}
551
				}
552
				lstUpdates.add(lstTerms);
553
				//lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));	
554
			}
555
			for (ArrayList<String> lstUpdate : lstUpdates) {
556
				int termType = Integer.parseInt(lstUpdate.get(0)); 
557
				switch (termType) {
558
				//Case 0 = UseCategory
559
				case 0:
560
					Pager<State> useCategoryPager = termService.findByRepresentationText(lstUpdate.get(1), State.class, null, null);
561
					State useCategory = null;
562
					State useSubCat = null;
563
					if (useCategoryPager.getCount()>0) {
564
						useCategory = useCategoryPager.getRecords().get(0);
565
					}
566
					if(useCategory == null) {
567
						useCategory = State.NewInstance(lstUpdate.get(1), lstUpdate.get(1), null);
568
					}
569
					//State useCategory = (State) termService.
570
					if(lstUpdate.size() > 2) {
571
						useSubCat = State.NewInstance(lstUpdate.get(2), lstUpdate.get(2), null);
572
						useCategory.addIncludes(useSubCat);
573
					}
574
					stateVocabulary.addTerm(useCategory);
575
					vocabularyService.saveOrUpdate(stateVocabulary);
576
					conversation.commit(true);
577
					break;
578
				
579
				//case 1: = HumanGroup
580
				case 1:
581
					Pager<Modifier> humanGroupPager = termService.findByRepresentationText(lstUpdate.get(1), Modifier.class, null, null);
582
					
583
					Modifier humanGroup = null;
584
					Modifier ethnicGroup = null;
585
					if(humanGroupPager.getCount()>0) {
586
						humanGroup = humanGroupPager.getRecords().get(0);
587
					}
588
					
589
					if(humanGroup == null) {
590
						humanGroup = Modifier.NewInstance(lstUpdate.get(1), lstUpdate.get(1), null);
591
					}
592
					
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff