Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

JDBCLabelFactory.java

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 import java.util.HashMap;
00034 
00035 import org.openmobileis.database.DatabaseException;
00036 import org.openmobileis.common.util.exception.ServiceException;
00037 import org.openmobileis.common.util.exception.SynchroException;
00038 import org.openmobileis.common.util.log.LogManager;
00039 import org.openmobileis.common.util.log.LogServices;
00040 import org.openmobileis.modules.crm.data.common.Label;
00041 import org.openmobileis.modules.crm.data.common.LabelFactory;
00042 import org.openmobileis.modules.crm.data.common.LabelManager;
00043 import org.openmobileis.common.util.collection.Array;
00044 
00045 
00054 public abstract class JDBCLabelFactory implements LabelFactory {
00055         protected LabelJDBCQuery query;
00056         private HashMap categoriesMap;
00060         public JDBCLabelFactory(LabelJDBCQuery q) {
00061                 super();
00062                 query = q;
00063                 this.initLabelDB();
00064                 this.loadLabelList();
00065         }
00066   
00067         private void loadLabelList()    {
00068                 try     {
00069                         categoriesMap = new HashMap(10);
00070                         String[][] catList = this.getCategoriesList();
00071                         Label l = null;
00072                         for (int i=0; i<catList.length; i++)    {
00073                                 HashMap labelMap = new HashMap(20);
00074                                 categoriesMap.put(catList[i][1], labelMap);
00075                                 Array labelList = this.getDBLabelListForCategorie(catList[i][1]);
00076                                 for (int j=0; j<labelList.size(); j++)  {
00077                                         l = (Label)labelList.get(j);
00078                                         labelMap.put(l.getLabelId(), l);
00079                                 }
00080                         }
00081                 } catch (Exception ex)  {
00082                         LogManager.traceError(0, ex);
00083                 }
00084         }
00085         
00086         public Array getLabelListForCategorie(String categorie) {
00087                 if (categoriesMap == null) this.loadLabelList();
00088                 Array labelList = new Array();  
00089                 HashMap map = (HashMap) categoriesMap.get(categorie);
00090                 if (map!= null) {
00091                         labelList = new Array(map.values().toArray());
00092                 }
00093                 LabelManager.getManager().sortLabelArray(labelList);
00094                 return labelList;
00095         }
00096   
00097         protected Array getDBLabelListForCategorie(String categorie)    {
00098                 Array labelList = new Array();
00099                 try     {
00100                         String[] param = new String[]{categorie};
00101                         ResultSet result = query.getLabelListForCategorie(param);
00102                         while (result.next()) {
00103                             Label label = query.convertResultSetToLabel(result);
00104                             if (label != null) {
00105                                 labelList.add(label);
00106                             } else {
00107                                 LogManager.traceWarning(LogServices.DATABASESERVICE, "JDBCLabelFactory : getDBLabelListForCategorie : Null Label");
00108                             }
00109                         }
00110                         result.close();
00111                 } catch (Exception ex)  {
00112                         LogManager.traceError(0, ex);
00113                 } finally {
00114                         query.getDbManager().garbageOpenedConnection();
00115                 }
00116                 return labelList;
00117         } 
00118         
00119         public Label getLabelWithIds(String id, String category)        {
00120                 if (categoriesMap == null) this.loadLabelList();
00121                 Label label = null;
00122                 HashMap map = (HashMap) categoriesMap.get(category);
00123                 if (map!= null) {
00124                         label = (Label)map.get(id);
00125                 }
00126                 return label;
00127         }
00128   
00129  protected Label getDBLabelWithIds(String id, String category)  {
00130         Label label = null;
00131         try     {
00132                 String[] param = new String[]{id, category};
00133                 ResultSet result = query.getLabelWithIds(param);
00134                 if (result.next()) {
00135                         label = query.convertResultSetToLabel(result);
00136                 }
00137                 result.close();
00138         } catch (Exception ex)  {
00139                 LogManager.traceError(0, ex);
00140         } finally {
00141                 query.getDbManager().garbageOpenedConnection();
00142         }
00143         return label;
00144  }
00145  
00146  public String convertIdsToLabelObjectIds(String labelid, String category)      {
00147                 StringBuffer str = new StringBuffer(labelid);
00148                 str.append('%');
00149                 str.append(category);
00150                 return str.toString();
00151  }
00152  
00153    
00157   public void storeLabel(Label label) throws ServiceException, DatabaseException {
00158                 try     {                 
00159                         //test because of trigger PB could not delete that does not exist in db
00160                         Label dblabel = this.getLabelWithIds(label.getLabelId(), label.getCategory());
00161                         if (dblabel != null)    {                 
00162                                 String[] param = query.getUpdateParamFromLabel(label);
00163                                 query.updateLabel(param);
00164                         } else  {
00165                                 String[] param = query.getInsertParamFromLabel(label);
00166                                 query.insertLabel(param);
00167                         }                       
00168                         this.notifyLabelUpdate(label);
00169                         //reload lab cache
00170                         this.loadLabelList();
00171                 } catch (Throwable ex)  {
00172                         throw new ServiceException(ex);
00173                 } finally {
00174                         query.getDbManager().garbageOpenedConnection();
00175                 }
00176   }
00177 
00178         public void deleteLabel(String id, String category) throws ServiceException, DatabaseException {
00179                 try     {                 
00180                         //test because of trigger PB could not delete that does not exist in db
00181                         Label dblabel = this.getLabelWithIds(id, category);
00182                         if (dblabel != null)    {                 
00183                                 //select tache if exist
00184                                 String[] param = {id, category};
00185                                 query.deleteLabel(param);
00186                                 //update never delete because after each modification for complet synchro
00187                                 //reload lab cache
00188                                 this.notifyLabeldelete(id, category);
00189                                 this.loadLabelList();
00190                         }
00191                 } catch (Throwable ex)  {
00192                         throw new ServiceException(ex);
00193                 } finally {
00194                         query.getDbManager().garbageOpenedConnection();
00195                 }
00196         }
00197         
00198         public abstract String[][] getCategoriesList();
00199         public abstract void initLabelDB();
00200 
00201         public abstract void notifyLabelUpdate(Label label) throws SynchroException;
00202         public abstract void notifyLabeldelete(String labelId, String categoryId) throws SynchroException;
00203 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4