Project

General

Profile

Download (6.12 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;
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.CdmUtils;
18
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
19
import eu.etaxonomy.cdm.database.ICdmDataSource;
20

    
21
/**
22
 * Class for updating term representations.
23
 * @author a.mueller
24
 * @since 27.09.2011
25
 *
26
 */
27
public class TermRepresentationUpdater
28
            extends SchemaUpdaterStepBase {
29

    
30
    @SuppressWarnings("unused")
31
	private static final Logger logger = Logger.getLogger(TermRepresentationUpdater.class);
32

    
33
	public static final TermRepresentationUpdater NewInstance(String stepName, UUID uuidTerm, String description,  String label, String abbrev, UUID uuidLanguage){
34
		return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, false, false);
35
	}
36
    public static final TermRepresentationUpdater NewInstanceWithTitleCache(String stepName, UUID uuidTerm, String description,  String label, String abbrev, UUID uuidLanguage){
37
        return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, false, true);
38
    }
39

    
40
	public static final TermRepresentationUpdater NewReverseInstance(String stepName, UUID uuidTerm, String description,  String label, String abbrev, UUID uuidLanguage){
41
		return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, true, false);
42
	}
43

    
44
	private UUID uuidTerm ;
45
	private String description;
46
	private String label;
47
	private String abbrev;
48
	private UUID uuidLanguage;
49
	private boolean isReverse = false;
50
	private boolean includeTitleCache = false;
51

    
52
	private TermRepresentationUpdater(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage, boolean isReverse, boolean includeTitleCache) {
53
		super(stepName);
54
		this.abbrev = abbrev;
55
		this.description = description;
56
		this.label = label;
57
		this.uuidTerm = uuidTerm;
58
		this.uuidLanguage = uuidLanguage;
59
		this.isReverse = isReverse;
60
		this.includeTitleCache = includeTitleCache;
61
	}
62

    
63
	@Override
64
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
65
            CaseType caseType, SchemaUpdateResult result) throws SQLException{
66

    
67
		String sqlCheckTermExists = " SELECT count(*) as n FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidTerm + "'";
68

    
69
		Long n = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCheckTermExists));
70
		if (n == 0){
71
			String name = label != null ? label : abbrev != null ? abbrev : description;
72
			String message = "Term for representations update does not exist. Term not updated: " + CdmUtils.Nz(name) + "(" + uuidTerm + ")";
73
			monitor.warning(message);
74
			result.addError(message, this, "invoke");
75
			return;
76
		}
77

    
78
		//language id
79
		Integer langId = null;
80
		if (uuidLanguage != null){
81
			langId = getLanguageId(uuidLanguage, datasource, monitor, caseType);
82
			if (langId == null){
83
				String message = "Language for language uuid (%s) could not be found. Term representations not updated.";
84
				message = String.format(message, uuidLanguage.toString());
85
				monitor.warning(message);
86
	            result.addError(message, this, "invoke");
87
	            return;
88
			}
89
		}
90

    
91
		Integer repId = getRepresentationId(datasource, monitor, langId, caseType);
92
		if (repId == null){
93
			String message = "repId is null";
94
		    result.addError(message, this, "invoke");
95
			return;
96
		}
97

    
98
		//standard representation
99
		String sqlUpdateRepresentationFormat = " UPDATE @@Representation@@ r SET %s = '%s' WHERE r.id = %d ";
100
		sqlUpdateRepresentationFormat = caseType.replaceTableNames(sqlUpdateRepresentationFormat);
101
		if (description != null){
102
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "text", description, repId);
103
			datasource.executeUpdate(sqlUpdateRepresentation);
104
		}
105
		if (label != null){
106
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "label", label, repId);
107
			datasource.executeUpdate(sqlUpdateRepresentation);
108
		}
109
		if (abbrev != null){
110
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "abbreviatedLabel", abbrev, repId);
111
			datasource.executeUpdate(sqlUpdateRepresentation);
112
		}
113

    
114
		if (includeTitleCache && label != null){
115
		    String sql = "UPDATE '%s' SET titleCache = '%s' WHERE uuid = '%s'";
116
		    sql = String.format(sql, caseType.transformTo("DefinedTermBase"), label, uuidTerm);
117
		    datasource.executeUpdate(sql);
118
		}
119

    
120
		return;
121
	}
122

    
123
	/**
124
	 * @param datasource
125
	 * @param monitor
126
	 * @param langId
127
	 * @param caseType
128
	 * @return
129
	 * @throws SQLException
130
	 */
131
	private Integer getRepresentationId(ICdmDataSource datasource,
132
			IProgressMonitor monitor, Integer langId, CaseType caseType) throws SQLException {
133
		//representation
134

    
135
		String tableName = isReverse ? "RelationshipTermBase_inverseRepresentation" : "DefinedTermBase_Representation" ;
136
		String repIdFkCol = isReverse ? "inverserepresentations_id" : "representations_id";
137
		String sqlId = " SELECT rep.id " +
138
			" FROM @@Representation@@ rep INNER JOIN %s MN ON MN.%s = rep.id " +
139
			" INNER JOIN @@DefinedTermBase@@ dtb ON MN.DefinedTermBase_id = dtb.id " +
140
			" WHERE dtb.uuid = '%s' ";
141
		tableName = caseType.transformTo(tableName);
142
		sqlId = String.format(sqlId, tableName, repIdFkCol, uuidTerm.toString());
143
		sqlId = caseType.replaceTableNames(sqlId);
144
		if (uuidLanguage != null){
145
			sqlId += " AND rep.language_id = " + langId;
146
		}
147
		ResultSet rs = datasource.executeQuery(sqlId);
148
		Integer repId;
149
		if (rs.next()){
150
			repId = rs.getInt("id");
151
		}else{
152
			String warning = "No representations do exist yet. Can't update term representation for term '%s'!";
153
			warning = String.format(warning, uuidTerm.toString());
154
			monitor.warning(warning);
155
			repId = null;
156
		}
157
		return repId;
158
	}
159

    
160
}
(32-32/36)