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
.SQLException
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
20 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
23 * Moves terms from one vocabulary to another.
24 * TODO does not yet check all DefinedTermBase_XXX tables except for representations.
25 * TODO Does also not handle AUD tables
26 * TODO Does not handle orderindex
32 public class TermMover
extends SchemaUpdaterStepBase
<TermMover
> implements ITermUpdaterStep
{
33 private static final Logger logger
= Logger
.getLogger(TermMover
.class);
35 public static final TermMover
NewInstance(String stepName
, UUID newVocabulary
, String uuidTerm
){
36 List
<String
> terms
= new ArrayList
<String
>();
38 return new TermMover(stepName
, newVocabulary
, terms
);
42 public static final TermMover
NewInstance(String stepName
, UUID newVocabulary
, List
<String
> terms
){
43 return new TermMover(stepName
, newVocabulary
, terms
);
47 private String uuidNewVocabulary
;
48 private List
<String
> termUuids
= new ArrayList
<String
>();
51 private TermMover(String stepName
, UUID newVocabulary
, List
<String
> terms
) {
53 this.uuidNewVocabulary
= newVocabulary
.toString();
54 this.termUuids
= terms
;
58 public Integer
invoke(ICdmDataSource datasource
, IProgressMonitor monitor
, CaseType caseType
) throws SQLException
{
59 //get new vocabulary id
60 String sql
= " SELECT id FROM %s WHERE uuid = '%s'";
61 Integer id
= (Integer
)datasource
.getSingleValue(String
.format(sql
,
62 caseType
.transformTo("TermVocabulary") , this.uuidNewVocabulary
));
63 if (id
== null || id
== 0){
64 String messageString
= "New vocabulary ("+uuidNewVocabulary
+") does not exist. Can't move terms";
65 monitor
.warning(messageString
);
66 logger
.warn(messageString
);
71 for (String uuid
: this.termUuids
){
72 sql
= " UPDATE %s SET vocabulary_id = %d WHERE uuid = '%s' ";
73 sql
= String
.format(sql
, caseType
.transformTo("DefinedTermBase"), id
, uuid
);
74 datasource
.executeUpdate(sql
);
80 public TermMover
addTermUuid(UUID uuid
){
81 this.termUuids
.add(uuid
.toString());