1 |
ce8b7634
|
Andreas Müller
|
/**
|
2 |
|
|
*
|
3 |
|
|
*/
|
4 |
|
|
package eu.etaxonomy.cdm.io.berlinModel;
|
5 |
|
|
|
6 |
|
|
import java.sql.ResultSet;
|
7 |
|
|
import java.sql.SQLException;
|
8 |
8160c112
|
Andreas Müller
|
import java.util.HashSet;
|
9 |
|
|
import java.util.Set;
|
10 |
ce8b7634
|
Andreas Müller
|
|
11 |
|
|
import org.apache.log4j.Logger;
|
12 |
|
|
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.*;
|
13 |
|
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
14 |
8160c112
|
Andreas Müller
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
15 |
ce8b7634
|
Andreas Müller
|
import eu.etaxonomy.cdm.io.source.Source;
|
16 |
8160c112
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
17 |
795d241d
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
18 |
|
|
import eu.etaxonomy.cdm.model.description.TextData;
|
19 |
ce8b7634
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
20 |
8160c112
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
21 |
ce8b7634
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
/**
|
25 |
|
|
* @author a.mueller
|
26 |
|
|
*
|
27 |
|
|
*/
|
28 |
|
|
public class BerlinModelFactsIO {
|
29 |
|
|
private static final Logger logger = Logger.getLogger(BerlinModelFactsIO.class);
|
30 |
|
|
|
31 |
|
|
private static int modCount = 10000;
|
32 |
|
|
|
33 |
|
|
|
34 |
16bfe57f
|
Andreas Müller
|
public static boolean invoke(BerlinModelImportConfigurator bmiConfig, CdmApplicationController cdmApp,
|
35 |
ce8b7634
|
Andreas Müller
|
MapWrapper<TaxonBase> taxonMap, MapWrapper<ReferenceBase> referenceMap){
|
36 |
8160c112
|
Andreas Müller
|
|
37 |
|
|
Set<TaxonBase> taxonStore = new HashSet<TaxonBase>();
|
38 |
16bfe57f
|
Andreas Müller
|
Source source = bmiConfig.getSource();
|
39 |
8160c112
|
Andreas Müller
|
ITaxonService taxonService = cdmApp.getTaxonService();
|
40 |
16bfe57f
|
Andreas Müller
|
|
41 |
ce8b7634
|
Andreas Müller
|
String dbAttrName;
|
42 |
|
|
String cdmAttrName;
|
43 |
|
|
|
44 |
|
|
logger.info("start makeFacts ...");
|
45 |
|
|
|
46 |
16bfe57f
|
Andreas Müller
|
boolean delete = bmiConfig.isDeleteAll();
|
47 |
ce8b7634
|
Andreas Müller
|
|
48 |
|
|
try {
|
49 |
|
|
//get data from database
|
50 |
|
|
String strQuery =
|
51 |
|
|
" SELECT Fact.*, PTaxon.RIdentifier as taxonId " +
|
52 |
|
|
" FROM Fact INNER JOIN " +
|
53 |
|
|
" dbo.PTaxon ON Fact.PTNameFk = PTaxon.PTNameFk AND Fact.PTRefFk = PTaxon.PTRefFk "+
|
54 |
|
|
" WHERE (1=1)";
|
55 |
|
|
ResultSet rs = source.getResultSet(strQuery) ;
|
56 |
795d241d
|
Andreas Müller
|
|
57 |
ce8b7634
|
Andreas Müller
|
int i = 0;
|
58 |
|
|
//for each reference
|
59 |
|
|
while (rs.next()){
|
60 |
|
|
|
61 |
|
|
if ((i++ % modCount) == 0){ logger.info("Facts handled: " + (i-1));}
|
62 |
|
|
|
63 |
|
|
int factId = rs.getInt("factId");
|
64 |
|
|
int taxonId = rs.getInt("taxonId");
|
65 |
|
|
int factRefFk = rs.getInt("factRefFk");
|
66 |
|
|
int ptDesignationRefFk = rs.getInt("PTDesignationRefFk");
|
67 |
|
|
int categoryFk = rs.getInt("factCategoryFk");
|
68 |
|
|
|
69 |
8160c112
|
Andreas Müller
|
TaxonBase taxonBase = taxonMap.get(taxonId);
|
70 |
795d241d
|
Andreas Müller
|
|
71 |
8160c112
|
Andreas Müller
|
if (taxonBase != null){
|
72 |
|
|
Taxon taxon;
|
73 |
55b03c52
|
Andreas Müller
|
if ( taxonBase instanceof Taxon ) {
|
74 |
8160c112
|
Andreas Müller
|
taxon = (Taxon) taxonBase;
|
75 |
|
|
}else{
|
76 |
71aa0b5a
|
Andreas Müller
|
logger.warn("TaxonBase for Fact " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
|
77 |
8160c112
|
Andreas Müller
|
continue;
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
TaxonDescription taxonDescription = TaxonDescription.NewInstance();
|
81 |
|
|
|
82 |
|
|
taxon.addDescription(taxonDescription);
|
83 |
|
|
//textData
|
84 |
e6554b2a
|
Andreas Müller
|
// TextData textData = TextData.NewInstance();
|
85 |
|
|
// textData.addText("XXX", bmiConfig.getFactLanguage());
|
86 |
|
|
// taxonDescription.addFeature(textData);
|
87 |
8160c112
|
Andreas Müller
|
|
88 |
|
|
//commonNames
|
89 |
|
|
String commonNameString;
|
90 |
|
|
if (taxon.getName() != null){
|
91 |
|
|
commonNameString = "Common " + taxon.getName().getNameCache();
|
92 |
|
|
}else{
|
93 |
|
|
commonNameString = "Common (null)";
|
94 |
|
|
}
|
95 |
|
|
CommonTaxonName commonName = CommonTaxonName.NewInstance(commonNameString, bmiConfig.getFactLanguage());
|
96 |
|
|
taxonDescription.addFeature(commonName);
|
97 |
|
|
|
98 |
ce8b7634
|
Andreas Müller
|
if (categoryFk == FACT_DESCRIPTION){
|
99 |
8160c112
|
Andreas Müller
|
//;
|
100 |
ce8b7634
|
Andreas Müller
|
}else if (categoryFk == FACT_OBSERVATION){
|
101 |
|
|
//;
|
102 |
|
|
}else if (categoryFk == FACT_DISTIRBUTION_EM){
|
103 |
|
|
//
|
104 |
|
|
}else {
|
105 |
|
|
//TODO
|
106 |
|
|
logger.warn("FactCategory " + categoryFk + " not yet implemented");
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
//TODO
|
110 |
|
|
//References
|
111 |
|
|
//etc.
|
112 |
8160c112
|
Andreas Müller
|
|
113 |
|
|
taxonStore.add(taxon);
|
114 |
ce8b7634
|
Andreas Müller
|
}else{
|
115 |
|
|
//TODO
|
116 |
8160c112
|
Andreas Müller
|
logger.warn("Taxon for Fact " + factId + " does not exist in store");
|
117 |
ce8b7634
|
Andreas Müller
|
}
|
118 |
8160c112
|
Andreas Müller
|
//put
|
119 |
ce8b7634
|
Andreas Müller
|
}
|
120 |
8160c112
|
Andreas Müller
|
logger.info("Taxa to save: " + taxonStore.size());
|
121 |
|
|
taxonService.saveTaxonAll(taxonStore);
|
122 |
ce8b7634
|
Andreas Müller
|
|
123 |
|
|
logger.info("end makeFacts ...");
|
124 |
|
|
return true;
|
125 |
|
|
} catch (SQLException e) {
|
126 |
|
|
logger.error("SQLException:" + e);
|
127 |
|
|
return false;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
}
|