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 public void setSendSynchroMetaData(Object metadata)throws OpenMSPException {
00091
00092 }
00093
00094
00095
00096
00097 public Object getCollectionObjectWithId(String id) throws OpenMSPException {
00098 String[] params = {id};
00099 BaseData data = null;
00100 try {
00101 ResultSet result = dbfactory.getBaseData(params);
00102 if (result.next()) {
00103 data = new BaseData(result.getString(1));
00104 data.setData(result.getString(2));
00105 }
00106 } catch (Throwable ex) {
00107 throw new OpenMSPException(ex);
00108 } finally {
00109
00110 ManagerDB.getManager().garbageOpenedConnection();;
00111 }
00112 return data;
00113 }
00114
00115
00116
00117
00118 public void updateCollectionObject(Object obj) throws OpenMSPException {
00119 BaseData data = (BaseData)obj;
00120
00121 try {
00122 Connection connection = dbfactory.getDbManager().getConnection();
00123 connection.setAutoCommit(false);
00124
00125 try {
00126 BaseData dbData = (BaseData)this.getCollectionObjectWithId(data.getId());
00127 if (dbData == null) {
00128 String[] params = {data.getId(), data.getData()};
00129 dbfactory.create(params);
00130 } else {
00131 String[] params = {data.getData(), data.getId()};
00132 dbfactory.update(params);
00133 }
00134 SynchroAtomicObjectManager.getManager().updateAtomicObject(new DefaultSynchroAtomicObject(data.getId(), ""), this.getCollectionName(), null);
00135
00136 connection.commit();
00137 } catch (Throwable ex) {
00138 connection.rollback();
00139 throw new OpenMSPException(ex);
00140 } finally {
00141 connection.setAutoCommit(true);
00142
00143 }
00144 } catch (SQLException ex) {
00145 throw new OpenMSPException(ex);
00146 }
00147 }
00148
00149
00150
00151
00152 public void deleteCollectionObject(String id) throws OpenMSPException {
00153 try {
00154 String[] params = {id};
00155 dbfactory.delete(params);
00156 } catch (Throwable ex) {
00157 throw new OpenMSPException(ex);
00158 }
00159
00160 }
00161
00162
00163
00164
00165 public SynchroConflicResolver getConflicResolver() {
00166
00167 return new AlwaysUpdateServerSynchroConflicResolver();
00168 }
00169
00170
00171
00172
00173 public Array getAllCollectionObject() throws OpenMSPException {
00174 Array ret = new Array();
00175 try {
00176 ResultSet result = dbfactory.getAllBaseData();
00177 while (!result.next()) {
00178 BaseData data = new BaseData(result.getString(1));
00179 data.setData(result.getString(2));
00180 ret.add(data);
00181 }
00182 } catch (Throwable ex) {
00183 throw new OpenMSPException(ex);
00184 } finally {
00185
00186 ManagerDB.getManager().garbageOpenedConnection();;
00187 }
00188 return ret;
00189 }
00190
00191
00192
00193
00194 public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long syncnumber) throws OpenMSPException {
00195 try {
00196 return SynchroAtomicObjectManager.getManager().getAllModifiedAtomicObjectForServiceSince(this.getCollectionName(), syncnumber, null);
00197 } catch (Throwable ex) {
00198 throw new OpenMSPException(ex);
00199 }
00200 }
00201
00202
00203
00204
00205 public int getUpdateMaxNbRow() {
00206 return 2;
00207 }
00208
00209
00210
00211
00212 public void updateSynchroDB(FastObjectDB db) throws OpenMSPException {
00213
00214 }
00215
00216
00217
00218
00219 public void connect(Credential cred) throws UserNotFoundException, ServiceException {
00220 }
00221
00222
00223
00224
00225 public void disconnect() {
00226 }
00227
00228 }