Project

General

Profile

Download (8.44 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.test.function;
10

    
11
import java.util.UUID;
12

    
13
import org.apache.log4j.Logger;
14
import org.springframework.transaction.TransactionStatus;
15

    
16
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
17
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
18
import eu.etaxonomy.cdm.config.AccountStore;
19
import eu.etaxonomy.cdm.database.CdmDataSource;
20
import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
21
import eu.etaxonomy.cdm.database.DbSchemaValidation;
22
import eu.etaxonomy.cdm.database.ICdmDataSource;
23
import eu.etaxonomy.cdm.database.update.CdmUpdater;
24
import eu.etaxonomy.cdm.database.update.SchemaUpdateResult;
25
import eu.etaxonomy.cdm.model.description.TemporalData;
26

    
27
/**
28
 * This class is meant for functional testing of model changes. It is not meant
29
 * for running in maven.
30
 *
31
 * For testing
32
 *
33
 * 1. First run with CREATE first against H2, than MySQL, PostGreSQL, (SQLServer)
34
 * 2. Save old schema databases
35
 * 3. Run with VALIDATE
36
 *
37
 * @author a.mueller
38
 * @since 22.05.2015
39
 * @see CdmUpdater
40
 */
41
public class TestModelUpdate {
42

    
43
    @SuppressWarnings("unused")
44
	private static final Logger logger = Logger.getLogger(TestModelUpdate.class);
45

    
46
	private void testSelectedDb(){
47
		DbSchemaValidation schema = DbSchemaValidation.VALIDATE;
48

    
49
		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
50
		String database = (schema == DbSchemaValidation.VALIDATE  ? "cdm515" : "cdm518");
51
//		database = "cdm_test1";
52

    
53
		CdmDataSource dataSource = getDatasource(dbType, database);
54
 		try {
55
// 		    int n = dataSource.executeUpdate("UPDATE CdmMetaData SET value = '3.1.0.0.201607300000' WHERE propertyname = 0 ");
56
			CdmUpdater updater = new CdmUpdater();
57
			if (schema == DbSchemaValidation.VALIDATE){
58
				SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource,
59
				        DefaultProgressMonitor.NewInstance());
60
				String report = result.createReport().toString();
61
				System.out.println(report);
62
			}
63
		} catch (Exception e) {
64
			e.printStackTrace();
65
		}
66
 		try{
67
    		CdmApplicationController appCtr = CdmApplicationController.NewInstance(dataSource, schema);
68

    
69
    //		Classification classification = Classification.NewInstance("Me");
70
    //		Taxon taxon = Taxon.NewInstance(null, null);
71
    //		Person person = Person.NewInstance();
72
    //		TaxonNode node = classification.addChildTaxon(taxon, null, null);
73
    //		DefinedTerm lastScrutiny = (DefinedTerm)appCtr.getTermService().find(DefinedTerm.uuidLastScrutiny);
74
    //		TaxonNodeAgentRelation rel = node.addAgentRelation(lastScrutiny, person);
75
    //      appCtr.getClassificationService().save(classification);
76

    
77
    		if (schema == DbSchemaValidation.CREATE){
78
    		    System.out.println("fillData");
79
    		    appCtr.getCommonService().createFullSampleData();
80
    		    appCtr.getNameService().list(null, null, null, null, null);
81
    		    TransactionStatus tx = appCtr.startTransaction(false);
82
    		    TemporalData td = (TemporalData)appCtr.getDescriptionService().getDescriptionElementByUuid(
83
    		            UUID.fromString("9a1c91c0-fc58-4310-94cb-8c26115985d3"));
84
    		    td.getFeature().setSupportsCategoricalData(true);
85
    		    appCtr.getTermService().saveOrUpdate(td.getFeature());
86
    		    System.out.println(td.getPeriod());
87
                appCtr.commitTransaction(tx);
88
    		}
89

    
90
    		appCtr.close();
91
 		}catch (Exception e) {
92
 		    e.printStackTrace();
93
 		}
94
 		System.out.println("Ready");
95
	}
96

    
97
    private CdmDataSource getDatasource(DatabaseTypeEnum dbType, String database) {
98
        String server = "localhost";
99
        String username = "edit";
100
        String serverSql = "130.133.70.26";
101
//        server = "160.45.63.175";
102

    
103
        if (dbType == DatabaseTypeEnum.MySQL){
104
            return CdmDataSource.NewMySqlInstance(server, database, username, AccountStore.readOrStorePassword(server, database, username, null));
105
        }else if (dbType == DatabaseTypeEnum.H2){
106
            //H2
107
            String path = "C:\\Users\\a.mueller\\.cdmLibrary\\writableResources\\h2\\LocalH2_" + database;
108
//            String path = "C:\\Users\\a.mueller\\.cdmLibrary\\writableResources\\h2\\LocalH2_xyz";
109
            username = "sa";
110
            CdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("cdmTest", username, "", path);
111
            return dataSource;
112
        }else if (dbType == DatabaseTypeEnum.SqlServer2005){
113
            server = serverSql;
114
            username = "cdmupdater";
115
            CdmDataSource dataSource = CdmDataSource.NewSqlServer2005Instance(server, database, 1433, username, AccountStore.readOrStorePassword(server, database, username, null));
116
            return dataSource;
117
        }else if (dbType == DatabaseTypeEnum.PostgreSQL){
118
            server = serverSql;
119
            username = "postgres";
120
            CdmDataSource dataSource = CdmDataSource.NewPostgreSQLInstance(server, database, 5432, username,  AccountStore.readOrStorePassword(server, database, username, null));
121
            return dataSource;
122
        }else{
123
            throw new IllegalArgumentException("dbType not supported:" + dbType);
124
        }
125
    }
126

    
127
	/**
128
	 * Updates the H2 test database in remote web-app.
129
	 * Requires that the local path to the database is adapted
130
	 */
131
	private void updateRemoteWebappTestH2(){
132
	    String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\cdmlib\\cdmlib-remote-webapp\\";
133
	    updateH2(pathToProject);
134
	}
135

    
136
    /**
137
     * Updates the H2 test database in TaxEditor.
138
     * Requires that the local path to the database is adapted
139
     */
140
    private void updateTaxEditorH2(){
141
        String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\taxeditor2\\eu.etaxonomy.taxeditor.test\\";
142
        updateH2(pathToProject);
143
    }
144

    
145
    /**
146
     * Updates the H2 test database in CDM vaadin.
147
     * Requires that the local path to the database is adapted
148
     */
149
    private void updateVaadinH2(){
150
        String pathToProject = "C:\\Users\\a.mueller\\eclipse\\git\\cdm-vaadin\\";
151
        updateH2(pathToProject);
152
    }
153

    
154
    private void updateH2(String pathToProject) {
155
        String pathInProject = "src\\test\\resources\\h2";
156

    
157
	    String path = pathToProject + pathInProject;
158
		ICdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("cdmTest", "sa", "", path);
159

    
160
 		try {
161
			CdmUpdater updater = new CdmUpdater();
162
			SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource, DefaultProgressMonitor.NewInstance());
163
			System.out.println(result.createReport());
164
		} catch (Exception e) {
165
			e.printStackTrace();
166
		}
167

    
168
		//CdmPersistentDataSource.save(dataSource.getName(), dataSource);
169
		CdmApplicationController appCtr;
170
		appCtr = CdmApplicationController.NewInstance(dataSource,DbSchemaValidation.VALIDATE);
171
		appCtr.close();
172
		System.out.println("\nEnd Datasource");
173
    }
174

    
175
    @SuppressWarnings("unused")  //enable only if needed
176
    private void updateEdaphobasePostgres(){
177
       String serverSql = "130.133.70.26";
178
       String database = "cdm_edaphobase";
179
       int port = 5433;
180
       String username = "edaphobase";
181
       String password = AccountStore.readOrStorePassword(database, serverSql, username, null);
182

    
183
       ICdmDataSource dataSource = CdmDataSource.NewPostgreSQLInstance(serverSql,
184
                database, port, username, password);
185
        try {
186
            CdmUpdater updater = new CdmUpdater();
187
            SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource, DefaultProgressMonitor.NewInstance());
188
            System.out.println(result.createReport());
189
        } catch (Exception e) {
190
            e.printStackTrace();
191
        }
192

    
193
        //CdmPersistentDataSource.save(dataSource.getName(), dataSource);
194
        CdmApplicationController appCtr;
195
        appCtr = CdmApplicationController.NewInstance(dataSource,DbSchemaValidation.VALIDATE);
196
        appCtr.close();
197
        System.out.println("\nEnd Datasource");
198
    }
199

    
200
	private void test(){
201
		System.out.println("Start TestModelUpdate");
202
		testSelectedDb();
203

    
204
//		updateRemoteWebappTestH2();
205
//		updateAllTestH2();
206
//		updateEdaphobasePostgres();
207

    
208
		System.out.println("\nEnd Datasource");
209
	}
210

    
211
	/**
212
     * Updates all H2 test DBs
213
     */
214
    private void updateAllTestH2() {
215
        updateRemoteWebappTestH2();
216
        updateTaxEditorH2();
217
        updateVaadinH2();
218
    }
219

    
220
	public static void  main(String[] args) {
221
	    TestModelUpdate cc = new TestModelUpdate();
222
		cc.test();
223
    	System.exit(0);
224
	}
225
}
(7-7/10)