Unifiy name and taxon creation
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / berlinModel / out / BerlinModelAuthorTeamExport.java
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 * @version 1.0
35 */
36 @Component
37 public class BerlinModelAuthorTeamExport extends BerlinModelExportBase<Team> {
38 private static final Logger logger = Logger.getLogger(BerlinModelAuthorTeamExport.class);
39
40 private static int modCount = 1000;
41 private static final String dbTableName = "AuthorTeam";
42 private static final String pluralString = "AuthorTeams";
43 private static final Class<? extends CdmBase> standardMethodParameter = Team.class;
44
45 public BerlinModelAuthorTeamExport(){
46 super();
47 }
48
49 /* (non-Javadoc)
50 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
51 */
52 @Override
53 protected boolean doCheck(BerlinModelExportState state){
54 boolean result = true;
55 logger.warn("Checking for "+pluralString+" not yet implemented");
56 //result &= checkArticlesWithoutJournal(bmiConfig);
57
58 return result;
59 }
60
61 public CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> getMapping(){
62 String tableName = dbTableName;
63 CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = new CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer>(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 protected void doInvoke(BerlinModelExportState state){
86 try{
87 BerlinModelExportConfigurator bmeConfig = (BerlinModelExportConfigurator)state.getConfig();
88
89 logger.info("start make "+pluralString+" ...");
90 boolean success = true ;
91 doDelete(bmeConfig);
92
93 TransactionStatus txStatus = startTransaction(true);
94 Class<Team> clazz = Team.class;
95 List<? extends AgentBase> list = getAgentService().list(clazz, 100000000, 0,null,null);
96
97 CdmDbExportMapping<BerlinModelExportState, BerlinModelExportConfigurator, IExportTransformer> mapping = getMapping();
98 mapping.initialize(state);
99
100 logger.info("save "+pluralString+" ...");
101 int count = 0;
102 for (AgentBase<?> team : list){
103 doCount(count++, modCount, pluralString);
104 if (team instanceof Team){
105 success &= mapping.invoke(team);
106 }
107 }
108
109 commitTransaction(txStatus);
110
111 logger.info("end make "+pluralString+" ..." + getSuccessString(success));
112 if (!success){
113 state.setUnsuccessfull();
114 }
115 return;
116 }catch(SQLException e){
117 e.printStackTrace();
118 logger.error(e.getMessage());
119 state.setUnsuccessfull();
120 return;
121 }
122 }
123
124 protected boolean doDelete(BerlinModelExportConfigurator config){
125 String sql;
126 Source destination = config.getDestination();
127 //RelPTaxon
128 sql = "DELETE FROM RelPTaxon";
129 destination.setQuery(sql);
130 destination.update(sql);
131 //Fact
132 sql = "DELETE FROM Fact";
133 destination.setQuery(sql);
134 destination.update(sql);
135 //PTaxon
136 sql = "DELETE FROM PTaxon";
137 destination.setQuery(sql);
138 destination.update(sql);
139
140 //NameHistory
141 sql = "DELETE FROM NameHistory";
142 destination.setQuery(sql);
143 destination.update(sql);
144 //RelName
145 sql = "DELETE FROM RelName";
146 destination.setQuery(sql);
147 destination.update(sql);
148 //NomStatusRel
149 sql = "DELETE FROM NomStatusRel";
150 destination.setQuery(sql);
151 destination.update(sql);
152 //Name
153 sql = "DELETE FROM Name";
154 destination.setQuery(sql);
155 destination.update(sql);
156 //RefDetail
157 sql = "DELETE FROM RefDetail";
158 destination.setQuery(sql);
159 destination.update(sql);
160 //Reference
161 sql = "DELETE FROM Reference";
162 destination.setQuery(sql);
163 destination.update(sql);
164 //AuthorTeamSequence
165 sql = "DELETE FROM AuthorTeamSequence";
166 destination.setQuery(sql);
167 destination.update(sql);
168 //AuthorTeam
169 sql = "DELETE FROM AuthorTeam";
170 destination.setQuery(sql);
171 destination.update(sql);
172 return true;
173 }
174
175 //called by MethodMapper
176 @SuppressWarnings("unused")
177 private static String getAuthorTeamCache(Team team){
178 if (team.isProtectedTitleCache()){
179 return team.getTitleCache();
180 }else{
181 return null;
182 }
183 }
184
185 //called by MethodMapper
186 @SuppressWarnings("unused")
187 private static String getFullAuthorTeamCache(Team team){
188 if (team.isProtectedTitleCache()){
189 return team.getNomenclaturalTitle();
190 }else{
191 return null;
192 }
193 }
194
195
196 /* (non-Javadoc)
197 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
198 */
199 protected boolean isIgnore(BerlinModelExportState state){
200 return ! state.getConfig().isDoAuthors();
201 }
202
203 /* (non-Javadoc)
204 * @see eu.etaxonomy.cdm.io.berlinModel.out.BerlinModelExportBase#getStandardMethodParameter()
205 */
206 @Override
207 public Class<? extends CdmBase> getStandardMethodParameter() {
208 return standardMethodParameter;
209 }
210
211 }