DefaultSynchroAtomicObjectDelegate.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  *  
00023  *  Modifications :
00024  *  2006 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.synchro.algo.replication.utils;
00029 
00030 import org.openmobileis.common.util.collection.Array;
00031 import org.openmobileis.common.util.exception.DatabaseException;
00032 import org.openmobileis.common.util.exception.SynchroException;
00033 import java.sql.ResultSet;
00034 import org.openmobileis.common.util.exception.ServiceException;
00035 import org.openmobileis.common.util.log.*;
00036 import org.openmobileis.synchro.algo.replication.SynchroAtomicObject;
00037 import org.openmobileis.synchro.algo.replication.utils.impl.AtomicObjectQueryManager;
00038 import org.openmobileis.synchro.algo.syncnumber.SyncNumberManager;
00039 import org.openmobileis.synchro.openmsp.client.db.DBImportFileCoder;
00040 
00048 public class DefaultSynchroAtomicObjectDelegate implements SynchroAtomicObjectDelegate {
00049   protected AtomicObjectQueryManager queryManager;
00050 
00051         public DefaultSynchroAtomicObjectDelegate(AtomicObjectQueryManager query) {
00052                 queryManager = query;
00053                 try {
00054                         queryManager.createTable();
00055                 }  catch (Exception e) {
00056                         LogManager.traceCritique(LogServices.SERVERSERVICE, new ServiceException("Error init Syncho Atomic Object Manager. Exit System",e));
00057                 }
00058         }
00059 
00063   public SynchroAtomicObject[] getAllModifiedAtomicObjectForServiceSince(String serviceName, long date, String userLink) throws SynchroException{
00064     Array retList = new Array();
00065     try {
00066         if (userLink==null)     userLink = "";
00067       String[] uids = {serviceName, Long.toString(date), userLink};
00068       ResultSet result = queryManager.getAllModifiedAtomicObjectForServiceSince(uids);
00069       SynchroAtomicObject syncObject = null;
00070       while (result.next()) {
00071         syncObject = new DefaultSynchroAtomicObject(DBImportFileCoder.getCoder().removeAntiSlash(result.getString(1)), null, 0, result.getShort(2), result.getLong(3));
00072         retList.add(syncObject);
00073       }
00074     }  catch (java.sql.SQLException ex) {
00075       throw new SynchroException(ex);
00076     } catch (DatabaseException ex) {
00077       throw new SynchroException(ex);
00078     } finally {
00079                         queryManager.getDbManager().garbageOpenedConnection();
00080     }
00081     SynchroAtomicObject[] ret = new SynchroAtomicObject[retList.size()];
00082     retList.toArray(ret);
00083     return ret;
00084   }
00085 
00089   public SynchroAtomicObject getAtomicObject(String uid, String serviceName) throws SynchroException{
00090     try {
00091       String[] params = {DBImportFileCoder.getCoder().cleanAndFormatStringforDB(uid), serviceName};
00092       ResultSet result = queryManager.getAtomicObject(params);
00093       if(result.next()) {
00094         return new DefaultSynchroAtomicObject(DBImportFileCoder.getCoder().removeAntiSlash(result.getString(1)), result.getString(2), result.getLong(3), result.getShort(4), result.getLong(5));
00095       }
00096       
00097     }  catch (java.sql.SQLException ex) {
00098       throw new SynchroException(ex);
00099     } catch (DatabaseException ex) {
00100       throw new SynchroException(ex);
00101     } finally {
00102                         queryManager.getDbManager().garbageOpenedConnection();
00103     }
00104     return null;
00105   }
00106 
00107   public void updateAtomicObject(SynchroAtomicObject object, String serviceName, String userLink) throws SynchroException {
00108     try {
00109       if (object.getModificationType() == SynchroAtomicObject.ADD)  {
00110         object.setModificationType(SynchroAtomicObject.REPLACE);
00111       }
00112       long modifSyncNumbner = object.getModifSyncNumber();
00113       if (modifSyncNumbner == 0)  {
00114         //if not syncnumber is defined, for to the MAX sync number.
00115         //During the synchronisation, if not db modification is done the MAX sync number == the new synchro number.
00116         modifSyncNumbner = SyncNumberManager.getManager().getNextSynchroNumber().getSynchroNumber();
00117       }
00118          if (userLink==null)    userLink = "";
00119       String[] data = {Long.toString(modifSyncNumbner), object.getCheckSum(), Short.toString(object.getModificationType()), userLink, DBImportFileCoder.getCoder().cleanAndFormatStringforDB(object.getUID()), serviceName};
00120       queryManager.updateAtomicObject(data);
00121     } catch (DatabaseException ex) {
00122       throw new SynchroException(ex);
00123     }
00124   }
00125 
00126   public void createAtomicObject(SynchroAtomicObject object, String serviceName, String userLink) throws SynchroException {
00127     try {
00128                         if (userLink==null)     userLink = "";
00129       long createSyncNumbner = object.getModifSyncNumber();
00130       if (createSyncNumbner == 0)  {
00131         createSyncNumbner = SyncNumberManager.getManager().getNextSynchroNumber().getSynchroNumber();
00132       }
00133       String[] data = {DBImportFileCoder.getCoder().cleanAndFormatStringforDB(object.getUID()), serviceName, Long.toString(createSyncNumbner), object.getCheckSum(), Short.toString(SynchroAtomicObject.ADD), Long.toString(createSyncNumbner), userLink};
00134       queryManager.createAtomicObject(data);
00135     } catch (DatabaseException ex) {
00136       throw new SynchroException(ex);
00137     }
00138   }
00139 
00145   public void purgeAtomicObjcet(String serviceName, long deletedTime) throws SynchroException  {
00146     try {
00147       String[] data = {serviceName, Long.toString(deletedTime)};
00148       queryManager.purgeDeletedAtomicObject(data);
00149     } catch (DatabaseException ex) {
00150       throw new SynchroException(ex);
00151     }
00152   }
00153 
00154 }

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