Project

General

Profile

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

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

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

    
15
import org.apache.log4j.Logger;
16

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

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

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

    
41
	// ********************** FACTORY METHOD *************************************
42

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

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

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

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

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

    
69

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

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

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

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

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

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

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

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

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

    
142

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

    
152

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

    
156
		return stepList;
157

    
158
	}
159

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

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

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

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

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

    
201
		// Continent => None has an id
202

    
203

    
204

    
205
	}
206

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

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

    
217
}
    (1-1/1)