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.v30_40;
|
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.logging.log4j.LogManager;import org.apache.logging.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.ISchemaUpdaterStep;
|
24
|
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
|
25
|
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
|
26
|
import eu.etaxonomy.cdm.model.common.Language;
|
27
|
import eu.etaxonomy.cdm.model.term.Representation;
|
28
|
import eu.etaxonomy.cdm.model.term.TermType;
|
29
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
30
|
import eu.etaxonomy.cdm.model.term.VocabularyEnum;
|
31
|
|
32
|
/**
|
33
|
* @author a.mueller
|
34
|
* @since 15.12.2013
|
35
|
*/
|
36
|
public class TermVocabularyRepresentationUpdater
|
37
|
extends SchemaUpdaterStepBase{
|
38
|
|
39
|
private static final Logger logger = LogManager.getLogger(TermVocabularyRepresentationUpdater.class);
|
40
|
|
41
|
private static final String stepName = "Update term vocabulary representations";
|
42
|
|
43
|
// **************************** STATIC METHODS ********************************/
|
44
|
|
45
|
public static final TermVocabularyRepresentationUpdater NewInstance(List<ISchemaUpdaterStep> stepList){
|
46
|
return new TermVocabularyRepresentationUpdater(stepList, stepName);
|
47
|
}
|
48
|
|
49
|
protected TermVocabularyRepresentationUpdater(List<ISchemaUpdaterStep> stepList, String stepName) {
|
50
|
super(stepList, stepName);
|
51
|
}
|
52
|
|
53
|
@Override
|
54
|
public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
|
55
|
CaseType caseType, SchemaUpdateResult result) throws SQLException {
|
56
|
|
57
|
try {
|
58
|
String sql = String.format(
|
59
|
" SELECT id " +
|
60
|
" FROM %s dtb " +
|
61
|
" WHERE dtb.uuid = '%s'",
|
62
|
caseType.transformTo("DefinedTermBase"),
|
63
|
Language.uuidEnglish);
|
64
|
String languageId = String.valueOf(datasource.getSingleValue(sql));
|
65
|
|
66
|
//for each vocabulary
|
67
|
for(VocabularyEnum vocabularyEnum : VocabularyEnum.values()) {
|
68
|
//read vocabulary from terms files
|
69
|
String filename = vocabularyEnum.name()+".csv";
|
70
|
CSVReader reader = new CSVReader(CdmUtils.getUtf8ResourceReader("terms/" + filename));
|
71
|
String [] nextLine = reader.readNext();
|
72
|
TermVocabulary<?> voc = TermVocabulary.NewInstance(TermType.Unknown);
|
73
|
voc.readCsvLine(arrayedLine(nextLine));
|
74
|
|
75
|
//get uuid, label and description for the vocabulary
|
76
|
UUID uuid = voc.getUuid();
|
77
|
Representation repEN = voc.getRepresentations().iterator().next();
|
78
|
String label = repEN.getLabel();
|
79
|
String description = repEN.getText();
|
80
|
|
81
|
//find representation in database
|
82
|
sql = caseType.replaceTableNames(
|
83
|
" SELECT rep.uuid " +
|
84
|
" FROM @@TermVocabulary@@ voc " +
|
85
|
" INNER JOIN @@TermVocabulary_Representation@@ MN ON MN.TermVocabulary_id = voc.id " +
|
86
|
" INNER JOIN @@Representation@@ rep ON rep.id = MN.representations_id " +
|
87
|
" WHERE voc.uuid = '%s' AND rep.language_id = %s");
|
88
|
sql = String.format(sql, uuid.toString(), languageId);
|
89
|
String repUuid = (String)datasource.getSingleValue(sql);
|
90
|
|
91
|
//update with correct label and representation
|
92
|
sql = " UPDATE %s SET label = '%s', text = '%s' WHERE uuid = '%s'";
|
93
|
sql = String.format(sql, caseType.transformTo("Representation"), label, description, repUuid);
|
94
|
|
95
|
//update vocabulary titleCache
|
96
|
sql = " UPDATE %s SET titleCache = '%s' WHERE uuid = '%s'";
|
97
|
sql = String.format(sql, caseType.transformTo("TermVocabulary"), label, uuid);
|
98
|
|
99
|
datasource.executeUpdate(sql);
|
100
|
}
|
101
|
|
102
|
return;
|
103
|
} catch (Exception e) {
|
104
|
String message = e.getMessage();
|
105
|
monitor.warning(message, e);
|
106
|
logger.warn(message);
|
107
|
result.addException(e, message, this, "invoke");
|
108
|
return;
|
109
|
}
|
110
|
}
|
111
|
|
112
|
private List<String> arrayedLine(String [] nextLine){
|
113
|
ArrayList<String> csvTermAttributeList = new ArrayList<String>(8);
|
114
|
for (String col : nextLine){
|
115
|
csvTermAttributeList.add(col);
|
116
|
}
|
117
|
while (csvTermAttributeList.size()<8){
|
118
|
csvTermAttributeList.add("");
|
119
|
}
|
120
|
return csvTermAttributeList;
|
121
|
}
|
122
|
|
123
|
|
124
|
}
|