1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
public class Cache extends AbstractInformation{
private Map<String, HashMap<String, String>> tabGlobal;
private static Cache instanceUnique=null;
private boolean isCacheValid=true;
public ArrayList<String> listCdTables;
public Cache()
{
tabGlobal= (HashMap<String, HashMap<String, String>>) Collections.synchronizedMap( new HashMap<String, HashMap<String, String>>());
}
public static synchronized Cache getInstance(){
if(instanceUnique==null){
instanceUnique = new Cache();
}
return instanceUnique;
}
/**
* Récupère le cdTable et le libelle selectionné par l'utilisateur et lui renvoit le cdKey pour requete sur la base
* @param cdTable : permet de se positionner sur la bonne table
* @param lbSel : libelle de la table dont l'Id est recherché
* @return : cdKey recherché
*/
public String recupIdByCdTableAndLbSel(String cdTable,String lbSel)
{
this.updateHashMap(cdTable);
HashMap<String, String> hashCdKeyLb=tabGlobal.get(cdTable);
for (Iterator i = hashCdKeyLb.keySet().iterator(); i.hasNext();) {
String cdKey = (String) i.next();
String value = (String) hashCdKeyLb.get(cdKey);
if(lbSel.equals(value)){return cdKey;}
}
return "";
}
public boolean isCacheValid() {
return isCacheValid;
}
public void setCacheValid(boolean isCacheValid) {
this.isCacheValid = isCacheValid;
}
public void reinitCache()
{
instanceUnique=null;
}
/**
* Met à jour la hashMap si elle ne contient pas encore le cdTable
* @param cdTable
*/
public void updateHashMap(String cdTable)
{
if (!tabGlobal.containsKey(cdTable))
{ HashMap<String, String> listeCdKeyCol1=new HashMap<String, String>();
String cdKey;
String col1;
try {
String[] tabParams={cdTable+"%"};
Collection<Object[]> colsCleValeurs=getGenericFacade().recupNColEntityGenerique("reqRecupCleValeurGlob", tabParams, "loadCdKeyLbByCdTable", "recupere les doublons clés=>valeurs");
for(Object[] entry:colsCleValeurs){
cdKey=(String)entry[0];
col1=(String)entry[1];
listeCdKeyCol1.put(col1, cdKey);
}
this.tabGlobal.put(cdTable, listeCdKeyCol1);
}catch (NoResultException e) {
log.debug(getClassName(), "loadCdKeyLbByCdTable", "pas de donnees dispo pour les cles :");
}
}
}
/**
* Retourne une collection de col1 correspondant à un cdTable
* @param cdTable
* @return
*/
public List<String> recupListeCol1(String cdTable)
{
this.updateHashMap(cdTable);
return new ArrayList<String>(tabGlobal.get(cdTable).keySet());
}
/**
* Retourne une collection de col1 correspondant à un cdTable
* @param cdTable
* @return
*/
public List<String> recupListeCdKey(String cdTable)
{
this.updateHashMap(cdTable);
return new ArrayList<String>(tabGlobal.get(cdTable).values());
}
/**
* Mets à jour la map si modif dans la base d'une cdTable
* @param cdTable
*/
public void updateOneTableByCdTable(String cdTable)
{
if (tabGlobal.containsKey(cdTable))
{
tabGlobal.remove(cdTable);
}
this.updateHashMap(cdTable);
}
/**
* Vide le cache
*/
public void clearCache()
{
this.loadListCdKeys();
for (String cdTable: listCdTables)
{
tabGlobal.get(cdTable).clear();
}
this.tabGlobal.clear();
}
} |
Partager