Project

General

Profile

Download (8.21 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.commons.lang.StringUtils;
20
import org.apache.log4j.Logger;
21
import org.springframework.stereotype.Component;
22

    
23
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
24
import eu.etaxonomy.cdm.io.common.IOValidator;
25
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26
import eu.etaxonomy.cdm.io.common.mapping.IMappingImport;
27
import eu.etaxonomy.cdm.io.globis.validation.GlobisReferenceImportValidator;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
31
import eu.etaxonomy.cdm.model.reference.ReferenceType;
32

    
33

    
34
/**
35
 * @author a.mueller
36
 * @created 20.02.2010
37
 * @version 1.0
38
 */
39
@Component
40
public class GlobisReferenceImport  extends GlobisImportBase<Reference> implements IMappingImport<Reference, GlobisImportState>{
41
	private static final Logger logger = Logger.getLogger(GlobisReferenceImport.class);
42
	
43
	private int modCount = 10000;
44
	private static final String pluralString = "references";
45
	private static final String dbTableName = "Literatur";
46
	private static final Class cdmTargetClass = Reference.class;
47

    
48
	public GlobisReferenceImport(){
49
		super(pluralString, dbTableName, cdmTargetClass);
50
	}
51

    
52

    
53
	
54
	
55
	/* (non-Javadoc)
56
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#getIdQuery()
57
	 */
58
	@Override
59
	protected String getIdQuery() {
60
		String strRecordQuery = 
61
			" SELECT refID " + 
62
			" FROM " + dbTableName
63
			+ " WHERE RefSource like 'Original' or refID in (SELECT fiSpecRefId FROM specTax)"; 
64
		return strRecordQuery;	
65
	}
66

    
67

    
68

    
69

    
70
	/* (non-Javadoc)
71
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
72
	 */
73
	@Override
74
	protected String getRecordQuery(GlobisImportConfigurator config) {
75
		String strRecordQuery = 
76
			" SELECT l.*, l.DateCreated as Created_When, l.CreatedBy as Created_Who," +
77
			"        l.ModifiedBy as Updated_who, l.DateModified as Updated_When, l.RefRemarks as Notes " + 
78
			" FROM " + getTableName() + " l " +
79
			" WHERE ( l.refId IN (" + ID_LIST_TOKEN + ") )";
80
		return strRecordQuery;
81
	}
82
	
83

    
84

    
85
	/* (non-Javadoc)
86
	 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
87
	 */
88
	@Override
89
	public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
90
		boolean success = true;
91
		
92
		Set<Reference> objectsToSave = new HashSet<Reference>();
93
		
94
//		Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
95
//		Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
96
		
97
		ResultSet rs = partitioner.getResultSet();
98

    
99
		try {
100
			
101
			int i = 0;
102

    
103
			//for each reference
104
            while (rs.next()){
105
                
106
        		if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
107
				
108
        		Integer refId = rs.getInt("RefId");
109
        		String title = rs.getString("RefTitle");
110
        		String refJournal = rs.getString("RefJournal");
111
				
112
				try {
113
					
114
					//source ref
115
					Reference<?> sourceRef = state.getTransactionalSourceReference();
116
				
117
					Reference<?> ref = createObject(rs, state);
118
					ref.setTitle(title);
119
					
120
					if (StringUtils.isNotBlank(refJournal)){
121
						if (ref.getType().equals(ReferenceType.Article) ){
122
							Reference<?> journal = getJournal(state, rs, refJournal);
123
							ref.setInJournal(journal);
124
						}else{
125
							logger.warn("Reference type not supported for journal: " + ref.getType().toString() + ", refId: " + refId );
126
						}
127
					}
128
					
129
					this.doIdCreatedUpdatedNotes(state, ref, rs, refId, REFERENCE_NAMESPACE);
130
					
131
					
132
					
133
					//DONE
134
//					RefType, RefTitle, RefJournal
135
					
136
					//TODO
137
					//Refid,CreatedBy,DateCreated,DateModified,ModifiedBy
138
					//RefAuthor,RefBookTitle,RefDatePublished
139
					//RefEdition, RefEditor, RefGeneralKeywords
140
					//RefGeoKeywords, RefIll only,RefISSN, RefJournal
141
					//RefLibrary, RefMarker,RefPages,RefPages only,
142
					//RefPlace, RefPublisher, RefRemarks,
143
					//RefSerial, RefSource, RefSpecificKeywords, RefTaxKeywords,
144
					//RefURL, RefVolPageFig, RefVolume, RefYear
145
					//SpecificKeywordDummy
146
					
147
					//no data
148
						//CountryDummy
149
					
150
					objectsToSave.add(ref); 
151
					
152

    
153
				} catch (Exception e) {
154
					logger.warn("Exception in literature: RefId " + refId + ". " + e.getMessage());
155
//					e.printStackTrace();
156
				} 
157
                
158
            }
159
           
160
//            logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
161

    
162
			logger.warn(pluralString + " to save: " + objectsToSave.size());
163
			getReferenceService().save(objectsToSave);	
164
			
165
			return success;
166
		} catch (SQLException e) {
167
			logger.error("SQLException:" +  e);
168
			return false;
169
		}
170
	}
171

    
172

    
173

    
174
	
175
	private Reference<?> getJournal(GlobisImportState state, ResultSet rs, String refJournal) throws SQLException {
176
		
177
		
178
		Reference<?> journal = ReferenceFactory.newJournal();
179
		String issn = rs.getString("RefISSN");
180
		if (StringUtils.isNotBlank(issn)){
181
			issn.replaceAll("ISSN", "").trim();
182
			journal.setIssn(issn);			
183
		}
184

    
185
		
186
		
187
		//TODO deduplicate
188
		journal.setTitle(refJournal);
189
		return journal;
190
	}
191

    
192

    
193

    
194

    
195
	/* (non-Javadoc)
196
	 * @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
197
	 */
198
	public Reference<?> createObject(ResultSet rs, GlobisImportState state)
199
			throws SQLException {
200
		String refJournal = rs.getString("RefJournal");
201
		boolean isInJournal =isNotBlank(refJournal); 
202
		String refBookTitle = rs.getString("RefBookTitle");
203
		boolean isInBook =isNotBlank(refBookTitle); 
204
		
205
		
206
		
207
		Reference<?> ref;
208
		String refType = rs.getString("RefType");
209
		if (refType == null){
210
			if (isInJournal && ! isInBook){
211
				ref = ReferenceFactory.newArticle();
212
			}else{
213
				ref = ReferenceFactory.newGeneric();
214
			}
215
		}else if (refType.equals("book")){
216
			ref = ReferenceFactory.newBook();
217
		}else if (refType.equals("paper in journal")){
218
			ref = ReferenceFactory.newArticle();
219
		}else if (refType.startsWith("unpublished") ){
220
			ref = ReferenceFactory.newGeneric();
221
		}else if (refType.endsWith("paper in journal")){
222
			ref = ReferenceFactory.newArticle();
223
		}else if (refType.equals("paper in book")){
224
			ref = ReferenceFactory.newBookSection();
225
		}else if (refType.matches("paper in journal.*website.*")){
226
			ref = ReferenceFactory.newArticle();
227
		}else{
228
			logger.warn("Unknown reference type: " + refType);
229
			ref = ReferenceFactory.newGeneric();
230
		}
231
		return ref;
232
	}
233

    
234
	/* (non-Javadoc)
235
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
236
	 */
237
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
238
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
239
		return result;  //not needed
240
	}
241
	
242
	/* (non-Javadoc)
243
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
244
	 */
245
	@Override
246
	protected boolean doCheck(GlobisImportState state){
247
		IOValidator<GlobisImportState> validator = new GlobisReferenceImportValidator();
248
		return validator.validate(state);
249
	}
250
	
251
	
252
	/* (non-Javadoc)
253
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
254
	 */
255
	protected boolean isIgnore(GlobisImportState state){
256
		//TODO
257
		return state.getConfig().getDoReferences() != IImportConfigurator.DO_REFERENCES.ALL;
258
	}
259

    
260

    
261

    
262

    
263

    
264
}
(6-6/9)