00001 /* 00002 * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM) 00003 * Copyright (C) 2004-2006 Philippe Delrieu 00004 * All rights reserved. 00005 * Contact: pdelrieu@openmobileis.org 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00020 * USA 00021 * 00022 * Author : Philippe Delrieu 00023 * 00024 * Modifications : 00025 * 2004 Creation P.Delrieu 00026 * 2005 Modification F.Salque 00027 * 00028 */ 00029 00030 package org.openmobileis.modules.common.database.jdbc; 00031 00032 import java.sql.ResultSet; 00033 00034 import org.openmobileis.modules.common.data.TerminalUser; 00035 import org.openmobileis.synchro.algo.replication.AlwaysUpdateClientSynchroConflicResolver; 00036 import org.openmobileis.synchro.algo.replication.SynchroAtomicObject; 00037 import org.openmobileis.synchro.algo.replication.SynchroConflicResolver; 00038 import org.openmobileis.synchro.algo.replication.utils.DefaultSynchroAtomicObject; 00039 import org.openmobileis.synchro.openmsp.OpenMSPException; 00040 import org.openmobileis.synchro.security.auth.Credential; 00041 import org.openmobileis.common.context.SessionContext; 00042 import org.openmobileis.common.context.SessionContextManager; 00043 import org.openmobileis.common.user.UserNotFoundException; 00044 import org.openmobileis.common.util.collection.Array; 00045 import org.openmobileis.common.util.exception.ServiceException; 00046 import org.openmobileis.common.util.exception.SynchroException; 00047 import org.openmobileis.common.util.log.LogManager; 00048 import org.openmobileis.database.fastobjectdb.FastObjectDB; 00049 import org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget; 00050 00059 public class JDBCTerminalUserFODBSyncTarget implements FODBSyncTarget { 00060 00061 protected TerminalUserJDBCQuery query; 00062 00066 public JDBCTerminalUserFODBSyncTarget(TerminalUserJDBCQuery q) { 00067 super(); 00068 this.query = q; 00069 } 00070 00071 /* (non-Javadoc) 00072 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getCollectionName() 00073 */ 00074 public String getCollectionName() { 00075 return "terminaluser"; 00076 } 00077 00085 public void setSendSynchroMetaData(Object metadata)throws OpenMSPException { 00086 00087 } 00088 00089 /* (non-Javadoc) 00090 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getCollectionObjectWithId(java.lang.String) 00091 */ 00092 public Object getCollectionObjectWithId(String id) throws OpenMSPException { 00093 TerminalUser user = null; 00094 try { 00095 String[] param = new String[] { id }; 00096 ResultSet result = query.getTerminalUser(param); 00097 if (result.next()) { 00098 user = query.convertResultSetToTerminalUser(result); 00099 SessionContext context = SessionContextManager.getManager().getSessionContext(); 00100 String userId = context.getUserId(); 00101 if (userId != null && userId.equals(user.getTerminalUserId())) { 00102 user.setInstallTerminalUser(true); 00103 } 00104 } 00105 result.close(); 00106 } catch (Exception ex) { 00107 LogManager.traceError(0, ex); 00108 } finally { 00109 query.getDbManager().garbageOpenedConnection(); 00110 } 00111 return user; 00112 } 00113 00114 /* (non-Javadoc) 00115 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#updateCollectionObject(java.lang.Object) 00116 */ 00117 public void updateCollectionObject(Object obj) throws OpenMSPException { 00118 TerminalUser user = (TerminalUser) obj; 00119 try { 00120 //test because of trigger PB could not delete that does not exist in db 00121 TerminalUser dbrep = (TerminalUser) this.getCollectionObjectWithId(user.getTerminalUserId()); 00122 if (dbrep != null) { 00123 String[] param = query.getUpdateParamFromTerminalUser(user); 00124 query.updateTerminalUser(param); 00125 } else { 00126 String[] param = query.getInsertParamFromTerminalUser(user); 00127 query.insertTerminalUser(param); 00128 } 00129 this.notifyTerminalUserUpdate(user); 00130 } catch (Throwable ex) { 00131 throw new OpenMSPException(ex); 00132 } finally { 00133 query.getDbManager().garbageOpenedConnection(); 00134 } 00135 } 00136 00137 /* (non-Javadoc) 00138 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#deleteCollectionObject(java.lang.String) 00139 */ 00140 public void deleteCollectionObject(String id) throws OpenMSPException { 00141 try { 00142 //test because of trigger PB could not delete that does not exist in db 00143 TerminalUser dbuser = (TerminalUser) this.getCollectionObjectWithId(id); 00144 if (dbuser != null) { 00145 //select tache if exist 00146 String[] param = { id }; 00147 query.deleteTerminalUser(param); 00148 //update never delete because after each modification for complet synchro 00149 this.notifyTerminalUserdelete(id); 00150 } 00151 } catch (Throwable ex) { 00152 throw new OpenMSPException(ex); 00153 } finally { 00154 query.getDbManager().garbageOpenedConnection(); 00155 } 00156 } 00157 00158 /* (non-Javadoc) 00159 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getConflicResolver() 00160 */ 00161 public SynchroConflicResolver getConflicResolver() { 00162 return new AlwaysUpdateClientSynchroConflicResolver(); //PDA WIN always update server 00163 } 00164 00165 /* (non-Javadoc) 00166 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getAllCollectionObject() 00167 */ 00168 public Array getAllCollectionObject() throws OpenMSPException { 00169 Array idList = new Array(); 00170 try { 00171 SessionContext context = SessionContextManager.getManager().getSessionContext(); 00172 String userId = context.getUserId(); 00173 ResultSet result = query.getAllTerminalUsers(); 00174 while (result.next()) { 00175 TerminalUser user = query.convertResultSetToTerminalUser(result); 00176 if (userId != null && userId.equals(user.getTerminalUserId())) { 00177 user.setInstallTerminalUser(true); 00178 } 00179 idList.add(user); 00180 } 00181 result.close(); 00182 } catch (Exception ex) { 00183 LogManager.traceError(0, ex); 00184 } finally { 00185 query.getDbManager().garbageOpenedConnection(); 00186 } 00187 return idList; 00188 } 00189 00190 /* (non-Javadoc) 00191 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getCollectionObjectId(java.lang.Object) 00192 */ 00193 public String getCollectionObjectId(Object obj) { 00194 return ((TerminalUser) obj).getTerminalUserId(); 00195 } 00196 00197 /* (non-Javadoc) 00198 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getAllModifiedAtomicObjectForServiceSince(java.lang.String, long) 00199 */ 00200 public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long date) throws OpenMSPException { 00201 //always do a complete synchro 00202 SynchroAtomicObject[] sync = new SynchroAtomicObject[1]; 00203 sync[0] = new DefaultSynchroAtomicObject("", ""); 00204 return sync; 00205 } 00206 00207 /* (non-Javadoc) 00208 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getUpdateMaxNbRow() 00209 */ 00210 public int getUpdateMaxNbRow() { 00211 return 0; //start complete when there is 2 or more account modifiation 00212 } 00213 00214 /* (non-Javadoc) 00215 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#updateSynchroDB(org.openmobileis.database.fastobjectdb.FastObjectDB) 00216 */ 00217 public void updateSynchroDB(FastObjectDB db) throws OpenMSPException { 00218 } 00219 00220 /* (non-Javadoc) 00221 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#connect(org.openmobileis.synchro.security.auth.Credential) 00222 */ 00223 public void connect(Credential cred) throws UserNotFoundException, ServiceException { 00224 // do nothing 00225 } 00226 00227 /* (non-Javadoc) 00228 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#disconnect() 00229 */ 00230 public void disconnect() { 00231 // do nothing 00232 } 00233 00234 public void notifyTerminalUserUpdate(TerminalUser rep) throws SynchroException { 00235 00236 } 00237 00238 public void notifyTerminalUserdelete(String repId) throws SynchroException { 00239 00240 } 00241 00242 }