Project

General

Profile

Download (8.85 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.TaxonName;
36
import eu.etaxonomy.cdm.model.reference.Reference;
37

    
38

    
39
/**
40
 * @author a.mueller
41
 * @since 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
	@Override
90
    protected void doInvoke(BerlinModelExportState state){
91
		try{
92
			logger.info("start make " + pluralString + " ...");
93
			boolean success = true ;
94
			doDelete(state);
95

    
96
			TransactionStatus txStatus = startTransaction(true);
97

    
98
			@SuppressWarnings({ "unchecked", "rawtypes" })
99
            List<RelationshipBase<?,?,?>> list = (List)getNameService().listNameRelationships(null, null, null, null, null);
100
			list.addAll(getNameService().listHybridRelationships(null, null, null, null, null));
101

    
102
			CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = getMapping();
103
			mapping.initialize(state);
104

    
105
			int count = 0;
106
			for (RelationshipBase<?,?,?> rel : list){
107
				if (rel.isInstanceOf(NameRelationship.class) || rel.isInstanceOf(HybridRelationship.class )){
108
					doCount(count++, modCount, pluralString);
109
					success &= mapping.invoke(rel);
110
				}
111
			}
112
			commitTransaction(txStatus);
113

    
114
			success &= makeIsHomotypicRelation(state, mapping);
115

    
116
			logger.info("end make " + pluralString + " ..." + getSuccessString(success));
117
			if (!success){
118
                String message = "An undefined error occurred during name relation export";
119
                state.getResult().addError(message);
120
			}
121
			return;
122
		}catch(SQLException e){
123
			e.printStackTrace();
124
			logger.error(e.getMessage());
125
			state.getResult().addException(e);
126
			return;
127
		}
128
	}
129

    
130

    
131
	private boolean makeIsHomotypicRelation(BerlinModelExportState state, CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping){
132
		boolean success = true ;
133
		try{
134
			Integer homotypicId = state.getConfig().getIsHomotypicId();
135
			if (homotypicId == null){
136
				return success;
137
			}
138
			logger.info("start make IsHomotypicRelations ...");
139

    
140
			TransactionStatus txStatus = startTransaction(true);
141

    
142
			List<HomotypicalGroup> list = getNameService().getAllHomotypicalGroups(100000000, 0);
143

    
144
			int count = 0;
145
			modCount = 1000;
146
			Set<NameRelationship> basionymNameRels = new HashSet<NameRelationship>();
147
			for (HomotypicalGroup homoGroup : list){
148
				doCount(count++, modCount, "homotypical groups");
149
				Set<TaxonName> allNames = homoGroup.getTypifiedNames();
150
				if (allNames.size() > 1){
151
					Set<TaxonName> readyNames = new HashSet<>();
152
					Set<TaxonName> unrelateds = homoGroup.getUnrelatedNames();
153
					for (TaxonName unrelated : unrelateds){
154
						for (TaxonName oneOfAllNames: allNames){
155
							if(!unrelated.equals(oneOfAllNames) && ! readyNames.contains(oneOfAllNames)){
156
								success &= invokeIsHomotypic(state, mapping, unrelated, oneOfAllNames, null, null);
157
							}
158
						}
159
						readyNames.add(unrelated);
160
					}
161
				}
162
			}
163
			commitTransaction(txStatus);
164

    
165
			logger.info("end make homotypical groups ... " +  getSuccessString(success));
166
			return success;
167
		}catch(SQLException e){
168
			logger.error(e.getMessage());
169
			e.printStackTrace();
170
			return false;
171
		}
172
	}
173

    
174
	private boolean invokeIsHomotypic(BerlinModelExportState state, CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping, TaxonName fromName, TaxonName toName, Reference refId, String microCitation) throws SQLException{
175
		try{
176
			logger.info(fromName.getTitleCache() + "->" + toName.getTitleCache());
177
			String maxQuery = " SELECT max(relNameId) as max FROM relName ";
178
			ResultSet rs = state.getConfig().getDestination().getResultSet(maxQuery);
179
			int maxId = 1;
180
			if (rs.next()){
181
				maxId = rs.getInt("max") + 1;
182
			}
183
			int fromNameId = state.getDbId(fromName);
184
			int toNameId = state.getDbId(toName);
185
			int catId = state.getConfig().getIsHomotypicId();
186
			String query = "INSERT INTO relName (relNameId, nameFk1, nameFk2, RelNameQualifierFk) " +
187
				" VALUES ("+maxId+","+fromNameId+","+toNameId+","+catId+")";
188
			int ui = state.getConfig().getDestination().getConnection().createStatement().executeUpdate(query);
189
		}catch(SQLException e){
190
			throw e;
191
		}
192
		return true;
193
	}
194

    
195
	private Set<TaxonName> getAllRelatedNames(Set<NameRelationship> rels){
196
		Set<TaxonName> result = new HashSet<>();
197
		for (NameRelationship rel : rels){
198
			result.add(rel.getFromName());
199
			result.add(rel.getToName());
200
		}
201
		return result;
202
	}
203

    
204
	protected boolean doDelete(BerlinModelExportState state){
205
		BerlinModelExportConfigurator bmeConfig = state.getConfig();
206

    
207
		String sql;
208
		Source destination =  bmeConfig.getDestination();
209
		//RelPTaxon
210
		sql = "DELETE FROM RelName";
211
		destination.setQuery(sql);
212
		destination.update(sql);
213

    
214
		return true;
215
	}
216

    
217

    
218
	/* (non-Javadoc)
219
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
220
	 */
221
	@Override
222
    protected boolean isIgnore(BerlinModelExportState state){
223
		return ! state.getConfig().isDoRelNames();
224
	}
225

    
226
	//called by MethodMapper
227
	@SuppressWarnings("unused")
228
	private static Integer getRelNameQualifierFk(RelationshipBase<?, ?, ?> rel) throws Exception {
229
//		if (config.getRelNameQualifierMethod() != null){
230
//			try {
231
//				return (Integer)config.getRelNameQualifierMethod().invoke(rel);
232
//			} catch (Exception e) {
233
//				logger.error(e.getMessage());
234
//				throw e;
235
//			}
236
//		}else{
237
			return BerlinModelTransformer.nameRel2RelNameQualifierFk(rel);
238
//		}
239
	}
240

    
241
	/* (non-Javadoc)
242
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
243
	 */
244
	@Override
245
	public Class<? extends CdmBase> getStandardMethodParameter() {
246
		return standardMethodParameter;
247
	}
248
}
(8-8/12)