BaseDataFODBSyncTarget.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.examples.server.synchro;
00026 
00027 import java.sql.Connection;
00028 import java.sql.ResultSet;
00029 import java.sql.SQLException;
00030 
00031 import org.openmobileis.common.user.UserNotFoundException;
00032 import org.openmobileis.common.util.collection.Array;
00033 import org.openmobileis.common.util.database.ManagerDB;
00034 import org.openmobileis.common.util.exception.ServiceException;
00035 import org.openmobileis.common.util.log.LogManager;
00036 import org.openmobileis.common.util.log.LogServices;
00037 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00038 import org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget;
00039 import org.openmobileis.examples.server.database.JDBCBaseDataFactory;
00040 import org.openmobileis.examples.simpleappli.data.BaseData;
00041 import org.openmobileis.synchro.algo.replication.AlwaysUpdateServerSynchroConflicResolver;
00042 import org.openmobileis.synchro.algo.replication.SynchroAtomicObject;
00043 import org.openmobileis.synchro.algo.replication.SynchroConflicResolver;
00044 import org.openmobileis.synchro.algo.replication.utils.DefaultSynchroAtomicObject;
00045 import org.openmobileis.synchro.algo.replication.utils.SynchroAtomicObjectManager;
00046 import org.openmobileis.synchro.openmsp.OpenMSPException;
00047 import org.openmobileis.synchro.security.auth.Credential;
00048 
00067 public final class BaseDataFODBSyncTarget implements FODBSyncTarget {
00068   private JDBCBaseDataFactory dbfactory;
00072   public BaseDataFODBSyncTarget() {
00073     super();
00074     dbfactory = new JDBCBaseDataFactory();
00075     try {
00076       dbfactory.createSyncTable();
00077     }  catch (Throwable ex) {
00078       LogManager.traceError(LogServices.SERVERSERVICE, "Error during BaseData db creationr :"+ex.getMessage());
00079     }
00080   }
00081 
00082   /* (non-Javadoc)
00083    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getCollectionName()
00084    */
00085   public String getCollectionName() {
00086     // TODO Auto-generated method stub
00087     return "basedata";
00088   }
00089   
00090   public void setSendSynchroMetaData(Object metadata)throws OpenMSPException    {
00091     
00092   }
00093 
00094   /* (non-Javadoc)
00095    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getCollectionObjectWithId(java.lang.String)
00096    */
00097   public Object getCollectionObjectWithId(String id) throws OpenMSPException {
00098       String[] params = {id};
00099       BaseData data = null;  //if return object is null, an error is generated by the synchro algo.
00100       try {
00101         ResultSet result = dbfactory.getBaseData(params);
00102         if (result.next()) {
00103           data = new BaseData(result.getString(1));
00104           data.setData(result.getString(2));
00105         }
00106       }  catch (Throwable ex) {
00107          throw new OpenMSPException(ex);
00108       } finally {
00109         //for select query the ManagerDB connection must be garbaged explicitly.
00110         ManagerDB.getManager().garbageOpenedConnection();;
00111       }
00112       return data;
00113   }
00114 
00115   /* (non-Javadoc)
00116    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#updateCollectionObject(java.lang.Object)
00117    */
00118   public void updateCollectionObject(Object obj) throws OpenMSPException {
00119     BaseData data = (BaseData)obj;
00120    
00121     try {
00122       Connection connection = dbfactory.getDbManager().getConnection();
00123       connection.setAutoCommit(false);
00124       // set manuel commit management to put in the same transaction db modication and SynchroAtomicObject update
00125       try {
00126         BaseData dbData = (BaseData)this.getCollectionObjectWithId(data.getId());
00127         if (dbData == null) {
00128           String[] params = {data.getId(), data.getData()};
00129           dbfactory.create(params);       
00130         } else  {
00131           String[] params = {data.getData(), data.getId()};
00132           dbfactory.update(params);       
00133         }        
00134         SynchroAtomicObjectManager.getManager().updateAtomicObject(new DefaultSynchroAtomicObject(data.getId(), ""), this.getCollectionName(), null);
00135         //user link is not use.
00136         connection.commit();
00137       } catch (Throwable ex)  {
00138         connection.rollback();
00139         throw new OpenMSPException(ex);
00140       } finally {
00141         connection.setAutoCommit(true);
00142         //for insert, update, delete query the ManagerDB connection is automaticly garbaged. Explicit garbage is not needed
00143       }
00144     }  catch (SQLException ex) {
00145        throw new OpenMSPException(ex);
00146     }
00147  }
00148 
00149   /* (non-Javadoc)
00150    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#deleteCollectionObject(java.lang.String)
00151    */
00152   public void deleteCollectionObject(String id) throws OpenMSPException {
00153     try {
00154         String[] params = {id};
00155         dbfactory.delete(params);       
00156     }  catch (Throwable ex) {
00157        throw new OpenMSPException(ex);
00158     }
00159     //for insert, update, delete query the ManagerDB connection is automaticly garbaged. Explicit garbage is not needed
00160   }
00161 
00162   /* (non-Javadoc)
00163    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getConflicResolver()
00164    */
00165   public SynchroConflicResolver getConflicResolver() {
00166     //if there is a conflic always update the server.
00167    return new AlwaysUpdateServerSynchroConflicResolver();
00168   }
00169 
00170   /* (non-Javadoc)
00171    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getAllCollectionObject()
00172    */
00173   public Array getAllCollectionObject() throws OpenMSPException {
00174     Array ret = new Array();
00175     try {
00176       ResultSet result = dbfactory.getAllBaseData();
00177       while (!result.next()) {
00178         BaseData data = new BaseData(result.getString(1));
00179         data.setData(result.getString(2));
00180         ret.add(data);
00181       }
00182     }  catch (Throwable ex) {
00183        throw new OpenMSPException(ex);
00184     } finally {
00185       //for select query the ManagerDB connection must be garbaged explicitly.
00186       ManagerDB.getManager().garbageOpenedConnection();;
00187     }
00188     return ret;
00189   }
00190 
00191   /* (non-Javadoc)
00192    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getAllModifiedAtomicObjectSince(long)
00193    */
00194   public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long syncnumber) throws OpenMSPException {
00195     try {
00196       return SynchroAtomicObjectManager.getManager().getAllModifiedAtomicObjectForServiceSince(this.getCollectionName(), syncnumber, null);
00197     } catch (Throwable ex)  {
00198       throw new OpenMSPException(ex);
00199     }
00200   }
00201 
00202   /* (non-Javadoc)
00203    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getUpdateMaxNbRow()
00204    */
00205   public int getUpdateMaxNbRow() {
00206     return 2;
00207   }
00208 
00209   /* (non-Javadoc)
00210    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#updateSynchroDB(org.openmobileis.database.fastobjectdb.FastObjectDB)
00211    */
00212   public void updateSynchroDB(FastObjectDB db) throws OpenMSPException {
00213     //do nothing because we have nothiing to change on the server side generated db.
00214   }
00215 
00216   /* (non-Javadoc)
00217    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#connect(org.openmobileis.synchro.security.auth.Credential)
00218    */
00219   public void connect(Credential cred) throws UserNotFoundException, ServiceException {
00220   }
00221 
00222   /* (non-Javadoc)
00223    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#disconnect()
00224    */
00225   public void disconnect() {
00226   }
00227 
00228 }

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