AtomicObjectArrayReplication.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 package org.openmobileis.synchro.algo.replication;
00029 
00030 import org.openmobileis.common.util.collection.Orderable;
00031 import org.openmobileis.common.util.collection.Sort;
00032 import org.openmobileis.common.util.exception.SynchroException;
00033 import org.openmobileis.common.util.log.LogManager;
00034 
00042 public class AtomicObjectArrayReplication implements Orderable {
00043         
00044         
00045         protected SynchroConflicResolver conflicResolver;
00046         
00047         public AtomicObjectArrayReplication (SynchroConflicResolver conflicResolver) {
00048                 this.conflicResolver = conflicResolver;
00049         }
00050         
00054         public ReplicationReturnData replicates(SynchroAtomicObject[] listAOClient, SynchroAtomicObject[] listAOServ)throws SynchroException {
00055     ReplicationReturnData SAOEventList = new ReplicationReturnData();
00056                 try {
00057                         if (listAOServ.length >1)       {
00058                                 Sort.shellsort(listAOServ, this);
00059                         }
00060                         if (listAOClient.length >1)     {
00061                                 Sort.shellsort(listAOClient, this);
00062                         }
00063                         
00064                         int comptServ = 0;
00065                         int comptClient = 0;
00066                         
00067                         while (comptServ < listAOServ.length && comptClient < listAOClient.length) {
00068                                 
00069                                 SynchroAtomicObject serverAtomicObject = listAOServ[comptServ];
00070                                 SynchroAtomicObject clientAtomicObject = listAOClient[comptClient];
00071                                 
00072                                 String serverUID = serverAtomicObject.getUID();
00073                                 String clientUID = clientAtomicObject.getUID();
00074         //                      LogManager.traceDebug(0, "AtomicObjectArrayReplication replicates PDA UID:"+serverUID);
00075         //                      LogManager.traceDebug(0, "AtomicObjectArrayReplication replicates server UID:"+clientUID);
00076                          
00077                                 if (serverUID.equals(clientUID)){
00078 //                                      LogManager.traceDebug(0, "AtomicObjectArrayReplication replicates equals");
00079                             
00080                                         // conflict
00081                                         ReplicationSAOEvent eventAO = null;
00082           if (clientAtomicObject.getModificationType() != SynchroAtomicObject.DELETE) {
00083             if ((serverAtomicObject.getModificationType() == SynchroAtomicObject.ADD))  {                                       
00084               eventAO = conflicResolver.resolveAddandPresent(serverAtomicObject);
00085             } else if (serverAtomicObject.getModificationType() == SynchroAtomicObject.DELETE) {
00086               eventAO = conflicResolver.resolveDeleteandModified(serverAtomicObject);
00087             } else  {
00088               eventAO = conflicResolver.resolveReplaceandModified(serverAtomicObject);
00089             }
00090           } else  {
00091             eventAO = conflicResolver.resolveReplaceandDeleted(serverAtomicObject);
00092           }                             
00093           SAOEventList.addReplicationSAOEvent(eventAO);
00094                                         
00095                                         comptServ++;
00096                                         comptClient++;
00097                                 } else if (serverUID.compareTo(clientUID) > 0) {
00098         //                              LogManager.traceDebug(0, "AtomicObjectArrayReplication replicates >0");
00099                             
00100                                         //gerer listAO2[comptClient]
00101                                         ReplicationSAOEvent eventAO = new ReplicationSAOEvent(clientUID);
00102                                         eventAO.setUpdateSide(ReplicationSAOEvent.CLIENT_SIDE);
00103                                         eventAO.setTypeModif(clientAtomicObject.getModificationType());
00104           eventAO.setAobject(clientAtomicObject);
00105           SAOEventList.addReplicationSAOEvent(eventAO);
00106                                         
00107                                         comptClient++;
00108                                 } else {
00109         //                              LogManager.traceDebug(0, "AtomicObjectArrayReplication replicates else");
00110                     
00111                                         //gerer listAO1[comptServ]
00112                                         ReplicationSAOEvent eventAO = new ReplicationSAOEvent(serverUID);
00113                                         eventAO.setUpdateSide(ReplicationSAOEvent.SERVER_SIDE);
00114                                         eventAO.setTypeModif(serverAtomicObject.getModificationType());
00115           eventAO.setAobject(serverAtomicObject);
00116           SAOEventList.addReplicationSAOEvent(eventAO);
00117                                         
00118                                         comptServ++;
00119                                 }
00120                         }
00121                         
00122                         if (comptServ >= listAOServ.length) {
00123         //                      LogManager.traceDebug(0, "AtomicObjectArrayReplication replicates comptServ >= listAOServ.length");
00124                         if (comptClient < listAOClient.length) {
00125                                         for (; comptClient < listAOClient.length; comptClient++) {
00126                                                 
00127                                                 SynchroAtomicObject clientAtomicObject = (SynchroAtomicObject)listAOClient[comptClient];
00128                                                 
00129                                                 ReplicationSAOEvent eventAO = new ReplicationSAOEvent(clientAtomicObject.getUID());
00130                                                 eventAO.setUpdateSide(ReplicationSAOEvent.CLIENT_SIDE);
00131                                                 eventAO.setTypeModif(clientAtomicObject.getModificationType());
00132             eventAO.setAobject(clientAtomicObject);
00133             SAOEventList.addReplicationSAOEvent(eventAO);
00134                                         }
00135                                 }
00136                         } else { // firstTab not finished ==> second one is finished.
00137         //                      LogManager.traceDebug(0, "AtomicObjectArrayReplication replicates comptServ < listAOServ.length");
00138                                 for (; comptServ < listAOServ.length; comptServ++) {
00139                                         
00140                                         SynchroAtomicObject serverAtomicObject = (SynchroAtomicObject)listAOServ[comptServ];
00141                                         
00142                                         ReplicationSAOEvent eventAO = new ReplicationSAOEvent(serverAtomicObject.getUID());
00143                                         eventAO.setUpdateSide(ReplicationSAOEvent.SERVER_SIDE);
00144                                         eventAO.setTypeModif(serverAtomicObject.getModificationType());
00145           eventAO.setAobject(serverAtomicObject);
00146           SAOEventList.addReplicationSAOEvent(eventAO);
00147                                 }
00148                         }
00149                         
00150                         
00151                 } catch (Throwable e) {
00152                         throw new SynchroException(e);
00153                 }
00154     return SAOEventList;
00155         }
00156 
00157   
00158         // < 0 if p1 < p2, 0 if equal, > 0 if p1 > p2
00159         public int compareTo(Object AO1, Object AO2) {
00160                 if (AO1 == null) return 1;
00161                 if (AO2 == null) return -1;
00162                 String AoID1 = ((SynchroAtomicObject)AO1).getUID();
00163                 String AoID2 = ((SynchroAtomicObject)AO2).getUID();
00164    
00165                 return AoID1.compareTo(AoID2);
00166         }
00167 
00168 }

Generated on Mon Jan 11 21:19:13 2010 for OpenMobileIS by  doxygen 1.5.4