📄 jahiacontainerdefpropdb.java
字号:
package org.jahia.services.containers;import java.sql.*; // ResultSetimport java.util.*; // Vectorimport org.jahia.params.*; // ParamBeanimport org.jahia.data.*; // JahiaDataimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.data.fields.*; // JahiaFieldimport org.jahia.data.containers.*; // JahiaContainersimport org.jahia.registries.*; // ServiceRegistryimport org.jahia.exceptions.JahiaException;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;/** * <p>Title: Database serialization of containerList properties.</p> * <p>Description: This object manages all the operations of serialization * of containerList properties in the database.</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: Jahia Ltd</p> * @author Serge Huber * @version 1.0 */public class JahiaContainerDefPropDB { /** * Default constructor, not much to say here... */ public JahiaContainerDefPropDB() { } /** * Retrieves all the properties for a given containerList. * * @param containerListID the identifier of the containerList whose properties * we want to retrieve from the database * * @return a Properties object that contains all the properties that are * available for this containerList in the database * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public Properties getProperties(int containerListID) throws JahiaException { Properties result = new Properties(); Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { String sqlQuery = "SELECT * FROM jahia_ctndef_prop"; sqlQuery += " WHERE id_jahia_ctn_def=" + containerListID; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(20); stmt = dbConn.createStatement(); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt, sqlQuery ); while (rs.next()) { int id = rs.getInt( "id_jahia_ctn_def" ); String name = rs.getString( "name_jahia_ctndef_prop" ); String value = rs.getString( "value_jahia_ctndef_prop" ); result.setProperty(name, value); } } catch (SQLException se) { String errorMsg = "Error in JahiaContainerDefPropDB.getProperties() : " + se.getMessage() + " -> BAILING OUT"; JahiaConsole.println( "JahiaContainerDefPropDB.getProperties", errorMsg ); throw new JahiaException( "Cannot load container def properties from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "JahiaContainerDefPropDB.getProperties : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } return result; } /** * Saves a whole set of properties in the database for the specified * containerList. WARNING : the way this is implemented (for speed reasons) * is that first all the existing properties for a containerList are * DELETED from the database. Remember to always load the full set of * properties before calling this method or this will result in dataloss. * Also if the operation with the database fails during the delete operation * or the insertion of the new elements this will also occur in data loss. * * A safer way would be to test the existence of each property in the database * and then INSERT or UPDATE the value, but that would take forever. * * @param containerListID identifier of the containerList whose properties * we are serializing in the database. * @param containerListProperties the Properties object that contains all * the properties to save in the database. Only what is passed here will * exist in the database. If the database contained properties that aren't * in this hashtable they will be deleted. * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public void setProperties(int containerListID, Properties containerListProperties) throws JahiaException { // First we clear all the existing properties in the database... // Warning this is dangerous if the operation is interrupted in the // middle. removeProperties(containerListID); Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { // opens connection dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(20); stmt = dbConn.createStatement(); Enumeration propNames = containerListProperties.keys(); while (propNames.hasMoreElements()) { String curPropName = (String) propNames.nextElement(); String curPropValue = containerListProperties.getProperty(curPropName); String sqlQuery = "INSERT INTO jahia_ctndef_prop(id_jahia_ctn_def, name_jahia_ctndef_prop, value_jahia_ctndef_prop) VALUES(" + containerListID + "," + "'" + curPropName + "'," + "'" + curPropValue + "')"; // executes the query, then closes the connection stmt.execute( sqlQuery ); } try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "JahiaContainerDefPropDB.setProperties : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } catch (SQLException se) { String errorMsg = "Error in JahiaContainerDefPropDB.setProperties : " + se.getMessage() + " -> BAILING OUT"; JahiaConsole.println( "JahiaContainerDefPropDB.setProperties", errorMsg ); throw new JahiaException( "Cannot create container definition properties in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "JahiaContainerDefPropDB.setProperties : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } /** * Removes all the properties for the specified containerList ID. * * @param containerListID idenfitifer of the containerList to delete all the * properties * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public void removeProperties(int containerListID) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { // composes the query String sqlQuery = "DELETE FROM jahia_ctndef_prop "; sqlQuery += "WHERE id_jahia_ctn_def = " + containerListID; // executes the query dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(22); stmt = dbConn.createStatement(); ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); } // catches error if cannot execute update query catch (SQLException se) { String errorMsg = "Error in JahiaContainerDefPropDB.removeProperties : " + se.getMessage(); JahiaConsole.println( "JahiaContainerDefPropDB.removeProperties", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot delete container definition properties in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "JahiaContainerDefPropDB.removeProperties : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } /** * Retrieves the containerList property from the database. This method may * also be used as a test for existence of a property in the database. * * @param containerListID identifier of the containerList * @param propertyName name of the property to retrieve * * @return a String containing the value of the property, or null if the * property doesn't have a value in the database (ie if it doesn't exist) * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public String getProperty(int containerListID, String propertyName) throws JahiaException { String result = null; Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { String sqlQuery = "SELECT value_jahia_ctndef_prop FROM jahia_ctndef_prop"; sqlQuery += " WHERE id_jahia_ctn_def=" + containerListID; sqlQuery += " AND name_jahia_ctndef_prop='" + propertyName + "'"; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(20); stmt = dbConn.createStatement(); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt, sqlQuery ); if (rs.next()) { String value = rs.getString( "value_jahia_ctndef_prop" ); result = value; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -