Project

General

Profile

Download (4.35 KB) Statistics
| Branch: | Tag: | Revision:
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.v31_33;
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 au.com.bytecode.opencsv.CSVReader;
19
import eu.etaxonomy.cdm.common.CdmUtils;
20
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
21
import eu.etaxonomy.cdm.database.ICdmDataSource;
22
import eu.etaxonomy.cdm.database.update.CaseType;
23
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
24
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
25
import eu.etaxonomy.cdm.model.common.Language;
26
import eu.etaxonomy.cdm.model.common.Representation;
27
import eu.etaxonomy.cdm.model.common.TermType;
28
import eu.etaxonomy.cdm.model.common.TermVocabulary;
29
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
30

    
31
/**
32
 * @author a.mueller
33
 \* @since 15.12.2013
34
 */
35
public class TermVocabularyRepresentationUpdater
36
            extends SchemaUpdaterStepBase{
37

    
38
    private static final Logger logger = Logger.getLogger(TermVocabularyRepresentationUpdater.class);
39

    
40
    private static final String stepName = "Update term vocabulary representations";
41

    
42
// **************************** STATIC METHODS ********************************/
43

    
44
	public static final TermVocabularyRepresentationUpdater NewInstance(){
45
		return new TermVocabularyRepresentationUpdater(stepName);
46
	}
47

    
48
	protected TermVocabularyRepresentationUpdater(String stepName) {
49
		super(stepName);
50
	}
51

    
52
	@Override
53
	public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
54
	        CaseType caseType, SchemaUpdateResult result) throws SQLException {
55

    
56
		try {
57
			String sql = String.format(
58
					" SELECT id " +
59
					" FROM %s dtb " +
60
					" WHERE dtb.uuid = '%s'",
61
					 caseType.transformTo("DefinedTermBase"),
62
					 Language.uuidEnglish);
63
			String languageId = String.valueOf(datasource.getSingleValue(sql));
64

    
65
			//for each vocabulary
66
			for(VocabularyEnum vocabularyEnum : VocabularyEnum.values()) {
67
				//read vocabulary from terms files
68
				String filename = vocabularyEnum.name()+".csv";
69
				CSVReader reader = new CSVReader(CdmUtils.getUtf8ResourceReader("terms" + CdmUtils.getFolderSeperator() + filename));
70
				String [] nextLine = reader.readNext();
71
				TermVocabulary<?> voc = TermVocabulary.NewInstance(TermType.Unknown);
72
				voc.readCsvLine(arrayedLine(nextLine));
73

    
74
				//get uuid, label and description for the vocabulary
75
				UUID uuid = voc.getUuid();
76
				Representation repEN = voc.getRepresentations().iterator().next();
77
				String label = repEN.getLabel();
78
				String description = repEN.getText();
79

    
80
				//find representation in database
81
				sql = caseType.replaceTableNames(
82
						" SELECT rep.uuid " +
83
						" FROM @@TermVocabulary@@ voc " +
84
							" INNER JOIN @@TermVocabulary_Representation@@ MN ON MN.TermVocabulary_id = voc.id " +
85
							" INNER JOIN @@Representation@@ rep ON rep.id = MN.representations_id " +
86
						" WHERE voc.uuid = '%s' AND rep.language_id = %s");
87
				sql = String.format(sql, uuid.toString(), languageId);
88
				String repUuid = (String)datasource.getSingleValue(sql);
89

    
90
				//update with correct label and representation
91
				sql = " UPDATE %s SET label = '%s', text = '%s' WHERE uuid = '%s'";
92
				sql = String.format(sql, caseType.transformTo("Representation"), label, description, repUuid);
93

    
94
				//update vocabulary titleCache
95
				sql = " UPDATE %s SET titleCache = '%s' WHERE uuid = '%s'";
96
				sql = String.format(sql, caseType.transformTo("TermVocabulary"), label, uuid);
97

    
98
				datasource.executeUpdate(sql);
99
			}
100

    
101
			return;
102
		} catch (Exception e) {
103
		    String message = e.getMessage();
104
            monitor.warning(message, e);
105
            logger.warn(message);
106
            result.addException(e, message, this, "invoke");
107
            return;
108
		}
109
	}
110

    
111
	private List<String> arrayedLine(String [] nextLine){
112
		ArrayList<String> csvTermAttributeList = new ArrayList<String>(8);
113
		for (String col : nextLine){
114
			csvTermAttributeList.add(col);
115
		}
116
		while (csvTermAttributeList.size()<8){
117
			csvTermAttributeList.add("");
118
		}
119
		return csvTermAttributeList;
120
	}
121

    
122

    
123
}
(6-6/6)