Project

General

Profile

Download (8.21 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 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

    
11
package eu.etaxonomy.cdm.database.update.v34_35;
12

    
13
import java.util.ArrayList;
14
import java.util.List;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.database.update.ClassBaseTypeUpdater;
19
import eu.etaxonomy.cdm.database.update.ColumnAdder;
20
import eu.etaxonomy.cdm.database.update.ISchemaUpdater;
21
import eu.etaxonomy.cdm.database.update.ISchemaUpdaterStep;
22
import eu.etaxonomy.cdm.database.update.SchemaUpdaterBase;
23
import eu.etaxonomy.cdm.database.update.SimpleSchemaUpdaterStep;
24
import eu.etaxonomy.cdm.database.update.TableCreator;
25
import eu.etaxonomy.cdm.database.update.TableDroper;
26
import eu.etaxonomy.cdm.database.update.v33_34.SchemaUpdater_34_341;
27
import eu.etaxonomy.cdm.database.update.v35_36.SchemaUpdater_35_36;
28
import eu.etaxonomy.cdm.model.location.Country;
29
import eu.etaxonomy.cdm.model.location.NamedArea;
30

    
31
/**
32
 * @author a.mueller
33
 * @created Mar 01, 2015
34
 */
35
public class SchemaUpdater_341_35 extends SchemaUpdaterBase {
36

    
37
	@SuppressWarnings("unused")
38
	private static final Logger logger = Logger.getLogger(SchemaUpdater_341_35.class);
39
	private static final String startSchemaVersion = "3.4.1.0.201411210000";
40
	private static final String endSchemaVersion = "3.5.0.0.201531030000";
41

    
42
	// ********************** FACTORY METHOD *************************************
43

    
44
	public static SchemaUpdater_341_35 NewInstance() {
45
		return new SchemaUpdater_341_35();
46
	}
47

    
48
	/**
49
	 * @param startSchemaVersion
50
	 * @param endSchemaVersion
51
	 */
52
	protected SchemaUpdater_341_35() {
53
		super(startSchemaVersion, endSchemaVersion);
54
	}
55

    
56
	@Override
57
	protected List<ISchemaUpdaterStep> getUpdaterList() {
58

    
59
		String stepName;
60
		String tableName;
61
		ISchemaUpdaterStep step;
62
		String newColumnName;
63
		String query;
64
		String columnNames[];
65
		String referencedTables[];
66
		String columnTypes[];
67

    
68
		List<ISchemaUpdaterStep> stepList = new ArrayList<ISchemaUpdaterStep>();
69

    
70

    
71
		//IntextReference
72
		//#4706
73
		stepName = "Add IntextReference table";
74
		tableName = "IntextReference";
75
		columnNames = new String[]{"startpos","endpos","agent_id","annotation_id",
76
				"languagestring_id","media_id","occurrence_id","reference_id","taxon_id","taxonname_id"};
77
		referencedTables = new String[]{null, null, "AgentBase","Annotation","LanguageString","Media",
78
				"SpecimenOrObservationBase","Reference","TaxonBase","TaxonNameBase"};
79
		columnTypes = new String[]{"int","int","int","int","int","int","int","int","int","int"};
80
		step = TableCreator.NewVersionableInstance(stepName, tableName, columnNames,
81
				columnTypes, referencedTables, INCLUDE_AUDIT);
82
		stepList.add(step);
83

    
84
		//Drop EntityValidationResult and EntityConstraintViolation
85
		//#4709
86
		stepName = "Drop EntityConstraintViolation table";
87
		tableName = "EntityConstraintViolation";
88
		step = TableDroper.NewInstance(stepName, tableName, !INCLUDE_AUDIT);
89
		stepList.add(step);
90

    
91
		stepName = "Drop EntityValidationResult table";
92
		tableName = "EntityValidationResult";
93
		step = TableDroper.NewInstance(stepName, tableName, !INCLUDE_AUDIT);
94
		stepList.add(step);
95

    
96
        //... and create new entity validation and
97
        stepName = "Create EntityValidation table";
98
        tableName = "EntityValidation";
99
        columnNames = new String[]{"updated","crudeventtype","userfriendlydescription","userfriendlytypename",
100
                "validatedentityclass","validatedentityid","validatedentityuuid", "validationcount", "status"};
101
        columnTypes = new String[]{"datetime","string_255","string_255","string_255","string_255","int","string_36","int","string_20"};
102
        referencedTables = new String[]{null,null,null,null,null,null,null,null,null};
103
        step = TableCreator.NewNonVersionableInstance(stepName, tableName, columnNames, columnTypes, referencedTables);
104
        stepList.add(step);
105

    
106
        //... constraint violation
107
		stepName = "Create EntityConstraintViolation table";
108
        tableName = "EntityConstraintViolation";
109
        columnNames = new String[]{"invalidvalue","message","propertypath","severity","userfriendlyfieldname",
110
        		"validationgroup","validator","entityvalidation_id"};
111
        columnTypes = new String[]{"string_255","string_255","string_255","string_255","string_255","string_255","string_255","int"};
112
        referencedTables = new String[]{null,null,null,null,null,null,null,"EntityValidationResult"};
113
        step = TableCreator.NewNonVersionableInstance(stepName, tableName, columnNames, columnTypes, referencedTables);
114
        stepList.add(step);
115

    
116
        //Delete orphaned taxon nodes #2341
117
        stepName = "Delete orhphaned taxon nodes";
118
        String sql = "DELETE FROM @@TaxonNode@@ WHERE classification_id IS NULL";
119
        tableName = "TaxonNode";
120
        step = SimpleSchemaUpdaterStep.NewAuditedInstance(stepName, sql, tableName, 0);
121
        stepList.add(step);
122

    
123
        //identifier versionable -> annotatable
124
        stepName = "Upgrade identifier from versionable to annotatable";
125
        tableName = "Identifier";
126
        step = ClassBaseTypeUpdater.NewVersionableToAnnotatableInstance(stepName, tableName, INCLUDE_AUDIT);
127
        stepList.add(step);
128

    
129
        //agent - collector title  #4311
130
        stepName = "Add collector title for TeamOrPersonBase";
131
        tableName = "AgentBase";
132
        newColumnName = "collectorTitle";
133
        step = ColumnAdder.NewStringInstance(stepName, tableName, newColumnName, INCLUDE_AUDIT);
134
        stepList.add(step);
135

    
136
        //agent - collector title  #4311
137
        stepName = "Add protectedCollectorTitleCache to Team";
138
        tableName = "AgentBase";
139
        newColumnName = "protectedCollectorTitleCache";
140
        step = ColumnAdder.NewBooleanInstance(stepName, tableName, newColumnName, INCLUDE_AUDIT, false);
141
        stepList.add(step);
142

    
143

    
144
		//update DerivationEvent.taxonname_id  #4578, #3448, #4203, #4518
145
		stepName = "Update taxon name in derivation event";
146
		query = "UPDATE DeterminationEvent " +
147
				" SET taxonname_id = (SELECT name_id FROM TaxonBase tb WHERE tb.id = taxon_id) " +
148
				" WHERE taxon_id IS NOT NULL ";
149
		tableName = "DeterminationEvent";
150
		step = SimpleSchemaUpdaterStep.NewAuditedInstance(stepName, query, "", -99);
151
		stepList.add(step);
152

    
153

    
154
        //#4110 update idInVocabulary for some new databases
155
        updateAreas(stepList);
156

    
157
		return stepList;
158

    
159
	}
160

    
161
	//#4110 update idInVocabulary for some new databases
162
    private void updateAreas(List<ISchemaUpdaterStep> stepList) {
163
		String stepName;
164
		String uuid;
165
		ISchemaUpdaterStep step;
166
		String tableName = "DefinedTermBase";
167

    
168
		//ANSI - SQL
169
		String queryVocUuid = " UPDATE @@DefinedTermBase@@ "
170
				+ " SET idInVocabulary = " +
171
					" (SELECT abbreviatedlabel "
172
					+ " FROM @@DefinedTermBase_Representation@@ MN "
173
					+ " INNER JOIN @@Representation@@ r ON r.id = MN.representations_id "
174
					+ " WHERE MN.DefinedTermBase_id = @@DefinedTermBase@@.id) "
175
				+ " WHERE idInVocabulary IS NULL AND EXISTS (SELECT * FROM @@TermVocabulary@@ voc WHERE voc.id = @@DefinedTermBase@@.vocabulary_id " +
176
						" AND voc.uuid = '%s') ";
177

    
178
	    // Country => all
179
		stepName = "Update idInVocabulary for Countries if necessary";
180
		uuid = Country.uuidCountryVocabulary.toString();
181
		step = SimpleSchemaUpdaterStep.NewNonAuditedInstance(stepName,
182
				String.format(queryVocUuid, uuid), 99)
183
				.setDefaultAuditing(tableName);
184
		stepList.add(step);
185

    
186
		// TdwgAreas => all
187
		stepName = "Update idInVocabulary for TDWG areas if necessary";
188
		uuid = NamedArea.uuidTdwgAreaVocabulary.toString();
189
		step = SimpleSchemaUpdaterStep.NewNonAuditedInstance(stepName,
190
				String.format(queryVocUuid, uuid), 99)
191
				.setDefaultAuditing(tableName);
192
		stepList.add(step);
193

    
194
		// Waterbody => all
195
		stepName = "Update idInVocabulary for Waterbody if necessary";
196
		uuid = NamedArea.uuidWaterbodyVocabulary.toString();
197
		step = SimpleSchemaUpdaterStep.NewNonAuditedInstance(stepName,
198
				String.format(queryVocUuid, uuid), 99)
199
				.setDefaultAuditing(tableName);
200
		stepList.add(step);
201

    
202
		// Continent => None has an id
203

    
204

    
205

    
206
	}
207

    
208
	@Override
209
	public ISchemaUpdater getNextUpdater() {
210
		return SchemaUpdater_35_36.NewInstance();
211
	}
212

    
213
	@Override
214
	public ISchemaUpdater getPreviousUpdater() {
215
		return SchemaUpdater_34_341.NewInstance();
216
	}
217

    
218
}
(1-1/2)