1 40 41 package org.jahia.services.fields; 42 43 import org.apache.log4j.Logger; 44 import org.jahia.data.fields.JahiaField; 45 import org.jahia.exceptions.JahiaException; 46 import org.jahia.exceptions.JahiaInitializationException; 47 import org.jahia.services.cache.Cache; 48 import org.jahia.services.cache.CacheFactory; 49 import org.jahia.services.cache.CacheListener; 50 51 import java.sql.Connection ; 52 import java.sql.ResultSet ; 53 import java.sql.SQLException ; 54 import java.sql.Statement ; 55 import java.util.Enumeration ; 56 import java.util.Properties ; 57 import java.util.Vector ; 58 59 public class JahiaFieldPropertiesDB implements CacheListener { 60 61 62 private static Logger logger = Logger.getLogger (JahiaFieldBaseService.class); 63 64 public static final String FIELD_PROPS_CACHE = "FieldPropsCache"; 66 67 private Cache cacheFieldProps = null; 68 private boolean tableEmpty = false; 69 70 71 74 public JahiaFieldPropertiesDB () { 75 try { 76 cacheFieldProps = CacheFactory.createCache (FIELD_PROPS_CACHE); 77 cacheFieldProps.registerListener(this); 78 79 } catch (JahiaInitializationException e) { 80 logger.warn("Could not instanciate the Field Properties Cache ... bad sign!!", e); 81 } 82 83 cacheAllFieldProperties (); 85 } 86 87 92 private static final int CHUNK_SIZE = 100; 93 94 protected void cacheAllFieldProperties () { 95 Connection dbConn = null; 96 Statement stmt = null; 97 ResultSet rs = null; 98 try { 99 100 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 101 stmt = dbConn.createStatement (); 102 103 String query = "SELECT fieldid_jahia_fields_prop FROM jahia_fields_prop ORDER BY fieldid_jahia_fields_prop ASC"; 104 rs = stmt.executeQuery (query); 105 Vector entries = new Vector (); 106 tableEmpty = true; 107 if (rs != null) { 108 while (rs.next ()) { 109 tableEmpty = false; 110 entries.add (new Integer (rs.getInt ( 111 "fieldid_jahia_fields_prop"))); 112 } 113 } 114 115 int ptr = 0; 117 while (ptr < entries.size ()) { 118 119 int max = (ptr + CHUNK_SIZE < entries.size () ? 120 ptr + CHUNK_SIZE : entries.size () - 1); 121 query = 122 "SELECT * FROM jahia_fields_prop WHERE fieldid_jahia_fields_prop>=" + 123 (entries.elementAt (ptr)) + 124 " AND fieldid_jahia_fields_prop<=" + 125 (entries.elementAt (max)); 126 127 String sqlQuery = "SELECT * FROM jahia_fields_prop"; 128 129 rs = stmt.executeQuery (sqlQuery); 130 131 while (rs.next ()) { 132 String propName = rs.getString ("propertyname_jahia_fields_prop"); 133 String propValue = rs.getString ("propvalue_jahia_fields_prop"); 134 String propFieldID = rs.getString ("fieldid_jahia_fields_prop"); 135 if ((propName != null) && (propValue != null)) { 136 Properties cachedTable = (Properties ) cacheFieldProps.get ( 137 new Integer (propFieldID)); 138 if (cachedTable == null) { 140 cachedTable = new Properties (); 141 cacheFieldProps.put (new Integer (propFieldID), cachedTable); 142 } 143 cachedTable.put (propName, propValue); 144 } 145 } 146 ptr += CHUNK_SIZE; 147 } 148 } catch (SQLException se) { 149 String errorMsg = "Error in db_load_field_properties : " + se.getMessage () + 150 "-> BAILING OUT"; 151 logger.warn (errorMsg, se); 152 153 } finally { 154 closeStatement(stmt); 155 } 156 } 157 158 165 public void db_load_field_properties (JahiaField theField) 166 throws JahiaException { 167 if ((cacheFieldProps.size () == 0) && (!tableEmpty)) { 168 cacheAllFieldProperties (); 169 } 170 Properties cachedTable = (Properties ) cacheFieldProps.get ( 171 new Integer (theField.getID ())); 172 if (cachedTable == null) { 174 theField.setProperties (new Properties ()); 175 } 176 theField.setProperties (cachedTable); 177 } 178 179 180 187 public void db_save_field_properties (JahiaField theField) 188 throws JahiaException { 189 if (theField.getProperties () == null) { 190 return; 192 } 193 cacheFieldProps.put (new Integer (theField.getID ()), theField.getProperties ()); 194 Connection dbConn = null; 195 Statement stmt = null; 196 197 try { 198 db_delete_field_properties (theField.getID ()); 200 201 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 203 stmt = dbConn.createStatement (); 204 String sqlQuery = ""; 205 206 Properties fProps = theField.getProperties (); 208 Enumeration keys = fProps.keys (); 209 while (keys.hasMoreElements ()) { 210 String theKey = (String ) keys.nextElement (); 211 String theVal = (String ) fProps.get (theKey); 212 sqlQuery = "INSERT INTO jahia_fields_prop (fieldid_jahia_fields_prop,propertyname_jahia_fields_prop,propvalue_jahia_fields_prop) VALUES("; 213 sqlQuery += theField.getID () + ","; 214 sqlQuery += "'" + theKey + "',"; 215 sqlQuery += "'" + theVal + "')"; 216 stmt.execute (sqlQuery); 217 } 218 } catch (SQLException se) { 219 String errorMsg = "Error in db_save_field_properties : " + se.getMessage (); 220 logger.error (errorMsg + " -> BAILING OUT", se); 221 throw new JahiaException ("Cannot load fields from the database", 222 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR_SEVERITY); 223 224 } finally { 225 closeStatement(stmt); 226 } 227 } 228 229 230 237 public void db_delete_field_properties (int fieldID) 238 throws JahiaException { 239 cacheFieldProps.remove (new Integer (fieldID)); 240 Connection dbConn = null; 241 Statement stmt = null; 242 try { 243 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 245 stmt = dbConn.createStatement (); 246 String sqlQuery = ""; 247 248 sqlQuery = "DELETE FROM jahia_fields_prop "; 250 sqlQuery += "WHERE (fieldid_jahia_fields_prop=" + fieldID + ")"; 251 stmt.execute (sqlQuery); 252 253 } catch (SQLException se) { 254 String errorMsg = "Error in db_delete_field_properties : " + se.getMessage (); 255 logger.error (errorMsg + " -> BAILING OUT"); 256 throw new JahiaException ("Cannot load fields from the database", 257 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR_SEVERITY); 258 259 } finally { 260 closeStatement (stmt); 261 } 262 } 263 264 private void closeStatement(Statement statement) { 265 try { 267 if (statement != null) { 268 statement.close(); 269 } 270 } catch (SQLException sqlEx) { 271 logger.warn("Cannot close a statement", sqlEx); 272 } 273 } 274 275 280 public void onCacheFlush(String cacheName) { 281 282 if (FIELD_PROPS_CACHE.equals(cacheName)) { 283 cacheFieldProps.flush(false); 284 cacheAllFieldProperties(); 285 } 286 } 287 288 public void onCachePut(String cacheName, Object entryKey) { 289 } 291 292 } 293 | Popular Tags |