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.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
 * @since 20.03.2008
35
 */
36
@Component
37
public class BerlinModelAuthorTeamImport extends BerlinModelImportBase {
38

    
39
    private static final long serialVersionUID = -4318481607033688522L;
40
    private static final Logger logger = Logger.getLogger(BerlinModelAuthorTeamImport.class);
41

    
42
	public static final String NAMESPACE = "AuthorTeam";
43

    
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
	@Override
57
    protected void doInvoke(BerlinModelImportState state){
58
		BerlinModelImportConfigurator config = state.getConfig();
59
		source = config.getSource();
60

    
61
		logger.info("start make " + pluralString + " ...");
62

    
63
		//queryStrings
64
		String strIdQuery = getIdQuery(state);
65

    
66
		String strRecordQuery = getRecordQuery(config);
67
		String strWhere = " WHERE (1=1) ";
68
		if (state.getConfig().getAuthorTeamFilter() != null){
69
			strWhere += " AND " + state.getConfig().getAuthorTeamFilter();
70
			strWhere = strWhere.replaceFirst("authorTeamId", "authorTeamFk");
71
		}
72
		String strQuerySequence =
73
			" SELECT *  " +
74
            " FROM AuthorTeamSequence " +
75
				strWhere +
76
            " ORDER By authorTeamFk, Sequence ";
77

    
78
		int recordsPerTransaction = config.getRecordsPerTransaction();
79
		try{
80
			ResultSetPartitioner partitioner = ResultSetPartitioner.NewInstance(source, strIdQuery, strRecordQuery, recordsPerTransaction);
81
			rsSequence = source.getResultSet(strQuerySequence) ; //only here, to reduce deadlock/timeout probability
82
			while (partitioner.nextPartition()){
83
				partitioner.doPartition(this, state);
84
			}
85
		} catch (SQLException e) {
86
			logger.error("SQLException:" +  e);
87
			state.setUnsuccessfull();
88
			return;
89
		}
90

    
91

    
92
		logger.info("end make " + pluralString + " ... " + getSuccessString(true));
93
		return;
94
	}
95

    
96
	@Override
97
	protected String getIdQuery(BerlinModelImportState state){
98
		String strWhere = " WHERE (1=1) ";
99
		if (state.getConfig().getAuthorTeamFilter() != null){
100
			strWhere += " AND " + state.getConfig().getAuthorTeamFilter();
101
		}
102
		String idQuery =
103
				" SELECT authorTeamId " +
104
                " FROM AuthorTeam " +
105
                strWhere +
106
                " ORDER BY authorTeamId ";
107
		return idQuery;
108
	}
109

    
110

    
111
	@Override
112
	protected String getRecordQuery(BerlinModelImportConfigurator config) {
113
		String strRecordQuery =
114
			" SELECT *  " +
115
            " FROM AuthorTeam " +
116
            " WHERE authorTeamId IN ( " + ID_LIST_TOKEN + " )" +
117
            " ORDER By authorTeamId ";
118
		return strRecordQuery;
119
	}
120

    
121

    
122
	@Override
123
    public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
124
		boolean success = true ;
125
		BerlinModelImportConfigurator config = state.getConfig();
126
		Set<Team> teamsToSave = new HashSet<Team>();
127
		Map<String, Person> personMap = partitioner.getObjectMap(BerlinModelAuthorImport.NAMESPACE);
128

    
129
		ResultSet rs = partitioner.getResultSet();
130
		//for each reference
131
		try{
132
			while (rs.next()){
133
				try{
134
					//if ((i++ % modCount ) == 0 && i!= 1 ){ logger.info(""+pluralString+" handled: " + (i-1));}
135

    
136
					//create Agent element
137
					int teamId = rs.getInt("AuthorTeamId");
138
					if (teamId == 0 && config.isIgnore0AuthorTeam()){
139
						continue;
140
					}
141

    
142
					Team team = Team.NewInstance();
143

    
144
					Boolean preliminaryFlag = rs.getBoolean("PreliminaryFlag");
145
					String authorTeamCache = rs.getString("AuthorTeamCache");
146
					String fullAuthorTeamCache = rs.getString("FullAuthorTeamCache");
147
					if (CdmUtils.isBlank(fullAuthorTeamCache)){
148
						fullAuthorTeamCache = authorTeamCache;
149
					}
150
					team.setTitleCache(fullAuthorTeamCache, preliminaryFlag);
151
					team.setNomenclaturalTitle(authorTeamCache, preliminaryFlag);
152

    
153
					success &= makeSequence(team, teamId, rsSequence, personMap);
154
					if (team.getTeamMembers().size()== 0 && preliminaryFlag == false){
155
						team.setProtectedTitleCache(true);
156
						team.setProtectedNomenclaturalTitleCache(true);
157
					}
158

    
159
					//created, notes
160
					doIdCreatedUpdatedNotes(state, team, rs, teamId, NAMESPACE);
161

    
162
					teamsToSave.add(team);
163
				}catch(Exception ex){
164
					logger.error(ex.getMessage());
165
					ex.printStackTrace();
166
					success = false;
167
				}
168
			} //while rs.hasNext()
169
		} catch (SQLException e) {
170
			logger.error("SQLException:" +  e);
171
			return false;
172
		}
173

    
174
		//logger.info(i + " " + pluralString + " handled");
175
		getAgentService().saveOrUpdate((Collection)teamsToSave);
176

    
177
		return success;
178
	}
179

    
180

    
181
	@Override
182
	public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state)  {
183
		String nameSpace;
184
		Class<?> cdmClass;
185
		Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
186

    
187
		//person map
188
		Set<String> idInSourceList = makeAuthorIdList(rs);
189
		nameSpace = BerlinModelAuthorImport.NAMESPACE;
190
		cdmClass = Person.class;
191
		Map<String, Person> personMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idInSourceList, nameSpace);
192
		result.put(nameSpace, personMap);
193

    
194
		return result;
195
	}
196

    
197
	/**
198
	 * @param rs
199
	 * @return
200
	 * @throws SQLException
201
	 * @throws SQLException
202
	 */
203
	private Set<String> makeAuthorIdList(ResultSet rs) {
204
		Set<String> result = new HashSet<String>();
205

    
206
		String authorTeamIdList = "";
207
		try {
208
			while (rs.next()){
209
				int id = rs.getInt("AuthorTeamId");
210
				authorTeamIdList = CdmUtils.concat(",", authorTeamIdList, String.valueOf(id));
211
			}
212

    
213
			String strQuerySequence =
214
				" SELECT DISTINCT authorFk " +
215
	            " FROM AuthorTeamSequence " +
216
	            " WHERE authorTeamFk IN (@) ";
217
			strQuerySequence = strQuerySequence.replace("@", authorTeamIdList);
218

    
219
			rs = source.getResultSet(strQuerySequence) ;
220
			while (rs.next()){
221
				int authorFk = rs.getInt("authorFk");
222
				result.add(String.valueOf(authorFk));
223
			}
224
		} catch (SQLException e) {
225
			throw new RuntimeException(e);
226
		}
227
		return result;
228
	}
229

    
230
	private boolean makeSequence(Team team, int teamId, ResultSet rsSequence, Map<String, Person> personMap){
231
		try {
232
			if (rsSequence.isBeforeFirst()){
233
				rsSequence.next();
234
			}
235
			if (rsSequence.isAfterLast()){
236
				return true;
237
			}
238
			int sequenceTeamFk;
239
			try {
240
				sequenceTeamFk = rsSequence.getInt("AuthorTeamFk");
241
			} catch (SQLException e) {
242
				if (rsSequence.next() == false){
243
					return true;
244
				}else{
245
					throw e;
246
				}
247
			}
248
			while (sequenceTeamFk < teamId){
249
				logger.warn("Sequence team FK is smaller then team ID. Some teams for a sequence may not be available");
250
				rsSequence.next();
251
				sequenceTeamFk = rsSequence.getInt("AuthorTeamFk");
252
			}
253
			while (sequenceTeamFk == teamId){
254
				int authorFk = rsSequence.getInt("AuthorFk");
255
				Person author = personMap.get(String.valueOf(authorFk));
256
				if (author != null){
257
				team.addTeamMember(author);
258
				}else{
259
					logger.error("Author " + authorFk + " was not found for team " + teamId);
260
				}
261
				if (rsSequence.next()){
262
					sequenceTeamFk = rsSequence.getInt("AuthorTeamFk");
263
				}else{
264
					break;
265
				}
266
			}
267
			return true;
268
		} catch (SQLException e) {
269
			e.printStackTrace();
270
			return false;
271
		}
272
	}
273

    
274

    
275
	@Override
276
	protected boolean doCheck(BerlinModelImportState state){
277
		IOValidator<BerlinModelImportState> validator = new BerlinModelAuthorTeamImportValidator();
278
		return validator.validate(state);
279
	}
280

    
281

    
282
	@Override
283
    protected boolean isIgnore(BerlinModelImportState state){
284
		return ! state.getConfig().isDoAuthors();
285
	}
286

    
287

    
288

    
289
}
(2-2/21)