00001 /* 00002 * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM) 00003 * Copyright (C) 2004-2005 Philippe Delrieu 00004 * All rights reserved. 00005 * Contact: openmobileis@e-care.fr 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 00041 public class AtomicObjectArrayReplication implements Orderable { 00042 00043 00044 protected SynchroConflicResolver conflicResolver; 00045 00046 public AtomicObjectArrayReplication (SynchroConflicResolver conflicResolver) { 00047 this.conflicResolver = conflicResolver; 00048 } 00049 00053 public ReplicationReturnData replicates(SynchroAtomicObject[] listAOClient, SynchroAtomicObject[] listAOServ)throws SynchroException { 00054 ReplicationReturnData SAOEventList = new ReplicationReturnData(); 00055 try { 00056 if (listAOServ.length >1) { 00057 Sort.shellsort(listAOServ, this); 00058 } 00059 if (listAOClient.length >1) { 00060 Sort.shellsort(listAOClient, this); 00061 } 00062 00063 int comptServ = 0; 00064 int comptClient = 0; 00065 00066 while (comptServ < listAOServ.length && comptClient < listAOClient.length) { 00067 00068 SynchroAtomicObject serverAtomicObject = listAOServ[comptServ]; 00069 SynchroAtomicObject clientAtomicObject = listAOClient[comptClient]; 00070 00071 String serverUID = serverAtomicObject.getUID(); 00072 String clientUID = clientAtomicObject.getUID(); 00073 00074 if (serverUID.equals(clientUID)){ 00075 00076 // conflict 00077 ReplicationSAOEvent eventAO = null; 00078 if (serverAtomicObject.getModificationType() != SynchroAtomicObject.DELETE) { 00079 if ((clientAtomicObject.getModificationType() == SynchroAtomicObject.ADD)) { 00080 eventAO = conflicResolver.resolveAddandPresent(clientAtomicObject); 00081 } else if (clientAtomicObject.getModificationType() == SynchroAtomicObject.DELETE) { 00082 eventAO = conflicResolver.resolveDeleteandModified(clientAtomicObject); 00083 } else { 00084 eventAO = conflicResolver.resolveReplaceandModified(clientAtomicObject); 00085 } 00086 } else { 00087 eventAO = conflicResolver.resolveReplaceandDeleted(clientAtomicObject); 00088 } 00089 SAOEventList.addReplicationSAOEvent(eventAO); 00090 00091 comptServ++; 00092 comptClient++; 00093 } else if (serverUID.compareTo(clientUID) > 0) { 00094 00095 //gerer listAO2[comptClient] 00096 ReplicationSAOEvent eventAO = new ReplicationSAOEvent(clientUID); 00097 eventAO.setUpdateSide(ReplicationSAOEvent.CLIENT_SIDE); 00098 eventAO.setTypeModif(clientAtomicObject.getModificationType()); 00099 eventAO.setAobject(clientAtomicObject); 00100 SAOEventList.addReplicationSAOEvent(eventAO); 00101 00102 comptClient++; 00103 } else { 00104 00105 //gerer listAO1[comptServ] 00106 ReplicationSAOEvent eventAO = new ReplicationSAOEvent(serverUID); 00107 eventAO.setUpdateSide(ReplicationSAOEvent.SERVER_SIDE); 00108 eventAO.setTypeModif(serverAtomicObject.getModificationType()); 00109 eventAO.setAobject(serverAtomicObject); 00110 SAOEventList.addReplicationSAOEvent(eventAO); 00111 00112 comptServ++; 00113 } 00114 } 00115 00116 if (comptServ >= listAOServ.length) { 00117 if (comptClient < listAOClient.length) { 00118 for (; comptClient < listAOClient.length; comptClient++) { 00119 00120 SynchroAtomicObject clientAtomicObject = (SynchroAtomicObject)listAOClient[comptClient]; 00121 00122 ReplicationSAOEvent eventAO = new ReplicationSAOEvent(clientAtomicObject.getUID()); 00123 eventAO.setUpdateSide(ReplicationSAOEvent.CLIENT_SIDE); 00124 eventAO.setTypeModif(clientAtomicObject.getModificationType()); 00125 eventAO.setAobject(clientAtomicObject); 00126 SAOEventList.addReplicationSAOEvent(eventAO); 00127 } 00128 } 00129 } else { // firstTab not finished ==> second one is finished. 00130 for (; comptServ < listAOServ.length; comptServ++) { 00131 00132 SynchroAtomicObject serverAtomicObject = (SynchroAtomicObject)listAOServ[comptServ]; 00133 00134 ReplicationSAOEvent eventAO = new ReplicationSAOEvent(serverAtomicObject.getUID()); 00135 eventAO.setUpdateSide(ReplicationSAOEvent.SERVER_SIDE); 00136 eventAO.setTypeModif(serverAtomicObject.getModificationType()); 00137 eventAO.setAobject(serverAtomicObject); 00138 SAOEventList.addReplicationSAOEvent(eventAO); 00139 } 00140 } 00141 00142 00143 } catch (Throwable e) { 00144 throw new SynchroException(e); 00145 } 00146 return SAOEventList; 00147 } 00148 00149 00150 // < 0 if p1 < p2, 0 if equal, > 0 if p1 > p2 00151 public int compareTo(Object AO1, Object AO2) { 00152 String AoID1 = ((SynchroAtomicObject)AO1).getUID(); 00153 String AoID2 = ((SynchroAtomicObject)AO2).getUID(); 00154 00155 return AoID1.compareTo(AoID2); 00156 } 00157 00158 }