Project

General

Profile

Download (9.86 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.v47_50;
10

    
11
import java.sql.ResultSet;
12
import java.sql.SQLException;
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
import eu.etaxonomy.cdm.database.update.CaseType;
21
import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
22
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
23
import eu.etaxonomy.cdm.database.update.SchemaUpdaterStepBase;
24
import eu.etaxonomy.cdm.database.update.SingleTermUpdater;
25
import eu.etaxonomy.cdm.model.common.Language;
26
import eu.etaxonomy.cdm.model.common.TermType;
27
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
28

    
29

    
30
/**
31
 * Updates the pro parte and partial synonyms. Makes them real concept relationships
32
 * #7334
33
 *
34
 * @author a.mueller
35
 * @since 11.05.2018
36
 */
37
public class ProParteSynonymUpdater extends SchemaUpdaterStepBase {
38
    @SuppressWarnings("unused")
39
    private static final Logger logger = Logger.getLogger(ProParteSynonymUpdater.class);
40

    
41
	private static final String stepName = "Make pro parte synonyms concept relationships";
42

    
43

    
44
// **************************** STATIC METHODS ********************************/
45

    
46
	public static final ProParteSynonymUpdater NewInstance(List<ISchemaUpdaterStep> stepList){
47
		ProParteSynonymUpdater result = new ProParteSynonymUpdater();
48
		stepList.add(result);
49

    
50
		return result;
51
	}
52

    
53
	private ProParteSynonymUpdater() {
54
		super(stepName);
55
	}
56

    
57
    @Override
58
    public void invoke(ICdmDataSource datasource, IProgressMonitor monitor,
59
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
60

    
61
        int proParteId = createProParteRelType(datasource, monitor, caseType, result);
62
        int partialId = createPartialRelType(datasource, monitor, caseType, result);
63

    
64
        try{
65
            invokeSingle(false, proParteId, datasource, monitor, caseType, result);
66
            invokeSingle(true, partialId, datasource, monitor, caseType, result);
67

    
68
        } catch (Exception e) {
69
            String message = e.getMessage();
70
            monitor.warning(message, e);
71
            result.addException(e, message, this, "invoke");
72
        }
73

    
74
	    return;
75
	}
76

    
77
    private int createProParteRelType(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
78
            SchemaUpdateResult result) throws SQLException {
79

    
80
        UUID uuidTerm = TaxonRelationshipType.uuidProParteSynonymFor;
81
        String symbol = "p.p.";
82
        String description = "Pro parte synonym for";
83
        UUID uuidAfterTerm = UUID.fromString("605b1d01-f2b1-4544-b2e0-6f08def3d6ed");
84
        String reverseDescription = "Has pro parte synonym";
85
        String reverseAbbrev = "⊃p.p.";
86

    
87
        return createRelType(datasource, monitor, caseType, result,
88
                uuidTerm, symbol, description, uuidAfterTerm,
89
                reverseDescription, reverseAbbrev);
90
    }
91

    
92
    private int createPartialRelType(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
93
            SchemaUpdateResult result) throws SQLException {
94

    
95
        UUID uuidTerm = TaxonRelationshipType.uuidPartialSynonymFor;
96
        String symbol = "partial";
97
        String description = "Partial synonym for";
98
        UUID uuidAfterTerm = UUID.fromString("8a896603-0fa3-44c6-9cd7-df2d8792e577");
99
        String reverseDescription = "Has partial synonym";
100
        String reverseAbbrev = "⊃partim";
101

    
102
        return createRelType(datasource, monitor, caseType, result,
103
                uuidTerm, symbol, description, uuidAfterTerm,
104
                reverseDescription, reverseAbbrev);
105

    
106
    }
107

    
108
    private int createRelType(ICdmDataSource datasource, IProgressMonitor monitor, CaseType caseType,
109
            SchemaUpdateResult result, UUID uuidTerm, String symbol,
110
            String description, UUID uuidAfterTerm,
111
            String reverseDescription, String reverseAbbrev) throws SQLException {
112

    
113
        String inverseSymbol = "⊃" + symbol;
114
        String query = "SELECT count(*) n FROM @@DefinedTermBase@@ dtb WHERE dtb.uuid = '%s'";
115
        query = String.format(query, uuidTerm);
116
        query = caseType.replaceTableNames(query);
117
        Long n = (Long)datasource.getSingleValue(query);
118
        if (n == 0){
119

    
120
            //create type term
121
            String idInVocabulary = symbol;
122
            String label = description;
123
            String abbrev = idInVocabulary;
124
            String reverseLabel = reverseDescription;
125
            String dtype = "TaxonRelationshipType";
126
            UUID uuidVocabulary = UUID.fromString("15db0cf7-7afc-4a86-a7d4-221c73b0c9ac");
127
            UUID uuidLanguage = Language.uuidEnglish;
128
            boolean isOrdered = true;
129
            ISchemaUpdaterStep step = SingleTermUpdater.NewReverseInstance(stepName, TermType.TaxonRelationshipType,
130
                    uuidTerm, idInVocabulary, symbol,
131
                    description, label, abbrev, reverseDescription, reverseLabel, reverseAbbrev,
132
                    dtype, uuidVocabulary, uuidLanguage, isOrdered, uuidAfterTerm);
133
            step.invoke(datasource, monitor, caseType, result);
134

    
135
//            //reverse  representation
136
//            step = TermRepresentationAdder.NewReverseInstance(dtype, uuidTerm, reverseDescription, reverseLabel, reverseAbbrev, uuidLanguage);
137
//            step.invoke(datasource, monitor, caseType, result);
138
        }
139

    
140
        //update some fields
141
        query = " UPDATE @@DefinedTermBase@@ dtb " +
142
                " SET symbol = '%s', inverseSymbol = '%s', transitive = @FALSE@ " +
143
                " WHERE uuid = '%s'";
144
        query = String.format(query, symbol, inverseSymbol, uuidTerm.toString());
145
        query = doReplacements(query, caseType, datasource);
146
        datasource.executeUpdate(query);
147

    
148
        //select id
149
        query = "SELECT id n FROM @@DefinedTermBase@@ dtb WHERE dtb.uuid = '%s'";
150
        query = String.format(query, uuidTerm);
151
        query = doReplacements(query, caseType, datasource);
152
        Integer id = (Integer)datasource.getSingleValue(query);
153

    
154
        return id;
155
    }
156

    
157

    
158
    private void invokeSingle(boolean isPartial, int typeId, ICdmDataSource datasource, IProgressMonitor monitor,
159
            CaseType caseType, SchemaUpdateResult result) throws SQLException {
160

    
161
        try {
162
            //get maxId from taxonRelationship
163
            Integer maxId = 0;
164
            String query = "SELECT max(id) id FROM @@TaxonRelationship@@";
165
            query = doReplacements(query, caseType, datasource);
166
            ResultSet rs = datasource.executeQuery(query);
167
            if (rs.next()){
168
                maxId = rs.getInt("id");
169
            }
170

    
171
            //SELECT all record IDs
172
            String attributeName = isPartial? "partial" : "proParte";
173
            query = " SELECT tb.id synId, titleCache " +
174
                    " FROM @@TaxonBase@@ tb  " +
175
                    " WHERE tb.%s = @TRUE@ ";
176
            query = String.format(query, attributeName);
177
            query = doReplacements(query, caseType, datasource);
178
            rs = datasource.executeQuery(query);
179

    
180
            while (rs.next()){
181
                maxId++;
182
                Integer synId = rs.getInt("synId");
183
                //insert record in TaxonRelationship
184
                String insert = "INSERT INTO @@TaxonRelationship@@ (id, uuid, relatedFrom_id, relatedTo_id, "
185
                        +       " doubtful, citation_id, citationMicroReference, type_id, "
186
                        +       " created, updated, createdBy_id, updatedBy_id )"
187
                        + " SELECT %d, '%s', syn.id, acceptedTaxon_id,  "
188
                        +       " syn.doubtful, syn.sec_id secId, syn.secMicroReference, %d, "
189
                        +       " syn.created, syn.updated, syn.createdBy_id, syn.updatedBy_id "
190
                        + " FROM TaxonBase syn "
191
                        + " WHERE syn.id = %d ";
192
                insert = String.format(insert, maxId, UUID.randomUUID(), typeId, synId );
193
                insert = doReplacements(insert, caseType, datasource);
194
                datasource.executeUpdate(insert);
195

    
196
                //update Synonym record
197
                String titleCache = rs.getString("titleCache");
198
                titleCache = normalizeTitleCache(titleCache);
199
                String update = "UPDATE @@TaxonBase@@ "
200
                        + " SET DTYPE = 'Taxon', sec_id = null, secMicroReference = null,"
201
                        + "     %s = null, titleCache = '%s', taxonStatusUnknown = @FALSE@,"
202
                        + "     doubtful = @FALSE@, acceptedTaxon_id = null, type_id = null "
203
                        + " WHERE id = %d ";
204
                update = String.format(update, attributeName, titleCache, synId);
205
                update = doReplacements(update, caseType, datasource);
206
                datasource.executeUpdate(update);
207
            }
208
        } catch (Exception e) {
209
            result.addException(e, "Error occurred when trying to update proParte/partial synonyms",
210
                    this, "invokeSingle");
211
        }
212

    
213
    }
214

    
215

    
216
    /**
217
     * @param titleCache
218
     * @return
219
     */
220
    private String normalizeTitleCache(String titleCache) {
221
        if(titleCache == null){
222
            return "";
223
        }
224
        int index = titleCache.indexOf("syn. sec.");
225
        if (index < 0){
226
            return titleCache;
227
        }else{
228
            titleCache = titleCache.substring(0, index) + "sec. ???";
229
            return titleCache;
230
        }
231
    }
232

    
233
    private String doReplacements(String query, CaseType caseType, ICdmDataSource datasource) {
234
        query = caseType.replaceTableNames(query);
235
        query = query.replaceAll("@FALSE@", getBoolean(false, datasource));
236
        query = query.replaceAll("@TRUE@", getBoolean(true, datasource));
237
        return query;
238
    }
239

    
240
}
(1-1/3)