00001 /* 00002 * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM) 00003 * Copyright (C) 2004-2005 Philippe Delrieu 00004 * All rights reserved. 00005 * Contact: openmobileis@e-care.fr 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.database.DatabaseException; 00035 import org.openmobileis.modules.crm.data.common.Representant; 00036 import org.openmobileis.modules.crm.data.common.RepresentantFactory; 00037 import org.openmobileis.common.context.SessionContext; 00038 import org.openmobileis.common.context.SessionContextManager; 00039 import org.openmobileis.common.util.collection.Array; 00040 import org.openmobileis.common.util.exception.ServiceException; 00041 import org.openmobileis.common.util.exception.SynchroException; 00042 import org.openmobileis.common.util.log.LogManager; 00043 00052 public abstract class JDBCRepresentantFactory implements RepresentantFactory { 00053 00054 protected RepresentantJDBCQuery query; 00058 public JDBCRepresentantFactory(RepresentantJDBCQuery q) { 00059 super(); 00060 this.query = q; 00061 } 00062 00063 public String[] getRepIdsList() { 00064 Array idList = new Array(); 00065 try { 00066 ResultSet result = query.getRepIdsList(); 00067 while (result.next()) { 00068 idList.add(result.getString(1)); 00069 } 00070 result.close(); 00071 } catch (Exception ex) { 00072 LogManager.traceError(0, ex); 00073 } finally { 00074 query.getDbManager().garbageOpenedConnection(); 00075 } 00076 String[] ids = new String[idList.size()]; 00077 idList.toArray(ids); 00078 return ids; 00079 } 00080 00081 public Representant getRepresentant(String id) { 00082 Representant rep = null; 00083 try { 00084 String[] param = new String[]{id}; 00085 ResultSet result = query.getRepresentant(param); 00086 if (result.next()) { 00087 rep = query.convertResultSetToRepresentant(result); 00088 } 00089 result.close(); 00090 } catch (Exception ex) { 00091 LogManager.traceError(0, ex); 00092 } finally { 00093 query.getDbManager().garbageOpenedConnection(); 00094 } 00095 return rep; 00096 } 00097 00098 public Representant getSelectedRepresentant() { 00099 SessionContext context = SessionContextManager.getManager().getSessionContext(); 00100 String repId = (String)context.getAttribute("userId"); 00101 return this.getRepresentant(repId); 00102 } 00103 00104 public void setSelectedRepresentant(String id) throws ServiceException, DatabaseException { 00105 //do nothing no way to change 00106 } 00107 00108 public Representant getInstallRepresentant() { 00109 return this.getSelectedRepresentant(); 00110 } 00111 00112 public void setInstallRepresentant(String id) throws ServiceException, DatabaseException { 00113 //do nothing no way to change 00114 } 00115 00116 public Representant[] getAllRepresentants() throws DatabaseException { 00117 Array idList = new Array(); 00118 try { 00119 ResultSet result = query.getAllRepresentants(); 00120 while (result.next()) { 00121 idList.add(query.convertResultSetToRepresentant(result)); 00122 } 00123 result.close(); 00124 } catch (Exception ex) { 00125 LogManager.traceError(0, ex); 00126 } finally { 00127 query.getDbManager().garbageOpenedConnection(); 00128 } 00129 Representant[] ids = new Representant[idList.size()]; 00130 idList.toArray(ids); 00131 return ids; 00132 } 00133 00137 public void addRepresentant(Representant rep) throws DatabaseException { 00138 try { 00139 //test because of trigger PB could not delete that does not exist in db 00140 Representant dbrep = this.getRepresentant(rep.getRepresentantId()); 00141 if (dbrep != null) { 00142 String[] param = query.getUpdateParamFromRepresentant(rep); 00143 query.updateRepresentant(param); 00144 } else { 00145 String[] param = query.getInsertParamFromRepresentant(rep); 00146 query.insertRepresentant(param); 00147 } 00148 this.notifyRepresentantUpdate(rep); 00149 } catch (Throwable ex) { 00150 throw new DatabaseException(ex); 00151 } finally { 00152 query.getDbManager().garbageOpenedConnection(); 00153 } 00154 } 00155 00156 public void removeRepresentant(String repId) throws DatabaseException { 00157 try { 00158 //test because of trigger PB could not delete that does not exist in db 00159 Representant dbrep = this.getRepresentant(repId); 00160 if (dbrep != null) { 00161 //select tache if exist 00162 String[] param = {repId}; 00163 query.deleteRepresentant(param); 00164 //update never delete because after each modification for complet synchro 00165 this.notifyRepresentantdelete(repId); 00166 } 00167 } catch (Throwable ex) { 00168 throw new DatabaseException(ex); 00169 } finally { 00170 query.getDbManager().garbageOpenedConnection(); 00171 } 00172 } 00173 00174 public abstract void notifyRepresentantUpdate(Representant rep) throws SynchroException; 00175 public abstract void notifyRepresentantdelete(String repId) throws SynchroException; 00176 00177 }