Project

General

Profile

Download (5.73 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.database.update.v30_40;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
19
import eu.etaxonomy.cdm.database.ICdmDataSource;
20
import eu.etaxonomy.cdm.database.update.CaseType;
21
import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
22
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
23
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
24

    
25
/**
26
 * @author a.mueller
27
 * @since 15.12.2013
28
 */
29
public class SpecimenMediaMoverUpdater
30
            extends SchemaUpdaterStepBase{
31

    
32
    private static final Logger logger = LogManager.getLogger(SpecimenMediaMoverUpdater.class);
33

    
34
	private static final String stepName = "Update rank class values";
35

    
36
// **************************** STATIC METHODS ********************************/
37

    
38
	public static final SpecimenMediaMoverUpdater NewInstance(List<ISchemaUpdaterStep> stepList){
39
		return new SpecimenMediaMoverUpdater(stepList, stepName);
40
	}
41

    
42
	protected SpecimenMediaMoverUpdater(List<ISchemaUpdaterStep> stepList, String stepName) {
43
		super(stepList, stepName);
44
	}
45

    
46
    @Override
47
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
48
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
49

    
50
		try {
51
			Integer featureId = null;
52

    
53
			//get existing media
54
			String sql = caseType.replaceTableNames(
55
					" SELECT SpecimenOrObservationBase_id, media_id " +
56
					" FROM @@SpecimenOrObservationBase_Media@@");
57
			ResultSet rs = datasource.executeQuery(sql);
58
			while (rs.next()){
59
				if (featureId == null){
60
					featureId = getFeatureId(datasource, caseType);
61
				}
62

    
63
				Integer specimenId = rs.getInt("SpecimenOrObservationBase_id");
64
				Integer mediaId = rs.getInt("media_id");
65

    
66
				//image gallery
67
				Number galleryId = getOrCreateImageGallery(datasource, monitor, specimenId, caseType);
68

    
69
				//textData
70
				Number textDataId = getOrCreateTextData(datasource, monitor, galleryId, featureId, caseType);
71

    
72
				//sortIndex
73
				Number sortIndex = getSortIndex(datasource, monitor, textDataId, mediaId, caseType);
74

    
75
				//insert
76
				sql = caseType.replaceTableNames(
77
						" INSERT INTO @@DescriptionElementBase_Media@@" +
78
								" (DescriptionElementBase_id, media_id, sortIndex) " +
79
						" VALUES (%d, %d, %d)");
80
				sql = String.format(sql, textDataId, mediaId, sortIndex);
81
				datasource.executeUpdate(sql);
82
			}
83

    
84
			return;
85
		} catch (Exception e) {
86
			String message = e.getMessage();
87
		    monitor.warning(message, e);
88
			logger.warn(message);
89
			result.addException(e, message, this, "invoke");
90
			return;
91
		}
92
	}
93

    
94
	private Integer getFeatureId(ICdmDataSource datasource, CaseType caseType) throws SQLException {
95
		String sql = caseType.replaceTableNames(
96
				" SELECT id " +
97
				" FROM @@DefinedTermBase@@ " +
98
				" WHERE uuid = '84193b2c-327f-4cce-90ef-c8da18fd5bb5'");
99
		return (Integer)datasource.getSingleValue(sql);
100
	}
101

    
102
	private Number getSortIndex(ICdmDataSource datasource, IProgressMonitor monitor, Number textDataId, Integer mediaId, CaseType caseType) throws SQLException {
103
		String sql = caseType.replaceTableNames(
104
				" SELECT max(sortIndex) " +
105
				" FROM @@DescriptionElementBase_Media@@ MN " +
106
				" WHERE MN.DescriptionElementBase_id = "+textDataId+" AND MN.media_id = " + mediaId);
107
		Number sortIndex = (Long)datasource.getSingleValue(sql);
108
		if (sortIndex == null){
109
			sortIndex = 0;
110
		}
111

    
112
		return sortIndex;
113
	}
114

    
115
	private Number getOrCreateTextData(ICdmDataSource datasource, IProgressMonitor monitor, Number galleryId, Integer featureId, CaseType caseType) throws SQLException {
116

    
117
		String sql = caseType.replaceTableNames(
118
				" SELECT deb.id " +
119
				" FROM @@DescriptionElementBase@@ deb " +
120
				" WHERE deb.DTYPE = 'TextData' AND feature_id = "+ featureId +" AND deb.indescription_id = " + galleryId);
121
		Number textDataId = (Integer)datasource.getSingleValue(sql);
122

    
123
		if (textDataId == null){
124
			sql = caseType.replaceTableNames(
125
					" SELECT max(id)+1 FROM @@DescriptionElementBase@@ ");
126
			textDataId = (Long)datasource.getSingleValue(sql);
127

    
128
			sql = caseType.replaceTableNames(
129
					" INSERT INTO @@DescriptionElementBase@@ (DTYPE, id, created, uuid, feature_id, indescription_id)  " +
130
					" VALUES  ('TextData', %d, '%s', '%s', %d, %d) ");
131
			sql = String.format(sql, textDataId, this.getNowString(), UUID.randomUUID().toString(), featureId, galleryId);
132
			datasource.executeUpdate(sql);
133
		}
134
		return textDataId;
135

    
136
	}
137

    
138
	private Number getOrCreateImageGallery(ICdmDataSource datasource, IProgressMonitor monitor, Integer specimenId, CaseType caseType) throws SQLException {
139
		String sql = caseType.replaceTableNames(
140
				" SELECT  db.id " +
141
				" FROM @@DescriptionBase@@ db " +
142
				" WHERE db.imagegallery = True AND db.specimen_id = " + specimenId);
143
		Number descriptionId = (Number)datasource.getSingleValue(sql);
144

    
145
		if (descriptionId == null){
146
			sql = caseType.replaceTableNames(
147
					" SELECT max(id)+1 FROM @@DescriptionBase@@ ");
148
			descriptionId = (Long)datasource.getSingleValue(sql);
149

    
150
			sql = caseType.replaceTableNames(
151
					" INSERT INTO @@DescriptionBase@@ (DTYPE, id, created, uuid, protectedtitlecache, titleCache, imagegallery, specimen_id) " +
152
					" VALUES ('SpecimenDescription',  %d, '%s', '%s', 1, 'Specimenimage(s) moved for schema update', 1, %d) ");
153
			sql = String.format(sql, descriptionId, this.getNowString(), UUID.randomUUID().toString(), specimenId);
154
			datasource.executeUpdate(sql);
155
		}
156
		return descriptionId;
157
	}
158

    
159
}
(14-14/16)