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
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
00090 public void setSendSynchroMetaData(Object metadata)throws OpenMSPException {
00091
00092 }
00093
00094
00095
00096
00097 private void createSyncTable() throws DatabaseException, java.sql.SQLException {
00098 try {
00099 Connection con = this.getDbManager().getConnection();
00100 DatabaseMetaData dbmd = con.getMetaData();
00101 String[] tableType = { "TABLE" };
00102 ResultSet result = dbmd.getTables(null, null, "ACCOUNT", tableType);
00103 if (!result.next()) {
00104 this.executeUpdate(MyCrmLeadsSynchroTarget.queryCreateLeadsTable, null);
00105
00106
00107 Calendar cal = Calendar.getInstance();
00108 cal.add(Calendar.MONTH, 1);
00109 long time = cal.getTimeInMillis();
00110 String date = Long.toString(time);
00111 String[] parameters = new String[] { "1111", "1111", "0", "250000", date, "Boat construction" };
00112 this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00113 parameters = new String[] { "2222", "1111", "0", "50000", date, "Boat transport" };
00114 this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00115 parameters = new String[] { "3333", "1111", "0", "100000", date, "Main store construction" };
00116 this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00117 parameters = new String[] { "4444", "2222", "0", "10000", date, "Server architecture" };
00118 this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00119 parameters = new String[] { "5555", "3333", "0", "150000", date, "Architecture update" };
00120 this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, parameters);
00121 }
00122 } finally {
00123 this.getDbManager().garbageOpenedConnection();
00124 }
00125 }
00126
00127
00128
00129
00130 public String getCollectionName() {
00131 return "leads";
00132 }
00133
00134
00135
00136
00137 public Object getCollectionObjectWithId(String id) throws OpenMSPException {
00138 try {
00139 Leads lead = null;
00140 String[] param = { id };
00141 ResultSet result = this.executeQuery(MyCrmLeadsSynchroTarget.queryGetLeads, param);
00142 if (result.next()) {
00143
00144 lead = new Leads(result.getString(1));
00145 lead.setIdaccount(result.getString(2));
00146 lead.setState(result.getInt(3));
00147 lead.setAmount(result.getInt(4));
00148 lead.setCloseDate(new Date(result.getLong(5)));
00149 lead.setDescription(result.getString(6));
00150 }
00151 return lead;
00152 } catch (Throwable ex) {
00153 throw new OpenMSPException(ex);
00154 } finally {
00155 this.getDbManager().garbageOpenedConnection();
00156 }
00157 }
00158
00159
00160
00161
00162 public void updateCollectionObject(Object obj) throws OpenMSPException {
00163 try {
00164 Leads lead = (Leads) obj;
00165 String[] param = { lead.getId() };
00166 this.getDbManager().getConnection().setAutoCommit(false);
00167 try {
00168
00169 this.executeUpdate(MyCrmLeadsSynchroTarget.queryDeleteLeads, param);
00170
00171
00172
00173 param = new String[] { lead.getId(), lead.getIdaccount(), Integer.toString(lead.getState()), Long.toString(lead.getCloseDate().getTime()), lead.getDescription() };
00174 this.executeUpdate(MyCrmLeadsSynchroTarget.queryInsertLeads, param);
00175 this.getDbManager().getConnection().commit();
00176 } finally {
00177 this.getDbManager().getConnection().setAutoCommit(true);
00178 }
00179 } catch (Throwable ex) {
00180 throw new OpenMSPException(ex);
00181 } finally {
00182 this.getDbManager().garbageOpenedConnection();
00183 }
00184
00185 }
00186
00187
00188
00189
00190 public void deleteCollectionObject(String id) throws OpenMSPException {
00191 try {
00192 String[] param = { id };
00193 this.getDbManager().getConnection().setAutoCommit(false);
00194 try {
00195 this.executeUpdate(MyCrmLeadsSynchroTarget.queryDeleteLeads, param);
00196
00197 this.getDbManager().getConnection().commit();
00198 } finally {
00199 this.getDbManager().getConnection().setAutoCommit(true);
00200 }
00201 } catch (Throwable ex) {
00202 throw new OpenMSPException(ex);
00203 } finally {
00204 this.getDbManager().garbageOpenedConnection();
00205 }
00206 }
00207
00208
00209
00210
00211 public SynchroConflicResolver getConflicResolver() {
00212 return new AlwaysUpdateServerSynchroConflicResolver();
00213 }
00214
00215
00216
00217
00218 public Array getAllCollectionObject() throws OpenMSPException {
00219
00220 Array ret = new Array();
00221 try {
00222 Array list = new Array();
00223 ResultSet result = this.executeQuery(MyCrmLeadsSynchroTarget.queryGetAllLeads, null);
00224 while (result.next()) {
00225 list.add(result.getString(1));
00226 }
00227
00228 for (int i = 0; i < list.size(); i++) {
00229 ret.add(this.getCollectionObjectWithId((String) list.get(i)));
00230 }
00231
00232 } catch (Throwable ex) {
00233 throw new OpenMSPException(ex);
00234 } finally {
00235 this.getDbManager().garbageOpenedConnection();
00236 }
00237 return ret;
00238 }
00239
00240
00241
00242
00243 public String getCollectionObjectId(Object obj) {
00244 return ((Leads) obj).getId();
00245 }
00246
00247
00248
00249
00250 public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long date) throws OpenMSPException {
00251 if (date == 0) {
00252 SynchroAtomicObject[] sync = new SynchroAtomicObject[1];
00253 sync[0] = new DefaultSynchroAtomicObject("", "");
00254 return sync;
00255 }
00256 return new SynchroAtomicObject[0];
00257 }
00258
00259
00260
00261
00262 public int getUpdateMaxNbRow() {
00263 return 0;
00264 }
00265
00266
00267
00268
00269 public void updateSynchroDB(FastObjectDB db) throws OpenMSPException {
00270 }
00271
00272
00273
00274
00275 public void connect(Credential cred) throws UserNotFoundException, ServiceException {
00276
00277 }
00278
00279
00280
00281
00282 public void disconnect() {
00283
00284 }
00285
00286 }