Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

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   /* (non-Javadoc)
00091    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getCollectionObjectWithId(java.lang.String)
00092    */
00093   public Object getCollectionObjectWithId(String id) throws OpenMSPException {
00094       String[] params = {id};
00095       BaseData data = null;  //if return object is null, an error is generated by the synchro algo.
00096       try {
00097         ResultSet result = dbfactory.getBaseData(params);
00098         if (result.next()) {
00099           data = new BaseData(result.getString(1));
00100           data.setData(result.getString(2));
00101         }
00102       }  catch (Throwable ex) {
00103          throw new OpenMSPException(ex);
00104       } finally {
00105         //for select query the ManagerDB connection must be garbaged explicitly.
00106         ManagerDB.getManager().garbageOpenedConnection();;
00107       }
00108       return data;
00109   }
00110 
00111   /* (non-Javadoc)
00112    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#updateCollectionObject(java.lang.Object)
00113    */
00114   public void updateCollectionObject(Object obj) throws OpenMSPException {
00115     BaseData data = (BaseData)obj;
00116    
00117     try {
00118       Connection connection = dbfactory.getDbManager().getConnection();
00119       connection.setAutoCommit(false);
00120       // set manuel commit management to put in the same transaction db modication and SynchroAtomicObject update
00121       try {
00122         BaseData dbData = (BaseData)this.getCollectionObjectWithId(data.getId());
00123         if (dbData == null) {
00124           String[] params = {data.getId(), data.getData()};
00125           dbfactory.create(params);       
00126         } else  {
00127           String[] params = {data.getData(), data.getId()};
00128           dbfactory.update(params);       
00129         }        
00130         SynchroAtomicObjectManager.getManager().updateAtomicObject(new DefaultSynchroAtomicObject(data.getId(), ""), this.getCollectionName(), null);
00131         //user link is not use.
00132         connection.commit();
00133       } catch (Throwable ex)  {
00134         connection.rollback();
00135         throw new OpenMSPException(ex);
00136       } finally {
00137         connection.setAutoCommit(true);
00138         //for insert, update, delete query the ManagerDB connection is automaticly garbaged. Explicit garbage is not needed
00139       }
00140     }  catch (SQLException ex) {
00141        throw new OpenMSPException(ex);
00142     }
00143  }
00144 
00145   /* (non-Javadoc)
00146    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#deleteCollectionObject(java.lang.String)
00147    */
00148   public void deleteCollectionObject(String id) throws OpenMSPException {
00149     try {
00150         String[] params = {id};
00151         dbfactory.delete(params);       
00152     }  catch (Throwable ex) {
00153        throw new OpenMSPException(ex);
00154     }
00155     //for insert, update, delete query the ManagerDB connection is automaticly garbaged. Explicit garbage is not needed
00156   }
00157 
00158   /* (non-Javadoc)
00159    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getConflicResolver()
00160    */
00161   public SynchroConflicResolver getConflicResolver() {
00162     //if there is a conflic always update the server.
00163    return new AlwaysUpdateServerSynchroConflicResolver();
00164   }
00165 
00166   /* (non-Javadoc)
00167    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getAllCollectionObject()
00168    */
00169   public Array getAllCollectionObject() throws OpenMSPException {
00170     Array ret = new Array();
00171     try {
00172       ResultSet result = dbfactory.getAllBaseData();
00173       while (!result.next()) {
00174         BaseData data = new BaseData(result.getString(1));
00175         data.setData(result.getString(2));
00176         ret.add(data);
00177       }
00178     }  catch (Throwable ex) {
00179        throw new OpenMSPException(ex);
00180     } finally {
00181       //for select query the ManagerDB connection must be garbaged explicitly.
00182       ManagerDB.getManager().garbageOpenedConnection();;
00183     }
00184     return ret;
00185   }
00186 
00187   /* (non-Javadoc)
00188    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#getAllModifiedAtomicObjectSince(long)
00189    */
00190   public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long syncnumber) throws OpenMSPException {
00191     try {
00192       return SynchroAtomicObjectManager.getManager().getAllModifiedAtomicObjectForServiceSince(this.getCollectionName(), syncnumber, null);
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 2;
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     //do nothing because we have nothiing to change on the server side generated db.
00210   }
00211 
00212   /* (non-Javadoc)
00213    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#connect(org.openmobileis.synchro.security.auth.Credential)
00214    */
00215   public void connect(Credential cred) throws UserNotFoundException, ServiceException {
00216   }
00217 
00218   /* (non-Javadoc)
00219    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#disconnect()
00220    */
00221   public void disconnect() {
00222   }
00223 
00224 }

Generated on Mon Jul 10 10:29:29 2006 for OpenMobileIS by  doxygen 1.4.4