Project

General

Profile

Download (7.11 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.List;
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
20
import eu.etaxonomy.cdm.database.ICdmDataSource;
21

    
22
/**
23
 * Class for updating term representations.
24
 * @author a.mueller
25
 * @since 27.09.2011
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(List<ISchemaUpdaterStep> stepList,
34
	        String stepName, UUID uuidTerm, String description,  String label, String abbrev,
35
	        UUID uuidLanguage){
36
		return new TermRepresentationUpdater(stepList, stepName, uuidTerm, description, label,
37
		        abbrev, uuidLanguage, false, false, false);
38
	}
39
    public static final TermRepresentationUpdater NewInstance(List<ISchemaUpdaterStep> stepList,
40
            String stepName, UUID uuidTerm, String description,  String label, String abbrev,
41
            UUID uuidLanguage, boolean withIdInVoc){
42
        return new TermRepresentationUpdater(stepList, stepName, uuidTerm, description, label,
43
                abbrev, uuidLanguage, false, false, withIdInVoc);
44
    }
45
    public static final TermRepresentationUpdater NewInstanceWithTitleCache(List<ISchemaUpdaterStep> stepList,
46
            String stepName, UUID uuidTerm, String description,  String label, String abbrev,
47
            UUID uuidLanguage){
48
        return new TermRepresentationUpdater(stepList, stepName, uuidTerm, description, label,
49
                abbrev, uuidLanguage, false, true, false);
50
    }
51

    
52
	public static final TermRepresentationUpdater NewInverseInstance(List<ISchemaUpdaterStep> stepList,
53
	        String stepName, UUID uuidTerm, String description,  String label, String abbrev,
54
	        UUID uuidLanguage){
55
		return new TermRepresentationUpdater(stepList, stepName, uuidTerm, description, label,
56
		        abbrev, uuidLanguage, true, false, false);
57
	}
58

    
59
	private UUID uuidTerm ;
60
	private String description;
61
	private String label;
62
	private String abbrev;
63
	private UUID uuidLanguage;
64
	private boolean isInverse = false;
65
	private boolean includeTitleCache = false;
66
	private boolean includeIdInVoc = false;
67

    
68
	private TermRepresentationUpdater(List<ISchemaUpdaterStep> stepList, String stepName, UUID uuidTerm,
69
	        String description, String label, String abbrev, UUID uuidLanguage, boolean isReverse,
70
	        boolean includeTitleCache, boolean includeIdInVoc) {
71
		super(stepList, stepName);
72
		this.abbrev = abbrev;
73
		this.description = description;
74
		this.label = label;
75
		this.uuidTerm = uuidTerm;
76
		this.uuidLanguage = uuidLanguage;
77
		this.isInverse = isReverse;
78
		this.includeTitleCache = includeTitleCache;
79
		this.includeIdInVoc = includeIdInVoc;
80
	}
81

    
82
	@Override
83
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
84
            CaseType caseType, SchemaUpdateResult result) throws SQLException{
85

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

    
88
		Long n = (Long)datasource.getSingleValue(caseType.replaceTableNames(sqlCheckTermExists));
89
		if (n == 0){
90
			String name = label != null ? label : abbrev != null ? abbrev : description;
91
			String message = "Term for representations update does not exist. Term not updated: " + CdmUtils.Nz(name) + "(" + uuidTerm + ")";
92
			monitor.warning(message);
93
			result.addError(message, this, "invoke");
94
			return;
95
		}
96

    
97
		//language id
98
		Integer langId = null;
99
		if (uuidLanguage != null){
100
			langId = getLanguageId(uuidLanguage, datasource, monitor, caseType);
101
			if (langId == null){
102
				String message = "Language for language uuid (%s) could not be found. Term representations not updated.";
103
				message = String.format(message, uuidLanguage.toString());
104
				monitor.warning(message);
105
	            result.addError(message, this, "invoke");
106
	            return;
107
			}
108
		}
109

    
110
		Integer repId = getRepresentationId(datasource, monitor, langId, caseType);
111
		if (repId == null){
112
			String message = "repId is null";
113
		    result.addError(message, this, "invoke");
114
			return;
115
		}
116

    
117
		//standard representation
118
		String sqlUpdateRepresentationFormat = " UPDATE @@Representation@@ r SET %s = '%s' WHERE r.id = %d ";
119
		sqlUpdateRepresentationFormat = caseType.replaceTableNames(sqlUpdateRepresentationFormat);
120
		if (description != null){
121
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "text", description, repId);
122
			datasource.executeUpdate(sqlUpdateRepresentation);
123
		}
124
		if (label != null){
125
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "label", label, repId);
126
			datasource.executeUpdate(sqlUpdateRepresentation);
127
		}
128
		if (abbrev != null){
129
			String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "abbreviatedLabel", abbrev, repId);
130
			datasource.executeUpdate(sqlUpdateRepresentation);
131
		}
132

    
133
		if (includeTitleCache && label != null){
134
		    String sql = "UPDATE %s SET titleCache = '%s' WHERE uuid = '%s'";
135
		    sql = String.format(sql, caseType.transformTo("DefinedTermBase"), label, uuidTerm);
136
		    datasource.executeUpdate(sql);
137
		}
138
        if (includeIdInVoc && abbrev != null){
139
            String sql = "UPDATE %s SET idInVocabulary = '%s' WHERE uuid = '%s'";
140
            sql = String.format(sql, caseType.transformTo("DefinedTermBase"), abbrev, uuidTerm);
141
            datasource.executeUpdate(sql);
142
        }
143

    
144

    
145

    
146
		return;
147
	}
148

    
149
	private Integer getRepresentationId(ICdmDataSource datasource,
150
			IProgressMonitor monitor, Integer langId, CaseType caseType) throws SQLException {
151
		//representation
152

    
153
		String tableName = isInverse ? "DefinedTermBase_InverseRepresentation" : "DefinedTermBase_Representation" ;
154
		String repIdFkCol = isInverse ? "inverserepresentations_id" : "representations_id";
155
		String sqlId = " SELECT rep.id " +
156
			" FROM @@Representation@@ rep INNER JOIN %s MN ON MN.%s = rep.id " +
157
			" INNER JOIN @@DefinedTermBase@@ dtb ON MN.DefinedTermBase_id = dtb.id " +
158
			" WHERE dtb.uuid = '%s' ";
159
		tableName = caseType.transformTo(tableName);
160
		sqlId = String.format(sqlId, tableName, repIdFkCol, uuidTerm.toString());
161
		sqlId = caseType.replaceTableNames(sqlId);
162
		if (uuidLanguage != null){
163
			sqlId += " AND rep.language_id = " + langId;
164
		}
165
		ResultSet rs = datasource.executeQuery(sqlId);
166
		Integer repId;
167
		if (rs.next()){
168
			repId = rs.getInt("id");
169
		}else{
170
			String warning = "No representations do exist yet. Can't update term representation for term '%s'!";
171
			warning = String.format(warning, uuidTerm.toString());
172
			monitor.warning(warning);
173
			repId = null;
174
		}
175
		return repId;
176
	}
177
}
(37-37/41)