Project

General

Profile

Download (5.25 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
 * @date 27.09.2011
25
 *
26
 */
27
public class TermRepresentationUpdater extends SchemaUpdaterStepBase<TermRepresentationUpdater> implements ITermUpdaterStep{
28
	@SuppressWarnings("unused")
29
	private static final Logger logger = Logger.getLogger(TermRepresentationUpdater.class);
30
	
31
	public static final TermRepresentationUpdater NewInstance(String stepName, UUID uuidTerm, String description,  String label, String abbrev, UUID uuidLanguage){
32
		return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, false);	
33
	}
34
	
35
	public static final TermRepresentationUpdater NewReverseInstance(String stepName, UUID uuidTerm, String description,  String label, String abbrev, UUID uuidLanguage){
36
		return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, true);	
37
	}
38
	
39
	private UUID uuidTerm ;
40
	private String description;
41
	private String label;
42
	private String abbrev;
43
	private UUID uuidLanguage;
44
	private boolean isReverse = false;
45
	
46
	private TermRepresentationUpdater(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage, boolean isReverse) {
47
		super(stepName);
48
		this.abbrev = abbrev;
49
		this.description = description;
50
		this.label = label;
51
		this.uuidTerm = uuidTerm;
52
		this.uuidLanguage = uuidLanguage;
53
		this.isReverse = isReverse;
54
	}
55

    
56
	
57

    
58
	public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType) throws SQLException{
59
		
60
		String sqlCheckTermExists = " SELECT count(*) as n FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidTerm + "'";
61
		
62
		Long n = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCheckTermExists));
63
		if (n == 0){
64
			String name = label != null ? label : abbrev != null ? abbrev : description;
65
			monitor.warning("Term for representations update does not exist. Term not updated: " + CdmUtils.Nz(name) + "(" + uuidTerm + ")");
66
			return null;
67
		}
68

    
69
		//language id
70
		Integer langId = null;
71
		if (uuidLanguage != null){
72
			langId = getLanguageId(uuidLanguage, datasource, monitor, caseType);
73
			if (langId == null){
74
				String warning = "Language for language uuid (%s) could not be found. Term representations not updated.";
75
				warning = String.format(warning, uuidLanguage.toString());
76
				monitor.warning(warning);
77
				return null;
78
			}
79
		}
80
		
81
		Integer repId = getRepresentationId(datasource, monitor, langId, caseType);
82
		if (repId == null){
83
			return null;
84
		}
85
		
86
		//standard representation
87
		String sqlUpdateRepresentationFormat = " UPDATE @@Representation@@ r SET %s = '%s' WHERE r.id = %d ";
88
		sqlUpdateRepresentationFormat = caseType.replaceTableNames(sqlUpdateRepresentationFormat);
89
		if (description != null){
90
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "text", description, repId);
91
			datasource.executeUpdate(sqlUpdateRepresentation);
92
		}
93
		if (label != null){
94
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "label", label, repId);
95
			datasource.executeUpdate(sqlUpdateRepresentation);
96
		}
97
		if (abbrev != null){
98
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "abbreviatedLabel", abbrev, repId);
99
			datasource.executeUpdate(sqlUpdateRepresentation);
100
		}
101
		
102
		return repId;
103
	}
104

    
105
	/**
106
	 * @param datasource
107
	 * @param monitor
108
	 * @param langId
109
	 * @param caseType 
110
	 * @return
111
	 * @throws SQLException
112
	 */
113
	private Integer getRepresentationId(ICdmDataSource datasource,
114
			IProgressMonitor monitor, Integer langId, CaseType caseType) throws SQLException {
115
		//representation
116
		
117
		String tableName = isReverse ? "RelationshipTermBase_inverseRepresentation" : "DefinedTermBase_Representation" ;
118
		String repIdFkCol = isReverse ? "inverserepresentations_id" : "representations_id";
119
		String sqlId = " SELECT rep.id " + 
120
			" FROM @@Representation@@ rep INNER JOIN %s MN ON MN.%s = rep.id " + 
121
			" INNER JOIN @@DefinedTermBase@@ dtb ON MN.DefinedTermBase_id = dtb.id " +
122
			" WHERE dtb.uuid = '%s' ";
123
		tableName = caseType.transformTo(tableName);
124
		sqlId = String.format(sqlId, tableName, repIdFkCol, uuidTerm.toString());
125
		sqlId = caseType.replaceTableNames(sqlId);
126
		if (uuidLanguage != null){
127
			sqlId += " AND rep.language_id = " + langId;
128
		}
129
		ResultSet rs = datasource.executeQuery(sqlId);
130
		Integer repId;
131
		if (rs.next()){
132
			repId = rs.getInt("id");
133
		}else{
134
			String warning = "No representations do exist yet. Can't update term representation for term '%s'!";
135
			warning = String.format(warning, uuidTerm.toString());
136
			monitor.warning(warning);
137
			repId = null;
138
		}
139
		return repId;
140
	}
141

    
142
}
(31-31/36)