📄 jahiapagedefinitionpropdb.java
字号:
package org.jahia.services.pages;import java.sql.*; // ResultSetimport java.util.*; // Vectorimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.registries.*; // ServiceRegistryimport org.jahia.exceptions.JahiaException;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;/** * <p>Title: Database serialization of page defintion properties.</p> * <p>Description: This object manages all the operations of serialization * of page definition properties in the database.</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: Jahia Ltd</p> * @author Khue Nguyen * @version 1.0 */class JahiaPageDefinitionPropDB { private static final String CLASS_NAME = JahiaPageDefinitionPropDB.class.getName(); //-------------------------------------------------------------------------- /** * Retrieves all the properties for a given page definition. * * @param int id the page definition id * * @return a Properties object that contains all the properties * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public static Properties getProperties(int id) throws JahiaException { Properties result = new Properties(); Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { String sqlQuery = "SELECT * FROM jahia_pages_def_prop"; sqlQuery += " WHERE id_jahia_pages_def_prop=" + id; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(30); stmt = dbConn.createStatement(); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); while (rs.next()) { id = rs.getInt( "id_jahia_pages_def_prop" ); int siteID = rs.getInt( "jahiaid_pages_def_prop" ); String name = rs.getString( "name_pages_def_prop" ); String value = rs.getString( "value_pages_def_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 page def 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 JahiaPageDefinition pageDef, the page definition of the page def whose properties * we are serializing in the database. * @param Properties props, 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 static void setProperties(JahiaPageDefinition pageDef,Properties props) throws JahiaException { if ( pageDef == null ) return; // First we clear all the existing properties in the database... // Warning this is dangerous if the operation is interrupted in the // middle. removeProperties(pageDef.getID()); Connection dbConn = null; ResultSet rs = null; PreparedStatement pstmt = null; try { // opens connection dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(31); Enumeration propNames = props.keys(); while (propNames.hasMoreElements()) { String curPropName = (String) propNames.nextElement(); String curPropValue = props.getProperty(curPropName); pstmt = dbConn.prepareStatement("INSERT INTO jahia_pages_def_prop VALUES(?,?,?,?)"); pstmt.setInt(1, pageDef.getID()); pstmt.setInt(2, pageDef.getJahiaID()); pstmt.setString(3, curPropName); pstmt.setString(4, curPropValue); pstmt.execute(); } closeDBConnection (dbConn); closeStatement ((Statement)pstmt); } catch (SQLException se) { String errorMsg = "Error in " + CLASS_NAME +".setProperties : " + se.getMessage(); JahiaConsole.println( CLASS_NAME+".setProperties", errorMsg ); throw new JahiaException( "Cannot create page def properties in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement ((Statement)pstmt); } } //-------------------------------------------------------------------------- /** * Removes all the properties for the specified page def id. * * @param int id idenfitifer of the page def to delete all the * properties * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public static void removeProperties(int id) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { // composes the query String sqlQuery = "DELETE FROM jahia_pages_def_prop "; sqlQuery += "WHERE id_jahia_pages_def_prop = " + id; // executes the query dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(32); 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 page def properties in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (stmt); } } //-------------------------------------------------------------------------- /** * Retrieves the page def property from the database. This method may * also be used as a test for existence of a property in the database. * * @param int id, identifier of the page def * @param String name, 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 static String getProperty(int id, String name) throws JahiaException { String result = null; Connection dbConn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(33); String sqlQuery = "SELECT value_pages_def_prop FROM jahia_pages_def_prop"; sqlQuery += " WHERE id_jahia_pages_def_prop=? AND name_pages_def_prop=?"; pstmt = dbConn.prepareStatement(sqlQuery); pstmt.setInt(1,id); pstmt.setString(2,name); rs = pstmt.executeQuery(); if (rs.next()) { String value = rs.getString( "value_pages_def_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 page def 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 page def. This * operation starts by deleting any existing entry and then inserting a * new value. * * @param int id, identifier of the page def * @param String name, name of the property to add in the database * @param String siteKey, the site Key of the page def * @param String value, value of the property to add in the database * * @throws JahiaException generated if there were problems executing the * query or communicating with the database.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -