KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > process > ReplicationRemote


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.process;
15
16 import javax.sql.*;
17 import java.math.BigDecimal JavaDoc;
18 import java.io.*;
19
20 import org.compiere.db.*;
21 import org.compiere.util.*;
22 import org.compiere.process.*;
23 import org.compiere.model.*;
24 import org.compiere.interfaces.*;
25 import java.sql.*;
26
27 /**
28  * Remote Data Replication.
29  * Note: requires migration technology
30  *
31  * @author Jorg Janke
32  * @version $Id: ReplicationRemote.java,v 1.5 2003/08/10 05:30:46 jjanke Exp $
33  */

34 public class ReplicationRemote extends SvrProcess
35 {
36     /**
37      * Constructor
38      */

39     public ReplicationRemote()
40     {
41         m_system = MSystem.get (getCtx());
42     } // ReplicationRemote
43

44     /** System Record */
45     private MSystem m_system = null;
46     /** Local Timestamp */
47     private Timestamp m_startDate = new Timestamp(System.currentTimeMillis());
48
49     /**
50      * Prepare - e.g., get Parameters.
51      */

52     public void prepare()
53     {
54         ProcessInfoParameter[] para = getParameter();
55         for (int i = 0; i < para.length; i++)
56         {
57             String JavaDoc name = para[i].getParameterName();
58             if (para[i].getParameter() == null)
59                 ;
60             else
61                 log.error("prepare - Unknown Parameter: " + name);
62         }
63     } // prepare
64

65     /**
66      * Perrform process.
67      * @return Message
68      * @throws Exception if not successful
69      */

70     public String JavaDoc doIt() throws Exception JavaDoc
71     {
72         ProcessInfo pi = getProcessInfo();
73         log.info("doIt - " + pi.getTitle());
74         if (pi.getSerializableObject() instanceof RemoteSetupVO)
75             return setupRemote();
76         else if (pi.getSerializableObject() instanceof RemoteMergeDataVO)
77             return mergeDataWithCentral();
78         else if (pi.getSerializableObject() instanceof RemoteUpdateVO)
79             return receiveUpdateFromCentral();
80         else if (pi.getSerializableObject() instanceof Timestamp)
81             return exit();
82         else
83             throw new Exception JavaDoc ("ReplicationRemote - unknown VO - " + pi.getSerializableObject());
84     } // doIt
85

86     /*************************************************************************/
87
88     /**
89      * Setup Remote AD_System/AD_Table/AD_Sequence for Remote Management.
90      * @return "" or error message
91      * @throws Exception
92      */

93     private String JavaDoc setupRemote() throws Exception JavaDoc
94     {
95         ProcessInfo pi = getProcessInfo();
96         RemoteSetupVO data = (RemoteSetupVO)pi.getSerializableObject();
97         log.info("setupRemote Start (" + pi + ") " + data);
98
99         RowSet rs = data.ReplicationTable;
100         try
101         {
102             // Update AD_System ****
103
if (data.IDRangeStart == null || data.IDRangeEnd == null)
104                 throw new Exception JavaDoc ("setupRemote - IDRange cannot be null");
105             if (!data.Test.booleanValue())
106             {
107                 setupRemoteAD_System (data.IDRangeStart, data.IDRangeEnd);
108                 // Update AD_Sequence ****
109
setupRemoteAD_Sequence (data.IDRangeStart);
110                 // Update DocNo Prefix/Suffix ****
111
setupRemoteC_DocType (data.AD_Client_ID, data.Prefix, data.Suffix);
112             }
113             // Update Tables ****
114
while (rs.next ())
115             {
116                 int AD_Table_ID = rs.getInt(1);
117                 String JavaDoc ReplicationType = rs.getString(2);
118                 String JavaDoc TableName = rs.getString(3);
119                 log.debug("setupRemote - " + TableName + " - " + ReplicationType);
120                 if (!data.Test.booleanValue())
121                     setupRemoteAD_Table(TableName, ReplicationType);
122             }
123             pi.setSummary("OK - Test=" + data.Test, false);
124         }
125         catch (Exception JavaDoc ex)
126         {
127             log.error("setupRemote", ex);
128             pi.setSummary(ex.toString(), true);
129         }
130         Object JavaDoc result = doIt(ReplicationLocal.START, "init", new Object JavaDoc[]{m_system});
131         if (result == null || !Boolean.TRUE.equals(result))
132             throw new Exception JavaDoc("setupRemote - Init Error - " + result);
133         pi.setSerializableObject(null);
134         pi.addLog(0,0, m_startDate, null, null);
135         log.info("setupRemote End (" + pi + ") " + data);
136         return "Remote SetupRemote OK";
137     } // setupRemote
138

139     /**
140      * Update Replication Type and ID Range of AD_System
141      * @param IDRangeStart start
142      * @param IDRangeEnd end
143      * @throws Exception if sql error
144      */

145     private void setupRemoteAD_System (BigDecimal JavaDoc IDRangeStart, BigDecimal JavaDoc IDRangeEnd) throws Exception JavaDoc
146     {
147         m_system.setIDRangeStart(IDRangeStart);
148         m_system.setIDRangeEnd(IDRangeEnd);
149         m_system.setReplicationType(MSystem.REPLICATIONTYPE_Merge);
150         m_system.save();
151     } // setupRemoteAD_System
152

153     /**
154      * Update StartNo/CurrentNext/CurrentNextSys in AD_Sequence
155      * @param IDRangeStart start
156      * @throws Exception if sql error
157      */

158     private void setupRemoteAD_Sequence (BigDecimal JavaDoc IDRangeStart) throws Exception JavaDoc
159     {
160         String JavaDoc sql = "UPDATE AD_Sequence SET StartNo = " + IDRangeStart
161             + " WHERE IsTableID='Y' AND StartNo < " + IDRangeStart;
162         int no = DB.executeUpdate(sql);
163         if (no == -1)
164             throw new Exception JavaDoc("setupRemoteAD_Sequence_Start");
165         //
166
sql = "UPDATE AD_Sequence SET CurrentNext = " + IDRangeStart
167             + " WHERE IsTableID='Y' AND CurrentNext < " + IDRangeStart;
168         no = DB.executeUpdate(sql);
169         if (no == -1)
170             throw new Exception JavaDoc("setupRemoteAD_Sequence_Next");
171         //
172
sql = "UPDATE AD_Sequence SET CurrentNextSys = -1"
173             + " WHERE IsTableID='Y' AND CurrentNextSys <> -1";
174         no = DB.executeUpdate(sql);
175         if (no == -1)
176             throw new Exception JavaDoc("setupRemoteAD_Sequence_Sys");
177     } // setupRemoteAD_Sequence
178

179     /**
180      * Update Document Prefix/Suffix
181      * @param AD_Client_ID client
182      * @param Prefix prefix
183      * @param Suffix suffix
184      * @throws Exception if sql error
185      */

186     private void setupRemoteC_DocType (int AD_Client_ID, String JavaDoc Prefix, String JavaDoc Suffix) throws Exception JavaDoc
187     {
188         if (Prefix == null)
189             Prefix = "";
190         if (Suffix == null)
191             Suffix = "";
192         // DocNoSequence_ID
193
String JavaDoc sql = "UPDATE AD_Sequence SET Prefix=" + DB.TO_STRING(Prefix) + ", Suffix=" + DB.TO_STRING(Suffix)
194             + " WHERE AD_Sequence_ID IN (SELECT DocNoSequence_ID FROM C_DocType"
195             + " WHERE AD_Client_ID=" + AD_Client_ID + " AND DocNoSequence_ID IS NOT NULL)";
196         int no = DB.executeUpdate(sql);
197         if (no == -1)
198             throw new Exception JavaDoc("setupRemoteC_DocType_DocNo");
199         // BatchNoSequence_ID
200
sql = "UPDATE AD_Sequence SET Prefix=" + DB.TO_STRING(Prefix) + ", Suffix=" + DB.TO_STRING(Suffix)
201             + " WHERE AD_Sequence_ID IN (SELECT BatchNoSequence_ID FROM C_DocType"
202             + " WHERE AD_Client_ID=" + AD_Client_ID + " AND BatchNoSequence_ID IS NOT NULL)";
203         no = DB.executeUpdate(sql);
204         if (no == -1)
205             throw new Exception JavaDoc("setupRemoteC_DocType_Batch");
206     } // setupRemoteC_DocType
207

208     /**
209      * Set ReplicationType of AD_Table
210      * @param TableName table name
211      * @param ReplicationType replication type
212      * @throws Exception if sql error
213      */

214     private void setupRemoteAD_Table(String JavaDoc TableName, String JavaDoc ReplicationType) throws Exception JavaDoc
215     {
216         String JavaDoc sql = "UPDATE AD_Table SET ReplicationType = '" + ReplicationType
217             + "' WHERE TableName='" + TableName + "' AND ReplicationType <> '" + ReplicationType + "'";
218         int no = DB.executeUpdate(sql);
219         if (no == -1)
220             throw new Exception JavaDoc("setupRemoteAD_Table");
221     } // setupRemoteAD_Table
222

223     /*************************************************************************/
224
225     /**
226      * Send new Data from Remote.
227      * Transcation Data changed
228      * @return info
229      * @throws Exception
230      */

231     private String JavaDoc mergeDataWithCentral() throws Exception JavaDoc
232     {
233         ProcessInfo pi = getProcessInfo();
234         RemoteMergeDataVO data = (RemoteMergeDataVO)pi.getSerializableObject();
235         log.info("mergeDataWithCentral Start (" + pi + ") " + data);
236         // Merge Data from Central
237
RowSet sourceRS = data.CentralData;
238         if (sourceRS == null)
239             log.error("mergeDataWithCentral - No Data - " + data);
240         else if (!data.Test.booleanValue())
241         {
242             RowSet targetRS = ReplicationLocal.getRowSet(data.Sql, null);
243             Object JavaDoc result = doIt (ReplicationLocal.START, "sync", new Object JavaDoc[] // Merge
244
{data.TableName, data.KeyColumns, sourceRS, targetRS, data.Test, Boolean.TRUE});
245             log.debug("receiveUpdateFromCentral - " + data.TableName + " - " + result);
246             pi.setSerializableObject(null);
247             boolean replicated = ReplicationLocal.isReplicated(result);
248             if (!replicated)
249                 pi.setError(true);
250             if (result != null)
251                 pi.addLog(0,null,null, result.toString());
252             if (Boolean.FALSE.equals(result))
253                 throw new Exception JavaDoc ("receiveUpdateFromCentral - " + data.TableName + " - " + result);
254         }
255
256         // Local Remote Data
257
RowSet rowset = ReplicationLocal.getRowSet(data.Sql, null);
258         // Result
259
pi.setSerializableObject((Serializable)rowset);
260         log.info("mergeDataWithCentral End (" + pi + ") " + data);
261         return "Remote MergeDataWithCentral - " + data.TableName;
262     } // sendNewDataToCentral
263

264     /*************************************************************************/
265
266     /**
267      * Receive Update from Central
268      * @return info
269      * @throws Exception
270      */

271     private String JavaDoc receiveUpdateFromCentral() throws Exception JavaDoc
272     {
273         ProcessInfo pi = getProcessInfo();
274         RemoteUpdateVO data = (RemoteUpdateVO)pi.getSerializableObject();
275         log.info("receiveUpdateFromCentral Start (" + pi + ") - " + data);
276         //
277
RowSet sourceRS = data.CentralData;
278         if (sourceRS == null)
279         {
280             log.error("receiveUpdateFromCentral - No Data - " + data);
281             pi.setSummary("NoData", true);
282         }
283         else if (!data.Test.booleanValue())
284         {
285             RowSet targetRS = ReplicationLocal.getRowSet(data.Sql, null);
286             Object JavaDoc result = doIt (ReplicationLocal.START, "sync", new Object JavaDoc[] // Sync
287
{data.TableName, data.KeyColumns, sourceRS, targetRS, data.Test, Boolean.FALSE});
288             log.debug("receiveUpdateFromCentral - " + data.TableName + " - " + result);
289             pi.setSerializableObject(null);
290             boolean replicated = ReplicationLocal.isReplicated(result);
291             if (!replicated)
292                 pi.setError(true);
293             if (result != null)
294                 pi.addLog(0,null,null, result.toString());
295             if (Boolean.FALSE.equals(result))
296                 throw new Exception JavaDoc ("receiveUpdateFromCentral - " + data.TableName + " - " + result);
297         }
298         //
299
pi.setSerializableObject(null);
300         log.info("receiveUpdateFromCentral End (" + pi + ") - " + data);
301         return "Remote Receive Update from Central OK";
302     } // receiveUpdateFromCentral
303

304     /*************************************************************************/
305
306     /**
307      * Clean up resources (connections)
308      * @return exit
309      * @throws Exception
310      */

311     private String JavaDoc exit() throws Exception JavaDoc
312     {
313         log.info ("exit");
314         ProcessInfo pi = getProcessInfo();
315         Object JavaDoc result = doIt(ReplicationLocal.START, "exit", null);
316         return "exit";
317     } // exit
318

319 } // ReplicationRemote
320
Popular Tags