Project

General

Profile

Download (6.29 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.out;
10

    
11
import java.sql.SQLException;
12
import java.util.List;
13

    
14
import org.apache.log4j.Logger;
15
import org.springframework.stereotype.Component;
16
import org.springframework.transaction.TransactionStatus;
17

    
18
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.CreatedAndNotesMapper;
19
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbBooleanMapper;
20
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.IdMapper;
21
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.MethodMapper;
22
import eu.etaxonomy.cdm.io.common.Source;
23
import eu.etaxonomy.cdm.model.agent.AgentBase;
24
import eu.etaxonomy.cdm.model.agent.Team;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26

    
27

    
28
/**
29
 * @author a.mueller
30
 * @created 20.03.2008
31
 * @version 1.0
32
 */
33
@Component
34
public class BerlinModelAuthorTeamExport extends BerlinModelExportBase<Team> {
35
	private static final Logger logger = Logger.getLogger(BerlinModelAuthorTeamExport.class);
36

    
37
	private static int modCount = 1000;
38
	private static final String dbTableName = "AuthorTeam";
39
	private static final String pluralString = "AuthorTeams";
40
	private static final Class<? extends CdmBase> standardMethodParameter = Team.class;
41
	
42
	public BerlinModelAuthorTeamExport(){
43
		super();
44
	}
45
	
46
	/* (non-Javadoc)
47
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
48
	 */
49
	@Override
50
	protected boolean doCheck(BerlinModelExportState state){
51
		boolean result = true;
52
		logger.warn("Checking for "+pluralString+" not yet implemented");
53
		//result &= checkArticlesWithoutJournal(bmiConfig);
54
		
55
		return result;
56
	}
57
	
58
	public BerlinModelExportMapping getMapping(){
59
		String tableName = dbTableName;
60
		BerlinModelExportMapping mapping = new BerlinModelExportMapping(tableName);
61
		mapping.addMapper(IdMapper.NewInstance("AuthorTeamId"));
62
		mapping.addMapper(MethodMapper.NewInstance("AuthorTeamCache", this));
63
		mapping.addMapper(MethodMapper.NewInstance("FullAuthorTeamCache", this));
64
		mapping.addMapper(DbBooleanMapper.NewFalseInstance("isProtectedTitleCache", "PreliminaryFlag"));
65
		mapping.addMapper(CreatedAndNotesMapper.NewInstance());
66
		mapping.addCollectionMapping(getTeamMemberMapping());
67
		return mapping;
68
	}
69
	
70
	private CollectionExportMapping getTeamMemberMapping(){
71
		String tableName = "AuthorTeamSequence";
72
		String collectionAttribute = "teamMembers";
73
		IdMapper parentMapper = IdMapper.NewInstance("AuthorTeamFk");
74
		int sequenceStart = 0;
75
		String sequenceAttribute = "Sequence";
76
		CollectionExportMapping mapping = CollectionExportMapping.NewInstance(tableName, collectionAttribute, parentMapper, sequenceAttribute, sequenceStart);
77
		mapping.addMapper(IdMapper.NewInstance("AuthorFk"));
78
		return mapping;
79
	}
80
	
81
	
82
	protected void doInvoke(BerlinModelExportState state){
83
		try{
84
			BerlinModelExportConfigurator bmeConfig = (BerlinModelExportConfigurator)state.getConfig();
85
			
86
			logger.info("start make "+pluralString+" ...");
87
			boolean success = true ;
88
			doDelete(bmeConfig);
89
			
90
			TransactionStatus txStatus = startTransaction(true);
91
			Class<Team> clazz = Team.class;
92
			List<? extends AgentBase> list = getAgentService().list(clazz, 100000000, 0,null,null);
93
			
94
			BerlinModelExportMapping mapping = getMapping();
95
			mapping.initialize(state);
96
			
97
			logger.info("save "+pluralString+" ...");
98
			int count = 0;
99
			for (AgentBase<?> team : list){
100
				doCount(count++, modCount, pluralString);
101
				if (team instanceof Team){
102
					success &= mapping.invoke(team);
103
				}
104
			}
105
			
106
			commitTransaction(txStatus);
107
			
108
			logger.info("end make "+pluralString+"  ..." + getSuccessString(success));
109
			if (!success){
110
				state.setUnsuccessfull();
111
			}
112
			return;
113
		}catch(SQLException e){
114
			e.printStackTrace();
115
			logger.error(e.getMessage());
116
			state.setUnsuccessfull();
117
			return;
118
		}
119
	}
120
	
121
	protected boolean doDelete(BerlinModelExportConfigurator config){
122
		String sql;
123
		Source destination =  config.getDestination();
124
		//RelPTaxon
125
		sql = "DELETE FROM RelPTaxon";
126
		destination.setQuery(sql);
127
		destination.update(sql);
128
		//Fact
129
		sql = "DELETE FROM Fact";
130
		destination.setQuery(sql);
131
		destination.update(sql);
132
		//PTaxon
133
		sql = "DELETE FROM PTaxon";
134
		destination.setQuery(sql);
135
		destination.update(sql);
136
		
137
		//NameHistory
138
		sql = "DELETE FROM NameHistory";
139
		destination.setQuery(sql);
140
		destination.update(sql);
141
		//RelName
142
		sql = "DELETE FROM RelName";
143
		destination.setQuery(sql);
144
		destination.update(sql);
145
		//NomStatusRel
146
		sql = "DELETE FROM NomStatusRel";
147
		destination.setQuery(sql);
148
		destination.update(sql);
149
		//Name
150
		sql = "DELETE FROM Name";
151
		destination.setQuery(sql);
152
		destination.update(sql);
153
		//RefDetail
154
		sql = "DELETE FROM RefDetail";
155
		destination.setQuery(sql);
156
		destination.update(sql);
157
		//Reference
158
		sql = "DELETE FROM Reference";
159
		destination.setQuery(sql);
160
		destination.update(sql);
161
		//AuthorTeamSequence
162
		sql = "DELETE FROM AuthorTeamSequence";
163
		destination.setQuery(sql);
164
		destination.update(sql);
165
		//AuthorTeam
166
		sql = "DELETE FROM AuthorTeam";
167
		destination.setQuery(sql);
168
		destination.update(sql);
169
		return true;
170
	}
171
	
172
	//called by MethodMapper
173
	@SuppressWarnings("unused")
174
	private static String getAuthorTeamCache(Team team){
175
		if (team.isProtectedTitleCache()){
176
			return team.getTitleCache();
177
		}else{
178
			return null;
179
		}
180
	}
181

    
182
	//called by MethodMapper
183
	@SuppressWarnings("unused")
184
	private static String getFullAuthorTeamCache(Team team){
185
		if (team.isProtectedTitleCache()){
186
			return team.getNomenclaturalTitle();
187
		}else{
188
			return null;
189
		}
190
	}
191
	
192
	
193
	/* (non-Javadoc)
194
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
195
	 */
196
	protected boolean isIgnore(BerlinModelExportState state){
197
		return ! state.getConfig().isDoAuthors();
198
	}
199

    
200
	/* (non-Javadoc)
201
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
202
	 */
203
	@Override
204
	public Class<? extends CdmBase> getStandardMethodParameter() {
205
		return standardMethodParameter;
206
	}
207

    
208
}
(2-2/14)