2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.database
.update
;
11 import java
.sql
.SQLException
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
18 import eu
.etaxonomy
.cdm
.common
.monitor
.IProgressMonitor
;
19 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
22 * Moves terms from one vocabulary to another.
23 * TODO does not yet check all DefinedTermBase_XXX tables except for representations.
24 * TODO Does also not handle AUD tables
25 * TODO Does not handle orderindex
31 public class TermMover
extends SchemaUpdaterStepBase
{
33 private static final Logger logger
= Logger
.getLogger(TermMover
.class);
35 public static final TermMover
NewInstance(List
<ISchemaUpdaterStep
> stepList
, String stepName
, UUID newVocabulary
, String uuidTerm
){
36 List
<String
> terms
= new ArrayList
<>();
38 return new TermMover(stepList
, stepName
, newVocabulary
, terms
);
42 public static final TermMover
NewInstance(List
<ISchemaUpdaterStep
> stepList
, String stepName
, UUID newVocabulary
, List
<String
> terms
){
43 return new TermMover(stepList
, stepName
, newVocabulary
, terms
);
47 private String uuidNewVocabulary
;
48 private List
<String
> termUuids
= new ArrayList
<>();
51 private TermMover(List
<ISchemaUpdaterStep
> stepList
, String stepName
, UUID newVocabulary
, List
<String
> terms
) {
52 super(stepList
, stepName
);
53 this.uuidNewVocabulary
= newVocabulary
.toString();
54 this.termUuids
= terms
;
58 public void invoke(ICdmDataSource datasource
, IProgressMonitor monitor
,
59 CaseType caseType
, SchemaUpdateResult result
) throws SQLException
{
60 //get new vocabulary id
61 String sql
= " SELECT id FROM %s WHERE uuid = '%s'";
62 Integer id
= (Integer
)datasource
.getSingleValue(String
.format(sql
,
63 caseType
.transformTo("TermVocabulary") , this.uuidNewVocabulary
));
64 if (id
== null || id
== 0){
65 String message
= "New vocabulary ("+uuidNewVocabulary
+") does not exist. Can't move terms";
66 monitor
.warning(message
);
68 result
.addError(message
, this, "invoke");
73 for (String uuid
: this.termUuids
){
74 sql
= " UPDATE %s SET vocabulary_id = %d WHERE uuid = '%s' ";
75 sql
= String
.format(sql
, caseType
.transformTo("DefinedTermBase"), id
, uuid
);
76 datasource
.executeUpdate(sql
);
82 public TermMover
addTermUuid(UUID uuid
){
83 this.termUuids
.add(uuid
.toString());