1 14 package org.compiere.process; 15 16 import javax.sql.*; 17 import java.math.BigDecimal ; 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 34 public class ReplicationRemote extends SvrProcess 35 { 36 39 public ReplicationRemote() 40 { 41 m_system = MSystem.get (getCtx()); 42 } 44 45 private MSystem m_system = null; 46 47 private Timestamp m_startDate = new Timestamp(System.currentTimeMillis()); 48 49 52 public void prepare() 53 { 54 ProcessInfoParameter[] para = getParameter(); 55 for (int i = 0; i < para.length; i++) 56 { 57 String name = para[i].getParameterName(); 58 if (para[i].getParameter() == null) 59 ; 60 else 61 log.error("prepare - Unknown Parameter: " + name); 62 } 63 } 65 70 public String doIt() throws Exception 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 ("ReplicationRemote - unknown VO - " + pi.getSerializableObject()); 84 } 86 87 88 93 private String setupRemote() throws Exception 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 if (data.IDRangeStart == null || data.IDRangeEnd == null) 104 throw new Exception ("setupRemote - IDRange cannot be null"); 105 if (!data.Test.booleanValue()) 106 { 107 setupRemoteAD_System (data.IDRangeStart, data.IDRangeEnd); 108 setupRemoteAD_Sequence (data.IDRangeStart); 110 setupRemoteC_DocType (data.AD_Client_ID, data.Prefix, data.Suffix); 112 } 113 while (rs.next ()) 115 { 116 int AD_Table_ID = rs.getInt(1); 117 String ReplicationType = rs.getString(2); 118 String 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 ex) 126 { 127 log.error("setupRemote", ex); 128 pi.setSummary(ex.toString(), true); 129 } 130 Object result = doIt(ReplicationLocal.START, "init", new Object []{m_system}); 131 if (result == null || !Boolean.TRUE.equals(result)) 132 throw new Exception ("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 } 139 145 private void setupRemoteAD_System (BigDecimal IDRangeStart, BigDecimal IDRangeEnd) throws Exception 146 { 147 m_system.setIDRangeStart(IDRangeStart); 148 m_system.setIDRangeEnd(IDRangeEnd); 149 m_system.setReplicationType(MSystem.REPLICATIONTYPE_Merge); 150 m_system.save(); 151 } 153 158 private void setupRemoteAD_Sequence (BigDecimal IDRangeStart) throws Exception 159 { 160 String 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 ("setupRemoteAD_Sequence_Start"); 165 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 ("setupRemoteAD_Sequence_Next"); 171 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 ("setupRemoteAD_Sequence_Sys"); 177 } 179 186 private void setupRemoteC_DocType (int AD_Client_ID, String Prefix, String Suffix) throws Exception 187 { 188 if (Prefix == null) 189 Prefix = ""; 190 if (Suffix == null) 191 Suffix = ""; 192 String 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 ("setupRemoteC_DocType_DocNo"); 199 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 ("setupRemoteC_DocType_Batch"); 206 } 208 214 private void setupRemoteAD_Table(String TableName, String ReplicationType) throws Exception 215 { 216 String 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 ("setupRemoteAD_Table"); 221 } 223 224 225 231 private String mergeDataWithCentral() throws Exception 232 { 233 ProcessInfo pi = getProcessInfo(); 234 RemoteMergeDataVO data = (RemoteMergeDataVO)pi.getSerializableObject(); 235 log.info("mergeDataWithCentral Start (" + pi + ") " + data); 236 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 result = doIt (ReplicationLocal.START, "sync", new Object [] {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 ("receiveUpdateFromCentral - " + data.TableName + " - " + result); 254 } 255 256 RowSet rowset = ReplicationLocal.getRowSet(data.Sql, null); 258 pi.setSerializableObject((Serializable)rowset); 260 log.info("mergeDataWithCentral End (" + pi + ") " + data); 261 return "Remote MergeDataWithCentral - " + data.TableName; 262 } 264 265 266 271 private String receiveUpdateFromCentral() throws Exception 272 { 273 ProcessInfo pi = getProcessInfo(); 274 RemoteUpdateVO data = (RemoteUpdateVO)pi.getSerializableObject(); 275 log.info("receiveUpdateFromCentral Start (" + pi + ") - " + data); 276 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 result = doIt (ReplicationLocal.START, "sync", new Object [] {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 ("receiveUpdateFromCentral - " + data.TableName + " - " + result); 297 } 298 pi.setSerializableObject(null); 300 log.info("receiveUpdateFromCentral End (" + pi + ") - " + data); 301 return "Remote Receive Update from Central OK"; 302 } 304 305 306 311 private String exit() throws Exception 312 { 313 log.info ("exit"); 314 ProcessInfo pi = getProcessInfo(); 315 Object result = doIt(ReplicationLocal.START, "exit", null); 316 return "exit"; 317 } 319 } | Popular Tags |