2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.app
.wp6
.palmae
;
12 import java
.io
.FileInputStream
;
13 import java
.io
.FileNotFoundException
;
14 import java
.io
.IOException
;
15 import java
.io
.InputStream
;
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
;
23 import java
.util
.UUID
;
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
;
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
.TimePeriod
;
58 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
59 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
60 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
61 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
62 import eu
.etaxonomy
.cdm
.model
.description
.State
;
63 import eu
.etaxonomy
.cdm
.model
.description
.StateData
;
64 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
65 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
66 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
67 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
68 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
69 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
70 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
71 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
73 public class UseImport
{
74 public static final Logger logger
= Logger
.getLogger(UseImport
.class);
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
);
85 private static ICdmDataSource
makeDestination(DatabaseTypeEnum dbType
,
86 String cdmServer
, String cdmDB
, int port
, String cdmUserName
,
88 // establish connection
89 pwd
= AccountStore
.readOrStorePassword(cdmServer
, cdmDB
, cdmUserName
,
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
);
100 throw new RuntimeException("Unsupported DatabaseType");
105 public boolean importFromExcelSS(String xlsPath
) throws InvalidFormatException
{
106 boolean success
= true;
108 CdmApplicationController applicationController
= CdmApplicationController
.NewInstance(dataSource());
109 ConversationHolder conversation
= applicationController
.NewConversation();
110 conversation
.startTransaction();
112 ITaxonService service
= applicationController
.getTaxonService();
113 ITermService termService
= applicationController
.getTermService();
114 IDescriptionService descService
= applicationController
.getDescriptionService();
115 IReferenceService referenceService
= applicationController
.getReferenceService();
116 InputStream inputStream
= null;
119 inputStream
= new FileInputStream(xlsPath
);
121 } catch (FileNotFoundException e
) {
123 System
.out
.println("File not found in the specified path.");
127 // POIFSFileSystem fileSystem = null;
130 // fileSystem = new POIFSFileSystem(inputStream);
132 // HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
133 Workbook workBook
= WorkbookFactory
.create(inputStream
);
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;
144 while (rows
.hasNext()) {
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();
153 int intCellType
= cell
.getCellType();
154 switch (intCellType
) {
156 int cellValue
= (int) cell
.getNumericCellValue();
157 lstTaxon
.add(Integer
.toString(cellValue
));
160 lstTaxon
.add(cell
.getStringCellValue());
164 lstUpdates
.add(lstTaxon
);
165 lstTaxonIDs
.add(Integer
.parseInt(lstTaxon
.get(0)));
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());
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));
190 TimePeriod year
= TimePeriod
.NewInstance(Integer
.parseInt(lstUpdate
.get(5)));
191 citation
.setDatePublished(year
);
192 citation
.setTitleCache(lstUpdate
.get(6));
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 Taxon bestCandidate
= null;
201 Synonym synonym
= CdmBase
.deproxy(taxon
, Synonym
.class);
202 Taxon acceptetdTaxon
= synonym
.getAcceptedTaxon();
203 if(acceptetdTaxon
!= null){
204 Set
<TaxonDescription
> taxonDescriptions
= acceptetdTaxon
.getDescriptions();
205 if(!taxonDescriptions
.isEmpty()) {
206 TaxonDescription firstDescription
= taxonDescriptions
.iterator().next();
207 //newUseSummary.addSource(null, null, citation, null);
208 //firstDescription.addElement(newUseSummary);
211 logger
.warn("No description container for: " + acceptetdTaxon
.getName());
216 Taxon taxonAccepted
= (Taxon
) taxon
;
217 /*Set<TaxonDescription> taxonDescriptions = taxonAccepted.getDescriptions();
218 if(!taxonDescriptions.isEmpty()) {
219 TaxonDescription firstDescription = taxonDescriptions.iterator().next();
220 //newUseSummary.addSource(null, null, citation, null);
221 //firstDescription.addElement(newUseSummary);
224 logger.warn("No description container for: " + taxonAccepted.getName());
226 taxonAccepted
.addDescription(newUseDescription
);
227 service
.saveOrUpdate(taxonAccepted
);
234 conversation
.commit(false);
236 } catch (IOException e
) {
245 private boolean loadUses() throws InvalidFormatException
{
246 boolean success
= true;
247 //String xslUseSummaryPathString = "C://workspace//Matched_UseSummary_referenceIdTaxEd_TaxonName.xls";
248 //String xslUseSummaryPathString = "C://workspace//testUseSummaries.xls";
251 String xslUseSummaryPathString
= "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//Matched_UseSummary_referenceIdTaxEd_TaxonName.xls";
253 //String xslUseRecordPathString = "C://workspace//UseRecordTerms_UseSummaryId.xls";
254 //String xslUseRecordPathString = "C://workspace//testUseRecords.xls";
255 //String xslUseRecordPathString = "C://workspace//test_useRecord.xls";
256 String xslUseRecordPathString
= "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//UseRecordTerms_UseSummaryId.xls";
258 InputStream inputStream
= null;
261 CdmApplicationController applicationController
= CdmApplicationController
.NewInstance(dataSource());
262 ConversationHolder conversation
= applicationController
.NewConversation();
263 conversation
.startTransaction();
265 ITaxonService taxonService
= applicationController
.getTaxonService();
266 ITermService termService
= applicationController
.getTermService();
267 IDescriptionService descService
= applicationController
.getDescriptionService();
268 IReferenceService referenceService
= applicationController
.getReferenceService();
271 ArrayList
<ArrayList
<String
>> lstUseSummaries
= loadSpreadsheet(xslUseSummaryPathString
);
272 ArrayList
<ArrayList
<String
>> lstUseRecords
= loadSpreadsheet(xslUseRecordPathString
);
274 MarkerType useMarkerType
= (MarkerType
) termService
.find(UUID
.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
275 Feature featureUseRecord
= (Feature
) termService
.find(UUID
.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
276 Feature featureUseSummary
= (Feature
) termService
.find(UUID
.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
277 Pager
<DefinedTermBase
> notAvailModPager
= termService
.findByTitle(DefinedTerm
.class, "N/A", null, null, null, null, null, null);
278 Pager
<DefinedTermBase
> notAvailStatePager
= termService
.findByTitle(State
.class, "N/A", null, null, null, null, null, null);
279 DefinedTerm notAvailMod
= (DefinedTerm
) notAvailModPager
.getRecords().get(0);
280 State notAvailState
= (State
) notAvailStatePager
.getRecords().get(0);
285 for (ArrayList
<String
> lstUseSummary
: lstUseSummaries
) {
287 String idTaxonToUpdate
= lstUseSummary
.get(3);
288 TaxonBase taxon
= taxonService
.find(UUID
.fromString(idTaxonToUpdate
));
290 TaxonDescription newUseDescription
= TaxonDescription
.NewInstance();
291 Marker useMarker
= Marker
.NewInstance(useMarkerType
, true);
292 newUseDescription
.addMarker(useMarker
);
293 Reference useReference
= null;
294 Pager
<Reference
> reference
= referenceService
.findByTitle(Reference
.class, lstUseSummary
.get(5), null, null, null, null, null, null);
295 if(reference
.getCount() == 0) {
296 System
.out
.println("Reference title: " + lstUseSummary
.get(5) + " not found.");
297 } else if(reference
.getCount() > 0 ) {
298 useReference
= reference
.getRecords().get(0);
300 IdentifiableSource source
=IdentifiableSource
.NewPrimarySourceInstance(useReference
, null); //is type correct?
301 source
.setOriginalNameString(taxon
.getName().toString());
302 newUseDescription
.addSource(source
);
303 TextData useSummary
= TextData
.NewInstance(featureUseSummary
);
304 LanguageString languageString
= LanguageString
.NewInstance(lstUseSummary
.get(1), Language
.ENGLISH());
305 useSummary
.putText(languageString
);
306 descService
.saveDescriptionElement(useSummary
);
307 newUseDescription
.addElement(useSummary
);
308 for (ArrayList
<String
> lstUseRecord
: lstUseRecords
) {
310 //System.out.println("Processing UseSummary#: " + i + " ID:" + lstUseSummary.get(0) + "UseRecord: " + lstUseRecord.get(1));
311 if(lstUseSummary
.get(0).equals(lstUseRecord
.get(0))) {
312 CategoricalData useRecord
= CategoricalData
.NewInstance();
313 useRecord
.setFeature(featureUseRecord
);
314 String modifyingText
= "";
315 if(lstUseRecord
.get(3) != null && lstUseRecord
.get(3).length() > 0) {
316 Pager
<DefinedTermBase
> useCategoryPager
= termService
.findByTitle(State
.class, lstUseRecord
.get(3), null, null, null, null, null, null);
317 State useCategory
= null;
318 if(useCategoryPager
.getCount() > 0) {
319 useCategory
= (State
) useCategoryPager
.getRecords().get(0);
321 useCategory
= notAvailState
;
323 StateData stateCatData
= StateData
.NewInstance(useCategory
);
324 stateCatData
.setState(useCategory
);
325 stateCatData
.putModifyingText(Language
.ENGLISH(), "Use Category");
326 modifyingText
+= useCategory
.toString() + ";";
327 useRecord
.addStateData(stateCatData
);
330 //useRecord.addState(stateData);
332 State useCategory
= notAvailState
;
333 StateData stateCatData
= StateData
.NewInstance(useCategory
);
334 stateCatData
.setState(useCategory
);
335 stateCatData
.putModifyingText(Language
.ENGLISH(), "Use Category");
336 modifyingText
+= useCategory
.toString() + ";";
337 useRecord
.addStateData(stateCatData
);
341 if(lstUseRecord
.get(4) != null && lstUseRecord
.get(4).length() > 0) {
342 Pager
<DefinedTermBase
> useSubCategoryPager
= termService
.findByTitle(State
.class, lstUseRecord
.get(4), null, null, null, null, null, null);
343 State useSubCategory
= null;
344 if(useSubCategoryPager
.getCount() > 0) {
345 useSubCategory
= (State
) useSubCategoryPager
.getRecords().get(0);
348 useSubCategory
= notAvailState
;
350 StateData stateSubCatData
= StateData
.NewInstance(useSubCategory
);
351 stateSubCatData
.setState(useSubCategory
);
352 stateSubCatData
.putModifyingText(Language
.ENGLISH(), "Use SubCategory");
353 modifyingText
+= useSubCategory
.toString() + ";";
354 useRecord
.addStateData(stateSubCatData
);
358 State useSubCategory
= notAvailState
;
359 StateData stateSubCatData
= StateData
.NewInstance(useSubCategory
);
360 stateSubCatData
.setState(useSubCategory
);
361 stateSubCatData
.putModifyingText(Language
.ENGLISH(), "Use SubCategory");
362 modifyingText
+= useSubCategory
.toString() + ";";
363 useRecord
.addStateData(stateSubCatData
);
366 if(lstUseRecord
.get(5) != null && lstUseRecord
.get(5).length() > 0) {
367 Pager
<DefinedTermBase
> countryPager
= termService
.findByTitle(DefinedTerm
.class, lstUseRecord
.get(5), null, null, null, null, null, null);
368 DefinedTerm country
= null;
369 if(countryPager
.getCount() > 0) {
370 country
= (DefinedTerm
) countryPager
.getRecords().get(0);
372 country
= notAvailMod
;
374 modifyingText
+= country
.toString() + ";";
375 useRecord
.addModifier(country
);
377 DefinedTerm country
= notAvailMod
;
378 modifyingText
+= country
.toString() + ";";
379 useRecord
.addModifier(country
);
382 if(lstUseRecord
.get(6) != null && lstUseRecord
.get(6).length() > 0) {
383 Pager
<DefinedTermBase
> plantPartPager
= termService
.findByTitle(DefinedTerm
.class, lstUseRecord
.get(6), null, null, null, null, null, null);
384 DefinedTerm plantPart
= null;
385 if(plantPartPager
.getCount() > 0) {
386 plantPart
= (DefinedTerm
) plantPartPager
.getRecords().get(0);
388 plantPart
= notAvailMod
;
390 modifyingText
+= plantPart
.toString() + ";";
391 useRecord
.addModifier(plantPart
);
393 DefinedTerm plantPart
= notAvailMod
;
394 modifyingText
+= plantPart
.toString() + ";";
395 useRecord
.addModifier(plantPart
);
397 if(lstUseRecord
.get(7) != null && lstUseRecord
.get(7).length() > 0) {
398 Pager
<DefinedTermBase
> humanGroupPager
= termService
.findByTitle(DefinedTerm
.class, lstUseRecord
.get(7), null, null, null, null, null, null);
399 DefinedTerm humanGroup
= null;
400 if(humanGroupPager
.getCount() > 0) {
401 humanGroup
= (DefinedTerm
) humanGroupPager
.getRecords().get(0);
403 humanGroup
= notAvailMod
;
405 modifyingText
+= humanGroup
.toString() + ";";
406 useRecord
.addModifier(humanGroup
);
408 DefinedTerm humanGroup
= notAvailMod
;
409 modifyingText
+= humanGroup
.toString() + ";";
410 useRecord
.addModifier(humanGroup
);
412 if(lstUseRecord
.get(8) != null && lstUseRecord
.get(8).length() > 0) {
413 Pager
<DefinedTermBase
> ethnicGroupPager
= termService
.findByTitle(DefinedTerm
.class, lstUseRecord
.get(8), null, null, null, null, null, null);
414 DefinedTerm ethnicGroup
= null;
415 if(ethnicGroupPager
.getCount() > 0) {
416 ethnicGroup
= (DefinedTerm
) ethnicGroupPager
.getRecords().get(0);
417 modifyingText
+= ethnicGroup
.toString() + ";";
419 ethnicGroup
= notAvailMod
;
421 useRecord
.addModifier(ethnicGroup
);
424 DefinedTerm ethnicGroup
= notAvailMod
;
425 modifyingText
+= ethnicGroup
.toString() + ";";
426 useRecord
.addModifier(ethnicGroup
);
428 useRecord
.putModifyingText(Language
.ENGLISH(), modifyingText
);
429 descService
.saveDescriptionElement(useRecord
);
430 newUseDescription
.addElement(useRecord
);
436 if (taxon
.isInstanceOf(Synonym
.class)){
437 Taxon bestCandidate
= null;
438 Synonym synonym
= CdmBase
.deproxy(taxon
, Synonym
.class);
439 Taxon acceptedTaxon
= synonym
.getAcceptedTaxon();
440 if(acceptedTaxon
!= null){
441 acceptedTaxon
.addDescription(newUseDescription
);
442 taxonService
.saveOrUpdate(bestCandidate
);
443 conversation
.commit();
446 Taxon taxonAccepted
= (Taxon
) taxon
;
447 taxonAccepted
.addDescription(newUseDescription
);
448 taxonService
.saveOrUpdate(taxonAccepted
);
449 conversation
.commit();
453 System
.out
.println("Processing UseSummary#: " + i
+ " ID:" + lstUseSummary
.get(0));
457 conversation
.close();
458 applicationController
.close();
460 } catch (Exception e
) {
468 //Completed and tested!
469 private boolean loadTerms() throws InvalidFormatException
{
470 boolean success
= true;
472 //String xslPathString = "C://workspace//terms.xls";
473 String xslPathString
= "//Users//alextheys//Projects//CDM_Trunk//Palm_Use_Data_Extension//CDMLib-apps//cdmlib-apps//UseImport//src//main//resources//terms.xls";
475 CdmApplicationController applicationController
= CdmApplicationController
.NewInstance(dataSource());
476 ConversationHolder conversation
= applicationController
.NewConversation();
477 conversation
.startTransaction();
479 ITaxonService service
= applicationController
.getTaxonService();
480 ITermService termService
= applicationController
.getTermService();
481 IVocabularyService vocabularyService
= applicationController
.getVocabularyService();
482 IReferenceService referenceService
= applicationController
.getReferenceService();
484 TermVocabulary
<State
> stateVocabulary
= vocabularyService
.find(UUID
.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
485 TermVocabulary
<DefinedTermBase
<?
>> countryVocabulary
= vocabularyService
.find(UUID
.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
486 TermVocabulary
<DefinedTerm
> plantPartVocabulary
= vocabularyService
.find(UUID
.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
487 TermVocabulary
<DefinedTerm
> humanGroupVocabulary
= vocabularyService
.find(UUID
.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
489 IDescriptionService descService
= applicationController
.getDescriptionService();
490 InputStream inputStream
= null;
493 inputStream
= new FileInputStream(xslPathString
);
495 } catch (FileNotFoundException e
) {
497 System
.out
.println("File not found in the specified path.");
503 // POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
504 // HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
506 Workbook workBook
= WorkbookFactory
.create(inputStream
);
509 Sheet sheet
= workBook
.getSheetAt(0);
510 Iterator
<Row
> rows
= sheet
.rowIterator();
512 ArrayList
<ArrayList
<String
>> lstUpdates
= new ArrayList
<ArrayList
<String
>>();
514 while (rows
.hasNext()) {
516 Row row
= rows
.next();
517 System
.out
.println("Row No.: " + row
.getRowNum());
518 Iterator
<Cell
> cells
= row
.cellIterator();
519 ArrayList
<String
> lstTerms
= new ArrayList
<String
>();
520 while (cells
.hasNext()) {
521 Cell cell
= cells
.next();
523 int intCellType
= cell
.getCellType();
524 switch (intCellType
) {
526 int cellValue
= (int) cell
.getNumericCellValue();
527 lstTerms
.add(Integer
.toString(cellValue
));
530 lstTerms
.add(cell
.getStringCellValue());
534 lstUpdates
.add(lstTerms
);
535 //lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));
537 for (ArrayList
<String
> lstUpdate
: lstUpdates
) {
538 int termType
= Integer
.parseInt(lstUpdate
.get(0));
540 //Case 0 = UseCategory
542 Pager
<State
> useCategoryPager
= termService
.findByRepresentationText(lstUpdate
.get(1), State
.class, null, null);
543 State useCategory
= null;
544 State useSubCat
= null;
545 if (useCategoryPager
.getCount()>0) {
546 useCategory
= useCategoryPager
.getRecords().get(0);
548 if(useCategory
== null) {
549 useCategory
= State
.NewInstance(lstUpdate
.get(1), lstUpdate
.get(1), null);
551 //State useCategory = (State) termService.
552 if(lstUpdate
.size() > 2) {
553 useSubCat
= State
.NewInstance(lstUpdate
.get(2), lstUpdate
.get(2), null);
554 useCategory
.addIncludes(useSubCat
);
556 stateVocabulary
.addTerm(useCategory
);
557 vocabularyService
.saveOrUpdate(stateVocabulary
);
558 conversation
.commit(true);
561 //case 1: = HumanGroup
563 Pager
<DefinedTerm
> humanGroupPager
= termService
.findByRepresentationText(lstUpdate
.get(1), DefinedTerm
.class, null, null);
565 DefinedTerm humanGroup
= null;
566 DefinedTerm ethnicGroup
= null;
567 if(humanGroupPager
.getCount()>0) {
568 humanGroup
= humanGroupPager
.getRecords().get(0);
571 if(humanGroup
== null) {
572 humanGroup
= DefinedTerm
.NewModifierInstance(lstUpdate
.get(1), lstUpdate
.get(1), null);
575 if(lstUpdate
.size() >2) {
576 ethnicGroup
= DefinedTerm
.NewModifierInstance(lstUpdate
.get(2), lstUpdate
.get(2), null);
577 humanGroup
.addIncludes(ethnicGroup
);
579 humanGroupVocabulary
.addTerm(humanGroup
);
580 vocabularyService
.saveOrUpdate(humanGroupVocabulary
);
581 conversation
.commit(true);
586 Pager
<DefinedTerm
> countryPager
= termService
.findByRepresentationText(lstUpdate
.get(1), DefinedTerm
.class, null, null);
587 DefinedTermBase country
= null;
589 if(countryPager
.getCount()>0) {
590 country
= countryPager
.getRecords().get(0);
593 if(country
== null) {
594 country
= NamedArea
.NewInstance(lstUpdate
.get(1), lstUpdate
.get(1), null);
595 countryVocabulary
.addTerm(country
);
596 vocabularyService
.saveOrUpdate(countryVocabulary
);
598 conversation
.commit(true);
601 //case 3: //plantPart
603 Pager
<DefinedTerm
> plantPartPager
= termService
.findByRepresentationText(lstUpdate
.get(1), DefinedTerm
.class, null, null);
604 DefinedTerm plantPart
= null;
606 if(plantPartPager
.getCount()>0) {
607 plantPart
= plantPartPager
.getRecords().get(0);
610 if(plantPart
== null) {
611 plantPart
= DefinedTerm
.NewModifierInstance(lstUpdate
.get(1), lstUpdate
.get(1), null);
612 plantPartVocabulary
.addTerm(plantPart
);
613 vocabularyService
.saveOrUpdate(plantPartVocabulary
);
615 conversation
.commit(true);
620 conversation
.close();
621 applicationController
.close();
623 } catch (IOException e
) {
631 private ArrayList
<ArrayList
<String
>> loadSpreadsheet(String xslPathString
) throws InvalidFormatException
{
632 ArrayList
<ArrayList
<String
>> lstUpdates
= new ArrayList
<ArrayList
<String
>>();
633 InputStream inputStream
= null;
636 inputStream
= new FileInputStream(xslPathString
);
638 } catch (FileNotFoundException e
) {
639 System
.out
.println("File not found in the specified path.");
644 // POIFSFileSystem fileSystem = new POIFSFileSystem(inputStream);
645 // HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
647 Workbook workBook
= WorkbookFactory
.create(inputStream
);
650 Sheet sheet
= workBook
.getSheetAt(0);
651 Iterator
<Row
> rows
= sheet
.rowIterator();
652 // Iterator rows = sheet.rowIterator();
653 //Set<Integer> lstTaxonIDs;
656 while (rows
.hasNext()) {
658 Row row
= rows
.next();
659 System
.out
.println("Row No.: " + row
.getRowNum());
660 Iterator
<Cell
> cells
= row
.cellIterator();
661 ArrayList
<String
> lstTerms
= new ArrayList
<String
>();
662 while (cells
.hasNext()) {
663 Cell cell
= cells
.next();
665 int intCellType
= cell
.getCellType();
666 switch (intCellType
) {
668 int cellValue
= (int) cell
.getNumericCellValue();
669 lstTerms
.add(Integer
.toString(cellValue
));
672 lstTerms
.add(cell
.getStringCellValue());
676 lstUpdates
.add(lstTerms
);
677 //lstTaxonIDs.add(Integer.parseInt(lstTaxon.get(0)));
679 } catch (IOException e
) {
686 private boolean setupNecessaryItems() {
687 boolean success
= false;
688 CdmApplicationController applicationController
= CdmApplicationController
.NewInstance(dataSource());
689 ConversationHolder conversation
= applicationController
.NewConversation();
692 ITaxonService service
= applicationController
.getTaxonService();
693 ITermService termService
= applicationController
.getTermService();
694 IVocabularyService vocabularyService
= applicationController
.getVocabularyService();
695 IFeatureTreeService featureTreeService
= applicationController
.getFeatureTreeService();
697 MarkerType existingMarkertype
= (MarkerType
)termService
.find(UUID
.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
698 Feature featureUseRecord
= (Feature
) termService
.find(UUID
.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
699 Feature featureUseSummary
= (Feature
) termService
.find(UUID
.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
700 TermVocabulary
<State
> stateVocabulary
= vocabularyService
.find(UUID
.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
701 TermVocabulary
<DefinedTerm
> countryVocabulary
= vocabularyService
.find(UUID
.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
702 TermVocabulary
<DefinedTerm
> plantPartVocabulary
= vocabularyService
.find(UUID
.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
703 TermVocabulary
<DefinedTerm
> humanGroupVocabulary
= vocabularyService
.find(UUID
.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
704 Pager
<DefinedTermBase
> notAvailModPager
= termService
.findByTitle(DefinedTerm
.class, "N/A", null, null, null, null, null, null);
705 Pager
<DefinedTermBase
> notAvailStatePager
= termService
.findByTitle(State
.class, "N/A", null, null, null, null, null, null);
707 conversation
.startTransaction();
708 if (existingMarkertype
== null) {
709 existingMarkertype
= MarkerType
.NewInstance("use", "use", null);
710 existingMarkertype
.setUuid( UUID
.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
711 TermVocabulary
<MarkerType
> markerTypeVocabulary
= vocabularyService
.find((UUID
.fromString("19dffff7-e142-429c-a420-5d28e4ebe305")));
712 markerTypeVocabulary
.addTerm(existingMarkertype
);
713 vocabularyService
.saveOrUpdate(markerTypeVocabulary
);
714 conversation
.commit(true);
716 if (stateVocabulary
== null) {
718 URI termSourceUri
= null;
720 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.State");
721 } catch (URISyntaxException e
) {
724 stateVocabulary
= TermVocabulary
.NewInstance(TermType
.State
, "Use Category", "Use Category", null, termSourceUri
);
725 stateVocabulary
.setUuid(UUID
.fromString("67430d7c-fd43-4e9d-af5e-d0dca3f74931"));
726 vocabularyService
.saveOrUpdate(stateVocabulary
);
727 conversation
.commit(true);
729 if (countryVocabulary
== null) {
730 URI termSourceUri
= null;
732 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
733 } catch (URISyntaxException e
) {
736 countryVocabulary
= TermVocabulary
.NewInstance(TermType
.NamedArea
, "Country", "Country", null, termSourceUri
);
737 countryVocabulary
.setUuid(UUID
.fromString("116c51f1-e63a-46f7-a258-e1149a42868b"));
739 vocabularyService
.saveOrUpdate(countryVocabulary
);
740 conversation
.commit(true);
742 if (plantPartVocabulary
== null) {
743 URI termSourceUri
= null;
745 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
746 } catch (URISyntaxException e
) {
749 plantPartVocabulary
= TermVocabulary
.NewInstance(TermType
.Modifier
, "Plant Part", "Plant Part", null, termSourceUri
);
750 plantPartVocabulary
.setUuid(UUID
.fromString("369914fe-d54b-4063-99ce-abc81d30ad35"));
751 vocabularyService
.saveOrUpdate(plantPartVocabulary
);
752 conversation
.commit(true);
754 if (humanGroupVocabulary
== null) {
755 URI termSourceUri
= null;
757 termSourceUri
= new URI("eu.etaxonomy.cdm.model.description.DefinedTerm");
758 } catch (URISyntaxException e
) {
761 humanGroupVocabulary
= TermVocabulary
.NewInstance(TermType
.Modifier
, "Human Group", "Human Group", null, termSourceUri
);
762 humanGroupVocabulary
.setUuid(UUID
.fromString("ca46cea5-bdf7-438d-9cd8-e2793d2178dc"));
763 vocabularyService
.saveOrUpdate(humanGroupVocabulary
);
764 conversation
.commit(true);
766 if(featureUseRecord
== null|| featureUseSummary
== null) {
767 TermVocabulary
<Feature
> featureVocabulary
= vocabularyService
.find((UUID
.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
768 FeatureTree palmWebFeatureTree
= featureTreeService
.find(UUID
.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
769 //List<FeatureTree> featureTrees = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
771 if (featureUseRecord
== null ) {
772 featureUseRecord
= Feature
.NewInstance("Use Record", "Use Record", null);
773 featureUseRecord
.setUuid(UUID
.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
774 featureUseRecord
.isSupportsCategoricalData();
775 featureUseRecord
.setSupportsCategoricalData(true);
776 featureVocabulary
.addTerm(featureUseRecord
);
777 FeatureNode useRecFeatureNode
= FeatureNode
.NewInstance(featureUseRecord
);
778 palmWebFeatureTree
.getRoot().addChild(useRecFeatureNode
);
780 if (featureUseSummary
== null) {
781 featureUseSummary
= Feature
.NewInstance("Use", "Use", null);
782 featureUseSummary
.setUuid(UUID
.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
783 featureUseSummary
.isSupportsTextData();
784 featureUseSummary
.setSupportsTextData(true);
785 //TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)CdmStore.getService(IVocabularyService.class).find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
786 featureVocabulary
.addTerm(featureUseSummary
);
787 FeatureNode useSumFeatureNode
= FeatureNode
.NewInstance(featureUseSummary
);
788 palmWebFeatureTree
.getRoot().addChild(useSumFeatureNode
);
791 vocabularyService
.saveOrUpdate(featureVocabulary
);
792 featureTreeService
.saveOrUpdate(palmWebFeatureTree
);
793 conversation
.commit(true);
796 if(notAvailModPager
.getCount() == 0) {
797 DefinedTerm notAvailMod
= DefinedTerm
.NewInstance(TermType
.Modifier
, "N/A", "N/A", null);
798 termService
.saveOrUpdate(notAvailMod
);
799 conversation
.commit(true);
802 if(notAvailStatePager
.getCount() == 0) {
803 State notAvailState
= State
.NewInstance("N/A", "N/A", null);
804 termService
.saveOrUpdate(notAvailState
);
805 conversation
.commit(true);
807 /*if(featureUseRecord == null) {
808 featureUseRecord = Feature.NewInstance("Use Record", "Use Record", null);
809 featureUseRecord.setUuid(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
810 featureUseRecord.isSupportsCategoricalData();
811 featureUseRecord.setSupportsCategoricalData(true);
812 //TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)vocabularyService.find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
813 featureVocabulary.addTerm(featureUseRecord);
814 FeatureTree palmWebFeatureTree = featureTreeService.find(UUID.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
815 FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseRecord);
816 palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
817 vocabularyService.saveOrUpdate(featureVocabulary);
818 featureTreeService.saveOrUpdate(palmWebFeatureTree);
819 conversation.commit(true);
822 if(featureUseSummary == null) {
823 featureUseSummary = Feature.NewInstance("Use Summary", "Use Summary", null);
824 featureUseSummary.setUuid(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));
825 featureUseSummary.isSupportsTextData();
826 featureUseSummary.setSupportsTextData(true);
827 //TermVocabulary<Feature> featureVocabulary = (TermVocabulary<Feature>)vocabularyService.find((UUID.fromString("b187d555-f06f-4d65-9e53-da7c93f8eaa8")));
828 featureVocabulary.addTerm(featureUseSummary);
829 FeatureTree palmWebFeatureTree = featureTreeService.find(UUID.fromString("72ccce05-7cc8-4dab-8e47-bf3f5fd848a0"));
830 FeatureNode useRecFeatureNode = FeatureNode.NewInstance(featureUseSummary);
831 palmWebFeatureTree.getRoot().addChild(useRecFeatureNode);
832 vocabularyService.saveOrUpdate(featureVocabulary);
833 featureTreeService.saveOrUpdate(palmWebFeatureTree);
834 conversation.commit(true);
837 conversation
.close();
838 applicationController
.close();
843 public static void main(String
[] args
) {
844 UseImport uiImport
= new UseImport();
845 // String xlsPath = ".//toload.xlsx";
846 //String xlsPath = "C://workspace//CDM Trunk//UseImport//src//main//java//eu//etaxonomy//cdm//toLoad2.xls";
848 uiImport
.setupNecessaryItems();
850 uiImport
.loadTerms();
852 } catch (InvalidFormatException e
) {
853 // TODO Auto-generated catch block
856 //String xlsPath = "C://workspace//toLoad3.xls";
857 //uiImport.importFromExcelSS(xlsPath);