Project

General

Profile

Download (8.65 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
package eu.etaxonomy.cdm.io.berlinModel.out;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

    
17
import org.apache.log4j.Logger;
18
import org.springframework.stereotype.Component;
19
import org.springframework.transaction.TransactionStatus;
20

    
21
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
22
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.RefDetailMapper;
23
import eu.etaxonomy.cdm.io.common.Source;
24
import eu.etaxonomy.cdm.io.common.mapping.out.CdmDbExportMapping;
25
import eu.etaxonomy.cdm.io.common.mapping.out.CreatedAndNotesMapper;
26
import eu.etaxonomy.cdm.io.common.mapping.out.DbObjectMapper;
27
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
28
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
29
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.common.RelationshipBase;
32
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
33
import eu.etaxonomy.cdm.model.name.HybridRelationship;
34
import eu.etaxonomy.cdm.model.name.NameRelationship;
35
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
36
import eu.etaxonomy.cdm.model.reference.Reference;
37

    
38

    
39
/**
40
 * @author a.mueller
41
 * @created 20.03.2008
42
 * @version 1.0
43
 */
44
@Component
45
public class BerlinModelNameRelationExport extends BerlinModelExportBase<RelationshipBase> {
46
	private static final Logger logger = Logger.getLogger(BerlinModelNameRelationExport.class);
47

    
48
	private static int modCount = 100;
49
	private static final String dbTableName = "RelName";
50
	private static final String pluralString = "NameRelationships";
51
	private static final Class<? extends CdmBase> standardMethodParameter = RelationshipBase.class;
52

    
53

    
54
	public BerlinModelNameRelationExport(){
55
		super();
56
	}
57
	
58
	/* (non-Javadoc)
59
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
60
	 */
61
	@Override
62
	protected boolean doCheck(BerlinModelExportState state){
63
		boolean result = true;
64
		logger.warn("Checking for " + pluralString + " not yet implemented");
65
		//result &= checkArticlesWithoutJournal(bmiConfig);
66
		//result &= checkPartOfJournal(bmiConfig);
67
		
68
		return result;
69
	}
70
	
71
	private CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> getMapping(){
72
		String tableName = dbTableName;
73
		CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = new CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer>(tableName);
74
		mapping.addMapper(IdMapper.NewInstance("RelNameId"));
75

    
76
		mapping.addMapper(DbObjectMapper.NewInstance("fromName", "NameFk1"));
77
		mapping.addMapper(DbObjectMapper.NewInstance("toName", "NameFk2"));
78

    
79
		
80
		mapping.addMapper(MethodMapper.NewInstance("RelNameQualifierFk", this));
81
		
82
		mapping.addMapper(DbObjectMapper.NewInstance("citation", "RefFk"));
83
		mapping.addMapper(RefDetailMapper.NewInstance("citationMicroReference","citation", "RefDetailFk"));
84
		mapping.addMapper(CreatedAndNotesMapper.NewInstance());
85
		
86
		return mapping;
87
	}
88
	
89
	protected void doInvoke(BerlinModelExportState state){
90
		try{
91
			logger.info("start make " + pluralString + " ...");
92
			boolean success = true ;
93
			doDelete(state);
94
			
95
			TransactionStatus txStatus = startTransaction(true);
96
			
97
			List<RelationshipBase> list = getNameService().getAllRelationships(100000000, 0);
98
			
99
			CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = getMapping();
100
			mapping.initialize(state);
101
			
102
			int count = 0;
103
			for (RelationshipBase<?,?,?> rel : list){
104
				if (rel.isInstanceOf(NameRelationship.class) || rel.isInstanceOf(HybridRelationship.class )){
105
					doCount(count++, modCount, pluralString);
106
					success &= mapping.invoke(rel);
107
				}
108
			}
109
			commitTransaction(txStatus);
110
			
111
			success &= makeIsHomotypicRelation(state, mapping);
112
			
113
			logger.info("end make " + pluralString + " ..." + getSuccessString(success));
114
			if (!success){
115
				state.setUnsuccessfull();
116
			}
117
			return;
118
		}catch(SQLException e){
119
			e.printStackTrace();
120
			logger.error(e.getMessage());
121
			state.setUnsuccessfull();
122
			return;
123
		}
124
	}
125

    
126
	
127
	private boolean makeIsHomotypicRelation(BerlinModelExportState state, CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping){
128
		boolean success = true ;
129
		try{
130
			Integer homotypicId = state.getConfig().getIsHomotypicId();
131
			if (homotypicId == null){
132
				return success;
133
			}
134
			logger.info("start make IsHomotypicRelations ...");
135
			
136
			TransactionStatus txStatus = startTransaction(true);
137
			
138
			List<HomotypicalGroup> list = getNameService().getAllHomotypicalGroups(100000000, 0);
139
			
140
			int count = 0;
141
			modCount = 1000;
142
			Set<NameRelationship> basionymNameRels = new HashSet<NameRelationship>(); 
143
			for (HomotypicalGroup homoGroup : list){
144
				doCount(count++, modCount, "homotypical groups");
145
				Set<TaxonNameBase> allNames = homoGroup.getTypifiedNames();
146
				if (allNames.size() > 1){
147
					Set<TaxonNameBase> readyNames = new HashSet<TaxonNameBase>();
148
					Set<TaxonNameBase> unrelateds = homoGroup.getUnrelatedNames();
149
					for (TaxonNameBase unrelated : unrelateds){
150
						for (TaxonNameBase oneOfAllNames: allNames){
151
							if(!unrelated.equals(oneOfAllNames) && ! readyNames.contains(oneOfAllNames)){
152
								success &= invokeIsHomotypic(state, mapping, unrelated, oneOfAllNames, null, null);
153
							}
154
						}
155
						readyNames.add(unrelated);
156
					}
157
				}
158
			}
159
			commitTransaction(txStatus);
160
			
161
			logger.info("end make homotypical groups ... " +  getSuccessString(success));
162
			return success;
163
		}catch(SQLException e){
164
			logger.error(e.getMessage());
165
			e.printStackTrace();
166
			return false;
167
		}	
168
	}
169
	
170
	private boolean invokeIsHomotypic(BerlinModelExportState state, CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping, TaxonNameBase fromName, TaxonNameBase toName, Reference refId, String microCitation) throws SQLException{
171
		try{
172
			logger.info(fromName.getTitleCache() + "->" + toName.getTitleCache());
173
			String maxQuery = " SELECT max(relNameId) as max FROM relName ";
174
			ResultSet rs = state.getConfig().getDestination().getResultSet(maxQuery);
175
			int maxId = 1;
176
			if (rs.next()){
177
				maxId = rs.getInt("max") + 1;
178
			}
179
			int fromNameId = state.getDbId(fromName);
180
			int toNameId = state.getDbId(toName);
181
			int catId = state.getConfig().getIsHomotypicId();
182
			String query = "INSERT INTO relName (relNameId, nameFk1, nameFk2, RelNameQualifierFk) " + 
183
				" VALUES ("+maxId+","+fromNameId+","+toNameId+","+catId+")";
184
			int ui = state.getConfig().getDestination().getConnection().createStatement().executeUpdate(query);
185
		}catch(SQLException e){
186
			throw e;
187
		}
188
		return true;
189
	}
190

    
191
	private Set<TaxonNameBase> getAllRelatedNames(Set<NameRelationship> rels){
192
		Set<TaxonNameBase> result = new HashSet<TaxonNameBase>();
193
		for (NameRelationship rel : rels){
194
			result.add(rel.getFromName());
195
			result.add(rel.getToName());
196
		}
197
		return result;
198
	}
199
	
200
	protected boolean doDelete(BerlinModelExportState state){
201
		BerlinModelExportConfigurator bmeConfig = state.getConfig();
202
		
203
		String sql;
204
		Source destination =  bmeConfig.getDestination();
205
		//RelPTaxon
206
		sql = "DELETE FROM RelName";
207
		destination.setQuery(sql);
208
		destination.update(sql);
209

    
210
		return true;
211
	}
212
		
213
	
214
	/* (non-Javadoc)
215
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
216
	 */
217
	protected boolean isIgnore(BerlinModelExportState state){
218
		return ! state.getConfig().isDoRelNames();
219
	}
220
	
221
	//called by MethodMapper
222
	@SuppressWarnings("unused")
223
	private static Integer getRelNameQualifierFk(RelationshipBase<?, ?, ?> rel) throws Exception {
224
//		if (config.getRelNameQualifierMethod() != null){
225
//			try {
226
//				return (Integer)config.getRelNameQualifierMethod().invoke(rel);
227
//			} catch (Exception e) {
228
//				logger.error(e.getMessage());
229
//				throw e;
230
//			}
231
//		}else{
232
			return BerlinModelTransformer.nameRel2RelNameQualifierFk(rel);
233
//		}
234
	}
235
	
236
	/* (non-Javadoc)
237
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
238
	 */
239
	@Override
240
	public Class<? extends CdmBase> getStandardMethodParameter() {
241
		return standardMethodParameter;
242
	}
243
}
(8-8/12)