Project

General

Profile

Download (4.34 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2009 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

    
10
package eu.etaxonomy.cdm.io.berlinModel.out.mapper;
11

    
12
import java.sql.PreparedStatement;
13
import java.sql.ResultSet;
14
import java.sql.SQLException;
15

    
16
import eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportConfigurator;
17
import eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportState;
18
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
19
import eu.etaxonomy.cdm.io.common.ImportHelper;
20
import eu.etaxonomy.cdm.io.common.Source;
21
import eu.etaxonomy.cdm.io.common.mapping.out.CdmDbExportMapping;
22
import eu.etaxonomy.cdm.io.common.mapping.out.DbObjectMapper;
23
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
24
import eu.etaxonomy.cdm.io.common.mapping.out.IndexCounter;
25
import eu.etaxonomy.cdm.model.agent.Person;
26
import eu.etaxonomy.cdm.model.agent.Team;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28

    
29
/**
30
 * @author a.mueller
31
 */
32
public class TeamOrPersonMapper extends DbObjectMapper {
33

    
34
	CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> teamMapping;
35
	PreparedStatement stmtInsertTeam;
36
	PreparedStatement stmtInsertSequence;
37
	PreparedStatement stmtMaxId;
38

    
39
	public static TeamOrPersonMapper NewInstance(String cdmAttributeString, String dbAttributeString){
40
		TeamOrPersonMapper result = new TeamOrPersonMapper(cdmAttributeString, dbAttributeString, null);
41
		return result;
42
	}
43

    
44
	protected TeamOrPersonMapper(String cdmAttributeString, String dbAttributeString, Object defaultValue) {
45
		super(cdmAttributeString, dbAttributeString, defaultValue, false, false);
46
	}
47

    
48
	Integer lastTeamId;
49

    
50
	@Override
51
    protected boolean doInvoke(CdmBase cdmBase) throws SQLException {
52
		CdmBase agent = (CdmBase)ImportHelper.getValue(cdmBase, this.getSourceAttribute(), false, obligatory);
53
		if (agent == null || agent.isInstanceOf(Team.class)){
54
			//if Team, do normal
55
			if (agent == null){
56
				lastTeamId = null;
57
			}else{
58
				lastTeamId = agent.getId();
59
			}
60
			return super.doInvoke(cdmBase);
61
		}else if (agent.isInstanceOf(Person.class)){
62
			lastTeamId = makePersonToTeam(CdmBase.deproxy(agent, Person.class));
63
			return super.doInvoke(cdmBase);
64
		}else{
65
			throw new IllegalStateException("Invoke argument for TeamOrPersonMapper must be either a Team or a Person but was " + agent.getClass().getName());
66
		}
67
	}
68

    
69
	private int makePersonToTeam(Person agent) {
70
		int teamFk = 0;
71
		try {
72
			ResultSet result = stmtMaxId.executeQuery();
73

    
74
			while (result.next()){
75
				teamFk = result.getInt(1) +1;
76
//				System.out.println(teamFk);
77
				stmtInsertTeam.setInt(1, teamFk);
78
				stmtInsertTeam.setString(2, agent.getTitleCache());
79
				stmtInsertTeam.setString(3, agent.getNomenclaturalTitleCache());
80
				stmtInsertTeam.executeUpdate();
81

    
82
				stmtInsertSequence.setInt(1, teamFk);
83
				stmtInsertSequence.setInt(2, agent.getId());
84
				stmtInsertSequence.executeUpdate();
85
			}
86

    
87
		} catch (SQLException e) {
88
			e.printStackTrace();
89
			throw new IllegalStateException("SQL Exception occurred in makePersonToTeam()");
90
		}
91

    
92
		return teamFk;
93
	}
94

    
95
	@Override
96
	protected Object getValue(CdmBase cdmBase) {
97
		return lastTeamId;
98
	}
99

    
100
	@Override
101
	public void initialize(PreparedStatement stmt, IndexCounter index, DbExportStateBase<?, IExportTransformer> state, String tableName) {
102
		super.initialize(stmt, index, state, tableName);
103

    
104
		Source db = this.getState().getConfig().getDestination();
105
		try {
106
			String insertAuthorTeam = "INSERT INTO authorTeam (AuthorTeamId, FullAuthorTeamCache,AuthorTeamCache, PreliminaryFlag ) " +
107
							" Values (?, ?,?,0)";
108
			stmtInsertTeam = db.getConnection().prepareStatement(insertAuthorTeam);
109

    
110
			String insertSequence = "INSERT INTO authorTeamSequence (authorTeamFk, authorFk, Sequence) " +
111
			" Values (?,?,1)";
112
			stmtInsertSequence = db.getConnection().prepareStatement(insertSequence);
113

    
114
			String getMaxId = "SELECT max(AuthorTeamId) as max FROM AuthorTeam";
115
			stmtMaxId = db.getConnection().prepareStatement(getMaxId);
116

    
117
		} catch (SQLException e1) {
118
			e1.printStackTrace();
119
			throw new IllegalStateException("An SQLException occurred when trying to prepare insert team statements");
120
		}
121
	}
122
}
(3-3/3)