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

MyCrmLeadsSynchroTarget.java

00001 
00025 package org.openmobileis.examples.mycrm.data.jdbc;
00026 
00027 import java.sql.Connection;
00028 import java.sql.DatabaseMetaData;
00029 import java.sql.ResultSet;
00030 import java.util.Calendar;
00031 import java.util.Date;
00032 
00033 import org.openmobileis.common.user.UserNotFoundException;
00034 import org.openmobileis.common.util.collection.Array;
00035 import org.openmobileis.common.util.database.AbstractQueryManager;
00036 import org.openmobileis.common.util.exception.DatabaseException;
00037 import org.openmobileis.common.util.exception.ServiceException;
00038 import org.openmobileis.database.fastobjectdb.FastObjectDB;
00039 import org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget;
00040 import org.openmobileis.examples.mycrm.data.Leads;
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.openmsp.OpenMSPException;
00046 import org.openmobileis.synchro.security.auth.Credential;
00047 
00048 /*
00049  * Title:        OpenMobileIS project source <BR>
00050  * Description:
00051  * @author      Philippe Delrieu
00052  * @since       JDK 1.1
00053  * @version 1.0.
00054  */
00055 
00056 public final class MyCrmLeadsSynchroTarget extends AbstractQueryManager implements FODBSyncTarget {
00057 
00058   private static String queryCreateLeadsTable = "CREATE TABLE LEADS (" + " ID varchar(30) NOT NULL," + " IDCLIENT varchar(30) NOT NULL," + " STATE int ,"
00059       + " AMOUNT int ," + " DATE long ," + " DESCRIPTION varchar(150) NOT NULL," + " CONSTRAINT dataid PRIMARY KEY (ID)" + ")";
00060 
00061   private static String queryInsertLeads = "INSERT INTO LEADS (ID,IDCLIENT,STATE,AMOUNT,DATE,DESCRIPTION )" + " VALUES ('%0%', '%1%', '%2%', %3%, %4%, '%5%')";
00062 
00063   private static String queryUpdateLeads = "UPDATE LEADS SET IDCLIENT=%0%, STATE=%1%, AMOUNT =%2%, DATE =%3%, DESCRIPTION='%4%' " + " WHERE ID='%5%'";
00064 
00065   private static String queryDeleteLeads = "DELETE FROM LEADS where ID='%0%'";
00066 
00067   private static String queryGetLeads = "SELECT ID,IDCLIENT,STATE,AMOUNT,DATE,DESCRIPTION from LEADS where ID='%0%'";
00068   private static String queryGetAllLeads = "SELECT ID from LEADS where STATE=0";
00069 
00073   public MyCrmLeadsSynchroTarget() throws ServiceException {
00074     super();
00075     try {
00076       this.createSyncTable();
00077 
00078     } catch (Throwable ex) {
00079       throw new ServiceException(ex);
00080     }
00081   }
00082 
00083   /*
00084    * check if the sync table exists. IF it does not exist, create it
00085    */
00086   private void createSyncTable() throws DatabaseException, java.sql.SQLException {
00087     try {
00088       Connection con = this.getDbManager().getConnection();
00089       DatabaseMetaData dbmd = con.getMetaData();
00090       String[] tableType = { "TABLE" };
00091       ResultSet result = dbmd.getTables(null, null, "ACCOUNT", tableType);
00092       if (!result.next()) {
00093         this.executeUpdate(MyCrmLeadsSynchroTarget.queryCreateLeadsTable, null);
00094 
00095         //create default Leads ID,IDCLIENT,STATE,AMOUNT,DATE,DESCRIPTION
00096         Calendar cal = Calendar.getInstance();
00097         cal.add(Calendar.MONTH, 1);
00098         long time = cal.getTimeInMillis();
00099         String date = Long.toString(time);
00100         String[] parameters = new String[] { "1111", "1111", "0", "250000", date, "Boat construction" };
00101         this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00102         parameters = new String[] { "2222", "1111", "0", "50000", date, "Boat transport" };
00103         this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00104         parameters = new String[] { "3333", "1111", "0", "100000", date, "Main store construction" };
00105         this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00106         parameters = new String[] { "4444", "2222", "0", "10000", date, "Server architecture" };
00107         this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00108         parameters = new String[] { "5555", "3333", "0", "150000", date, "Architecture update" };
00109         this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00110       }
00111     } finally {
00112       this.getDbManager().garbageOpenedConnection();
00113     }
00114   }
00115 
00116   /* (non-Javadoc)
00117    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getCollectionName()
00118    */
00119   public String getCollectionName() {
00120     return "leads";
00121   }
00122 
00123   /* (non-Javadoc)
00124    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getCollectionObjectWithId(java.lang.String)
00125    */
00126   public Object getCollectionObjectWithId(String id) throws OpenMSPException {
00127     try {
00128       Leads lead = null;
00129       String[] param = { id };
00130       ResultSet result = this.executeQuery(MyCrmLeadsSynchroTarget.queryGetLeads, param);
00131       if (result.next()) {
00132         //ID,IDCLIENT,STATE,AMOUNT,DATE,DESCRIPTION 
00133         lead = new Leads(result.getString(1));
00134         lead.setIdaccount(result.getString(2));
00135         lead.setState(result.getInt(3));
00136         lead.setAmount(result.getInt(4));
00137         lead.setCloseDate(new Date(result.getLong(5)));
00138         lead.setDescription(result.getString(6));
00139        }
00140       return lead;
00141     } catch (Throwable ex) {
00142       throw new OpenMSPException(ex);
00143     } finally {
00144       this.getDbManager().garbageOpenedConnection();
00145     }
00146   }
00147 
00148   /* (non-Javadoc)
00149    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#updateCollectionObject(java.lang.Object)
00150    */
00151   public void updateCollectionObject(Object obj) throws OpenMSPException {
00152     try {
00153       Leads lead = (Leads) obj;
00154       String[] param = { lead.getId() };
00155       this.getDbManager().getConnection().setAutoCommit(false);
00156       try {
00157         //first delete all
00158         this.executeUpdate(MyCrmLeadsSynchroTarget.queryDeleteLeads, param);
00159  
00160         //create all
00161         //ID,IDCLIENT,STATE,AMOUNT,DATE,DESCRIPTION
00162         param = new String[] { lead.getId(), lead.getIdaccount(), Integer.toString(lead.getState()), Long.toString(lead.getCloseDate().getTime()), lead.getDescription() };
00163         this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, param);
00164         this.getDbManager().getConnection().commit();
00165       } finally {
00166         this.getDbManager().getConnection().setAutoCommit(true);
00167       }
00168     } catch (Throwable ex) {
00169       throw new OpenMSPException(ex);
00170     } finally {
00171       this.getDbManager().garbageOpenedConnection();
00172     }
00173 
00174   }
00175 
00176   /* (non-Javadoc)
00177    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#deleteCollectionObject(java.lang.String)
00178    */
00179   public void deleteCollectionObject(String id) throws OpenMSPException {
00180     try {
00181       String[] param = { id };
00182       this.getDbManager().getConnection().setAutoCommit(false);
00183       try {
00184         this.executeUpdate(MyCrmLeadsSynchroTarget.queryDeleteLeads, param);
00185 
00186         this.getDbManager().getConnection().commit();
00187       } finally {
00188         this.getDbManager().getConnection().setAutoCommit(true);
00189       }
00190     } catch (Throwable ex) {
00191       throw new OpenMSPException(ex);
00192     } finally {
00193       this.getDbManager().garbageOpenedConnection();
00194     }
00195   }
00196 
00197   /* (non-Javadoc)
00198    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getConflicResolver()
00199    */
00200   public SynchroConflicResolver getConflicResolver() {
00201     return new AlwaysUpdateServerSynchroConflicResolver(); //PDA WIN always update server
00202   }
00203 
00204   /* (non-Javadoc)
00205    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getAllCollectionObject()
00206    */
00207   public Array getAllCollectionObject() throws OpenMSPException {
00208 
00209     Array ret = new Array();
00210     try {
00211       Array list = new Array();
00212       ResultSet result = this.executeQuery(MyCrmLeadsSynchroTarget.queryGetAllLeads, null);
00213       while (result.next()) {
00214         list.add(result.getString(1));
00215       }
00216 
00217       for (int i = 0; i < list.size(); i++) {
00218         ret.add(this.getCollectionObjectWithId((String) list.get(i)));
00219       }
00220 
00221     } catch (Throwable ex) {
00222       throw new OpenMSPException(ex);
00223     } finally {
00224       this.getDbManager().garbageOpenedConnection();
00225     }
00226     return ret;
00227   }
00228 
00229   /* (non-Javadoc)
00230    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getCollectionObjectId(java.lang.Object)
00231    */
00232   public String getCollectionObjectId(Object obj) {
00233     return ((Leads) obj).getId();
00234   }
00235 
00236   /* (non-Javadoc)
00237    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getAllModifiedAtomicObjectForServiceSince(java.lang.String, long)
00238    */
00239   public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long date) throws OpenMSPException {
00240     if (date == 0) { //synchronize only during complete synchro
00241       SynchroAtomicObject[] sync = new SynchroAtomicObject[1];
00242       sync[0] = new DefaultSynchroAtomicObject("", "");
00243       return sync;
00244     }
00245     return new SynchroAtomicObject[0];
00246   }
00247 
00248   /* (non-Javadoc)
00249    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getUpdateMaxNbRow()
00250    */
00251   public int getUpdateMaxNbRow() {
00252     return 0;
00253   }
00254 
00255   /* (non-Javadoc)
00256    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#updateSynchroDB(org.openmobileis.database.fastobjectdb.FastObjectDB)
00257    */
00258   public void updateSynchroDB(FastObjectDB db) throws OpenMSPException {
00259   }
00260 
00261   /* (non-Javadoc)
00262    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#connect(org.openmobileis.synchro.security.auth.Credential)
00263    */
00264   public void connect(Credential cred) throws UserNotFoundException, ServiceException {
00265     // do nothing
00266   }
00267 
00268   /* (non-Javadoc)
00269    * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#disconnect()
00270    */
00271   public void disconnect() {
00272     // do nothing
00273   }
00274 
00275 }

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