📄 jahiafieldpropertiesdb.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaFieldPropertiesDB// EV 05.01.2001//// db_load_field_properties( theField )// db_update_field_properties( theField )// db_delete_field_properties( theField )//package org.jahia.services.fields;import java.sql.*; // ResultSetimport java.util.*; // Vectorimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.settings.*; // SettingsBeanimport org.jahia.data.*;import org.jahia.data.fields.*; // JahiaField, FieldTypes, LoadFlagsimport org.jahia.params.*; // ParamBeanimport org.jahia.sharing.*; // JahiaDataSourceManagerimport org.jahia.registries.*; // ServicesRegistryimport org.jahia.services.files.*; // Jahia Files Servicesimport org.jahia.services.fields.*; // Jahia Fields Servicesimport org.jahia.services.applications.*; // Jahia Applications Serviceimport org.jahia.exceptions.JahiaException;import org.jahia.services.cache.JahiaCacheFactory;import org.jahia.data.cache.JahiaSimpleCache;public class JahiaFieldPropertiesDB{ private JahiaSimpleCache cacheFieldProps = null; /*** * constructor * */ public JahiaFieldPropertiesDB() { cacheFieldProps = JahiaCacheFactory.getInstance().createJahiaSimpleCache("FieldPropsCache", "This cache caches all field properties from the database", JahiaSimpleCache.UNLIMITED); cacheAllFieldProperties(); } // end constructor /** * Put put _ALL_ field properties in cache. For now we don't * use much field properties so it's ok. But else it may take * too much RAM so we'll have to think of a better way to cache it. */ private static final int CHUNK_SIZE = 100; private void cacheAllFieldProperties() { Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(48); stmt = dbConn.createStatement(); String query = "SELECT fieldid_jahia_fields_prop FROM jahia_fields_prop ORDER BY fieldid_jahia_fields_prop ASC"; rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery (stmt, query); Vector entries = new Vector(); if (rs != null) { while (rs.next()) entries.add(new Integer(rs.getInt ("fieldid_jahia_fields_prop"))); } // cache them, approx. 100 by 100 int ptr=0; while (ptr < entries.size()) { int max = (ptr+CHUNK_SIZE<entries.size()?ptr+CHUNK_SIZE:entries.size()-1); query = "SELECT * FROM jahia_fields_prop WHERE fieldid_jahia_fields_prop>="+((Integer)entries.elementAt(ptr))+ " AND fieldid_jahia_fields_prop<="+((Integer)entries.elementAt(max)); String sqlQuery = "SELECT * FROM jahia_fields_prop"; rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); while (rs.next()) { String propName = rs.getString ( "propertyname_jahia_fields_prop" ); String propValue = rs.getString ( "propvalue_jahia_fields_prop" ); String propFieldID = rs.getString ( "fieldid_jahia_fields_prop" ); if ((propName != null) && (propValue != null)) { Hashtable cachedTable = (Hashtable)cacheFieldProps.getValue(new Integer(propFieldID)); // field not already cached, let's cache it if (cachedTable == null) { cachedTable = new Hashtable(); cacheFieldProps.setValue (cachedTable, new Integer(propFieldID)); } cachedTable.put( propName, propValue ); } } ptr+=CHUNK_SIZE; } } catch (SQLException se) { String errorMsg = "Error in db_load_field_properties : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> BAILING OUT" ); JahiaException je = new JahiaException( "Cannot load fields from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "db_load_field_properties : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } /*** * loads field properties for a field * * @param theField the field to load properties for * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public void db_load_field_properties( JahiaField theField ) throws JahiaException { Hashtable cachedTable = (Hashtable)cacheFieldProps.getValue (new Integer(theField.getID())); // properties not it cache ----> there is no property for this field! if (cachedTable == null) { theField.setProperties(new Hashtable()); } theField.setProperties(cachedTable); } // end db_load_field_properties /*** * saves field properties * * @param theField the field to save properties for * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public void db_save_field_properties( JahiaField theField ) throws JahiaException { cacheFieldProps.setValue (theField.getProperties(), new Integer(theField.getID())); Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { // erases all existing properties db_delete_field_properties( theField.getID() ); // gets connection dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(49); stmt = dbConn.createStatement(); String sqlQuery = ""; // saves properties Hashtable fProps = theField.getProperties(); Enumeration keys = fProps.keys(); while (keys.hasMoreElements()) { String theKey = (String) keys.nextElement(); String theVal = (String) fProps.get( theKey ); sqlQuery = "INSERT INTO jahia_fields_prop "; sqlQuery += "(fieldid_jahia_fields_prop,"; sqlQuery += "propertyname_jahia_fields_prop,"; sqlQuery += "propvalue_jahia_fields_prop) "; sqlQuery += "VALUES(" + theField.getID() + ","; sqlQuery += "'" + theKey + "',"; sqlQuery += "'" + theVal + "')"; stmt.execute( sqlQuery ); } } catch (SQLException se) { String errorMsg = "Error in db_save_field_properties : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> BAILING OUT" ); JahiaException je = new JahiaException( "Cannot load fields from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "db_save_field_properties : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end db_save_field_properties /*** * deletes field properties * * @param fieldID the field id to delete properties for * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public void db_delete_field_properties( int fieldID) throws JahiaException { cacheFieldProps.removeValue (new Integer(fieldID)); Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { // gets connection dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(50); stmt = dbConn.createStatement(); String sqlQuery = ""; // erases all existing properties sqlQuery = "DELETE FROM jahia_fields_prop "; sqlQuery += "WHERE (fieldid_jahia_fields_prop=" + fieldID + ")"; stmt.execute( sqlQuery ); } catch (SQLException se) { String errorMsg = "Error in db_delete_field_properties : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> BAILING OUT" ); JahiaException je = new JahiaException( "Cannot load fields from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "db_delete_field_properties : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end db_delete_field_properties} // end JahiaFieldsDB
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -