add SchemaUpdater for 5.23.0 and fix #9536 (remove old columns and tables)
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / TermMover.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.database.update;
10
11 import java.sql.SQLException;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
19 import eu.etaxonomy.cdm.database.ICdmDataSource;
20
21 /**
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
26 *
27 * @author a.mueller
28 * @since 06.09.2013
29 *
30 */
31 public class TermMover extends SchemaUpdaterStepBase{
32
33 private static final Logger logger = Logger.getLogger(TermMover.class);
34
35 public static final TermMover NewInstance(List<ISchemaUpdaterStep> stepList, String stepName, UUID newVocabulary, String uuidTerm){
36 List<String> terms = new ArrayList<>();
37 terms.add(uuidTerm);
38 return new TermMover(stepList, stepName, newVocabulary, terms);
39 }
40
41
42 public static final TermMover NewInstance(List<ISchemaUpdaterStep> stepList, String stepName, UUID newVocabulary, List<String> terms){
43 return new TermMover(stepList, stepName, newVocabulary, terms);
44 }
45
46
47 private String uuidNewVocabulary ;
48 private List<String> termUuids = new ArrayList<>();
49
50
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;
55 }
56
57 @Override
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);
67 logger.warn(message);
68 result.addError(message, this, "invoke");
69 return;
70 }
71
72 //check if in use
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);
77 }
78
79 return;
80 }
81
82 public TermMover addTermUuid(UUID uuid){
83 this.termUuids.add(uuid.toString());
84 return this;
85 }
86
87
88
89 }