Subversion Repository Public Repository

FP_REPORT_SYNC_DB

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
package com.nru.test;

import java.sql.Connection;
import java.util.Vector;

public class MainClass {
	Utilities util = new Utilities();
	String query = "";
	Connection con_local = null;
	Connection con_remote = null;
	
	String sync_id = "";
	String last_visitor_id = "";
	String last_checkin_id = "";
	String new_visitor_id = "";
	String new_checkin_id = "";
	
	public MainClass() {
		Connection con_local = util.getConnection();
		Connection con_remote = util.getConnection_remote();
		
		this.con_local = con_local;
		this.con_remote = con_remote;
	}
	
	public void getLastSyncData(){
		// check t-sync_data table exist or not
		query = " select table_name from information_schema.tables where table_schema = 'bestray_infotech' and table_name = 't_sync_data' limit 1 ";
		String table_name = util.getField(query, con_remote);
		boolean table_exists = (table_name == null || table_name.length() <= 0 )? false : true ;
		
		if(!table_exists){
			System.out.println("--- table NOT found : t_sync_data ---");
			
			query = " CREATE TABLE t_sync_data(id bigint(20) NOT NULL AUTO_INCREMENT,last_visitor_id bigint(20) DEFAULT NULL,last_checkin_id bigint(20) DEFAULT NULL,PRIMARY KEY (id)) ";
			util.insertQuery(query, con_remote);
			System.out.println("--- table created successfully : t_sync_data ---");
			
			query = " insert into t_sync_data(last_visitor_id, last_checkin_id) values (0, 0 ) ";
			util.insertQuery(query, con_remote);
		}
		
		System.out.println("--- getLastSyncData() invoked ---");
		String checkin_id = "";
		query =" select id, last_visitor_id, last_checkin_id from t_sync_data order by id desc limit 1 ";
		Vector values = util.selectQuery(query, 3, con_remote);
		if(values.size() > 0){
			for (int i = 0; i < values.size(); i++) {
				Vector temp = (Vector) values.elementAt(i);
				sync_id			= ( temp.elementAt(0) == null || temp.elementAt(0).toString().length() <= 0 ) ? "0" : temp.elementAt(0).toString();
				last_visitor_id = ( temp.elementAt(1) == null || temp.elementAt(1).toString().length() <= 0 ) ? "0" : temp.elementAt(1).toString();
				last_checkin_id = ( temp.elementAt(2) == null || temp.elementAt(2).toString().length() <= 0 ) ? "0" : temp.elementAt(2).toString();
			}
		}else{
			System.out.println("--- No Sync Data Found ---");
			query = " insert into t_sync_data(last_visitor_id, last_checkin_id) values (0, 0 ) ";
			util.insertQuery(query, con_remote);
			
			query =" select id, last_visitor_id, last_checkin_id from t_sync_data order by id desc limit 1 ";
			values = util.selectQuery(query, 3, con_remote);
			for (int i = 0; i < values.size(); i++) {
				Vector temp = (Vector) values.elementAt(i);
				sync_id			= ( temp.elementAt(0) == null || temp.elementAt(0).toString().length() <= 0 ) ? "0" : temp.elementAt(0).toString();
				last_visitor_id = ( temp.elementAt(1) == null || temp.elementAt(1).toString().length() <= 0 ) ? "0" : temp.elementAt(1).toString();
				last_checkin_id = ( temp.elementAt(2) == null || temp.elementAt(2).toString().length() <= 0 ) ? "0" : temp.elementAt(2).toString();
			}
		}
		System.out.println("--- sync_id: "+sync_id+"  last_visitor_id: "+last_visitor_id+"  last_checkin_id: "+last_checkin_id);
		
		query = " DELETE FROM t_sync_data WHERE id != "+sync_id;
		util.insertQuery(query, con_remote);
	}
	
	public void syncData(){
		System.out.println("\n\n--- syncing VisitorData ---");
		if(last_visitor_id.trim().equalsIgnoreCase("0")){
			query =   " select id, visitor_id, visitor_type, student_id, student_name, visitor_fname, visitor_lname, visitor_dob, visitor_address, visitor_mobile, visitor_email, created_at, updated_at "
					+ " from t_visitor_master "
					+ " ";
		}else{
			query =   " select id, visitor_id, visitor_type, student_id, student_name, visitor_fname, visitor_lname, visitor_dob, visitor_address, visitor_mobile, visitor_email, created_at, updated_at "
					+ " from t_visitor_master "
					+ " where id > "+last_visitor_id;
		}
		
		Vector values = util.selectQuery(query, 13, con_local);
		if(values.size() > 0){
			for (int i = 0; i < values.size(); i++) {
				Vector rows = (Vector)values.elementAt(i);
				String _vid 			= (rows.elementAt(0)  == null)?"": rows.elementAt(0).toString();
				String visitor_id 		= (rows.elementAt(1)  == null)?"": rows.elementAt(1).toString();
				String visitor_type		= (rows.elementAt(2)  == null)?"": rows.elementAt(2).toString();
				String student_id		= (rows.elementAt(3)  == null)?"": rows.elementAt(3).toString();
				String student_name		= (rows.elementAt(4)  == null)?"": rows.elementAt(4).toString();
				String visitor_fname	= (rows.elementAt(5)  == null)?"": rows.elementAt(5).toString();
				String visitor_lname	= (rows.elementAt(6)  == null)?"": rows.elementAt(6).toString();
				String visitor_dob		= (rows.elementAt(7)  == null)?"": rows.elementAt(7).toString();
				String visitor_address	= (rows.elementAt(8)  == null)?"": rows.elementAt(8).toString();
				String visitor_mobile	= (rows.elementAt(9)  == null)?"": rows.elementAt(9).toString();
				String visitor_email	= (rows.elementAt(10) == null)?"": rows.elementAt(10).toString();
				String created_at		= (rows.elementAt(11) == null)?"": rows.elementAt(11).toString();
				String updated_at		= (rows.elementAt(12) == null)?"": rows.elementAt(12).toString();
				query = " insert into t_visitor_master (visitor_id, visitor_type, student_id, student_name, visitor_fname, visitor_lname, visitor_dob, visitor_address, visitor_mobile, visitor_email, created_at, updated_at) "
						+ " values ( "
						+ "'"+visitor_id+"', "
						+ "'"+visitor_type+"', "
						+ "'"+student_id+"', "
						+ "'"+student_name+"', "
						+ "'"+visitor_fname+"', "
						+ "'"+visitor_lname+"', "
						+ "'"+visitor_dob+"', "
						+ "'"+visitor_address+"', "
						+ "'"+visitor_mobile+"', "
						+ "'"+visitor_email+"', "
						+ "'"+created_at+"', "
						+ "'"+updated_at+"'  "
						+ " ) ";
				util.insertQuery(query, con_remote);
				new_visitor_id = _vid;
			}
			query = " update t_sync_data SET last_visitor_id = "+new_visitor_id+" where id =  "+sync_id;
			util.insertQuery(query, con_remote);
		}else{
			System.out.println("--- There is no changes in t_visitor_master data !!!");
		}
		
		
		System.out.println("\n\n--- syncing CheckinData ---");
		if(last_checkin_id.trim().equalsIgnoreCase("0")){
			query =   " select id, visitor_id,reason_for_visit_id, reason_for_visit, whom_to_meet_type, whom_to_meet_id, whom_to_meet_name, checkin_time, checkout_time, created_by, created_at "
					+ " from t_visitor_check_in_out "
					+ " ";
		}else{
			query =   " select id, visitor_id,reason_for_visit_id, reason_for_visit, whom_to_meet_type, whom_to_meet_id, whom_to_meet_name, checkin_time, checkout_time, created_by, created_at "
					+ " from t_visitor_check_in_out "
					+ " where id > "+last_checkin_id;
		}
		
		values = util.selectQuery(query, 11, con_local);
		if(values.size() > 0){
			for (int i = 0; i < values.size(); i++) {
				Vector rows = (Vector)values.elementAt(i);
				String _cid						= (rows.elementAt(0)   == null)?"" : rows.elementAt(0).toString(); 
				String visitor_id				= (rows.elementAt(1)   == null)?"" : rows.elementAt(1).toString();
				String reason_for_visit_id		= (rows.elementAt(2)   == null)?"" : rows.elementAt(2).toString();
				String reason_for_visit			= (rows.elementAt(3)   == null)?"" : rows.elementAt(3).toString();
				String whom_to_meet_type		= (rows.elementAt(4)   == null)?"" : rows.elementAt(4).toString();
				String whom_to_meet_id			= (rows.elementAt(5)   == null)?"0": rows.elementAt(5).toString();
				String whom_to_meet_name		= (rows.elementAt(6)   == null)?"" : rows.elementAt(6).toString();
				String checkin_time				= (rows.elementAt(7)   == null)?"" : rows.elementAt(7).toString();
				String checkout_time			= (rows.elementAt(8)   == null)?"" : rows.elementAt(8).toString();
				String created_by				= (rows.elementAt(9)   == null)?"0": rows.elementAt(9).toString();
				String created_at				= (rows.elementAt(10)  == null)?"" : rows.elementAt(10).toString();
				query = " insert into t_visitor_check_in_out (visitor_id,reason_for_visit_id, reason_for_visit, whom_to_meet_type, whom_to_meet_id, whom_to_meet_name, checkin_time, checkout_time, created_by, created_at) "
						+ " values ( "
						+ "'"+visitor_id+"', "
						+ "'"+reason_for_visit_id+"', "
						+ "'"+reason_for_visit+"', "
						+ "'"+whom_to_meet_type+"', "
						+ "'"+whom_to_meet_id+"', "
						+ "'"+whom_to_meet_name+"', "
						+ "'"+checkin_time+"', "
						+ "'"+checkout_time+"', "
						+ "'"+created_by+"', "
						+ "'"+created_at+"' "
						+ " ) ";
				util.insertQuery(query, con_remote);
				new_checkin_id = _cid;
			}
			query = " update t_sync_data SET last_checkin_id = "+new_checkin_id+" where id =  "+sync_id;
			util.insertQuery(query, con_remote);
		}else{
			System.out.println("--- There is no changes in t_visitor_check_in_out data !!!");
		}
		
	}
	
	public static void main(String[] args) {
		MainClass mainClass = new MainClass();
		mainClass.getLastSyncData();
		mainClass.syncData();
		System.out.println("\n\n--- DB sync successful !!! ---");
	}

}

Commits for FP_REPORT_SYNC_DB/src/com/nru/test/MainClass.java

Diff revisions: vs.
Revision Author Commited Message
1 lingaraj picture lingaraj Sat 24 Nov, 2018 12:32:58 +0000