3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.database
.update
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
18 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
19 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
20 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
23 * Class for updating term representations.
28 public class TermRepresentationUpdater
extends SchemaUpdaterStepBase
<TermRepresentationUpdater
> implements ITermUpdaterStep
{
29 @SuppressWarnings("unused")
30 private static final Logger logger
= Logger
.getLogger(TermRepresentationUpdater
.class);
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);
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);
40 private UUID uuidTerm
;
41 private String description
;
43 private String abbrev
;
44 private UUID uuidLanguage
;
45 private boolean isReverse
= false;
47 private TermRepresentationUpdater(String stepName
, UUID uuidTerm
, String description
, String label
, String abbrev
, UUID uuidLanguage
, boolean isReverse
) {
50 this.description
= description
;
52 this.uuidTerm
= uuidTerm
;
53 this.uuidLanguage
= uuidLanguage
;
54 this.isReverse
= isReverse
;
59 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws SQLException
{
61 String sqlCheckTermExists
= " SELECT count(*) as n FROM @@DefinedTermBase@@ WHERE uuid = '" + uuidTerm
+ "'";
63 Long n
= (Long
)datasource
.getSingleValue(caseType
.replaceTableNames(sqlCheckTermExists
));
65 String name
= label
!= null ? label
: abbrev
!= null ? abbrev
: description
;
66 monitor
.warning("Term for representations update does not exist. Term not updated: " + CdmUtils
.Nz(name
) + "(" + uuidTerm
+ ")");
71 Integer langId
= null;
72 if (uuidLanguage
!= null){
73 langId
= getLanguageId(uuidLanguage
, datasource
, monitor
, caseType
);
75 String warning
= "Language for language uuid (%s) could not be found. Term representations not updated.";
76 warning
= String
.format(warning
, uuidLanguage
.toString());
77 monitor
.warning(warning
);
82 Integer repId
= getRepresentationId(datasource
, monitor
, langId
, caseType
);
87 //standard representation
88 String sqlUpdateRepresentationFormat
= " UPDATE @@Representation@@ r SET %s = '%s' WHERE r.id = %d ";
89 sqlUpdateRepresentationFormat
= caseType
.replaceTableNames(sqlUpdateRepresentationFormat
);
90 if (description
!= null){
91 String sqlUpdateRepresentation
= String
.format(sqlUpdateRepresentationFormat
, "text", description
, repId
);
92 datasource
.executeUpdate(sqlUpdateRepresentation
);
95 String sqlUpdateRepresentation
= String
.format(sqlUpdateRepresentationFormat
, "label", label
, repId
);
96 datasource
.executeUpdate(sqlUpdateRepresentation
);
99 String sqlUpdateRepresentation
= String
.format(sqlUpdateRepresentationFormat
, "abbreviatedLabel", abbrev
, repId
);
100 datasource
.executeUpdate(sqlUpdateRepresentation
);
112 * @throws SQLException
114 private Integer
getRepresentationId(ICdmDataSource datasource
,
115 IProgressMonitor monitor
, Integer langId
, CaseType caseType
) throws SQLException
{
118 String tableName
= isReverse ?
"RelationshipTermBase_inverseRepresentation" : "DefinedTermBase_Representation" ;
119 String repIdFkCol
= isReverse ?
"inverserepresentations_id" : "representations_id";
120 String sqlId
= " SELECT rep.id " +
121 " FROM @@Representation@@ rep INNER JOIN %s MN ON MN.%s = rep.id " +
122 " INNER JOIN @@DefinedTermBase@@ dtb ON MN.DefinedTermBase_id = dtb.id " +
123 " WHERE dtb.uuid = '%s' ";
124 tableName
= caseType
.transformTo(tableName
);
125 sqlId
= String
.format(sqlId
, tableName
, repIdFkCol
, uuidTerm
.toString());
126 sqlId
= caseType
.replaceTableNames(sqlId
);
127 if (uuidLanguage
!= null){
128 sqlId
+= " AND rep.language_id = " + langId
;
130 ResultSet rs
= datasource
.executeQuery(sqlId
);
133 repId
= rs
.getInt("id");
135 String warning
= "No representations do exist yet. Can't update term representation for term '%s'!";
136 warning
= String
.format(warning
, uuidTerm
.toString());
137 monitor
.warning(warning
);