Project

General

Profile

Download (5.66 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.v31_33;
10

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

    
15
import org.apache.log4j.Logger;
16

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

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

    
30
    private static final Logger logger = Logger.getLogger(SpecimenMediaMoverUpdater.class);
31

    
32
	private static final String stepName = "Update rank class values";
33

    
34
// **************************** STATIC METHODS ********************************/
35

    
36
	public static final SpecimenMediaMoverUpdater NewInstance(){
37
		return new SpecimenMediaMoverUpdater(stepName);
38
	}
39

    
40
	protected SpecimenMediaMoverUpdater(String stepName) {
41
		super(stepName);
42
	}
43

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

    
48
		try {
49
			Integer featureId = null;
50

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

    
61
				Integer specimenId = rs.getInt("SpecimenOrObservationBase_id");
62
				Integer mediaId = rs.getInt("media_id");
63

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

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

    
70
				//sortIndex
71
				Number sortIndex = getSortIndex(datasource, monitor, textDataId, mediaId, caseType);
72

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

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

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

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

    
110
		return sortIndex;
111
	}
112

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

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

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

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

    
134
	}
135

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

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

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

    
157
}
(5-5/6)