Project

General

Profile

Download (8.02 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.io.globis;
11

    
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.Map;
17
import java.util.Set;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
23
import eu.etaxonomy.cdm.model.agent.Person;
24
import eu.etaxonomy.cdm.model.agent.Team;
25
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.Language;
28
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
29
import eu.etaxonomy.cdm.model.common.TimePeriod;
30
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
31
import eu.etaxonomy.cdm.model.description.TaxonDescription;
32
import eu.etaxonomy.cdm.model.location.NamedArea;
33
import eu.etaxonomy.cdm.model.location.Country;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38

    
39

    
40
/**
41
 * @author a.mueller
42
 * @created 20.02.2010
43
 * 
44
 * OPEN ISSUES:
45
 * 
46
 * ...
47
 */
48
@Component
49
public class GlobisCommonNameImport  extends GlobisImportBase<Taxon> {
50
	private static final Logger logger = Logger.getLogger(GlobisCommonNameImport.class);
51
	
52
	private int modCount = 10000;
53
	private static final String pluralString = "common names";
54
	private static final String dbTableName = "species_language";
55
	private static final Class<?> cdmTargetClass = Taxon.class;  //not needed
56
	
57
	public GlobisCommonNameImport(){
58
		super(pluralString, dbTableName, cdmTargetClass);
59
	}
60

    
61
	//dirty but acceptable for globis environment
62
	private Map<Integer,Reference> refMap = new HashMap<Integer,Reference>();
63
	
64
	@Override
65
	protected String getIdQuery() {
66
		String strRecordQuery = 
67
			" SELECT ID " + 
68
			" FROM " + dbTableName; 
69
		return strRecordQuery;	
70
	}
71

    
72

    
73
	@Override
74
	protected String getRecordQuery(GlobisImportConfigurator config) {
75
		String strRecordQuery = 
76
			" SELECT * " + 
77
			" FROM " + getTableName() + " sl " +
78
			" WHERE ( sl.ID IN (" + ID_LIST_TOKEN + ") )";
79
		return strRecordQuery;
80
	}
81
	
82
	@Override
83
	public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
84
		boolean success = true;
85
		
86
		Set<TaxonBase> objectsToSave = new HashSet<TaxonBase>();
87
		
88
		Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
89
		
90
		ResultSet rs = partitioner.getResultSet();
91

    
92
		try {
93
			
94
			int i = 0;
95

    
96
			//for each common name
97
            while (rs.next()){
98
                
99
        		if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
100
				
101
        		Integer idTaxon = nullSafeInt(rs,"IDCurrentSpec");
102
				
103
        		try {
104
					
105
					//source ref
106
					Reference<?> sourceRef = state.getTransactionalSourceReference();
107
			
108
					//common names
109
					Integer id = nullSafeInt(rs,"ID");
110
					String isoLang = rs.getString("ISO");
111
					String strCommonName = rs.getString("commonname");
112
					Integer refID = nullSafeInt(rs,"ReferenceID");
113
					String strCountryCode = rs.getString("Code2");
114
					
115
					
116
					Taxon taxon = taxonMap.get(String.valueOf(idTaxon));
117
					if (taxon == null){
118
						logger.warn("No taxon found for taxonId " + idTaxon);
119
					}else if (isBlank(strCommonName)){
120
						logger.warn("No common name string defined for common name ID: " + id);
121
					}else{
122
						Language language = getLanguage(isoLang);
123
						if (language == null){
124
							logger.warn("No language found for common name ID: " + id);
125
						}
126
						NamedArea area = Country.getCountryByIso3166A2(strCountryCode);
127
						if (area == null){
128
							logger.warn("No country found for common name ID: " + id);
129
						}
130

    
131
						TaxonDescription taxonDescription = getTaxonDescription(taxon, sourceRef, ! IMAGE_GALLERY,  CREATE);
132
						CommonTaxonName commonName = CommonTaxonName.NewInstance(strCommonName, language, area);
133
						taxonDescription.addElement(commonName);
134
						
135
						Reference<?> ref = handleReference(state, refID);
136
						if (ref == null && refID != null){
137
							logger.warn("No reference found for common name ID: " + id);
138
						}else{
139
							commonName.addSource(OriginalSourceType.Import, String.valueOf(refID), "reference", sourceRef, null);
140
						}
141
						
142
						objectsToSave.add(taxon); 
143
					}
144

    
145
				} catch (Exception e) {
146
					logger.warn("Exception in current_species: IDcurrentspec " + idTaxon + ". " + e.getMessage());
147
					e.printStackTrace();
148
				} 
149
                
150
            }
151
           
152
			logger.warn(pluralString + " to save: " + objectsToSave.size());
153
			getTaxonService().save(objectsToSave);	
154
			
155
			return success;
156
		} catch (SQLException e) {
157
			logger.error("SQLException:" +  e);
158
			return false;
159
		}
160
	}
161

    
162
	
163
	private Map<String,Language> languageMap = new HashMap<String,Language>();
164
	private Language getLanguage(String isoLang) {
165
		Language result = languageMap.get(isoLang);
166
		if (result == null){
167
		
168
			result = getTermService().getLanguageByIso(isoLang);
169
			if (result == null){
170
				logger.warn("No language found for iso code: " + isoLang);
171
			}
172
		}
173
		return result;
174

    
175
	}
176
	
177
	private Reference<?> handleReference(GlobisImportState state, Integer refId){
178
		if (refId == null){
179
			return null;
180
		}
181
		Reference<?> result = refMap.get(refId);
182
		
183
		if (result == null){
184
			try {
185
				String sql = "SELECT * FROM [references] WHERE ReferenceID = " + refId;
186
				ResultSet rs = state.getConfig().getSource().getResultSet(sql);
187
				rs.next();
188
				
189
				String authors = rs.getString("Author(s)");
190
				String title = rs.getString("Title");
191
				String details = rs.getString("Details");
192
				Integer year = nullSafeInt(rs, "year");
193
				result = ReferenceFactory.newGeneric();
194
				result.setTitleCache(details, true);
195
				result.setTitle(title);
196
				result.setDatePublished(TimePeriod.NewInstance(year));
197

    
198
				TeamOrPersonBase<?> author;
199
				String[] authorSplit = authors.split("&");
200
				if (authorSplit.length > 1){
201
					Team team = Team.NewInstance();
202
					author = team;
203
					for (String singleAuthor : authorSplit){
204
						Person person = makeSingleAuthor(singleAuthor);
205
						team.addTeamMember(person);
206
					}
207
				}else{
208
					author = makeSingleAuthor(authors);
209
				}
210
				
211
				result.setAuthorship(author);
212
				refMap.put(refId,result);
213
				rs.close();
214
			} catch (SQLException e) {
215
				e.printStackTrace();
216
			}
217

    
218
			
219
		}
220
		
221
		return result;
222
	}
223

    
224
	private Person makeSingleAuthor(String authors) {
225
		Person result = Person.NewTitledInstance(authors);
226
		return result;
227
	}
228

    
229
	@Override
230
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, GlobisImportState state) {
231
		String nameSpace;
232
		Class<?> cdmClass;
233
		Set<String> idSet;
234
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
235
		try{
236
			Set<String> taxonIdSet = new HashSet<String>();
237
			
238
			while (rs.next()){
239
				handleForeignKey(rs, taxonIdSet, "IDCurrentSpec");
240
			}
241
			
242
			//taxon map
243
			nameSpace = TAXON_NAMESPACE;
244
			cdmClass = Taxon.class;
245
			idSet = taxonIdSet;
246
			Map<String, Taxon> objectMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
247
			result.put(nameSpace, objectMap);
248

    
249
			
250
		} catch (SQLException e) {
251
			throw new RuntimeException(e);
252
		}
253
		return result;
254
	}
255
	
256

    
257
	@Override
258
	protected boolean doCheck(GlobisImportState state){
259
//		IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator();
260
		return true;
261
	}
262
	
263
	@Override
264
	protected boolean isIgnore(GlobisImportState state){
265
		return ! state.getConfig().isDoCommonNames();
266
	}
267

    
268

    
269

    
270

    
271

    
272
}
(2-2/10)