View Javadoc

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.berlinModel.out.mapper.MethodMapper;
20  import eu.etaxonomy.cdm.io.common.DbExportStateBase;
21  import eu.etaxonomy.cdm.io.common.Source;
22  import eu.etaxonomy.cdm.model.common.CdmBase;
23  import eu.etaxonomy.cdm.model.description.DescriptionBase;
24  import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
25  import eu.etaxonomy.cdm.model.description.TaxonDescription;
26  import eu.etaxonomy.cdm.model.taxon.Taxon;
27  
28  /**
29   * The export class for NoteSources.
30   * Inserts into DataWarehouse database table <code>NoteSource</code>.
31   * @author e.-m.lee
32   * @date 03.03.2010
33   *
34   */
35  @Component
36  @SuppressWarnings("unchecked")
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 boolean 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.error("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
102 			while ((list = getDescriptionService().listDescriptionElements(null, null, null, pageSize, pageNumber, null)).size() > 0) {
103 
104 				logger.error("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.error("Committed transaction.");
116 				logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
117 				pastCount = count;
118 	
119 				// Start transaction
120 				txStatus = startTransaction(true);
121 				logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
122 
123 				// Increment pageNumber
124 				pageNumber++;
125 			}
126 			if (list.size() == 0) {
127 				logger.error("No " + pluralString + " left to fetch.");
128 			}
129 			// Commit transaction
130 			commitTransaction(txStatus);
131 			logger.error("Committed transaction.");
132 	
133 			logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
134 			
135 			return success;
136 		} catch (SQLException e) {
137 			e.printStackTrace();
138 			logger.error(e.getMessage());
139 			return false;
140 		}
141 	}
142 
143 	/**
144 	 * Checks whether needed values for an entity are NULL.
145 	 * @return
146 	 */
147 	private boolean neededValuesNotNull(DescriptionElementBase descriptionElement, PesiExportState state) {
148 		boolean result = true;
149 		if (getSourceFk(descriptionElement, state) == null) {
150 			logger.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this descriptionElement: " + descriptionElement.getUuid());
151 			result = false;
152 		}
153 		return result;
154 	}
155 
156 	/**
157 	 * Deletes all entries of database tables related to <code>NoteSource</code>.
158 	 * @param state The {@link PesiExportState PesiExportState}.
159 	 * @return Whether the delete operation was successful or not.
160 	 */
161 	protected boolean doDelete(PesiExportState state) {
162 		PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
163 		
164 		String sql;
165 		Source destination =  pesiConfig.getDestination();
166 
167 		// Clear NoteSource
168 		sql = "DELETE FROM " + dbTableName;
169 		destination.setQuery(sql);
170 		destination.update(sql);
171 		return true;
172 	}
173 
174 	/* (non-Javadoc)
175 	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
176 	 */
177 	@Override
178 	protected boolean isIgnore(PesiExportState state) {
179 		// TODO
180 //		return ! state.getConfig().isDoNoteSource();
181 		return false;
182 	}
183 
184 	/**
185 	 * Returns the <code>NoteCategoryFk</code> attribute.
186 	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
187 	 * @return The <code>NoteCategoryFk</code> attribute.
188 	 */
189 	private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
190 		Integer result = null;
191 		result = PesiTransformer.textData2NodeCategoryFk(descriptionElement.getFeature());
192 		return result;
193 	}
194 
195 	/**
196 	 * Returns the <code>NoteFk</code> attribute.
197 	 * @param description The {@link TaxonDescription TaxonDescription}.
198 	 * @param state The {@link PesiExportState PesiExportState}.
199 	 * @return The <code>NoteFk</code> attribute.
200 	 * @see MethodMapper
201 	 */
202 	@SuppressWarnings("unused")
203 	private static Integer getNoteFk(DescriptionElementBase descriptionElement, PesiExportState state) {
204 		Integer result = null;
205 		result = state.getDbId(descriptionElement);
206 		return result;
207 	}
208 	
209 	/**
210 	 * Returns the <code>SourceFk</code> attribute.
211 	 * @param description The {@link TaxonDescription TaxonDescription}.
212 	 * @param state The {@link DbExportStateBase DbExportState}.
213 	 * @return The <code>SourceFk</code> attribute.
214 	 * @see MethodMapper
215 	 */
216 	private static Integer getSourceFk(DescriptionElementBase descriptionElement, PesiExportState state) {
217 		Integer result = null;
218 		result = state.getDbId(descriptionElement);
219 		return result;
220 	}
221 	
222 	/**
223 	 * Returns the <code>SourceNameCache</code> attribute.
224 	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
225 	 * @return The <code>SourceNameCache</code> attribute.
226 	 * @see MethodMapper
227 	 */
228 	@SuppressWarnings("unused")
229 	private static String getSourceNameCache(DescriptionElementBase descriptionElement) {
230 		String result = null;
231 
232 		DescriptionBase inDescription = descriptionElement.getInDescription();
233 		if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
234 			TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
235 			Taxon taxon = taxonDescription.getTaxon();
236 			result = taxon.getSec().getTitleCache();
237 		}
238 
239 		return result;
240 	}
241 	
242 	/**
243 	 * Returns the <code>SourceDetail</code> attribute.
244 	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
245 	 * @return The <code>SourceDetail</code> attribute.
246 	 * @see MethodMapper
247 	 */
248 	@SuppressWarnings("unused")
249 	private static String getSourceDetail(DescriptionElementBase descriptionElement) {
250 		return descriptionElement.getCitationMicroReference(); // TODO: What should be used instead?
251 	}
252 
253 	/**
254 	 * Returns the CDM to PESI specific export mappings.
255 	 * @return The {@link PesiExportMapping PesiExportMapping}.
256 	 */
257 	private PesiExportMapping getMapping() {
258 		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
259 
260 		mapping.addMapper(MethodMapper.NewInstance("NoteFk", this.getClass(), "getNoteFk", standardMethodParameter, PesiExportState.class));
261 		mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
262 		mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
263 		mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
264 
265 		return mapping;
266 	}
267 
268 }