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.crm.database.common.jdbc; 00031 00032 import java.sql.ResultSet; 00033 00034 import org.openmobileis.modules.crm.data.common.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 00078 /* (non-Javadoc) 00079 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getCollectionObjectWithId(java.lang.String) 00080 */ 00081 public Object getCollectionObjectWithId(String id) throws OpenMSPException { 00082 TerminalUser user = null; 00083 try { 00084 String[] param = new String[] { id }; 00085 ResultSet result = query.getTerminalUser(param); 00086 if (result.next()) { 00087 user = query.convertResultSetToTerminalUser(result); 00088 SessionContext context = SessionContextManager.getManager().getSessionContext(); 00089 String userId = context.getUserId(); 00090 if (userId != null && userId.equals(user.getTerminalUserId())) { 00091 user.setInstallTerminalUser(true); 00092 } 00093 } 00094 result.close(); 00095 } catch (Exception ex) { 00096 LogManager.traceError(0, ex); 00097 } finally { 00098 query.getDbManager().garbageOpenedConnection(); 00099 } 00100 return user; 00101 } 00102 00103 /* (non-Javadoc) 00104 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#updateCollectionObject(java.lang.Object) 00105 */ 00106 public void updateCollectionObject(Object obj) throws OpenMSPException { 00107 TerminalUser user = (TerminalUser) obj; 00108 try { 00109 //test because of trigger PB could not delete that does not exist in db 00110 TerminalUser dbrep = (TerminalUser) this.getCollectionObjectWithId(user.getTerminalUserId()); 00111 if (dbrep != null) { 00112 String[] param = query.getUpdateParamFromTerminalUser(user); 00113 query.updateTerminalUser(param); 00114 } else { 00115 String[] param = query.getInsertParamFromTerminalUser(user); 00116 query.insertTerminalUser(param); 00117 } 00118 this.notifyTerminalUserUpdate(user); 00119 } catch (Throwable ex) { 00120 throw new OpenMSPException(ex); 00121 } finally { 00122 query.getDbManager().garbageOpenedConnection(); 00123 } 00124 } 00125 00126 /* (non-Javadoc) 00127 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#deleteCollectionObject(java.lang.String) 00128 */ 00129 public void deleteCollectionObject(String id) throws OpenMSPException { 00130 try { 00131 //test because of trigger PB could not delete that does not exist in db 00132 TerminalUser dbuser = (TerminalUser) this.getCollectionObjectWithId(id); 00133 if (dbuser != null) { 00134 //select tache if exist 00135 String[] param = { id }; 00136 query.deleteTerminalUser(param); 00137 //update never delete because after each modification for complet synchro 00138 this.notifyTerminalUserdelete(id); 00139 } 00140 } catch (Throwable ex) { 00141 throw new OpenMSPException(ex); 00142 } finally { 00143 query.getDbManager().garbageOpenedConnection(); 00144 } 00145 } 00146 00147 /* (non-Javadoc) 00148 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getConflicResolver() 00149 */ 00150 public SynchroConflicResolver getConflicResolver() { 00151 return new AlwaysUpdateClientSynchroConflicResolver(); //PDA WIN always update server 00152 } 00153 00154 /* (non-Javadoc) 00155 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getAllCollectionObject() 00156 */ 00157 public Array getAllCollectionObject() throws OpenMSPException { 00158 Array idList = new Array(); 00159 try { 00160 SessionContext context = SessionContextManager.getManager().getSessionContext(); 00161 String userId = context.getUserId(); 00162 ResultSet result = query.getAllTerminalUsers(); 00163 while (result.next()) { 00164 TerminalUser user = query.convertResultSetToTerminalUser(result); 00165 if (userId != null && userId.equals(user.getTerminalUserId())) { 00166 user.setInstallTerminalUser(true); 00167 } 00168 idList.add(user); 00169 } 00170 result.close(); 00171 } catch (Exception ex) { 00172 LogManager.traceError(0, ex); 00173 } finally { 00174 query.getDbManager().garbageOpenedConnection(); 00175 } 00176 return idList; 00177 } 00178 00179 /* (non-Javadoc) 00180 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getCollectionObjectId(java.lang.Object) 00181 */ 00182 public String getCollectionObjectId(Object obj) { 00183 return ((TerminalUser) obj).getTerminalUserId(); 00184 } 00185 00186 /* (non-Javadoc) 00187 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getAllModifiedAtomicObjectForServiceSince(java.lang.String, long) 00188 */ 00189 public SynchroAtomicObject[] getAllModifiedAtomicObjectSince(long date) throws OpenMSPException { 00190 //always do a complete synchro 00191 SynchroAtomicObject[] sync = new SynchroAtomicObject[1]; 00192 sync[0] = new DefaultSynchroAtomicObject("", ""); 00193 return sync; 00194 } 00195 00196 /* (non-Javadoc) 00197 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#getUpdateMaxNbRow() 00198 */ 00199 public int getUpdateMaxNbRow() { 00200 return 0; //start complete when there is 2 or more account modifiation 00201 } 00202 00203 /* (non-Javadoc) 00204 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncService#updateSynchroDB(org.openmobileis.database.fastobjectdb.FastObjectDB) 00205 */ 00206 public void updateSynchroDB(FastObjectDB db) throws OpenMSPException { 00207 } 00208 00209 /* (non-Javadoc) 00210 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#connect(org.openmobileis.synchro.security.auth.Credential) 00211 */ 00212 public void connect(Credential cred) throws UserNotFoundException, ServiceException { 00213 // do nothing 00214 } 00215 00216 /* (non-Javadoc) 00217 * @see org.openmobileis.database.fastobjectdb.synchro.server.FODBSyncTarget#disconnect() 00218 */ 00219 public void disconnect() { 00220 // do nothing 00221 } 00222 00223 public void notifyTerminalUserUpdate(TerminalUser rep) throws SynchroException { 00224 00225 } 00226 00227 public void notifyTerminalUserdelete(String repId) throws SynchroException { 00228 00229 } 00230 00231 }