TestFODBSyncTarget.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  */
00025 package org.openmobileis.database.fastobjectdb.db.test;
00026 
00027 import org.odbms.ObjectSet;
00028 import org.odbms.Query;
00029 import org.openmobileis.common.user.UserNotFoundException;
00030 import org.openmobileis.common.util.collection.Array;
00031 import org.openmobileis.common.util.exception.ServiceException;
00032 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00033 import org.openmobileis.database.fastobjectdb.FastObjectDBManager;
00034 import org.openmobileis.database.fastobjectdb.db.query.soda.FODBSodaObjectSet;
00035 import org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget;
00036 import org.openmobileis.synchro.algo.replication.AlwaysUpdateServerSynchroConflicResolver;
00037 import org.openmobileis.synchro.algo.replication.SynchroAtomicObject;
00038 import org.openmobileis.synchro.algo.replication.SynchroConflicResolver;
00039 import org.openmobileis.synchro.algo.replication.utils.DefaultSynchroAtomicObject;
00040 import org.openmobileis.synchro.openmsp.OpenMSPException;
00041 import org.openmobileis.synchro.security.auth.Credential;
00042 
00050 public final class TestFODBSyncTarget implements FODBSyncTarget {
00051   private AlwaysUpdateServerSynchroConflicResolver conflicResolver;
00052 
00056   public TestFODBSyncTarget() {
00057     super();
00058     conflicResolver = new AlwaysUpdateServerSynchroConflicResolver();
00059   }
00060 
00061   /* (non-Javadoc)
00062    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getCollectionName()
00063    */
00064   public String getCollectionName() {
00065      return "CLIENT";
00066   }
00067   
00068   public void setSendSynchroMetaData(Object metadata)throws OpenMSPException    {
00069     
00070   }
00071 
00072   /* (non-Javadoc)
00073    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getCollectionObjectWithId(java.lang.String)
00074    */
00075   public Object getCollectionObjectWithId(String id) throws OpenMSPException {
00076     try {
00077     Query q = FastObjectDBManager.getCurrentFODB().query();
00078     q.constrain("SERVER");
00079     Query q2 = q.descend("getKey()");
00080     q2.constrain(id).equal();
00081     ObjectSet set = q.execute();
00082     if (set.hasNext())  {
00083       return (TestData) set.next();
00084     }
00085     return null;
00086     } catch (Throwable ex)  {
00087       throw new OpenMSPException(ex);
00088     }
00089   }
00090   
00091   public Array getAllCollectionObject()  throws OpenMSPException  {
00092     try {
00093       Query q = FastObjectDBManager.getCurrentFODB().query();
00094       q.constrain("SERVER");
00095       q.descend("getKey()");
00096       ObjectSet set = q.execute();
00097       return ((FODBSodaObjectSet)set).getArrayFromSet();
00098       } catch (Throwable ex)  {
00099         throw new OpenMSPException(ex);
00100       }
00101   }
00102 
00103 
00104   /* (non-Javadoc)
00105    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#updateCollectionObject(java.lang.Object)
00106    */
00107   public void updateCollectionObject(Object obj) throws OpenMSPException {
00108     try {
00109       TestData test = (TestData)obj;
00110       Object dbobj = this.getCollectionObjectWithId(test.getKey());
00111       if (dbobj == null)  {
00112         FastObjectDBManager.getCurrentFODB().add("SERVER", test);
00113       } else  {
00114         FastObjectDBManager.getCurrentFODB().replace("SERVER", test);
00115       }
00116     } catch (Throwable ex)  {
00117       throw new OpenMSPException(ex);
00118     }
00119   }
00120 
00121   /* (non-Javadoc)
00122    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#deleteCollectionObject(java.lang.String)
00123    */
00124   public void deleteCollectionObject(String id) throws OpenMSPException {
00125     try {
00126       FastObjectDBManager.getCurrentFODB().deleteWithId("SERVER", id);
00127     } catch (Throwable ex)  {
00128       throw new OpenMSPException(ex);
00129     }
00130   }
00131 
00132   /* (non-Javadoc)
00133    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getConflicResolver()
00134    */
00135   public SynchroConflicResolver getConflicResolver() {
00136     return conflicResolver;
00137   }
00138 
00139   /* (non-Javadoc)
00140    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getAllModifiedAtomicObjectForServiceSince(long)
00141    */
00142   public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long syncnumber) throws OpenMSPException {
00143 //    Object obj = this.getCollectionObjectWithId("FIRSTS");
00144     try {
00145     SynchroAtomicObject[] ret = new SynchroAtomicObject[0];
00146     if (syncnumber == 0)  {  // add
00147       ret = new SynchroAtomicObject[1];
00148       ret[0] = new DefaultSynchroAtomicObject("FIRSTS", "");
00149       ret[0].setModificationType(SynchroAtomicObject.ADD);
00150     }else if (syncnumber == 1)  {  //replace
00151       TestData data = new TestData("FIRSTS");
00152       data.setToModify("1S");
00153       try {
00154         FastObjectDBManager.getCurrentFODB().replace("SERVER", data);
00155       } catch (Throwable ex)  {
00156         throw new OpenMSPException(ex);
00157       }
00158       ret = new SynchroAtomicObject[1];
00159       ret[0] = new DefaultSynchroAtomicObject("FIRSTS", "");
00160       ret[0].setModificationType(SynchroAtomicObject.REPLACE);
00161     }else if (syncnumber == 2)  { //delete
00162       try {
00163         FastObjectDBManager.getCurrentFODB().deleteWithId("SERVER", "FIRSTS");
00164       } catch (Throwable ex)  {
00165         throw new OpenMSPException(ex);
00166       }
00167       ret = new SynchroAtomicObject[1];
00168       ret[0] = new DefaultSynchroAtomicObject("FIRSTS", "");
00169       ret[0].setModificationType(SynchroAtomicObject.DELETE);
00170     }else if (syncnumber == 3)  { //complete
00171       TestData data = new TestData("THIRD");
00172       data.setToModify("0S");
00173       try {
00174         FastObjectDBManager.getCurrentFODB().add("SERVER", data);
00175       } catch (Throwable ex)  {
00176         throw new OpenMSPException(ex);
00177       }
00178       data = new TestData("FOURTH");
00179       data.setToModify("0S");
00180       try {
00181         FastObjectDBManager.getCurrentFODB().add("SERVER", data);
00182       } catch (Throwable ex)  {
00183         throw new OpenMSPException(ex);
00184       }
00185       ret = new SynchroAtomicObject[2];
00186       ret[0] = new DefaultSynchroAtomicObject("THIRD", "");
00187       ret[0].setModificationType(SynchroAtomicObject.ADD);
00188       ret[1] = new DefaultSynchroAtomicObject("FOURTH", "");
00189       ret[1].setModificationType(SynchroAtomicObject.ADD);
00190     }
00191     syncnumber++;
00192     return ret;
00193     } catch (Throwable ex)  {
00194       throw new OpenMSPException(ex);
00195     }
00196   }
00197 
00198   /* (non-Javadoc)
00199    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getUpdateMaxNbRow()
00200    */
00201   public int getUpdateMaxNbRow() {
00202     return 1;
00203   }
00204 
00205   /* (non-Javadoc)
00206    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#updateSynchroDB(org.openmobileis.database.fastobjectdb.FastObjectDB)
00207    */
00208   public void updateSynchroDB(FastObjectDB db) throws OpenMSPException {
00209   }
00210 
00211   /* (non-Javadoc)
00212    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#connect(org.openmobileis.synchro.security.auth.Credential)
00213    */
00214   public void connect(Credential cred) throws UserNotFoundException, ServiceException {
00215   }
00216 
00217   /* (non-Javadoc)
00218    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#disconnect()
00219    */
00220   public void disconnect() {
00221   }
00222 
00223 }

Generated on Mon Dec 4 11:03:31 2006 for OpenMobileIS by  doxygen 1.5.1-p1