Project

General

Profile

Download (5.45 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);
35
	}
36

    
37
	public static final TermRepresentationUpdater NewReverseInstance(String stepName, UUID uuidTerm, String description,  String label, String abbrev, UUID uuidLanguage){
38
		return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, true);
39
	}
40

    
41
	private UUID uuidTerm ;
42
	private String description;
43
	private String label;
44
	private String abbrev;
45
	private UUID uuidLanguage;
46
	private boolean isReverse = false;
47

    
48
	private TermRepresentationUpdater(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage, boolean isReverse) {
49
		super(stepName);
50
		this.abbrev = abbrev;
51
		this.description = description;
52
		this.label = label;
53
		this.uuidTerm = uuidTerm;
54
		this.uuidLanguage = uuidLanguage;
55
		this.isReverse = isReverse;
56
	}
57

    
58

    
59

    
60
	@Override
61
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
62
            CaseType caseType, SchemaUpdateResult result) throws SQLException{
63

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

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

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

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

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

    
111
		return;
112
	}
113

    
114
	/**
115
	 * @param datasource
116
	 * @param monitor
117
	 * @param langId
118
	 * @param caseType
119
	 * @return
120
	 * @throws SQLException
121
	 */
122
	private Integer getRepresentationId(ICdmDataSource datasource,
123
			IProgressMonitor monitor, Integer langId, CaseType caseType) throws SQLException {
124
		//representation
125

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

    
151
}
(31-31/35)