Project

General

Profile

Download (6.71 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.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.common.Source;
19
import eu.etaxonomy.cdm.io.common.mapping.out.CdmDbExportMapping;
20
import eu.etaxonomy.cdm.io.common.mapping.out.CollectionExportMapping;
21
import eu.etaxonomy.cdm.io.common.mapping.out.CreatedAndNotesMapper;
22
import eu.etaxonomy.cdm.io.common.mapping.out.DbBooleanMapper;
23
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
24
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
25
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
26
import eu.etaxonomy.cdm.model.agent.AgentBase;
27
import eu.etaxonomy.cdm.model.agent.Team;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29

    
30

    
31
/**
32
 * @author a.mueller
33
 * @created 20.03.2008
34
 */
35
@Component
36
public class BerlinModelAuthorTeamExport extends BerlinModelExportBase<Team> {
37

    
38
    private static final long serialVersionUID = 3089928128608037344L;
39

    
40
    private static final Logger logger = Logger.getLogger(BerlinModelAuthorTeamExport.class);
41

    
42
	private static int modCount = 1000;
43
	private static final String dbTableName = "AuthorTeam";
44
	private static final String pluralString = "AuthorTeams";
45
	private static final Class<? extends CdmBase> standardMethodParameter = Team.class;
46

    
47
	public BerlinModelAuthorTeamExport(){
48
		super();
49
	}
50

    
51
	@Override
52
	protected boolean doCheck(BerlinModelExportState state){
53
		boolean result = true;
54
		logger.warn("Checking for "+pluralString+" not yet implemented");
55
		//result &= checkArticlesWithoutJournal(bmiConfig);
56

    
57
		return result;
58
	}
59

    
60
	public CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> getMapping(){
61
		String tableName = dbTableName;
62
		CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping
63
		            = new CdmDbExportMapping<>(tableName);
64
		mapping.addMapper(IdMapper.NewInstance("AuthorTeamId"));
65
		mapping.addMapper(MethodMapper.NewInstance("AuthorTeamCache", this));
66
		mapping.addMapper(MethodMapper.NewInstance("FullAuthorTeamCache", this));
67
		mapping.addMapper(DbBooleanMapper.NewFalseInstance("isProtectedTitleCache", "PreliminaryFlag"));
68
		mapping.addMapper(CreatedAndNotesMapper.NewInstance());
69
		mapping.addCollectionMapping(getTeamMemberMapping());
70
		return mapping;
71
	}
72

    
73
	private CollectionExportMapping getTeamMemberMapping(){
74
		String tableName = "AuthorTeamSequence";
75
		String collectionAttribute = "teamMembers";
76
		IdMapper parentMapper = IdMapper.NewInstance("AuthorTeamFk");
77
		int sequenceStart = 0;
78
		String sequenceAttribute = "Sequence";
79
		CollectionExportMapping mapping = CollectionExportMapping.NewInstance(tableName, collectionAttribute, parentMapper, sequenceAttribute, sequenceStart);
80
		mapping.addMapper(IdMapper.NewInstance("AuthorFk"));
81
		return mapping;
82
	}
83

    
84

    
85
	@Override
86
    protected void doInvoke(BerlinModelExportState state){
87
		try{
88
			BerlinModelExportConfigurator bmeConfig = state.getConfig();
89

    
90
			logger.info("start make "+pluralString+" ...");
91
			boolean success = true ;
92
			doDelete(bmeConfig);
93

    
94
			TransactionStatus txStatus = startTransaction(true);
95
			Class<Team> clazz = Team.class;
96
			List<? extends AgentBase> list = getAgentService().list(clazz, 100000000, 0,null,null);
97

    
98
			CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = getMapping();
99
			mapping.initialize(state);
100

    
101
			logger.info("save "+pluralString+" ...");
102
			int count = 0;
103
			for (AgentBase<?> team : list){
104
				doCount(count++, modCount, pluralString);
105
				if (team instanceof Team){
106
					success &= mapping.invoke(team);
107
				}
108
			}
109

    
110
			commitTransaction(txStatus);
111

    
112
			logger.info("end make "+pluralString+"  ..." + getSuccessString(success));
113
			if (!success){
114
                String message = "An undefined error occurred during AuthorTeam export";
115
                state.getResult().addError(message);
116
			}
117
			return;
118
		}catch(SQLException e){
119
			e.printStackTrace();
120
			logger.error(e.getMessage());
121
			state.getResult().addException(e);
122
			return;
123
		}
124
	}
125

    
126
	protected boolean doDelete(BerlinModelExportConfigurator config){
127
		String sql;
128
		Source destination =  config.getDestination();
129
		//RelPTaxon
130
		sql = "DELETE FROM RelPTaxon";
131
		destination.setQuery(sql);
132
		destination.update(sql);
133
		//Fact
134
		sql = "DELETE FROM Fact";
135
		destination.setQuery(sql);
136
		destination.update(sql);
137
		//PTaxon
138
		sql = "DELETE FROM PTaxon";
139
		destination.setQuery(sql);
140
		destination.update(sql);
141

    
142
		//NameHistory
143
		sql = "DELETE FROM NameHistory";
144
		destination.setQuery(sql);
145
		destination.update(sql);
146
		//RelName
147
		sql = "DELETE FROM RelName";
148
		destination.setQuery(sql);
149
		destination.update(sql);
150
		//NomStatusRel
151
		sql = "DELETE FROM NomStatusRel";
152
		destination.setQuery(sql);
153
		destination.update(sql);
154
		//Name
155
		sql = "DELETE FROM Name";
156
		destination.setQuery(sql);
157
		destination.update(sql);
158
		//RefDetail
159
		sql = "DELETE FROM RefDetail";
160
		destination.setQuery(sql);
161
		destination.update(sql);
162
		//Reference
163
		sql = "DELETE FROM Reference";
164
		destination.setQuery(sql);
165
		destination.update(sql);
166
		//AuthorTeamSequence
167
		sql = "DELETE FROM AuthorTeamSequence";
168
		destination.setQuery(sql);
169
		destination.update(sql);
170
		//AuthorTeam
171
		sql = "DELETE FROM AuthorTeam";
172
		destination.setQuery(sql);
173
		destination.update(sql);
174
		return true;
175
	}
176

    
177
	//called by MethodMapper
178
	@SuppressWarnings("unused")
179
	private static String getAuthorTeamCache(Team team){
180
		if (team.isProtectedTitleCache()){
181
			return team.getTitleCache();
182
		}else{
183
			return null;
184
		}
185
	}
186

    
187
	//called by MethodMapper
188
	@SuppressWarnings("unused")
189
	private static String getFullAuthorTeamCache(Team team){
190
		if (team.isProtectedTitleCache()){
191
			return team.getNomenclaturalTitle();
192
		}else{
193
			return null;
194
		}
195
	}
196

    
197

    
198
	/* (non-Javadoc)
199
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
200
	 */
201
	@Override
202
    protected boolean isIgnore(BerlinModelExportState state){
203
		return ! state.getConfig().isDoAuthors();
204
	}
205

    
206
	/* (non-Javadoc)
207
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
208
	 */
209
	@Override
210
	public Class<? extends CdmBase> getStandardMethodParameter() {
211
		return standardMethodParameter;
212
	}
213

    
214
}
(2-2/12)