Project

General

Profile

Download (3.92 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.cdm.io.berlinModel;
5

    
6
import java.sql.ResultSet;
7
import java.sql.SQLException;
8
import java.util.HashSet;
9
import java.util.Set;
10

    
11
import org.apache.log4j.Logger;
12
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.*;
13
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
14
import eu.etaxonomy.cdm.api.service.ITaxonService;
15
import eu.etaxonomy.cdm.io.source.Source;
16
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
17
import eu.etaxonomy.cdm.model.description.TaxonDescription;
18
import eu.etaxonomy.cdm.model.description.TextData;
19
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
20
import eu.etaxonomy.cdm.model.taxon.Taxon;
21
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
	public static boolean invoke(BerlinModelImportConfigurator bmiConfig, CdmApplicationController cdmApp, 
35
			MapWrapper<TaxonBase> taxonMap, MapWrapper<ReferenceBase> referenceMap){
36
		
37
		Set<TaxonBase> taxonStore = new HashSet<TaxonBase>();
38
		Source source = bmiConfig.getSource();
39
		ITaxonService taxonService = cdmApp.getTaxonService();
40
		
41
		String dbAttrName;
42
		String cdmAttrName;
43
		
44
		logger.info("start makeFacts ...");
45
		
46
		boolean delete = bmiConfig.isDeleteAll();
47

    
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

    
57
			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
				TaxonBase taxonBase = taxonMap.get(taxonId);
70
				
71
				if (taxonBase != null){
72
					Taxon taxon;
73
					if ( taxonBase instanceof Taxon ) {
74
						taxon = (Taxon) taxonBase;
75
					}else{
76
						logger.warn("TaxonBase for Fact " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
77
						continue;
78
					}
79
					
80
					TaxonDescription taxonDescription = TaxonDescription.NewInstance();
81
					
82
					taxon.addDescription(taxonDescription);
83
					//textData
84
//					TextData textData = TextData.NewInstance();
85
//					textData.addText("XXX", bmiConfig.getFactLanguage());
86
//					taxonDescription.addFeature(textData);
87
					
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
					if (categoryFk == FACT_DESCRIPTION){
99
						//;
100
					}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
					
113
					taxonStore.add(taxon);
114
				}else{
115
					//TODO
116
					logger.warn("Taxon for Fact " + factId + " does not exist in store");
117
				}
118
				//put
119
			}
120
			logger.info("Taxa to save: " + taxonStore.size());
121
			taxonService.saveTaxonAll(taxonStore);	
122
			
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
}
(2-2/10)