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
) throws SQLException
{
61 String sqlCheckTermExists
= " SELECT count(*) as n FROM DefinedTermBase WHERE uuid = '" + uuidTerm
+ "'";
62 Long n
= (Long
)datasource
.getSingleValue(sqlCheckTermExists
);
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
+ ")");
70 Integer langId
= null;
71 if (uuidLanguage
!= null){
72 langId
= getLanguageId(uuidLanguage
, datasource
, monitor
);
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
);
81 Integer repId
= getRepresentationId(datasource
, monitor
, langId
);
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
);
93 String sqlUpdateRepresentation
= String
.format(sqlUpdateRepresentationFormat
, "label", label
, repId
);
94 datasource
.executeUpdate(sqlUpdateRepresentation
);
97 String sqlUpdateRepresentation
= String
.format(sqlUpdateRepresentationFormat
, "abbreviatedLabel", abbrev
, repId
);
98 datasource
.executeUpdate(sqlUpdateRepresentation
);
109 * @throws SQLException
111 private Integer
getRepresentationId(ICdmDataSource datasource
,
112 IProgressMonitor monitor
, Integer langId
) throws SQLException
{
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
;
125 ResultSet rs
= datasource
.executeQuery(sqlId
);
128 repId
= rs
.getInt("id");
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
);