Dennis Kao
2013-08-06 711aa588d14fd0a6baf4dcf94dcdb0ba1032a645
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package com.ximple.eofms.jobs;
 
import com.ximple.eofms.geoserver.config.XGeosDataConfig;
import com.ximple.eofms.geoserver.config.XGeosDataConfigMapping;
import com.ximple.eofms.jobs.context.AbstractOracleJobContext;
import com.ximple.eofms.util.PrintfFormat;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder;
import it.geosolutions.geoserver.rest.manager.GeoServerRESTStoreManager;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.digester3.Digester;
import org.apache.commons.digester3.binder.DigesterLoader;
import org.apache.commons.digester3.xmlrules.FromXmlRulesModule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.geotools.data.Transaction;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.xml.sax.SAXException;
 
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
public class GeoserverIntegrateConfigJob extends OracleConvertDgn2PostGISJob {
    final static Log logger = LogFactory.getLog(GeoserverIntegrateConfigJob.class);
 
    private static final String SKIPCONFIGJOB = "SKIPCONFIGJOB";
    private static final String MASTERMODE = "MASTERMODE";
    private static final String EPSG = "EPSG:";
    private static final String DEFAULTNAMESPACE = "xtpc";
    private static final String XGEOSDATACONFIG_PATH = "xgeosdataconfig.xml";
    private static final String XGEOSRULES_NAME = "DefaultXGeosDataConfigRules.xml";
    private static final String GEOSERVER_BASEURL = "GEOSERVER_URL";
    private static final String GEOSERVER_USER = "GEOSERVER_USER";
    private static final String GEOSERVER_PASS = "GEOSERVER_PASS";
 
    // private static final int MAGIC_BLOCKSIZE = (64 * 1024 * 1024) - (32 * 1024);
 
    private static final String QUERY_VIEWDEFSQL = "SELECT table_name, view_definition FROM information_schema.views " +
            "WHERE table_schema = ? AND table_name LIKE ";
 
    private static final String CREATE_VIEWSQL = "CREATE OR REPLACE VIEW \"%s\" AS SELECT * FROM \"%s\".\"%s\"";
    private static final String EXTRAWHERE_VIEWSQL = " WHERE \"%s\".level = %s AND \"%s\".symweight = %s";
 
    private static final String ALTER_VIEWSQL = "ALTER TABLE \"%s\" OWNER TO ";
    // private static final String GRANT_VIEWSQL = "GRANT SELECT ON TABLE \"%s\" TO public";
    private static final int SRSID_TWD97_ZONE119 = 3825;
    private static final int SRSID_TWD97_ZONE121 = 3826;
 
    private static XGeosDataConfigMapping xgeosDataConfigMapping = null;
 
    protected String _geoServerURL;
    protected String _geoServerUser;
    protected String _geoServerPass;
 
    private long queryTime = 0;
    private long queryTimeStart = 0;
 
    public Log getLogger() {
        return logger;
    }
 
    protected AbstractOracleJobContext prepareJobContext(String targetSchemaName, String filterPath,
                                                         boolean profileMode,
                                                         boolean useTransform) {
        return super.prepareJobContext(targetSchemaName, filterPath, profileMode, useTransform);
    }
 
    protected void extractJobConfiguration(JobDetail jobDetail) throws JobExecutionException {
        super.extractJobConfiguration(jobDetail);
 
        JobDataMap dataMap = jobDetail.getJobDataMap();
        _geoServerURL = dataMap.getString(GEOSERVER_BASEURL);
        _geoServerUser = dataMap.getString(GEOSERVER_USER);
        _geoServerPass = dataMap.getString(GEOSERVER_PASS);
 
        if (_geoServerURL == null) {
            logger.warn("GEOSERVER_URL is null");
            throw new JobExecutionException("Unknown GEOSERVER_URL.");
        }
        if (_geoServerUser == null) {
            logger.warn("GEOSERVER_USER is null");
            throw new JobExecutionException("Unknown GEOSERVER_USER.");
        }
        if (_geoServerPass == null) {
            logger.warn("GEOSERVER_PASS is null");
            throw new JobExecutionException("Unknown GEOSERVER_PASS.");
        }
    }
 
    protected XGeosDataConfigMapping getConfigMapping() {
        if (xgeosDataConfigMapping == null) {
            final URL rulesURL = XGeosDataConfigMapping.class.getResource(XGEOSRULES_NAME);
            assert rulesURL != null;
 
            DigesterLoader loader = DigesterLoader.newLoader( new FromXmlRulesModule() {
                        @Override
                        protected void loadRules() {
                            loadXMLRules(rulesURL);
                        }
 
                    });
            Digester digester = loader.newDigester();
            /*
            File rootDir = GeoserverDataDirectory.getGeoserverDataDirectory();
            File xfmsConfigDir;
 
            try {
                xfmsConfigDir = GeoserverDataDirectory.findConfigDir(rootDir, "xdgnjobs");
            } catch (ConfigurationException cfe) {
                logger.warn("no xmark dir found, creating new one");
                //if for some bizarre reason we don't fine the dir, make a new one.
                xfmsConfigDir = new File(rootDir, "xdgnjobs");
            }                            x
 
            File xfmsConfigFile = new File(xfmsConfigDir, XGEOSDATACONFIG_PATH);
            try {
                xgeosDataConfigMapping = (XGeosDataConfigMapping) digester.parse(xfmsConfigFile);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            } catch (SAXException e) {
                logger.warn(e.getMessage(), e);
            }
            */
            final URL configDataURL = XGeosDataConfigMapping.class.getResource(XGEOSDATACONFIG_PATH);
            try {
                xgeosDataConfigMapping = (XGeosDataConfigMapping) digester.parse(configDataURL);
            } catch (IOException e) {
                logger.warn(e.getMessage(), e);
            } catch (SAXException e) {
                logger.warn(e.getMessage(), e);
            }
 
        }
        return xgeosDataConfigMapping;
    }
 
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
 
        super.execute(jobExecutionContext);
 
        createTargetDataStore();
        if (getSourceDataStore() == null) {
            logger.warn("Cannot connect source oracle database.");
            throw new JobExecutionException("Cannot connect source oracle database.");
        }
 
        if (getTargetDataStore() == null) {
            logger.warn("Cannot connect source postgreSQL database.");
            throw new JobExecutionException("Cannot connect source postgreSQL database.");
        }
 
        if (isProfileMode()) {
            queryTime = 0;
        }
 
        long t1 = System.currentTimeMillis();
        String targetSchemaName;
 
        try {
            resetPostgisViewMapping(jobExecutionContext);
            resetGeoServerConfig(jobExecutionContext);
        } finally {
            disconnect();
        }
    }
 
    /**
     * 重新建立所有重新建立所有PostGIS中的資料庫視景
     *
     * @param executionContext 批次執行的關係
     */
    private void resetPostgisViewMapping(JobExecutionContext executionContext) {
        assert executionContext != null;
        try {
            Connection connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
            String ownerName = _pgUsername;
            String currentTargetSchema = retrieveCurrentSchemaName(connection,
                DataReposVersionManager.VSSTATUS_READY);
            if (currentTargetSchema == null) {
                logger.info("Cannot found schema that status is VSSTATUS_READY[" +
                    DataReposVersionManager.VSSTATUS_READY + "]");
                return;
            }
 
            ArrayList<String> realTableNames = new ArrayList<String>();
            retrieveAllRealTableName(connection, currentTargetSchema, realTableNames);
 
            HashMap<String, String> viewDefs = retrieveViewDef(connection, "public", "fsc%");
            HashMap<String, String> tempViewDefs = retrieveViewDef(connection, "public", "indexshape%");
            viewDefs.putAll(tempViewDefs);
            tempViewDefs = viewDefs = retrieveViewDef(connection, "public", "lndtpc%");
            viewDefs.putAll(tempViewDefs);
 
            for (String tableName : realTableNames) {
                resetPostgisDataView(connection, viewDefs, ownerName, currentTargetSchema, tableName);
            }
 
            resetExtraPostgisDataView(connection, ownerName, currentTargetSchema, realTableNames);
 
            updateCurrentRepositoryStatus(connection, currentTargetSchema,
                DataReposVersionManager.VSSTATUS_LINKVIEW);
 
            // String[] featureNames = dataStore.getTypeNames();
            // logger.info("featureNames[] size = " + featureNames.length);
        } catch (IOException e) {
            logger.warn(e.getMessage(), e);
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
        } finally {
            // if (dataStore != null) dataStore.dispose();
        }
    }
 
    private void retrieveAllRealTableName(Connection connection, String targetSchema,
                                          ArrayList<String> realTableNames) throws SQLException {
        ResultSet rsMeta = null;
        try {
            rsMeta = connection.getMetaData().getTables("", targetSchema, "fsc%", new String[]{"TABLE"});
            while (rsMeta.next()) {
                String tableName = rsMeta.getString(3);
                realTableNames.add(tableName);
            }
            rsMeta.close();
            rsMeta = null;
 
            rsMeta = connection.getMetaData().getTables("", targetSchema, "index%", new String[]{"TABLE"});
            while (rsMeta.next()) {
                String tableName = rsMeta.getString(3);
                realTableNames.add(tableName);
            }
            rsMeta.close();
            rsMeta = null;
 
            rsMeta = connection.getMetaData().getTables("", targetSchema, "lndtpc%", new String[]{"TABLE"});
            while (rsMeta.next()) {
                String tableName = rsMeta.getString(3);
                realTableNames.add(tableName);
            }
        } finally {
            if (rsMeta != null) rsMeta.close();
        }
    }
 
    private void resetPostgisDataView(Connection connection, HashMap<String, String> viewDefs,
                                      String ownerName, String schemaName, String tableName) throws SQLException {
        String[] splits = tableName.split("-");
        if (splits.length > 3) {
            // feature table
 
            StringBuilder viewBuilder = new StringBuilder();
            viewBuilder.append(splits[0]);
            viewBuilder.append('-');
            viewBuilder.append(splits[1]);
            viewBuilder.append('-');
            viewBuilder.append(splits[2]);
            viewBuilder.append(splits[3]);
            String viewName = viewBuilder.toString();
            if (viewDefs.containsKey(viewName)) {
                String viewDef = viewDefs.get(viewName);
                int pos = viewDef.indexOf("FROM");
                String subView = viewDef.substring(pos + 4);
                // String[] viewSources = subView.split("\\.");
                String[] viewSources = subView.split("(\\.\"|\")");
                if (!viewSources[0].equalsIgnoreCase(schemaName)) {
                    createOrReplaceView(connection, schemaName, tableName, viewName, ownerName);
                }
            } else {
                createOrReplaceView(connection, schemaName, tableName, viewName, ownerName);
            }
 
        } else {
 
            splits = tableName.split("_");
            if (splits.length > 0) {
                StringBuilder viewBuilder = new StringBuilder();
                viewBuilder.append(splits[0]);
                if (splits.length > 1) viewBuilder.append(splits[1]);
                if (splits.length > 2) viewBuilder.append(splits[2]);
                String viewName = viewBuilder.toString();
                if (viewDefs.containsKey(viewName)) {
                    String viewDef = viewDefs.get(viewName);
                    int pos = viewDef.indexOf("FROM");
                    String subView = viewDef.substring(pos + 4);
                    String[] viewSources = subView.split("(\\.\"|\")");
                    if (!viewSources[0].equalsIgnoreCase(schemaName)) {
                        createOrReplaceView(connection, schemaName, tableName, viewName, ownerName);
                    }
                } else {
                    createOrReplaceView(connection, schemaName, tableName, viewName, ownerName);
                }
            }
        }
    }
 
    private void resetExtraPostgisDataView(Connection connection, String ownerName, String currentSchema,
                                           ArrayList<String> realTableNames) {
        try {
            // ArrayList<String> extraViewNames = new ArrayList<String>();
            XGeosDataConfigMapping configMapping = getConfigMapping();
            MultiMap configMultiMap = configMapping.getMapping();
            for (Object key : configMultiMap.keySet()) {
                List values = (List) configMultiMap.get(key);
                for (Object value : values) {
                    XGeosDataConfig xgeosConfig = (XGeosDataConfig) value;
                    short tid = xgeosConfig.getFSC();
                    short cid = xgeosConfig.getCOMP();
                    StringBuilder sbTable = new StringBuilder("fsc-");
                    sbTable.append(tid).append("-c-");
                    sbTable.append(cid);
                    int index = realTableNames.indexOf(sbTable.toString());
                    if (index == -1) {
                        logger.debug("Cannot found-" + xgeosConfig.toString());
                        continue;
                    }
                    StringBuilder sbView = new StringBuilder("fsc-");
                    sbView.append(tid).append("-c");
                    sbView.append(cid).append("-l");
                    sbView.append(xgeosConfig.getLEV()).append("-w");
                    sbView.append(xgeosConfig.getWEIGHT());
                    // extraViewNames.add(sbView.toString());
 
                    createOrReplaceExtraView(connection, currentSchema, sbTable.toString(), sbView.toString(),
                            ownerName, xgeosConfig);
                }
            }
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
        }
    }
 
    private HashMap<String, String> retrieveViewDef(Connection connection, String schemaName, String tablePattern) throws SQLException {
        PreparedStatement stmt = connection.prepareStatement(QUERY_VIEWDEFSQL + "'" + tablePattern + "'");
        stmt.setString(1, schemaName);
        // stmt.setString(2, tablePattern);
        HashMap<String, String> result = new HashMap<String, String>();
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            String tableName = rs.getString(1);
            String viewDef = rs.getString(2);
            result.put(tableName, viewDef);
        }
        rs.close();
        stmt.close();
        return result;
    }
 
    private void createOrReplaceView(Connection connection, String schemaName, String tableName, String viewName,
                                     String ownerName) throws SQLException {
        PrintfFormat pf = new PrintfFormat(CREATE_VIEWSQL);
        String sql = pf.sprintf(new Object[]{viewName, schemaName, tableName});
        Statement stmt = connection.createStatement();
        stmt.execute(sql);
 
        pf = new PrintfFormat(ALTER_VIEWSQL + ownerName);
        sql = pf.sprintf(viewName);
        stmt.execute(sql);
        stmt.close();
        connection.commit();
    }
 
    private void createOrReplaceExtraView(Connection connection, String schemaName, String tableName, String viewName,
                                          String ownerName, XGeosDataConfig xgeosConfig) throws SQLException {
        PrintfFormat pf = new PrintfFormat(CREATE_VIEWSQL);
        String sql = pf.sprintf(new Object[]{viewName, schemaName, tableName});
 
        PrintfFormat pfWhere = new PrintfFormat(EXTRAWHERE_VIEWSQL);
        sql += pfWhere.sprintf(new String[]{tableName, Short.toString(xgeosConfig.getLEV()),
                tableName, Short.toString(xgeosConfig.getWEIGHT())});
 
        Statement stmt = connection.createStatement();
        stmt.execute(sql);
 
        pf = new PrintfFormat(ALTER_VIEWSQL + ownerName);
        sql = pf.sprintf(viewName);
        stmt.execute(sql);
        stmt.close();
        connection.commit();
    }
 
    private Timestamp retrieveCurrentSchemaTimestamp(Connection connection, short status) throws SQLException {
        StringBuilder sbSQL = new StringBuilder("SELECT vstimestamp, vsschema, vsstatus FROM ");
        sbSQL.append(DataReposVersionManager.XGVERSIONTABLE_NAME);
        sbSQL.append(" WHERE vsstatus = ");
        sbSQL.append(status);
        sbSQL.append(" ORDER BY vsid");
 
        Timestamp result = null;
        Statement stmt = null;
        ResultSet rs = null;
 
        try {
            stmt = connection.createStatement();
            rs = stmt.executeQuery(sbSQL.toString());
            // get first result
            if (rs.next()) {
                result = rs.getTimestamp(1);
            }
            return result;
        } finally {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
        }
    }
 
    private void updateCurrentRepositoryStatus(Connection connection, String schemaName, short newStatus)
            throws SQLException {
        StringBuilder sbSQL = new StringBuilder("UPDATE ");
        sbSQL.append(DataReposVersionManager.XGVERSIONTABLE_NAME).append(' ');
        sbSQL.append(" SET vsstatus = ");
        sbSQL.append(newStatus);
        sbSQL.append(", vstimestamp = CURRENT_TIMESTAMP WHERE vsschema = '");
        sbSQL.append(schemaName).append("'");
 
        Statement stmt = null;
        try {
            stmt = connection.createStatement();
            stmt.executeUpdate(sbSQL.toString());
        } finally {
            if (stmt != null) stmt.close();
        }
    }
 
    /*
    private boolean checkCurrentRepositoryStatus(DataStore dataStore, short status) {
        try {
            if (dataStore instanceof PostgisDataStore) {
                PostgisDataStore pgDataStore = (PostgisDataStore) dataStore;
                DataSource dataSource = pgDataStore.getDataSource();
                Connection connection = dataSource.getConnection();
                return checkCurrentRepositoryStatus(connection, status);
            }
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
        }
        return false;
    }
    */
 
    private boolean checkCurrentRepositoryStatus(Connection connection, short status) {
        try {
            return (retrieveCurrentSchemaName(connection, status) != null);
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
            return false;
        }
    }
 
    private String retrieveCurrentSchemaName(Connection connection, short status) throws SQLException {
        StringBuilder sbSQL = new StringBuilder("SELECT vsschema, vstimestamp, vsstatus FROM ");
        sbSQL.append(DataReposVersionManager.XGVERSIONTABLE_NAME);
        sbSQL.append(" WHERE vsstatus = ");
        sbSQL.append(status);
        sbSQL.append(" ORDER BY vsid");
 
        String result = null;
        Statement stmt = null;
        ResultSet rs = null;
 
        try {
            stmt = connection.createStatement();
            rs = stmt.executeQuery(sbSQL.toString());
            // get first result
            if (rs.next()) {
                result = rs.getString(1);
            }
            return result;
        } finally {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
        }
    }
 
    private void resetGeoServerConfig(JobExecutionContext jobExecutionContext) {
        try {
            URL geoServerURL = new URL(_geoServerURL);
            GeoServerRESTManager manager = new GeoServerRESTManager(geoServerURL, _geoServerUser, _geoServerPass);
            GeoServerRESTReader reader = manager.getReader();
            List<String> workSpaces = reader.getWorkspaceNames();
            boolean found = false;
            for (String name : workSpaces) {
                if (name.equalsIgnoreCase(DEFAULTNAMESPACE)) {
                    found = true;
                    break;
                }
            }
 
            if (!found) {
                GeoServerRESTPublisher publisher = manager.getPublisher();
                publisher.createWorkspace(DEFAULTNAMESPACE, new URI("http://tpc.ximple.com.tw/geodmms"));
            }
 
            RESTDataStore dataStore = reader.getDatastore(DEFAULTNAMESPACE, "pgDMMS");
            if (dataStore == null) {
                GeoServerRESTStoreManager storeManager = manager.getStoreManager();
                GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder("pgDMMS");
                store.setHost(_pgHost);
                // store.setPort(_pgPort);
                store.setDatabase(_pgDatabase);
                store.setSchema("public");
                store.setUser(_pgUsername);
                store.setPassword(_pgPassword);
                storeManager.create(DEFAULTNAMESPACE, store);
            }
 
 
            XGeosDataConfigMapping configMapping = getConfigMapping();
            MultiMap configMultiMap = configMapping.getMapping();
            for (Object key : configMultiMap.keySet()) {
                List values = (List) configMultiMap.get(key);
                for (Object value : values) {
                    XGeosDataConfig xgeosConfig = (XGeosDataConfig) value;
                }
            }
 
 
        } catch (MalformedURLException e) {
            logger.warn(e.getMessage(), e);
        } catch (URISyntaxException e) {
            logger.warn(e.getMessage(), e);
        }
    }
}