Project

General

Profile

Download (5.62 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.ITermUpdaterStep;
21
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
22

    
23
/**
24
 * @author a.mueller
25
 * @date 15.12.2013
26
 */
27
public class SpecimenMediaMoverUpdater extends SchemaUpdaterStepBase<SpecimenMediaMoverUpdater> implements ITermUpdaterStep{
28
	private static final Logger logger = Logger.getLogger(SpecimenMediaMoverUpdater.class);
29

    
30
	private static final String stepName = "Update rank class values";
31
	
32
// **************************** STATIC METHODS ********************************/
33

    
34
	public static final SpecimenMediaMoverUpdater NewInstance(){
35
		return new SpecimenMediaMoverUpdater(stepName);	
36
	}
37

    
38
	protected SpecimenMediaMoverUpdater(String stepName) {
39
		super(stepName);
40
	}
41

    
42
	@Override
43
	public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException {
44
		
45
		try {
46
			Integer featureId = null;
47
			
48
			//get existing media
49
			String sql = caseType.replaceTableNames(
50
					" SELECT SpecimenOrObservationBase_id, media_id " +
51
					" FROM @@SpecimenOrObservationBase_Media@@");
52
			ResultSet rs = datasource.executeQuery(sql); 
53
			while (rs.next()){
54
				if (featureId == null){
55
					featureId = getFeatureId(datasource, caseType);
56
				}
57
				
58
				Integer specimenId = rs.getInt("SpecimenOrObservationBase_id");
59
				Integer mediaId = rs.getInt("media_id");
60
				
61
				//image gallery
62
				Number galleryId = getOrCreateImageGallery(datasource, monitor, specimenId, caseType);
63
				
64
				//textData
65
				Number textDataId = getOrCreateTextData(datasource, monitor, galleryId, featureId, caseType);
66
				
67
				//sortIndex
68
				Number sortIndex = getSortIndex(datasource, monitor, textDataId, mediaId, caseType);
69
				
70
				//insert
71
				sql = caseType.replaceTableNames(
72
						" INSERT INTO @@DescriptionElementBase_Media@@" +
73
								" (DescriptionElementBase_id, media_id, sortIndex) " +
74
						" VALUES (%d, %d, %d)");
75
				sql = String.format(sql, textDataId, mediaId, sortIndex);
76
				datasource.executeUpdate(sql);
77
			}
78
			
79
			return 0;
80
		} catch (Exception e) {
81
			monitor.warning(e.getMessage(), e);
82
			logger.warn(e.getMessage());
83
			return null;
84
		}
85
	}
86

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

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

    
105
		return sortIndex;
106
	}
107

    
108
	private Number getOrCreateTextData(ICdmDataSource datasource, IProgressMonitor monitor, Number galleryId, Integer featureId, CaseType caseType) throws SQLException {
109
	
110
		String sql = caseType.replaceTableNames(
111
				" SELECT deb.id " + 
112
				" FROM @@DescriptionElementBase@@ deb " +
113
				" WHERE deb.DTYPE = 'TextData' AND feature_id = "+ featureId +" AND deb.indescription_id = " + galleryId);
114
		Number textDataId = (Integer)datasource.getSingleValue(sql);
115
		
116
		if (textDataId == null){
117
			sql = caseType.replaceTableNames(
118
					" SELECT max(id)+1 FROM @@DescriptionElementBase@@ ");
119
			textDataId = (Long)datasource.getSingleValue(sql);
120
			
121
			sql = caseType.replaceTableNames(
122
					" INSERT INTO @@DescriptionElementBase@@ (DTYPE, id, created, uuid, feature_id, indescription_id)  " +
123
					" VALUES  ('TextData', %d, '%s', '%s', %d, %d) ");
124
			sql = String.format(sql, textDataId, this.getNowString(), UUID.randomUUID().toString(), featureId, galleryId);
125
			datasource.executeUpdate(sql);
126
		}
127
		return textDataId;
128

    
129
	}
130

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

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

    
152
}
(5-5/7)