removing @Override for compatibility with java 1.6
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / TermRepresentationUpdater.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;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
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 * @date 27.09.2011
26 *
27 */
28 public class TermRepresentationUpdater extends SchemaUpdaterStepBase<TermRepresentationUpdater> implements ITermUpdaterStep{
29 @SuppressWarnings("unused")
30 private static final Logger logger = Logger.getLogger(TermRepresentationUpdater.class);
31
32 public static final TermRepresentationUpdater NewInstance(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage){
33 return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, false);
34 }
35
36 public static final TermRepresentationUpdater NewReverseInstance(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage){
37 return new TermRepresentationUpdater(stepName, uuidTerm, description, label, abbrev, uuidLanguage, true);
38 }
39
40 private UUID uuidTerm ;
41 private String description;
42 private String label;
43 private String abbrev;
44 private UUID uuidLanguage;
45 private boolean isReverse = false;
46
47 private TermRepresentationUpdater(String stepName, UUID uuidTerm, String description, String label, String abbrev, UUID uuidLanguage, boolean isReverse) {
48 super(stepName);
49 this.abbrev = abbrev;
50 this.description = description;
51 this.label = label;
52 this.uuidTerm = uuidTerm;
53 this.uuidLanguage = uuidLanguage;
54 this.isReverse = isReverse;
55 }
56
57
58
59 public Integer invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException{
60
61 String sqlCheckTermExists = " SELECT count(*) as n FROM DefinedTermBase WHERE uuid = '" + uuidTerm + "'";
62 Long n = (Long)datasource.getSingleValue(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);
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);
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 if (description != null){
89 String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "text", description, repId);
90 datasource.executeUpdate(sqlUpdateRepresentation);
91 }
92 if (label != null){
93 String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "label", label, repId);
94 datasource.executeUpdate(sqlUpdateRepresentation);
95 }
96 if (abbrev != null){
97 String sqlUpdateRepresentation = String.format(sqlUpdateRepresentationFormat, "abbreviatedLabel", abbrev, repId);
98 datasource.executeUpdate(sqlUpdateRepresentation);
99 }
100
101 return repId;
102 }
103
104 /**
105 * @param datasource
106 * @param monitor
107 * @param langId
108 * @return
109 * @throws SQLException
110 */
111 private Integer getRepresentationId(ICdmDataSource datasource,
112 IProgressMonitor monitor, Integer langId) throws SQLException {
113 //representation
114
115 String tableName = isReverse ? "RelationshipTermBase_inverseRepresentation" : "DefinedTermBase_Representation" ;
116 String repIdFkCol = isReverse ? "inverserepresentations_id" : "representations_id";
117 String sqlId = " SELECT rep.id " +
118 " FROM Representation rep INNER JOIN %s MN ON MN.%s = rep.id " +
119 " INNER JOIN DefinedTermBase dtb ON MN.DefinedTermBase_id = dtb.id " +
120 " WHERE dtb.uuid = '%s' ";
121 sqlId = String.format(sqlId, tableName, repIdFkCol, uuidTerm.toString());
122 if (uuidLanguage != null){
123 sqlId += " AND rep.language_id = " + langId;
124 }
125 ResultSet rs = datasource.executeQuery(sqlId);
126 Integer repId;
127 if (rs.next()){
128 repId = rs.getInt("id");
129 }else{
130 String warning = "No representations do exist yet. Can't update term representation for term '%s'!";
131 warning = String.format(warning, uuidTerm.toString());
132 monitor.warning(warning);
133 repId = null;
134 }
135 return repId;
136 }
137
138
139
140 }