Project

General

Profile

« Previous | Next » 

Revision 8422c0cd

Added by Andreas Müller almost 8 years ago

Remove generics from Reference in cdmlib-app #5830

View differences:

app-import/src/main/java/eu/etaxonomy/cdm/io/algaterra/AlgaTerraFactEcologyImport.java
1 1
/**
2 2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
3
* European Distributed Institute of Taxonomy
4 4
* http://www.e-taxonomy.eu
5
* 
5
*
6 6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
* See LICENSE.TXT at the top of this package for the full license terms.
8 8
*/
......
56 56
public class AlgaTerraFactEcologyImport  extends AlgaTerraSpecimenImportBase {
57 57
	private static final Logger logger = Logger.getLogger(AlgaTerraFactEcologyImport.class);
58 58

  
59
	
59

  
60 60
	private static int modCount = 5000;
61 61
	private static final String pluralString = "determinations";
62
	private static final String dbTableName = "Fact"; 
62
	private static final String dbTableName = "Fact";
63 63

  
64 64

  
65 65
	public AlgaTerraFactEcologyImport(){
66 66
		super(dbTableName, pluralString);
67 67
	}
68
	
68

  
69 69

  
70 70
	@Override
71 71
	protected String getIdQuery(BerlinModelImportState state) {
72
		String result = " SELECT f.factId " + 
73
				" FROM Fact f LEFT JOIN PTaxon pt ON f.PTNameFk = pt.PTNameFk AND f.PTRefFk = pt.PTRefFk " 
72
		String result = " SELECT f.factId " +
73
				" FROM Fact f LEFT JOIN PTaxon pt ON f.PTNameFk = pt.PTNameFk AND f.PTRefFk = pt.PTRefFk "
74 74
				+ " WHERE f.FactCategoryFk = 202 "
75 75
				+ " ORDER BY pt.RIdentifier, f.FactId ";
76 76
		return result;
......
78 78

  
79 79
	@Override
80 80
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
81
			String strQuery =   
82
            " SELECT pt.RIdentifier as taxonId, f.* " + 
83
            " FROM Fact f " + 
81
			String strQuery =
82
            " SELECT pt.RIdentifier as taxonId, f.* " +
83
            " FROM Fact f " +
84 84
                 " LEFT JOIN PTaxon pt ON f.PTNameFk =pt.PTNameFk AND f.PTRefFk = pt.PTRefFk " +
85
             " WHERE f.FactCategoryFk = 202 AND (f.FactId IN (" + ID_LIST_TOKEN + ")  )"  
85
             " WHERE f.FactCategoryFk = 202 AND (f.FactId IN (" + ID_LIST_TOKEN + ")  )"
86 86
            + " ORDER BY pt.RIdentifier, f.FactId "
87 87
            ;
88 88
		return strQuery;
......
91 91
	@Override
92 92
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState bmState) {
93 93
		boolean success = true;
94
		
94

  
95 95
		AlgaTerraImportState state = (AlgaTerraImportState)bmState;
96 96
		Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
97
		
98
		Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
99
		Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
100
		
97

  
98
		Map<String, TaxonBase> taxonMap = partitioner.getObjectMap(BerlinModelTaxonImport.NAMESPACE);
99
		Map<String, DerivedUnit> ecoFactDerivedUnitMap = partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
100

  
101 101
		ResultSet rs = partitioner.getResultSet();
102 102

  
103 103
		try {
104
			
104

  
105 105
			int i = 0;
106 106

  
107 107
			//for each reference
108 108
            while (rs.next()){
109
                
109

  
110 110
        		if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
111
				
111

  
112 112
				Integer taxonId = nullSafeInt(rs, "taxonId");
113 113
				int factId = rs.getInt("FactId");
114 114
				Integer ecoFactId = nullSafeInt(rs, "ExtensionFk");
115 115
				String recordBasis = rs.getString("RecordBasis");
116
				
117
				
116

  
117

  
118 118
				try {
119
					
119

  
120 120
					//source ref
121
					Reference<?> sourceRef = state.getTransactionalSourceReference();
122
				
121
					Reference sourceRef = state.getTransactionalSourceReference();
122

  
123 123
					DerivedUnit ecoFact = ecoFactDerivedUnitMap.get(String.valueOf(ecoFactId));
124 124
					if (ecoFact == null){
125 125
						logger.warn("EcoFact is null for EcoFact: " + CdmUtils.Nz(ecoFactId) + ", taxonId: " + CdmUtils.Nz(taxonId));
126 126
					}
127
					
127

  
128 128
					//description element
129 129
					if (taxonId != null){
130
						Taxon taxon = getTaxon(state, taxonId, taxonMap, factId);		
131
						
130
						Taxon taxon = getTaxon(state, taxonId, taxonMap, factId);
131

  
132 132
						if(taxon != null){
133 133
							DerivedUnit identifiedSpecimen = makeIdentifiedSpecimen(ecoFact, recordBasis, taxonId, ecoFactId);
134
							
134

  
135 135
							makeDetermination(state, rs, taxon, identifiedSpecimen, factId, partitioner);
136
													
136

  
137 137
							makeIndividualsAssociation(state, taxon, sourceRef, identifiedSpecimen);
138
							
138

  
139 139
							this.doIdCreatedUpdatedNotes(state, identifiedSpecimen, rs, factId, getDerivedUnitNameSpace());
140 140

  
141 141
							identifiedSpecimen.setCacheStrategy(new DerivedUnitFacadeCacheStrategy());
......
144 144
					}else{
145 145
						logger.warn("No taxon defined for ecology fact: " +  factId);
146 146
					}
147
					
147

  
148 148

  
149 149
				} catch (Exception e) {
150 150
					logger.warn("Exception in FactEcology: FactId " + factId + ". " + e.getMessage());
151 151
					e.printStackTrace();
152
				} 
153
                
152
				}
153

  
154 154
            }
155
           
155

  
156 156
//            logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
157 157

  
158 158
			logger.warn("Taxa to save: " + taxaToSave.size());
159
			getTaxonService().save(taxaToSave);	
160
			
159
			getTaxonService().save(taxaToSave);
160

  
161 161
			return success;
162 162
		} catch (SQLException e) {
163 163
			logger.error("SQLException:" +  e);
164 164
			return false;
165 165
		}
166 166
	}
167
	
168
	private void makeIndividualsAssociation(AlgaTerraImportState state, Taxon taxon, Reference<?> sourceRef, DerivedUnit identifiedSpecimen){
167

  
168
	private void makeIndividualsAssociation(AlgaTerraImportState state, Taxon taxon, Reference sourceRef, DerivedUnit identifiedSpecimen){
169 169
		TaxonDescription taxonDescription = getTaxonDescription(state, taxon, sourceRef);
170 170
		IndividualsAssociation indAssociation = IndividualsAssociation.NewInstance();
171 171
		Feature feature = makeFeature(identifiedSpecimen.getRecordBasis(), state);
172 172
		indAssociation.setAssociatedSpecimenOrObservation(identifiedSpecimen);
173 173
		indAssociation.setFeature(feature);
174
		taxonDescription.addElement(indAssociation);	
174
		taxonDescription.addElement(indAssociation);
175 175
	}
176
	
176

  
177 177
	private void makeDetermination(AlgaTerraImportState state, ResultSet rs, Taxon taxon, DerivedUnit identifiedSpecimen, int factId, ResultSetPartitioner partitioner) throws SQLException {
178 178
		Date identifiedWhen = rs.getDate("IdentifiedWhen");
179 179
		Date identifiedWhenEnd = rs.getDate("IdentiedWhenEnd");
......
182 182
		String identifiedBy = rs.getString("IdentifiedBy");
183 183
		String identificationReference = rs.getString("IdentificationReference");
184 184
		Integer refFk = nullSafeInt(rs, "IdentifidationRefFk");
185
		
186
		
185

  
186

  
187 187
		DeterminationEvent determination = DeterminationEvent.NewInstance(taxon, identifiedSpecimen);
188 188
		TimePeriod determinationPeriod = TimePeriod.NewInstance(identifiedWhen, identifiedWhenEnd);
189 189
		determination.setTimeperiod(determinationPeriod);
190 190
		determination.setPreferredFlag(! restrictedFlag);
191
		//TODO 
192
		
191
		//TODO
192

  
193 193
		TeamOrPersonBase<?> author = getAuthor(identifiedBy);
194 194
		determination.setDeterminer(author);
195 195
		if (refFk != null){
196
			Map<String, Reference> refMap = (Map<String, Reference>) partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
197
			
198
			Reference<?> ref = refMap.get(String.valueOf(refFk));
196
			Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
197

  
198
			Reference ref = refMap.get(String.valueOf(refFk));
199 199
			if (ref != null){
200 200
				determination.addReference(ref);
201 201
			}else{
......
207 207
				logger.warn("IdentificationReference exists without IdentificationRefFk. FactId: "+  factId);
208 208
			}
209 209
		}
210
		
211
		
212
		
210

  
211

  
212

  
213 213
		//TODO
214
//		kind of identification, IdentificationUncertainty, IdentificationMethod, 
215
		
216
		
214
//		kind of identification, IdentificationUncertainty, IdentificationMethod,
215

  
216

  
217 217
	}
218 218

  
219 219

  
220
                                                                                                                                                                                                                                                                                          
220

  
221 221
	private DerivedUnit makeIdentifiedSpecimen(DerivedUnit ecoFact, String recordBasis, Integer taxonId, Integer ecoFactId) {
222 222
		//TODO event type
223 223
		DerivationEvent event = DerivationEvent.NewInstance();
......
227 227
			logger.warn(String.format(message, recordBasis, taxonId));
228 228
			derivedUnitType = SpecimenOrObservationType.PreservedSpecimen;
229 229
		}
230
		
230

  
231 231
		DerivedUnit result = DerivedUnit.NewInstance(derivedUnitType);
232 232
		result.setDerivedFrom(event);
233 233
		if (ecoFact == null){
......
241 241

  
242 242

  
243 243

  
244
	protected String getDerivedUnitNameSpace(){
244
	@Override
245
    protected String getDerivedUnitNameSpace(){
245 246
		return FACT_ECOLOGY_NAMESPACE;
246 247
	}
247
	
248
	protected String getFieldObservationNameSpace(){
248

  
249
	@Override
250
    protected String getFieldObservationNameSpace(){
249 251
		return null;
250 252
	}
251 253

  
......
256 258
	 * @param state
257 259
	 * @param ecoFactId
258 260
	 * @param derivedUnitMap
259
	 * @param type 
261
	 * @param type
260 262
	 * @return
261 263
	 */
262 264
	private DerivedUnitFacade getDerivedUnit(AlgaTerraImportState state, int ecoFactId, Map<String, DerivedUnit> derivedUnitMap, SpecimenOrObservationType type) {
......
274 276
				facade = DerivedUnitFacade.NewInstance(type);
275 277
			}
276 278
		}
277
		
279

  
278 280
		return facade;
279 281
	}
280 282

  
......
284 286
		Class<?> cdmClass;
285 287
		Set<String> idSet;
286 288
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
287
		
289

  
288 290
		try{
289 291
			Set<String> taxonIdSet = new HashSet<String>();
290 292
			Set<String> extensionFkSet = new HashSet<String>();
291 293
			Set<String> referenceIdSet = new HashSet<String>();
292
			
294

  
293 295
			while (rs.next()){
294 296
				handleForeignKey(rs, taxonIdSet, "taxonId");
295 297
				handleForeignKey(rs, extensionFkSet, "extensionFk");
296 298
				handleForeignKey(rs, referenceIdSet, "IdentifidationRefFk");
297 299
			}
298
			
300

  
299 301
			//taxon map
300 302
			nameSpace = BerlinModelTaxonImport.NAMESPACE;
301 303
			cdmClass = TaxonBase.class;
......
317 319
			Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
318 320
			result.put(nameSpace, referenceMap);
319 321

  
320
			
322

  
321 323
		} catch (SQLException e) {
322 324
			throw new RuntimeException(e);
323 325
		}
......
334 336
	protected boolean isIgnore(BerlinModelImportState state){
335 337
		return ! ((AlgaTerraImportState)state).getAlgaTerraConfigurator().isDoFactEcology();
336 338
	}
337
	
339

  
338 340
}

Also available in: Unified diff