forked from geodmms/xdgnjobs

unknown
2014-04-29 7ba0aac99ec32d80971384df559c54987c8c6ccb
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
package com.ximple.eofms.jobs;
 
import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
 
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.jobs.context.postgis.OracleConvertPostGISJobContext;
import com.ximple.eofms.util.ConnectivityDirectionEnum;
import com.ximple.eofms.util.DefaultColorTable;
import com.ximple.eofms.util.PrintfFormat;
import com.ximple.eofms.util.XGeosConfigDigesterUtils;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.dbcp.DelegatingConnection;
import org.apache.commons.digester3.Digester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.geotools.data.DataStore;
import org.geotools.data.Transaction;
import org.geotools.data.jdbc.JDBCUtils;
import org.geotools.data.postgis.PostgisNGDataStoreFactory;
import org.geotools.jdbc.JDBCDataStore;
import org.postgresql.PGConnection;
import org.postgresql.copy.CopyManager;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.xml.sax.SAXException;
 
public class OracleConvertThemes2PostGISJob extends AbstractOracleDatabaseJob {
    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 XGEOSDATACONFIG_PATH = "xgeosdataconfig.xml";
 
    // 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;
    public static final String DEFAULT_STORENAME = "pgDMMS";
    public static final String DEFAULT_GEODMMS_NAMESPACE = "http://tpc.ximple.com.tw/geodmms";
 
    private static final String PGHOST = "PGHOST";
    private static final String PGDATBASE = "PGDATBASE";
    private static final String PGPORT = "PGPORT";
    private static final String PGSCHEMA = "PGSCHEMA";
    private static final String PGUSER = "PGUSER";
    private static final String PGPASS = "PGPASS";
    private static final String USEWKB = "USEWKB";
 
    private static final boolean useTpclidText = false;
 
    private static final int FETCHSIZE = 30;
    private static final int COMMITSIZE = 100;
 
    public static final String FORWARDFLOW_MARK = "shape://ccarrow";
    public static final String BACKFLOW_MARK = "shape://rccarrow";
    public static final String UNFLOW_MARK = "shape://backslash";
    public static final String NONFLOW_MARK = "shape://slash";
 
    private static String FETCH_CONNFDR = "SELECT FSC, UFID, FDR1, DIR FROM BASEDB.CONNECTIVITY ORDER BY FSC";
    private static String FETCH_COLORTAB = "SELECT TAG_SFSC, TAG_LUFID, COLOR FROM OCSDB.COLOR WHERE TAG_BCOMPID = 0 ORDER BY TAG_SFSC";
 
    private static String CREATE_OWNERTABLE = "CREATE TABLE s (tid smallint not null, oid int not null, owner smallint not null)";
    private static String CREATE_COLORTABLE = "CREATE TABLE s (tid smallint not null, oid int not null, dyncolor varchar(10) not null)";
 
    public static final String FDYNCOLOR_SUFFIX = "_fdyncolor";
    public static final String FOWNER_SUFFIX = "_fowner";
 
    private static XGeosDataConfigMapping xgeosDataConfigMapping = null;
    protected JDBCDataStore targetDataStore;
    protected Map<String, String> pgProperties;
 
    protected static PostgisNGDataStoreFactory dataStoreFactory = new PostgisNGDataStoreFactory();
 
    protected String _pgHost;
    protected String _pgDatabase;
    protected String _pgPort;
    protected String _pgSchema;
    protected String _pgUsername;
    protected String _pgPassword;
    protected String _pgUseWKB;
 
    private long queryTime = 0;
    private long queryTimeStart = 0;
    private String currentThemeTable = null;
    private Short currentThemeStatus = -1;
 
    public Log getLogger() {
        return logger;
    }
 
    protected AbstractOracleJobContext prepareJobContext(String targetSchemaName, String filterPath,
                                                         boolean profileMode,
                                                         boolean useTransform) {
        return new OracleConvertPostGISJobContext(getDataPath(),
            getTargetDataStore(), targetSchemaName, filterPath, profileMode, useTransform);
    }
 
    protected void extractJobConfiguration(JobDetail jobDetail) throws JobExecutionException {
        super.extractJobConfiguration(jobDetail);
 
        JobDataMap dataMap = jobDetail.getJobDataMap();
        _pgHost = dataMap.getString(PGHOST);
        _pgDatabase = dataMap.getString(PGDATBASE);
        _pgPort = dataMap.getString(PGPORT);
        _pgSchema = dataMap.getString(PGSCHEMA);
        _pgUsername = dataMap.getString(PGUSER);
        _pgPassword = dataMap.getString(PGPASS);
        _pgUseWKB = dataMap.getString(USEWKB);
 
        Log logger = getLogger();
        if (_pgHost == null) {
            logger.warn("PGHOST is null");
            throw new JobExecutionException("Unknown PostGIS host.");
        }
        if (_pgDatabase == null) {
            logger.warn("PGDATABASE is null");
            throw new JobExecutionException("Unknown PostGIS database.");
        }
        if (_pgPort == null) {
            logger.warn("PGPORT is null");
            throw new JobExecutionException("Unknown PostGIS port.");
        }
        if (_pgSchema == null) {
            logger.warn("PGSCHEMA is null");
            throw new JobExecutionException("Unknown PostGIS schema.");
        }
        if (_pgUsername == null) {
            logger.warn("PGUSERNAME is null");
            throw new JobExecutionException("Unknown PostGIS username.");
        }
        if (_pgPassword == null) {
            logger.warn("PGPASSWORD is null");
            throw new JobExecutionException("Unknown PostGIS password.");
        }
 
        Map<String, String> remote = new TreeMap<String, String>();
        remote.put(PostgisNGDataStoreFactory.DBTYPE.key, "postgis");
        // remote.put("charset", "UTF-8");
        remote.put(PostgisNGDataStoreFactory.HOST.key, _pgHost);
        remote.put(PostgisNGDataStoreFactory.PORT.key, _pgPort);
        remote.put(PostgisNGDataStoreFactory.DATABASE.key, _pgDatabase);
        remote.put(PostgisNGDataStoreFactory.USER.key, _pgUsername);
        remote.put(PostgisNGDataStoreFactory.PASSWD.key, _pgPassword);
        // remote.put( "namespace", null);
        pgProperties = remote;
    }
 
    protected XGeosDataConfigMapping getConfigMapping() {
        if (xgeosDataConfigMapping == null) {
            Digester digester = XGeosConfigDigesterUtils.getXGeosConfigDigester();
            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;
    }
 
    private void logTimeDiff(String message, long tBefore, long tCurrent) {
        logger.warn(message + ":use time = " + ((int) ((tCurrent - tBefore) / 60000.0)) + " min - " +
                (((int) ((tCurrent - tBefore) % 60000.0)) / 1000) + " sec");
    }
 
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
 
        // Every job has its own job detail
        JobDetail jobDetail = context.getJobDetail();
 
        // The name is defined in the job definition
        String jobName = jobDetail.getKey().getName();
 
        // Log the time the job started
        logger.info(jobName + " fired at " + new Date());
        extractJobConfiguration(jobDetail);
 
        createSourceDataStore();
        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 targetThemeTable;
        try {
            logger.info("-- step:clearOutputDatabase --");
 
            OracleConvertPostGISJobContext jobContext = null;
 
            if (checkConvertPWThemes()) {
                targetThemeTable = determineTargetThemeTableName();
                jobContext = (OracleConvertPostGISJobContext) prepareJobContext("public", _filterPath,
                        isProfileMode(), isTransformed());
                jobContext.setSourceDataStore(getSourceDataStore());
                jobContext.setElementLogging(checkElementLogging());
                jobContext.setExecutionContext(context);
 
                long tStep = System.currentTimeMillis();
                if (!convertPowerOwnerThemeWithCopyAPI(jobContext, targetThemeTable)) {
                    convertPowerOwnerTheme(jobContext, targetThemeTable);
                }
                if (isProfileMode()) {
                    long tStepEnd = System.currentTimeMillis();
                    logTimeDiff("Profile-convertFeatureDesignFile", tStep, tStepEnd);
                }
                tStep = System.currentTimeMillis();
                if (!convertDynamicColorThemeWithCopyAPI(jobContext, targetThemeTable))
                    convertDynamicColorTheme(jobContext, targetThemeTable);
                if (isProfileMode()) {
                    long tStepEnd = System.currentTimeMillis();
                    logTimeDiff("Profile-convertFeatureDesignFile", tStep, tStepEnd);
                }
                jobContext.closeOracleConnection();
 
                updatePWThemeStatusToReady(targetThemeTable);
            }
 
            long t2 = System.currentTimeMillis();
            // public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
            // SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            logTimeDiff("Total ", t1, t2);
 
        } catch (IOException ex) {
            disconnect();
            logger.warn(ex.getMessage(), ex);
            throw new JobExecutionException("IO error. " + ex.getMessage(), ex);
        } finally {
            disconnect();
        }
        logger.warn(jobName + " end at " + new Date());
 
 
        createTargetDataStore();
        if (getTargetDataStore() == null) {
            logger.warn("Cannot connect source postgreSQL database.");
            throw new JobExecutionException("Cannot connect source postgreSQL database.");
        }
 
        try {
            logger.info("-- step:resetThemesViewMapping --");
            long tStep = System.currentTimeMillis();
            resetThemesViewMapping(context);
            if (isProfileMode()) {
                long tStepEnd = System.currentTimeMillis();
                logTimeDiff("Profile-resetThemesViewMapping", tStep, tStepEnd);
            }
            logger.info("-- step:resetGeoServerConfig --");
            tStep = System.currentTimeMillis();
            // resetGeoServerConfig(jobExecutionContext);
            if (isProfileMode()) {
                long tStepEnd = System.currentTimeMillis();
                logTimeDiff("Profile-resetGeoServerConfig", tStep, tStepEnd);
            }
        } finally {
            disconnect();
        }
    }
 
    /**
     * 重新建立所有重新建立所有PostGIS中的資料庫視景
     *
     * @param executionContext 批次執行的關係
     */
    private void resetThemesViewMapping(JobExecutionContext executionContext) throws JobExecutionException {
        assert executionContext != null;
        Connection connection = null;
        try {
            connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
            String ownerName = _pgUsername;
 
            String currentTargetThemesName = retrieveCurrentThemeName(connection,
                DataReposVersionManager.VSSTATUS_READY);
            if (currentTargetThemesName == null) {
                logger.info("Cannot found themes that status is VSSTATUS_READY[" +
                    DataReposVersionManager.VSSTATUS_READY + "]");
                return;
            }
 
            ArrayList<String> realTableNames = new ArrayList<String>();
            retrieveAllRealTableName(connection, currentTargetThemesName, realTableNames);
 
            resetThemesBaseView(connection, ownerName, currentTargetThemesName);
 
            if (currentThemeTable == null) {
                transferThemesVersionStatus(DataReposVersionManager.VSSTATUS_READY,
                    DataReposVersionManager.VSSTATUS_LINKVIEW, false);
            } else {
                transferThemesVersionStatus(DataReposVersionManager.VSSTATUS_READY,
                    currentThemeStatus, true);
            }
 
            /*
            updateCurrentThemeStatus(connection, currentTargetThemesName,
                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 (connection != null)
                JDBCUtils.close(connection, Transaction.AUTO_COMMIT, null);
            // 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 resetThemesBaseView(Connection connection, String ownerName, String currentThemesName)
            throws SQLException {
        String viewName = "xpwtheme" + FDYNCOLOR_SUFFIX;
        String tableName = currentThemesName + FDYNCOLOR_SUFFIX;
        PrintfFormat pf = new PrintfFormat("CREATE OR REPLACE VIEW \"%s\" AS SELECT * FROM \"%s\"");
        String sql = pf.sprintf(new Object[]{viewName, tableName});
        Statement stmt = connection.createStatement();
        try {
            stmt.execute(sql);
            pf = new PrintfFormat(ALTER_VIEWSQL + ownerName);
            sql = pf.sprintf(viewName);
            stmt.execute(sql);
 
            viewName = "xpwtheme" + FOWNER_SUFFIX;
            tableName = currentThemesName + FOWNER_SUFFIX;
            pf = new PrintfFormat("CREATE OR REPLACE VIEW \"%s\" AS SELECT * FROM \"%s\"");
            sql = pf.sprintf(new Object[]{viewName, tableName});
 
            stmt.execute(sql);
            pf = new PrintfFormat(ALTER_VIEWSQL + ownerName);
            sql = pf.sprintf(viewName);
            stmt.execute(sql);
        } catch (SQLException e) {
            // logger.warn(e.getMessage(), e);
            logger.info(sql == null ? "SQL=NULL" : "SQL=" + sql);
            throw e;
        } finally {
            stmt.close();
        }
    }
 
 
    private void resetThemesPostgisDataView(Connection connection, String ownerName,
                                            String currentSchema, String viewName) throws SQLException {
        String themeViewName = viewName + "-oms";
        // PrintfFormat pf = new PrintfFormat(CREATE_VIEWSQL);
        // String sql = pf.sprintf(new Object[]{viewName, schemaName, tableName});
        ResultSet rs = null;
        Statement stmt = connection.createStatement();
 
        try {
            StringBuilder sbSQL = new StringBuilder("CREATE OR REPLACE VIEW \"");
            sbSQL.append(themeViewName).append("\" AS SELECT ");
 
            rs = connection.getMetaData().getColumns(null, currentSchema, viewName, "%");
            while (rs.next()) {
                String fieldName = rs.getString("COLUMN_NAME");
                sbSQL.append("t." + fieldName).append(", ");
            }
            sbSQL.append("fc.dyncolor, fo.fowner FROM ");
            if (currentSchema != null)
                sbSQL.append("\"").append(currentSchema).append("\".\"").append(viewName).append("\" AS t,");
            else
                sbSQL.append("\"").append(viewName).append("\" AS t,");
            sbSQL.append("xpwtheme").append(FDYNCOLOR_SUFFIX).append(" AS fc,");
            sbSQL.append("xpwtheme").append(FOWNER_SUFFIX).append(" AS fo WHERE ");
            sbSQL.append("t.tid = fc.tid AND t.oid = fc.oid AND ");
            sbSQL.append("t.tid = fo.tid AND t.oid = fo.oid");
 
            // sbSQL.delete(sbSQL.length() - 2, sbSQL.length());
            String sql = sbSQL.toString();
            stmt.execute(sql);
            sbSQL.delete(0, sbSQL.length());
 
            PrintfFormat pf = new PrintfFormat(ALTER_VIEWSQL + ownerName);
            sql = pf.sprintf(themeViewName);
            stmt.execute(sql);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
        }
    }
 
    private void resetFlowThemesPostgisDataView(Connection connection, String ownerName,
                                            String currentSchema, String viewName) throws SQLException {
        String themeViewName = viewName + "-flow-oms";
        ResultSet rs = null;
        Statement stmt = connection.createStatement();
 
        try {
            StringBuilder sbSQL = new StringBuilder("CREATE OR REPLACE VIEW \"");
            sbSQL.append(themeViewName).append("\" AS SELECT ");
 
            rs = connection.getMetaData().getColumns(null, currentSchema, viewName, "%");
            while (rs.next()) {
                String fieldName = rs.getString("COLUMN_NAME");
                sbSQL.append("t." + fieldName).append(", ");
            }
            sbSQL.append("fc.dyncolor, fo.fowner, fo.flow FROM ");
            if (currentSchema != null)
                sbSQL.append("\"").append(currentSchema).append("\".\"").append(viewName).append("\" AS t,");
            else
                sbSQL.append("\"").append(viewName).append("\" AS t,");
            sbSQL.append("xpwtheme").append(FDYNCOLOR_SUFFIX).append(" AS fc,");
            sbSQL.append("xpwtheme").append(FOWNER_SUFFIX).append(" AS fo WHERE ");
            sbSQL.append("t.tid = fc.tid AND t.oid = fc.oid AND ");
            sbSQL.append("t.tid = fo.tid AND t.oid = fo.oid");
 
            // sbSQL.delete(sbSQL.length() - 2, sbSQL.length());
            String sql = sbSQL.toString();
            stmt.execute(sql);
            sbSQL.delete(0, sbSQL.length());
 
            PrintfFormat pf = new PrintfFormat(ALTER_VIEWSQL + ownerName);
            sql = pf.sprintf(themeViewName);
            stmt.execute(sql);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
        }
    }
 
    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();
        try {
            stmt.execute(sql);
            pf = new PrintfFormat(ALTER_VIEWSQL + ownerName);
            sql = pf.sprintf(viewName);
            stmt.execute(sql);
        } catch (SQLException e) {
            // logger.warn(e.getMessage(), e);
            logger.info(sql == null ? "SQL=NULL" : "SQL=" + sql);
            throw e;
        } finally {
            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 retrieveCurrentThemeTimestamp(Connection connection, short status) throws SQLException {
        StringBuilder sbSQL = new StringBuilder("SELECT vpttimestamp, vptname, vptstatus FROM ");
        sbSQL.append(DataReposVersionManager.XPTVERSIONTABLE_NAME);
        sbSQL.append(" WHERE vptstatus = ");
        sbSQL.append(status);
        sbSQL.append(" ORDER BY vptid");
 
        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 updateCurrentThemeStatus(Connection connection, String themeTableName, short newStatus)
            throws SQLException {
        StringBuilder sbSQL = new StringBuilder("UPDATE ");
        sbSQL.append(DataReposVersionManager.XPTVERSIONTABLE_NAME).append(' ');
        sbSQL.append(" SET vptstatus = ");
        sbSQL.append(newStatus);
        sbSQL.append(", vpttimestamp = CURRENT_TIMESTAMP WHERE vptname = '");
        sbSQL.append(themeTableName).append("'");
 
        Statement stmt = null;
        try {
            stmt = connection.createStatement();
            stmt.executeUpdate(sbSQL.toString());
        } finally {
            if (stmt != null) stmt.close();
        }
    }
 
 
    private boolean checkCurrentThemeStatus(Connection connection, short status) {
        try {
            return (retrieveCurrentThemeName(connection, status) != null);
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
            return false;
        }
    }
 
    private String retrieveCurrentThemeName(Connection connection, short status) throws SQLException {
        StringBuilder sbSQL = new StringBuilder("SELECT ");
        sbSQL.append("vptname, vpttimestamp, vptstatus FROM ");
        sbSQL.append(encodeSchemaTableName(_pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME));
        sbSQL.append(" WHERE vptstatus = ");
        sbSQL.append(status);
        sbSQL.append("ORDER BY vptid");
 
        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 {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
        }
    }
 
    protected String[] retrieveTargetStoreAllViewNames(Connection connection) {
        try {
            final int TABLE_NAME_COL = 3;
            List<String> list = new ArrayList<String>();
 
            DatabaseMetaData meta = connection.getMetaData();
            // String[] tableType = { "TABLE", "VIEW" };
            String[] tableType = { "VIEW" };
            ResultSet tables = meta.getTables(null, _pgSchema, "%", tableType);
 
            while (tables.next()) {
                String tableName = tables.getString(TABLE_NAME_COL);
                list.add(tableName);
                /*
                if (allowTable(tableName)) {
                    list.add(tableName);
                }
                */
            }
            tables.close();
            return (String[]) list.toArray(new String[list.size()]);
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
        }
        return null;
    }
 
    public DataStore getTargetDataStore() {
        return targetDataStore;
    }
 
    protected void createTargetDataStore() throws JobExecutionException {
        if (targetDataStore != null) {
            targetDataStore.dispose();
            targetDataStore = null;
        }
 
        /*
        if (!isDriverFound())
        {
            throw new JobExecutionException("Oracle JDBC Driver not found.-" + JDBC_DRIVER);
        }
        */
 
        if (!pgProperties.containsKey(PostgisNGDataStoreFactory.MAXCONN.key)) {
            pgProperties.put(PostgisNGDataStoreFactory.MAXCONN.key, "5");
        }
 
        if (!pgProperties.containsKey(PostgisNGDataStoreFactory.MINCONN.key)) {
            pgProperties.put(PostgisNGDataStoreFactory.MINCONN.key, "1");
        }
 
        if (!dataStoreFactory.canProcess(pgProperties)) {
            getLogger().warn("cannot process properties-");
            throw new JobExecutionException("cannot process properties-");
        }
        try {
            targetDataStore = dataStoreFactory.createDataStore(pgProperties);
        } catch (IOException e) {
            getLogger().warn(e.getMessage(), e);
            throw new JobExecutionException(e.getMessage(), e);
        }
    }
 
    protected void disconnect() {
        super.disconnect();
        if (targetDataStore != null) {
            targetDataStore.dispose();
            targetDataStore = null;
        }
    }
 
    private String determineTargetThemeTableName() throws IOException {
        if (targetDataStore == null) return null;
        Connection connection = null;
        Statement stmt = null;
        ResultSet rs = null;
        String targetTable = null;
        boolean needCreate = false;
        try {
            connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
            // Create XPTVERSIONTABLE_NAME
            needCreate = false;
            rs = connection.getMetaData().getTables(null, _pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME, new String[]{"TABLE"});
            if (!rs.next()) needCreate = true;
            if (needCreate) {
                createXPWThemeVersionTable(connection, _pgSchema);
            }
            rs.close();
            rs = null;
 
            StringBuilder sbSQL = new StringBuilder("SELECT ");
            sbSQL.append("vptname, vptstatus FROM ");
            sbSQL.append(encodeSchemaTableName(_pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME)).append(' ');
            sbSQL.append("ORDER BY vptid");
            stmt = connection.createStatement();
            rs = stmt.executeQuery(sbSQL.toString());
            ArrayList<Object[]> tmpTablenames = new ArrayList<Object[]>();
            int i = 0;
            int current = -1;
            while (rs.next()) {
                Object[] values = new Object[2];
                values[0] = rs.getString("vptname");
                values[1] = rs.getShort("vptstatus");
                tmpTablenames.add(values);
                if (((Short) values[1]) >= DataReposVersionManager.VSSTATUS_LINKVIEW) {
                    current = i;
                    currentThemeTable = (String) values[0];
                    currentThemeStatus = (Short) values[1];
                }
                i++;
            }
 
            if (current == -1) {
                Object[] values = tmpTablenames.get(0);
                targetTable = (String) values[0];
            } else if (current < (tmpTablenames.size() - 1)) {
                Object[] values = tmpTablenames.get(current + 1);
                targetTable = (String) values[0];
            } else {
                Object[] values = tmpTablenames.get(0);
                targetTable = (String) values[0];
            }
 
            sbSQL = new StringBuilder("UPDATE ");
            sbSQL.append(encodeSchemaTableName(_pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME)).append(' ');
            sbSQL.append(" SET vptstatus = ");
            sbSQL.append(DataReposVersionManager.VSSTATUS_COVERT);
            sbSQL.append(" WHERE vptname = '");
            sbSQL.append(targetTable).append("'");
            int count = stmt.executeUpdate(sbSQL.toString());
            if (count != 1) {
                logger.info("update status for " + targetTable + " update result count="
                    + count);
            }
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
            JDBCUtils.close(connection, Transaction.AUTO_COMMIT, null);
        }
        return targetTable;
    }
 
    public String encodeSchemaTableName(String schemaName, String tableName) {
        if (schemaName == null)
            return "\"" + tableName + "\"";
        return "\"" + schemaName + "\".\"" + tableName + "\"";
    }
 
    private boolean convertDynamicColorThemeWithCopyAPI(AbstractOracleJobContext context, String targetTableBaseName)
        throws IOException {
        if (context == null) {
            getLogger().info("jobContext is null in convertDynamicColorThemeWithCopyAPI");
            return false;
        }
        Connection connection = context.getOracleConnection();
        Connection connectionPG = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
        while (connectionPG instanceof DelegatingConnection) {
            connectionPG = ((DelegatingConnection) connectionPG).getDelegate();
        }
 
        if (!(connectionPG instanceof PGConnection)) {
            return false;
        }
 
        final int MAX_BATCHSIZE = 250;
        ResultSet rs = null;
        Statement stmt = null;
        try {
            // connectionPG.setAutoCommit(false);
            DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance();
            String targetTableName = targetTableBaseName + FDYNCOLOR_SUFFIX;
            String targetTempName = "tmp_" + targetTableName;
            logger.info("target table:" + targetTableName);
            stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(FETCH_COLORTAB);
            rs.setFetchSize(MAX_BATCHSIZE);
 
            createOrClearTempTargetTable(connectionPG, targetTempName,
                "(tid smallint not null, oid int not null, dyncolor varchar(10) not null)");
            StringBuilder sb = new StringBuilder();
 
            CopyManager cpMgr = ((PGConnection) connectionPG).getCopyAPI();
            PushbackReader reader = new PushbackReader(new StringReader(""), 10240);
 
            int count = 0;
            while (rs.next()) {
                int cid = rs.getInt(1);
                long oid = rs.getLong(2);
                int colorId = rs.getInt(3);
                String colorText = colorTable.getColorCode(colorId);
                if (cid > Short.MAX_VALUE) {
                    logger.info("Wrong Color Table:" + cid + "-" + oid);
                    continue;
                }
                sb.append(cid).append(',');
                sb.append(oid).append(',');
                sb.append(colorText).append("\n");
 
                if (count % MAX_BATCHSIZE == 0) {
                    reader.unread(sb.toString().toCharArray());
                    cpMgr.copyIn("COPY " + targetTempName + " FROM STDIN WITH CSV", reader);
                    sb.delete(0, sb.length());
                }
                ++count;
            }
 
            reader.unread(sb.toString().toCharArray());
            cpMgr.copyIn("COPY " + targetTempName + " FROM STDIN WITH CSV", reader);
            createTargetTableIndexAndDropTemp(connectionPG, targetTableName, targetTempName, "tid, oid, dyncolor");
 
            logger.info("Execute Copy Count=" + count);
        } catch (SQLException e) {
            logger.info(e.getMessage(), e);
            throw new IOException(e.getMessage(), e);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
            JDBCUtils.close(connectionPG, Transaction.AUTO_COMMIT, null);
        }
        return true;
    }
 
    private boolean convertPowerOwnerThemeWithCopyAPI(AbstractOracleJobContext context, String targetTableBaseName)
        throws IOException {
        if (context == null) {
            getLogger().info("jobContext is null in convertPowerOwnerThemeWithCopyAPI");
            return false;
        }
        Connection connection = context.getOracleConnection();
        Connection connectionPG = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
        while (connectionPG instanceof DelegatingConnection) {
            connectionPG = ((DelegatingConnection) connectionPG).getDelegate();
        }
 
        if (!(connectionPG instanceof PGConnection)) {
            return false;
        }
 
        final int MAX_BATCHSIZE = 250;
        ResultSet rs = null;
        Statement stmt = null;
        try {
            // connectionPG.setAutoCommit(false);
            String targetTableName = targetTableBaseName + FOWNER_SUFFIX;
            String targetTempName = "tmp_" + targetTableName;
            logger.info("target table:" + targetTableName);
            stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(FETCH_CONNFDR);
            rs.setFetchSize(MAX_BATCHSIZE);
 
            createOrClearTempTargetTable(connectionPG, targetTempName,
                "(tid smallint not null, oid int not null, fowner smallint not null, flow varchar(20) not null)");
 
            StringBuilder sb = new StringBuilder();
 
            CopyManager cpMgr = ((PGConnection) connectionPG).getCopyAPI();
            PushbackReader reader = new PushbackReader(new StringReader(""), 10240);
 
            int count = 0;
            while (rs.next()) {
                int cid = rs.getInt(1);
                long oid = rs.getLong(2);
                int ownerId = rs.getInt(3);
                short dirId = (short) rs.getInt(4);
                String flowMark;
                ConnectivityDirectionEnum dir = ConnectivityDirectionEnum.convertShort(dirId);
                if ((ConnectivityDirectionEnum.ForwardflowON == dir) ||
                        (ConnectivityDirectionEnum.ForwardFixflowON == dir)) {
                    flowMark = FORWARDFLOW_MARK;
 
                } else if ((ConnectivityDirectionEnum.BackflowON == dir) ||
                        (ConnectivityDirectionEnum.BackFixflowON == dir)) {
                    flowMark = BACKFLOW_MARK;
                } else if (ConnectivityDirectionEnum.Nondeterminate == dir) {
                    flowMark = NONFLOW_MARK;
                } else {
                    flowMark = UNFLOW_MARK;
                }
 
                if (cid > Short.MAX_VALUE) {
                    logger.info("Wrong Connectivity Table:" + cid + "-" + oid);
                    continue;
                }
 
                sb.append(cid).append(',');
                sb.append(oid).append(',');
                sb.append(ownerId).append(',');
                sb.append(flowMark).append('\n');
 
                if (count % MAX_BATCHSIZE == 0) {
                    reader.unread(sb.toString().toCharArray());
                    cpMgr.copyIn("COPY " + targetTempName + " FROM STDIN WITH CSV", reader);
                    sb.delete(0, sb.length());
                }
                ++count;
            }
 
            reader.unread(sb.toString().toCharArray());
            cpMgr.copyIn("COPY " + targetTempName + " FROM STDIN WITH CSV", reader);
            createTargetTableIndexAndDropTemp(connectionPG, targetTableName, targetTempName, "tid, oid, fowner, flow");
 
            logger.info("Execute Copy Count=" + count);
        } catch (SQLException e) {
            logger.info(e.getMessage(), e);
            throw new IOException(e.getMessage(), e);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
            JDBCUtils.close(connectionPG, Transaction.AUTO_COMMIT, null);
        }
        return true;
    }
 
    private void convertPowerOwnerTheme(AbstractOracleJobContext context, String targetTableBaseName) throws IOException {
        if (context == null) {
            getLogger().info("jobContext is null in convertPowerOwnerTheme");
            return;
        }
        Connection connection = context.getOracleConnection();
        Connection connectionPG = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
 
        boolean found = false;
        ResultSet rs = null;
        Statement stmt = null;
        PreparedStatement pstmt = null;
        try {
            connectionPG.setAutoCommit(false);
            String targetTableName = targetTableBaseName + FOWNER_SUFFIX;
            logger.info("target table:" + targetTableName);
            stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(FETCH_CONNFDR);
            rs.setFetchSize(50);
 
            createOrClearTargetTable(connectionPG, targetTableName,
                "(tid smallint not null, oid int not null, fowner smallint not null, flow varchar(20) not null)");
 
            pstmt = connectionPG.prepareStatement("INSERT INTO " +
                encodeSchemaTableName(_pgSchema, targetTableName) +
                " (tid, oid, fowner, flow) VALUES (?, ?, ?, ?)" );
 
            final int MAX_BATCHSIZE = 50;
            int count = 0;
            while (rs.next()) {
                int cid = rs.getInt(1);
                long oid = rs.getLong(2);
                int ownerId = rs.getInt(3);
                short dirId = (short) rs.getInt(4);
                pstmt.setShort(1, (short) cid);
                pstmt.setInt(2, (int) oid);
                pstmt.setShort(3, (short) ownerId);
                ConnectivityDirectionEnum dir = ConnectivityDirectionEnum.convertShort(dirId);
                if ((ConnectivityDirectionEnum.ForwardflowON == dir) ||
                        (ConnectivityDirectionEnum.ForwardFixflowON == dir)) {
                    pstmt.setString(4, "shape://ccarrow");
 
                } else if ((ConnectivityDirectionEnum.BackflowON == dir) ||
                        (ConnectivityDirectionEnum.BackFixflowON == dir)) {
                    pstmt.setString(4, "shape://rccarrow");
                } else {
                    pstmt.setString(4, "shape://backslash");
                }
                pstmt.addBatch();
 
                if (count % MAX_BATCHSIZE == 0) {
                    pstmt.executeBatch();
                }
                ++count;
            }
 
            pstmt.executeBatch();
            createTargetTableIndex(connectionPG, targetTableName);
 
            logger.info("Execute Update Count=" + count);
        } catch (SQLException e) {
            logger.info(e.getMessage(), e);
            throw new IOException(e.getMessage(), e);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
            JDBCUtils.close(pstmt);
            JDBCUtils.close(connectionPG, Transaction.AUTO_COMMIT, null);
        }
    }
 
    private void createOrClearTargetTable(Connection connection, String tableName, String sql) throws SQLException {
        Statement stmt = connection.createStatement();
        ResultSet rs = null;
        try {
            rs = connection.getMetaData().getTables(null, _pgSchema, tableName, new String[]{"TABLE"});
            if (rs.next()) {
                stmt.execute("DROP TABLE " + encodeSchemaTableName(_pgSchema, tableName) + "CASCADE");
            }
 
            stmt.executeUpdate("CREATE TABLE " + encodeSchemaTableName(_pgSchema, tableName) + " " + sql);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
        }
    }
 
    private void createTargetTableIndex(Connection connection, String tableName) throws SQLException {
        Statement stmt = connection.createStatement();
        ResultSet rs = null;
        try {
            rs = connection.getMetaData().getTables(null, _pgSchema, tableName, new String[]{"TABLE"});
            if (rs.next()) {
                stmt.execute("ALTER TABLE " + encodeSchemaTableName(_pgSchema, tableName) +
                    " ADD PRIMARY KEY (tid, oid)");
            }
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
        }
    }
 
    private void createTargetTableIndexAndDropTemp(Connection connection, String tableName, String tempTable,
                                                   String fields) throws SQLException {
        Statement stmt = connection.createStatement();
        ResultSet rs = null;
        try {
            boolean found = false;
            rs = connection.getMetaData().getTables(null, _pgSchema, tableName, new String[]{"TABLE"});
            if (rs.next()) {
                found = true;
            }
            JDBCUtils.close(rs);
 
            if (!found) {
                stmt.execute("CREATE TABLE " + tableName +" AS SELECT * FROM " + tempTable);
                rs = connection.getMetaData().getTables(null, _pgSchema, tableName, new String[]{"TABLE"});
                if (rs.next()) {
                    stmt.execute("ALTER TABLE " + encodeSchemaTableName(_pgSchema, tableName) +
                        " ADD PRIMARY KEY (tid, oid)");
                }
            } else {
                stmt.execute("TRUNCATE "+ tableName + " CASCADE");
                stmt.execute("INSERT INTO " + tableName + "(" + fields + ") SELECT " +
                    fields + " FROM " + tempTable);
                /*
                --insert into xpwtheme1_fdyncolor (tid, oid, dyncolor) select tid, oid, dyncolor from xpwtheme2_fdyncolor;
                --reindex table xpwtheme1_fdyncolor;
                --alter table xpwtheme1_fdyncolor drop constraint xpwtheme1_fdyncolor_pkey;
                --alter table xpwtheme1_fdyncolor ADD PRIMARY KEY (tid, oid);
                 */
            }
            stmt.execute("DROP TABLE " + tempTable);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
        }
    }
 
    private void createOrClearTempTargetTable(Connection connection, String tableName, String sql) throws SQLException {
        Statement stmt = connection.createStatement();
        ResultSet rs = null;
        try {
            rs = connection.getMetaData().getTables(null, null, tableName, new String[]{"TABLE"});
            if (rs.next()) {
                stmt.execute("DROP TABLE " + encodeSchemaTableName(null, tableName) + "CASCADE");
            }
 
            stmt.executeUpdate("CREATE TEMP TABLE " + encodeSchemaTableName(null, tableName) + " " + sql);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
        }
    }
 
    private void convertDynamicColorTheme(AbstractOracleJobContext context, String targetTableBaseName) throws IOException {
        if (context == null) {
            getLogger().info("jobContext is null in convertDynamicColorTheme");
            return;
        }
        Connection connection = context.getOracleConnection();
        Connection connectionPG = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
 
        boolean found = false;
        ResultSet rs = null;
        Statement stmt = null;
        PreparedStatement pstmt = null;
        try {
 
            DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance();
            String targetTableName = targetTableBaseName + FDYNCOLOR_SUFFIX;
            logger.info("target table:" + targetTableName);
            stmt = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(FETCH_COLORTAB);
            rs.setFetchSize(50);
 
            createOrClearTargetTable(connectionPG, targetTableName,
                "(tid smallint not null, oid int not null, dyncolor varchar(10) not null)");
 
            pstmt = connectionPG.prepareStatement("INSERT INTO " +
                encodeSchemaTableName(_pgSchema, targetTableName) +
                " (tid, oid, dyncolor) VALUES (?, ?, ?)" );
 
            final int MAX_BATCHSIZE = 50;
            int count = 0;
            while (rs.next()) {
                int cid = rs.getInt(1);
                long oid = rs.getLong(2);
                int colorId = rs.getInt(3);
                String colorText = colorTable.getColorCode(colorId);
 
                pstmt.setShort(1, (short) cid);
                pstmt.setInt(2, (int) oid);
                pstmt.setString(3, colorText);
                pstmt.addBatch();
 
                if (count % MAX_BATCHSIZE == 0) {
                    pstmt.executeBatch();
                }
                ++count;
            }
 
            pstmt.executeBatch();
            createTargetTableIndex(connectionPG, targetTableName);
 
            logger.info("Execute Update Count=" + count);
        } catch (SQLException e) {
            logger.info(e.getMessage(), e);
            throw new IOException(e.getMessage(), e);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
            JDBCUtils.close(pstmt);
            JDBCUtils.close(connectionPG, Transaction.AUTO_COMMIT, null);
        }
    }
 
    private void updatePWThemeStatusToReady(String targetSchema) {
        if (targetDataStore == null) return;
        Connection connection = null;
        Statement stmt = null;
        ResultSet rs = null;
        boolean needCreate = false;
        try {
            StringBuilder sbSQL = new StringBuilder("UPDATE ");
            sbSQL.append(encodeSchemaTableName(_pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME)).append(' ');
            sbSQL.append(" SET vptstatus = ");
            sbSQL.append(DataReposVersionManager.VSSTATUS_READY);
            sbSQL.append(" , vpttimestamp = CURRENT_TIMESTAMP WHERE vptname = '");
            sbSQL.append(targetSchema).append("'");
 
            connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
            stmt = connection.createStatement();
            int count = stmt.executeUpdate(sbSQL.toString());
            if (count != 1) {
                logger.info("update status for " + targetSchema + " update result count="
                    + count);
            }
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
        } catch (IOException e) {
            logger.warn(e.getMessage(), e);
        } finally {
            JDBCUtils.close(rs);
            JDBCUtils.close(stmt);
            JDBCUtils.close(connection, Transaction.AUTO_COMMIT, null);
        }
    }
 
    private void createXPWThemeVersionTable(Connection connection, String pgSchema) throws SQLException {
        Statement stmt = null;
        StringBuilder sql = new StringBuilder("CREATE TABLE ");
        sql.append(encodeSchemaTableName(pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME));
        sql.append(" ( vptid serial PRIMARY KEY, ");
        sql.append(" vptname character varying(64) NOT NULL, ");
        sql.append(" vptstatus smallint NOT NULL, ");
        sql.append(" vpttimestamp timestamp with time zone ) ");
        try {
            stmt = connection.createStatement();
            stmt.executeUpdate(sql.toString());
 
            sql = new StringBuilder("ALTER TABLE ");
            sql.append(encodeSchemaTableName(pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME));
            sql.append(" OWNER TO ").append(_pgUsername);
            stmt.executeUpdate(sql.toString());
 
            sql = new StringBuilder("GRANT ALL ON TABLE ");
            sql.append(encodeSchemaTableName(pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME));
            sql.append(" TO public");
            stmt.executeUpdate(sql.toString());
 
            for (String schemaName : DataReposVersionManager.DEFAULTXPTVERSIONTABLE_NAMES) {
                sql = new StringBuilder("INSERT INTO ");
                sql.append(encodeSchemaTableName(pgSchema, DataReposVersionManager.XPTVERSIONTABLE_NAME));
                sql.append(" (vptname, vptstatus) VALUES ('");
                sql.append(schemaName).append("', ");
                sql.append(DataReposVersionManager.VSSTATUS_AVAILABLE).append(" )");
                stmt.executeUpdate(sql.toString());
            }
 
        } finally {
            if (stmt != null) stmt.close();
        }
    }
 
    protected void transferThemesVersionStatus(Connection connection,
                                       short vsstatusBefore, short vsstatusAfter, boolean exclusive) throws JobExecutionException {
 
        try {
            String currentTargetTheme = retrieveCurrentThemeName(connection, vsstatusBefore);
            if (currentTargetTheme == null) {
                logger.info("Cannot found target schema in dataStore. status=" + vsstatusBefore);
                return;
            }
            String existTargetSchema = null;
            if (exclusive)
                existTargetSchema = retrieveCurrentThemeName(connection, vsstatusAfter);
 
 
            updateCurrentThemeStatus(connection, currentTargetTheme, vsstatusAfter);
            if ((exclusive) && (existTargetSchema != null)) {
                updateCurrentThemeStatus(connection, existTargetSchema,
                    DataReposVersionManager.VSSTATUS_AVAILABLE);
            }
        } catch (SQLException e) {
            logger.warn(e.getMessage(), e);
            throw new JobExecutionException("Update " + DataReposVersionManager.XPTVERSIONTABLE_NAME +
                " has error-", e);
        }
    }
 
    protected void transferThemesVersionStatus(short vsstatusBefore, short vsstatusAfter, boolean exclusive) throws JobExecutionException {
 
        if (targetDataStore == null) return;
        Connection connection = null;
 
        try {
            connection = targetDataStore.getConnection(Transaction.AUTO_COMMIT);
 
            transferThemesVersionStatus(connection, vsstatusBefore, vsstatusAfter, exclusive);
        } catch (IOException e) {
            logger.warn(e.getMessage(), e);
        } finally {
            JDBCUtils.close(connection, Transaction.AUTO_COMMIT, null);
        }
    }
}