Project

General

Profile

Download (2.63 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
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.
9
*/
10
package eu.etaxonomy.cdm.database.update;
11

    
12
import java.sql.SQLException;
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.UUID;
16

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
20
import eu.etaxonomy.cdm.database.ICdmDataSource;
21

    
22
/**
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
27
 * 
28
 * @author a.mueller
29
 * @date 06.09.2013
30
 *
31
 */
32
public class TermMover extends SchemaUpdaterStepBase<TermMover> implements ITermUpdaterStep{
33
	private static final Logger logger = Logger.getLogger(TermMover.class);
34
	
35
	public static final TermMover NewInstance(String stepName, UUID newVocabulary, String uuidTerm){
36
		List<String> terms = new ArrayList<String>();
37
		terms.add(uuidTerm);
38
		return new TermMover(stepName, newVocabulary, terms);	
39
	}
40
	
41

    
42
	public static final TermMover NewInstance(String stepName, UUID newVocabulary, List<String> terms){
43
		return new TermMover(stepName, newVocabulary, terms);	
44
	}
45
	
46
	
47
	private String uuidNewVocabulary ;
48
	private List<String> termUuids = new ArrayList<String>();
49
	
50

    
51
	private TermMover(String stepName, UUID newVocabulary, List<String> terms) {
52
		super(stepName);
53
		this.uuidNewVocabulary = newVocabulary.toString();
54
		this.termUuids = terms; 
55
	}
56

    
57
	@Override
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);
67
			return null;
68
		}
69
		
70
		//check if in use
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);
75
		}
76
		
77
		return 0;
78
	}
79

    
80
	public TermMover addTermUuid(UUID uuid){
81
		this.termUuids.add(uuid.toString());
82
		return this;
83
	}
84
	
85

    
86

    
87
}
(28-28/34)