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
00050
00051
00052
00053
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
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
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
00117
00118
00119 public String getCollectionName() {
00120 return "leads";
00121 }
00122
00123
00124
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
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
00149
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
00158 this.executeUpdate(MyCrmLeadsSynchroTarget.queryDeleteLeads, param);
00159
00160
00161
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
00177
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
00198
00199
00200 public SynchroConflicResolver getConflicResolver() {
00201 return new AlwaysUpdateServerSynchroConflicResolver();
00202 }
00203
00204
00205
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
00230
00231
00232 public String getCollectionObjectId(Object obj) {
00233 return ((Leads) obj).getId();
00234 }
00235
00236
00237
00238
00239 public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long date) throws OpenMSPException {
00240 if (date == 0) {
00241 SynchroAtomicObject[] sync = new SynchroAtomicObject[1];
00242 sync[0] = new DefaultSynchroAtomicObject("", "");
00243 return sync;
00244 }
00245 return new SynchroAtomicObject[0];
00246 }
00247
00248
00249
00250
00251 public int getUpdateMaxNbRow() {
00252 return 0;
00253 }
00254
00255
00256
00257
00258 public void updateSynchroDB(FastObjectDB db) throws OpenMSPException {
00259 }
00260
00261
00262
00263
00264 public void connect(Credential cred) throws UserNotFoundException, ServiceException {
00265
00266 }
00267
00268
00269
00270
00271 public void disconnect() {
00272
00273 }
00274
00275 }