Project

General

Profile

Download (9.21 KB) Statistics
| Branch: | Tag: | 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();
53
	}
54

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

    
60
		logger.info("start make " + pluralString + " ...");
61
		boolean success = true ;
62
		
63
		
64
		//queryStrings
65
		String strIdQuery = getIdQuery();
66
		
67
		String strRecordQuery = getRecordQuery(config);
68
		String strQuerySequence = 
69
			" SELECT *  " +
70
            " FROM AuthorTeamSequence " + 
71
            " ORDER By authorTeamFk, Sequence ";
72
		rsSequence = source.getResultSet(strQuerySequence) ;
73
		
74
		int recordsPerTransaction = config.getRecordsPerTransaction();
75
		try{
76
			ResultSetPartitioner partitioner = ResultSetPartitioner.NewInstance(source, strIdQuery, strRecordQuery, recordsPerTransaction);
77
			while (partitioner.nextPartition()){
78
				partitioner.doPartition(this, state);
79
			}
80
		} catch (SQLException e) {
81
			logger.error("SQLException:" +  e);
82
			return false;
83
		}
84
		
85
		
86
		logger.info("end make " + pluralString + " ... " + getSuccessString(success));
87
		return success;
88
	}
89
	
90
	protected String getIdQuery(){
91
		String idQuery = 
92
				" SELECT authorTeamId " +
93
                " FROM AuthorTeam " + 
94
                " ORDER BY authorTeamId ";
95
		return idQuery;
96
	}
97

    
98
	
99
	/* (non-Javadoc)
100
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
101
	 */
102
	@Override
103
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
104
		String strRecordQuery = 
105
			" SELECT *  " +
106
            " FROM AuthorTeam " + 
107
            " WHERE authorTeamId IN ( " + ID_LIST_TOKEN + " )" + 
108
            " ORDER By authorTeamId ";
109
		return strRecordQuery;
110
	}
111
	
112

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

    
170
		return success;
171
	}
172

    
173
	/* (non-Javadoc)
174
	 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
175
	 */
176
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs)  {
177
		String nameSpace;
178
		Class cdmClass;
179
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
180
		
181
		//person map
182
		Set<String> idInSourceList = makeAuthorIdList(rs);
183
		nameSpace = BerlinModelAuthorImport.NAMESPACE;
184
		cdmClass = Person.class;
185
		Map<String, Person> personMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idInSourceList, nameSpace);
186
		result.put(nameSpace, personMap);
187

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

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

    
303

    
304

    
305
}
(2-2/21)