📄 jahiacontainerlistpropdb.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//////// NK 27.02.2002 - added in Jahia//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) 2001</p> * <p>Company: Jahia</p> * @author Khue Nguyen * @version 1.0 */public class JahiaContainerListPropDB { private static final String CLASS_NAME = JahiaContainerListPropDB.class.getName(); /** * Default constructor, not much to say here... */ public JahiaContainerListPropDB() { } /** * 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_ctnlists_prop"; sqlQuery += " WHERE ctnlistid_ctnlists_prop=" + containerListID; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(20); stmt = dbConn.createStatement(); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); while (rs.next()) { int id = rs.getInt( "ctnlistid_ctnlists_prop" ); int siteID = rs.getInt( "jahiaid_ctnlists_prop" ); String name = rs.getString( "name_ctnlists_prop" ); String value = rs.getString( "value_ctnlists_prop" ); result.setProperty(name, value); } } catch (SQLException se) { String errorMsg = "Error in " + CLASS_NAME +".getProperties() : " + se.getMessage(); JahiaConsole.println( CLASS_NAME+".getProperties", errorMsg ); throw new JahiaException( "Cannot load container list properties from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (stmt); } 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 containerList the containerList whose properties * we are serializing in the database. * @param int jahiaID, the siteid * @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, int jahiaID, 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; PreparedStatement pstmt = null; ResultSet rs = null; try { // opens connection dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(20); Enumeration propNames = containerListProperties.keys(); while (propNames.hasMoreElements()) { String curPropName = (String) propNames.nextElement(); String curPropValue = containerListProperties.getProperty(curPropName); pstmt = dbConn.prepareStatement("INSERT INTO jahia_ctnlists_prop VALUES(?,?,?,?)"); pstmt.setInt(1, containerListID); pstmt.setInt(2, jahiaID); pstmt.setString(3, curPropName); pstmt.setString(4, curPropValue); pstmt.execute(); } } catch (SQLException se) { String errorMsg = "Error in " + CLASS_NAME+".setProperties : " + se.getMessage(); JahiaConsole.println( CLASS_NAME+".setProperties", errorMsg ); throw new JahiaException( "Cannot create container list properties in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement ((Statement)pstmt); } } /** * 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_ctnlists_prop "; sqlQuery += "WHERE ctnlistid_ctnlists_prop = " + 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 " + CLASS_NAME + ".removeProperties : " + se.getMessage(); JahiaConsole.println( CLASS_NAME + ".removeProperties", errorMsg ); throw new JahiaException( "Cannot delete container list properties in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (stmt); } } /** * 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; PreparedStatement pstmt = null; ResultSet rs = null; try { dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(20); String sqlQuery = "SELECT value_ctnlists_prop FROM jahia_ctnlists_prop"; sqlQuery += " WHERE ctnlistid_ctnlists_prop=?"; sqlQuery += " AND name_ctnlists_prop=?"; pstmt = dbConn.prepareStatement(sqlQuery); pstmt.setInt(1,containerListID); pstmt.setString(2,propertyName); rs = pstmt.executeQuery(); if (rs.next()) { String value = rs.getString( "value_ctnlists_prop" ); result = value; } } catch (SQLException se) { String errorMsg = "Error in " + CLASS_NAME + ".getProperty : " + se.getMessage(); JahiaConsole.println( CLASS_NAME +".getProperty", errorMsg ); throw new JahiaException( "Cannot load container list property from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement ((Statement)pstmt); } return result; } /** * Saves a single property in the database for a given containerList. This * operation starts by deleting any existing entry and then inserting a * new value. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -