latest updates to PESI export
[cdmlib-apps.git] / cdm-pesi / src / main / java / eu / etaxonomy / cdm / io / pesi / out / PesiNoteSourceExport.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.io.pesi.out;
11
12 import java.sql.SQLException;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16 import org.springframework.stereotype.Component;
17 import org.springframework.transaction.TransactionStatus;
18
19 import eu.etaxonomy.cdm.io.common.DbExportStateBase;
20 import eu.etaxonomy.cdm.io.common.Source;
21 import eu.etaxonomy.cdm.io.common.IExportConfigurator.DO_REFERENCES;
22 import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24 import eu.etaxonomy.cdm.model.description.DescriptionBase;
25 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26 import eu.etaxonomy.cdm.model.description.TaxonDescription;
27 import eu.etaxonomy.cdm.model.taxon.Taxon;
28
29 /**
30 * The export class for NoteSources.
31 * Inserts into DataWarehouse database table <code>NoteSource</code>.
32 * @author e.-m.lee
33 * @date 03.03.2010
34 *
35 */
36 @Component
37 public class PesiNoteSourceExport extends PesiExportBase {
38 private static final Logger logger = Logger.getLogger(PesiNoteSourceExport.class);
39 private static final Class<? extends CdmBase> standardMethodParameter = DescriptionElementBase.class;
40
41 private static int modCount = 1000;
42 private static final String dbTableName = "NoteSource";
43 private static final String pluralString = "NoteSources";
44
45 public PesiNoteSourceExport() {
46 super();
47 }
48
49 /* (non-Javadoc)
50 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
51 */
52 @Override
53 public Class<? extends CdmBase> getStandardMethodParameter() {
54 return standardMethodParameter;
55 }
56
57 /* (non-Javadoc)
58 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
59 */
60 @Override
61 protected boolean doCheck(PesiExportState state) {
62 boolean result = true;
63 return result;
64 }
65
66 /* (non-Javadoc)
67 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
68 */
69 @Override
70 protected void doInvoke(PesiExportState state) {
71 try {
72 logger.error("*** Started Making " + pluralString + " ...");
73
74 // Get the limit for objects to save within a single transaction.
75 // int pageSize = state.getConfig().getLimitSave();
76 int pageSize = 1000;
77
78 // pageNumber
79 int pageNumber = 1;
80
81 // Stores whether this invoke was successful or not.
82 boolean success = true;
83
84 // PESI: Clear the database table NoteSource.
85 doDelete(state);
86
87 // Get specific mappings: (CDM) ? -> (PESI) NoteSource
88 PesiExportMapping mapping = getMapping();
89
90 // Initialize the db mapper
91 mapping.initialize(state);
92
93 // PESI: Create the NoteSource
94 int count = 0;
95 int pastCount = 0;
96 TransactionStatus txStatus = null;
97 List<DescriptionElementBase> list = null;
98
99 // Start transaction
100 txStatus = startTransaction(true);
101 logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
102 while ((list = getDescriptionService().listDescriptionElements(null, null, null, pageSize, pageNumber, null)).size() > 0) {
103
104 logger.info("Fetched " + list.size() + " " + pluralString + ". Exporting...");
105 for (DescriptionElementBase descriptionElement : list) {
106
107 if (getNoteCategoryFk(descriptionElement) != null && neededValuesNotNull(descriptionElement, state)) {
108 doCount(count++, modCount, pluralString);
109 success &= mapping.invoke(descriptionElement);
110 }
111 }
112
113 // Commit transaction
114 commitTransaction(txStatus);
115 logger.debug("Committed transaction.");
116 logger.info("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
117 pastCount = count;
118
119 // Start transaction
120 txStatus = startTransaction(true);
121 logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
122
123 // Increment pageNumber
124 pageNumber++;
125 }
126 if (list.size() == 0) {
127 logger.info("No " + pluralString + " left to fetch.");
128 }
129 // Commit transaction
130 commitTransaction(txStatus);
131 logger.debug("Committed transaction.");
132
133 logger.info("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
134
135 if (!success){
136 state.setUnsuccessfull();
137 }
138 return;
139 } catch (SQLException e) {
140 e.printStackTrace();
141 logger.error(e.getMessage());
142 state.setUnsuccessfull();
143 return;
144 }
145 }
146
147 /**
148 * Checks whether needed values for an entity are NULL.
149 * @return
150 */
151 private boolean neededValuesNotNull(DescriptionElementBase descriptionElement, PesiExportState state) {
152 boolean result = true;
153 if (getSourceFk(descriptionElement, state) == null) {
154 logger.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this descriptionElement: " + descriptionElement.getUuid());
155 result = false;
156 }
157 return result;
158 }
159
160 /**
161 * Deletes all entries of database tables related to <code>NoteSource</code>.
162 * @param state The {@link PesiExportState PesiExportState}.
163 * @return Whether the delete operation was successful or not.
164 */
165 protected boolean doDelete(PesiExportState state) {
166 PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
167
168 String sql;
169 Source destination = pesiConfig.getDestination();
170
171 // Clear NoteSource
172 sql = "DELETE FROM " + dbTableName;
173 destination.setQuery(sql);
174 destination.update(sql);
175 return true;
176 }
177
178 /* (non-Javadoc)
179 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
180 */
181 @Override
182 protected boolean isIgnore(PesiExportState state) {
183 return ! ( state.getConfig().isDoNoteSources() && state.getConfig().isDoNotes() && state.getConfig().getDoReferences().equals(DO_REFERENCES.ALL));
184 }
185
186 /**
187 * Returns the <code>NoteCategoryFk</code> attribute.
188 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
189 * @return The <code>NoteCategoryFk</code> attribute.
190 */
191 private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
192 Integer result = null;
193 result = PesiTransformer.feature2NodeCategoryFk(descriptionElement.getFeature());
194 return result;
195 }
196
197 /**
198 * Returns the <code>NoteFk</code> attribute.
199 * @param description The {@link TaxonDescription TaxonDescription}.
200 * @param state The {@link PesiExportState PesiExportState}.
201 * @return The <code>NoteFk</code> attribute.
202 * @see MethodMapper
203 */
204 @SuppressWarnings("unused")
205 private static Integer getNoteFk(DescriptionElementBase descriptionElement, PesiExportState state) {
206 Integer result = null;
207 result = state.getDbId(descriptionElement);
208 return result;
209 }
210
211 /**
212 * Returns the <code>SourceFk</code> attribute.
213 * @param description The {@link TaxonDescription TaxonDescription}.
214 * @param state The {@link DbExportStateBase DbExportState}.
215 * @return The <code>SourceFk</code> attribute.
216 * @see MethodMapper
217 */
218 private static Integer getSourceFk(DescriptionElementBase descriptionElement, PesiExportState state) {
219 Integer result = null;
220 result = state.getDbId(descriptionElement);
221 return result;
222 }
223
224 /**
225 * Returns the <code>SourceNameCache</code> attribute.
226 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
227 * @return The <code>SourceNameCache</code> attribute.
228 * @see MethodMapper
229 */
230 @SuppressWarnings("unused")
231 private static String getSourceNameCache(DescriptionElementBase descriptionElement) {
232 String result = null;
233
234 DescriptionBase<?> inDescription = descriptionElement.getInDescription();
235 if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
236 TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
237 Taxon taxon = taxonDescription.getTaxon();
238 result = taxon.getSec().getTitleCache();
239 }
240
241 return result;
242 }
243
244 /**
245 * Returns the <code>SourceDetail</code> attribute.
246 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
247 * @return The <code>SourceDetail</code> attribute.
248 * @see MethodMapper
249 */
250 @SuppressWarnings("unused")
251 private static String getSourceDetail(DescriptionElementBase descriptionElement) {
252 //FIXME this is a replacement for the deprecated descriptionElement.getCitationMicroReference()
253 //it needs to be checked what should be done when multiple sources exist
254 if (descriptionElement.getSources().size() < 1){
255 return null;
256 }else{
257 if (descriptionElement.getSources().size() > 1){
258 logger.warn("Multiple sources exist");
259 }
260 return descriptionElement.getSources().iterator().next().getCitationMicroReference();
261 }
262 // return descriptionElement.getCitationMicroReference(); // TODO: What should be used instead?
263 }
264
265 /**
266 * Returns the CDM to PESI specific export mappings.
267 * @return The {@link PesiExportMapping PesiExportMapping}.
268 */
269 private PesiExportMapping getMapping() {
270 PesiExportMapping mapping = new PesiExportMapping(dbTableName);
271
272 mapping.addMapper(MethodMapper.NewInstance("NoteFk", this.getClass(), "getNoteFk", standardMethodParameter, PesiExportState.class));
273 mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
274 mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
275 mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
276
277 return mapping;
278 }
279
280 }