Project

General

Profile

Download (9.3 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.in;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.Collection;
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.common.CdmUtils;
23
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelAuthorTeamImportValidator;
24
import eu.etaxonomy.cdm.io.common.IOValidator;
25
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26
import eu.etaxonomy.cdm.io.common.Source;
27
import eu.etaxonomy.cdm.model.agent.Person;
28
import eu.etaxonomy.cdm.model.agent.Team;
29
import eu.etaxonomy.cdm.model.common.CdmBase;
30

    
31

    
32
/**
33
 * @author a.mueller
34
 * @created 20.03.2008
35
 * @version 1.0
36
 */
37
@Component
38
public class BerlinModelAuthorTeamImport extends BerlinModelImportBase {
39
	private static final Logger logger = Logger.getLogger(BerlinModelAuthorTeamImport.class);
40

    
41
	public static final String NAMESPACE = "AuthorTeam";
42
	
43
	private static int modCount = 1000;
44
	private static final String pluralString = "AuthorTeams";
45
	private static final String dbTableName = "AuthorTeam";
46
	 
47
	//TODO pass it in other way, not as a class variable
48
	private ResultSet rsSequence;
49
	private Source source;
50

    
51
	public BerlinModelAuthorTeamImport(){
52
		super(dbTableName, pluralString);
53
	}
54

    
55
	
56
	protected void doInvoke(BerlinModelImportState state){
57
		BerlinModelImportConfigurator config = state.getConfig();
58
		source = config.getSource();
59

    
60
		logger.info("start make " + pluralString + " ...");
61
				
62
		//queryStrings
63
		String strIdQuery = getIdQuery(state);
64
		
65
		String strRecordQuery = getRecordQuery(config);
66
		String strWhere = " WHERE (1=1) ";
67
		if (state.getConfig().getAuthorTeamFilter() != null){
68
			strWhere += " AND " + state.getConfig().getAuthorTeamFilter();
69
			strWhere = strWhere.replaceFirst("authorTeamId", "authorTeamFk");
70
		}
71
		String strQuerySequence = 
72
			" SELECT *  " +
73
            " FROM AuthorTeamSequence " +
74
				strWhere + 	
75
            " ORDER By authorTeamFk, Sequence ";
76
		
77
		int recordsPerTransaction = config.getRecordsPerTransaction();
78
		try{
79
			ResultSetPartitioner partitioner = ResultSetPartitioner.NewInstance(source, strIdQuery, strRecordQuery, recordsPerTransaction);
80
			rsSequence = source.getResultSet(strQuerySequence) ; //only here, to reduce deadlock/timeout probability
81
			while (partitioner.nextPartition()){
82
				partitioner.doPartition(this, state);
83
			}
84
		} catch (SQLException e) {
85
			logger.error("SQLException:" +  e);
86
			state.setUnsuccessfull();
87
			return;
88
		}
89
		
90
		
91
		logger.info("end make " + pluralString + " ... " + getSuccessString(true));
92
		return;
93
	}
94
	
95
	@Override
96
	protected String getIdQuery(BerlinModelImportState state){
97
		String strWhere = " WHERE (1=1) ";
98
		if (state.getConfig().getAuthorTeamFilter() != null){
99
			strWhere += " AND " + state.getConfig().getAuthorTeamFilter();
100
		}
101
		String idQuery = 
102
				" SELECT authorTeamId " +
103
                " FROM AuthorTeam " + 
104
                strWhere +
105
                " ORDER BY authorTeamId ";
106
		return idQuery;
107
	}
108

    
109
	
110
	/* (non-Javadoc)
111
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
112
	 */
113
	@Override
114
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
115
		String strRecordQuery = 
116
			" SELECT *  " +
117
            " FROM AuthorTeam " + 
118
            " WHERE authorTeamId IN ( " + ID_LIST_TOKEN + " )" + 
119
            " ORDER By authorTeamId ";
120
		return strRecordQuery;
121
	}
122
	
123

    
124
	/* (non-Javadoc)
125
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
126
	 */
127
	public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
128
		boolean success = true ;
129
		BerlinModelImportConfigurator config = state.getConfig();
130
		Set<Team> teamsToSave = new HashSet<Team>();
131
		Map<String, Person> personMap = (Map<String, Person>) partitioner.getObjectMap(BerlinModelAuthorImport.NAMESPACE);
132
		
133
		ResultSet rs = partitioner.getResultSet();
134
		//for each reference
135
		try{
136
			while (rs.next()){
137
				try{
138
					//if ((i++ % modCount ) == 0 && i!= 1 ){ logger.info(""+pluralString+" handled: " + (i-1));}
139
					
140
					//create Agent element
141
					int teamId = rs.getInt("AuthorTeamId");
142
					if (teamId == 0 && config.isIgnore0AuthorTeam()){
143
						continue;
144
					}
145
					
146
					Team team = Team.NewInstance();
147
					
148
					Boolean preliminaryFlag = rs.getBoolean("PreliminaryFlag");
149
					String authorTeamCache = rs.getString("AuthorTeamCache");
150
					String fullAuthorTeamCache = rs.getString("FullAuthorTeamCache");
151
					if (CdmUtils.isBlank(fullAuthorTeamCache)){
152
						fullAuthorTeamCache = authorTeamCache;
153
					}
154
					team.setTitleCache(fullAuthorTeamCache, preliminaryFlag);
155
					team.setNomenclaturalTitle(authorTeamCache, preliminaryFlag);
156
	
157
					success &= makeSequence(team, teamId, rsSequence, personMap);
158
					if (team.getTeamMembers().size()== 0 && preliminaryFlag == false){
159
						team.setProtectedTitleCache(true);
160
						team.setProtectedNomenclaturalTitleCache(true);
161
					}
162
					
163
					//created, notes
164
					doIdCreatedUpdatedNotes(state, team, rs, teamId, NAMESPACE);
165
	
166
					teamsToSave.add(team);
167
				}catch(Exception ex){
168
					logger.error(ex.getMessage());
169
					ex.printStackTrace();
170
					success = false;
171
				}
172
			} //while rs.hasNext()
173
		} catch (SQLException e) {
174
			logger.error("SQLException:" +  e);
175
			return false;
176
		}
177
			
178
		//logger.info(i + " " + pluralString + " handled");
179
		getAgentService().saveOrUpdate((Collection)teamsToSave);
180

    
181
		return success;
182
	}
183

    
184

    
185
	@Override
186
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state)  {
187
		String nameSpace;
188
		Class<?> cdmClass;
189
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
190
		
191
		//person map
192
		Set<String> idInSourceList = makeAuthorIdList(rs);
193
		nameSpace = BerlinModelAuthorImport.NAMESPACE;
194
		cdmClass = Person.class;
195
		Map<String, Person> personMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idInSourceList, nameSpace);
196
		result.put(nameSpace, personMap);
197

    
198
		return result;
199
	}
200
		
201
	/**
202
	 * @param rs 
203
	 * @return
204
	 * @throws SQLException 
205
	 * @throws SQLException 
206
	 */
207
	private Set<String> makeAuthorIdList(ResultSet rs) {
208
		Set<String> result = new HashSet<String>();
209
		
210
		String authorTeamIdList = "";
211
		try {
212
			while (rs.next()){
213
				int id = rs.getInt("AuthorTeamId");
214
				authorTeamIdList = CdmUtils.concat(",", authorTeamIdList, String.valueOf(id));
215
			}
216
		
217
			String strQuerySequence = 
218
				" SELECT DISTINCT authorFk " +
219
	            " FROM AuthorTeamSequence " + 
220
	            " WHERE authorTeamFk IN (@) ";
221
			strQuerySequence = strQuerySequence.replace("@", authorTeamIdList);
222
			
223
			rs = source.getResultSet(strQuerySequence) ;
224
			while (rs.next()){
225
				int authorFk = rs.getInt("authorFk");
226
				result.add(String.valueOf(authorFk));
227
			}
228
		} catch (SQLException e) {
229
			throw new RuntimeException(e);
230
		}
231
		return result;
232
	}
233

    
234
	private boolean makeSequence(Team team, int teamId, ResultSet rsSequence, Map<String, Person> personMap){
235
		try {
236
			if (rsSequence.isBeforeFirst()){
237
				rsSequence.next();
238
			}
239
			if (rsSequence.isAfterLast()){
240
				return true;
241
			}
242
			int sequenceTeamFk;
243
			try {
244
				sequenceTeamFk = rsSequence.getInt("AuthorTeamFk");
245
			} catch (SQLException e) {
246
				if (rsSequence.next() == false){
247
					return true;
248
				}else{
249
					throw e;
250
				}
251
			}
252
			while (sequenceTeamFk < teamId){
253
				logger.warn("Sequence team FK is smaller then team ID. Some teams for a sequence may not be available");
254
				rsSequence.next();
255
				sequenceTeamFk = rsSequence.getInt("AuthorTeamFk");
256
			}
257
			while (sequenceTeamFk == teamId){
258
				int authorFk = rsSequence.getInt("AuthorFk");
259
				Person author = personMap.get(String.valueOf(authorFk));
260
				if (author != null){
261
				team.addTeamMember(author);
262
				}else{
263
					logger.error("Author " + authorFk + " was not found for team " + teamId);
264
				}
265
				if (rsSequence.next()){
266
					sequenceTeamFk = rsSequence.getInt("AuthorTeamFk");
267
				}else{
268
					break;
269
				}
270
			}
271
			return true;
272
		} catch (SQLException e) {
273
			e.printStackTrace();
274
			return false;
275
		}
276
	}
277
	
278
	
279
	/* (non-Javadoc)
280
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
281
	 */
282
	@Override
283
	protected boolean doCheck(BerlinModelImportState state){
284
		IOValidator<BerlinModelImportState> validator = new BerlinModelAuthorTeamImportValidator();
285
		return validator.validate(state);
286
	}
287
	
288
	
289
	/* (non-Javadoc)
290
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
291
	 */
292
	protected boolean isIgnore(BerlinModelImportState state){
293
		return ! state.getConfig().isDoAuthors();
294
	}
295

    
296

    
297

    
298
}
(2-2/21)