Project

General

Profile

Download (2.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_31;
10

    
11
import java.sql.SQLException;
12

    
13
import org.apache.log4j.Logger;
14

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

    
21
/**
22
 * @author a.mueller
23
 \* @since 15.12.2010
24
 */
25
public class LanguageLabelUpdater extends SchemaUpdaterStepBase{
26

    
27
	private static final Logger logger = Logger.getLogger(LanguageLabelUpdater.class);
28

    
29
	private static final String stepName = "Update language labels by full language name";
30

    
31
// **************************** STATIC METHODS ********************************/
32

    
33
	public static final LanguageLabelUpdater NewInstance(){
34
		return new LanguageLabelUpdater(stepName);
35
	}
36

    
37
	protected LanguageLabelUpdater(String stepName) {
38
		super(stepName);
39
	}
40

    
41
    @Override
42
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
43
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
44

    
45
		try {
46
			//update representation label
47
			String sql;
48
			sql = " UPDATE @@Representation@@ " +
49
				" SET label = text " +
50
				" WHERE id IN ( SELECT MN.representations_id " +
51
					" FROM @@DefinedTermBase@@ lang " +
52
					" INNER JOIN @@DefinedTermBase_Representation@@ MN ON lang.id = MN.DefinedTermBase_id " +
53
					" WHERE lang.DTYPE = 'Language' " +
54
					" )";
55
			datasource.executeUpdate(caseType.replaceTableNames(sql));
56

    
57
			//update term titleCache
58
			sql = " UPDATE @@DefinedTermBase@@ dtb " +
59
				  " SET titleCache = " +
60
						" ( " +
61
						" SELECT rep.label  " +
62
						" FROM @@DefinedTermBase_Representation@@ MN " +
63
						" INNER JOIN @@Representation@@ rep ON MN.representations_id = rep.id " +
64
						" WHERE dtb.id = MN.DefinedTermBase_id AND rep.language_id = @langId) " +
65
					" WHERE dtb.DTYPE = 'Language'";
66
			String englishId = String.valueOf(getEnglishLanguageId(datasource, monitor, caseType));
67
			if (englishId == null){
68
				throw new NullPointerException("English id could not be found");
69
			}
70
			sql = sql.replace("@langId", englishId);
71
			datasource.executeUpdate(caseType.replaceTableNames(sql));
72

    
73
			return;
74
		} catch (Exception e) {
75
			String message = e.getMessage();
76
		    monitor.warning(message, e);
77
			logger.warn(message);
78
			result.addException(e, message, this, "invoke");
79
			return;
80
		}
81
	}
82

    
83

    
84
}
(1-1/2)