include more auditing in schema update and refactor to better allow transaction suppo...
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / v30_31 / LanguageLabelUpdater.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.database.update.v30_31;
11
12 import java.sql.SQLException;
13
14 import org.apache.log4j.Logger;
15
16 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
17 import eu.etaxonomy.cdm.database.ICdmDataSource;
18 import eu.etaxonomy.cdm.database.update.ITermUpdaterStep;
19 import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
20
21 /**
22 * @author a.mueller
23 * @date 15.12.2010
24 */
25 public class LanguageLabelUpdater extends SchemaUpdaterStepBase<LanguageLabelUpdater> implements ITermUpdaterStep{
26 @SuppressWarnings("unused")
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 Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor) {
43
44 try {
45 //update representation label
46 String sql;
47 sql = " UPDATE Representation " +
48 " SET label = text " +
49 " WHERE id IN ( SELECT MN.representations_id " +
50 " FROM DefinedTermBase lang " +
51 " INNER JOIN DefinedTermBase_Representation MN ON lang.id = MN.DefinedTermBase_id " +
52 " WHERE lang.DTYPE = 'Language' " +
53 " )";
54 datasource.executeUpdate(sql);
55
56 //update term titleCache
57 sql = " UPDATE DefinedTermBase dtb " +
58 " SET titleCache = " +
59 " ( " +
60 " SELECT rep.label " +
61 " FROM DefinedTermBase_Representation MN " +
62 " INNER JOIN Representation rep ON MN.representations_id = rep.id " +
63 " WHERE dtb.id = MN.DefinedTermBase_id AND rep.language_id = @langId) " +
64 " WHERE dtb.DTYPE = 'Language'";
65 String englishId = String.valueOf(getEnglishLanguageId(datasource, monitor));
66 if (englishId == null){
67 throw new NullPointerException("English id could not be found");
68 }
69 sql = sql.replace("@langId", englishId);
70 datasource.executeUpdate(sql);
71
72 return 0;
73 } catch (Exception e) {
74 monitor.warning(e.getMessage(), e);
75 logger.warn(e.getMessage());
76 return null;
77 }
78 }
79
80
81 }