00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00083
00084
00085 public String getCollectionName() {
00086
00087 return "basedata";
00088 }
00089
00090
00091
00092
00093 public Object getCollectionObjectWithId(String id) throws OpenMSPException {
00094 String[] params = {id};
00095 BaseData data = null;
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
00106 ManagerDB.getManager().garbageOpenedConnection();;
00107 }
00108 return data;
00109 }
00110
00111
00112
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
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
00132 connection.commit();
00133 } catch (Throwable ex) {
00134 connection.rollback();
00135 throw new OpenMSPException(ex);
00136 } finally {
00137 connection.setAutoCommit(true);
00138
00139 }
00140 } catch (SQLException ex) {
00141 throw new OpenMSPException(ex);
00142 }
00143 }
00144
00145
00146
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
00156 }
00157
00158
00159
00160
00161 public SynchroConflicResolver getConflicResolver() {
00162
00163 return new AlwaysUpdateServerSynchroConflicResolver();
00164 }
00165
00166
00167
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
00182 ManagerDB.getManager().garbageOpenedConnection();;
00183 }
00184 return ret;
00185 }
00186
00187
00188
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
00199
00200
00201 public int getUpdateMaxNbRow() {
00202 return 2;
00203 }
00204
00205
00206
00207
00208 public void updateSynchroDB(FastObjectDB db) throws OpenMSPException {
00209
00210 }
00211
00212
00213
00214
00215 public void connect(Credential cred) throws UserNotFoundException, ServiceException {
00216 }
00217
00218
00219
00220
00221 public void disconnect() {
00222 }
00223
00224 }