1
2
3
4
5
6
7
8
9
10 package eu.etaxonomy.cdm.io.pesi.out;
11
12 import java.sql.Connection;
13 import java.sql.PreparedStatement;
14 import java.sql.SQLException;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.apache.log4j.Logger;
19 import org.springframework.stereotype.Component;
20 import org.springframework.transaction.TransactionStatus;
21
22 import eu.etaxonomy.cdm.io.berlinModel.out.mapper.IdMapper;
23 import eu.etaxonomy.cdm.io.common.Source;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26 import eu.etaxonomy.cdm.model.description.TaxonDescription;
27 import eu.etaxonomy.cdm.model.description.TextData;
28 import eu.etaxonomy.cdm.model.media.Media;
29 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
30 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
31 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
32 import eu.etaxonomy.cdm.model.taxon.Taxon;
33 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34
35
36
37
38
39
40
41
42 @Component
43 @SuppressWarnings("unchecked")
44 public class PesiImageExport extends PesiExportBase {
45 private static final Logger logger = Logger.getLogger(PesiImageExport.class);
46 private static final Class<? extends CdmBase> standardMethodParameter = ReferenceBase.class;
47
48 private static int modCount = 1000;
49 private static final String dbTableName = "Image";
50 private static final String pluralString = "DescriptionElements";
51 private static final String parentPluralString = "Taxa";
52
53 public PesiImageExport() {
54 super();
55 }
56
57
58
59
60 @Override
61 public Class<? extends CdmBase> getStandardMethodParameter() {
62 return standardMethodParameter;
63 }
64
65
66
67
68 @Override
69 protected boolean doCheck(PesiExportState state) {
70 boolean result = true;
71 return result;
72 }
73
74
75
76
77 @Override
78 protected boolean doInvoke(PesiExportState state) {
79
80 logger.error("*** Started Making " + pluralString + " ...");
81
82
83
84 int limit = 1000;
85
86
87 boolean success = true;
88
89
90 doDelete(state);
91
92
93 int count = 0;
94 int taxonCount = 0;
95 int pastCount = 0;
96 TransactionStatus txStatus = null;
97 List<TaxonBase> list = null;
98
99 Connection connection = state.getConfig().getDestination().getConnection();
100
101 txStatus = startTransaction(true);
102 logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
103 while ((list = getTaxonService().list(null, limit, taxonCount, null, null)).size() > 0) {
104
105 taxonCount += list.size();
106 logger.error("Fetched " + list.size() + " " + parentPluralString + ".");
107
108 logger.error("Check for Images...");
109 for (TaxonBase taxonBase : list) {
110
111 if (taxonBase.isInstanceOf(Taxon.class)) {
112
113 Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
114
115
116 Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
117
118
119 for (TaxonDescription taxonDescription : taxonDescriptions) {
120
121
122 if (taxonDescription.isImageGallery()) {
123
124 Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
125
126 for (DescriptionElementBase descriptionElement : descriptionElements) {
127 if (descriptionElement.isInstanceOf(TextData.class)) {
128 List<Media> media = descriptionElement.getMedia();
129
130 for (Media image : media) {
131 Set<MediaRepresentation> representations = image.getRepresentations();
132
133 for (MediaRepresentation representation : representations) {
134 List<MediaRepresentationPart> representationParts = representation.getParts();
135
136 for (MediaRepresentationPart representationPart : representationParts) {
137 String mediaUri = representationPart.getUri();
138
139
140 String thumb = null;
141 Integer taxonFk = state.getDbId(taxonBase.getName());
142
143 if (taxonFk != null && mediaUri != null) {
144 doCount(count++, modCount, pluralString);
145 invokeImages(thumb, mediaUri, taxonFk, connection);
146 }
147 }
148 }
149
150 }
151 }
152 }
153
154 }
155
156 }
157 }
158
159 }
160 logger.error("Exported " + (count - pastCount) + " " + pluralString + ".");
161
162
163 commitTransaction(txStatus);
164 logger.error("Committed transaction.");
165 logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
166 pastCount = count;
167
168
169 txStatus = startTransaction(true);
170 logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
171 }
172 if (list.size() == 0) {
173 logger.error("No " + pluralString + " left to fetch.");
174 }
175
176 commitTransaction(txStatus);
177 logger.error("Committed transaction.");
178
179 logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
180
181 return success;
182 }
183
184
185
186
187
188
189
190
191 private void invokeImages(String thumb, String url, Integer taxonFk, Connection connection) {
192 String imagesSql = "INSERT INTO Image (taxonFk, img_thumb, img_url) VALUES" +
193 " (?, ?, ?)";
194 try {
195 PreparedStatement imagesStmt = connection.prepareStatement(imagesSql);
196
197 if (taxonFk != null) {
198 imagesStmt.setInt(1, taxonFk);
199 } else {
200 imagesStmt.setObject(1, null);
201 }
202
203 if (thumb != null) {
204 imagesStmt.setString(2, thumb);
205 } else {
206 imagesStmt.setObject(2, null);
207 }
208
209 if (url != null) {
210 imagesStmt.setString(3, url);
211 } else {
212 imagesStmt.setObject(3, null);
213 }
214
215 imagesStmt.executeUpdate();
216 } catch (SQLException e) {
217 logger.error("Image could not be created. TaxonFk: " + taxonFk + ", Thumb: " + thumb + ", URL: " + url);
218 e.printStackTrace();
219 }
220
221 }
222
223
224
225
226
227
228 protected boolean doDelete(PesiExportState state) {
229 PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
230
231 String sql;
232 Source destination = pesiConfig.getDestination();
233
234
235 sql = "DELETE FROM " + dbTableName;
236 destination.setQuery(sql);
237 destination.update(sql);
238 return true;
239 }
240
241
242
243
244 @Override
245 protected boolean isIgnore(PesiExportState state) {
246
247
248 return false;
249 }
250
251 }