📄 jahiacontainerdefpropdb.java
字号:
} catch (SQLException se) { String errorMsg = "Error in JahiaContainerListsPropDB.getProperty : " + se.getMessage() + " -> BAILING OUT"; JahiaConsole.println( "JahiaContainerListsPropDB.getProperty", errorMsg ); throw new JahiaException( "Cannot load container definition property 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", "JahiaContainerListsPropDB.getProperty : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } 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. * * @param containerListID identifier of the containerList * @param propertyName name of the property to add in the database * @param propertyValue name of the property to add in the database * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public void setProperty(int containerListID, String propertyName, String propertyValue) throws JahiaException { removeProperty(containerListID, propertyName); Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { // opens connection dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(20); stmt = dbConn.createStatement(); String sqlQuery = "INSERT INTO jahia_ctndef_prop(id_jahia_ctn_def, name_jahia_ctndef_prop, value_jahia_ctndef_prop) VALUES(" + containerListID + "," + "'" + propertyName + "'," + "'" + propertyValue + "')"; // 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.setProperty : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } catch (SQLException se) { String errorMsg = "Error in JahiaContainerDefPropDB.setProperty : " + se.getMessage() + " -> BAILING OUT"; JahiaConsole.println( "JahiaContainerDefPropDB.setProperty", errorMsg ); throw new JahiaException( "Cannot create container definition property 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.setProperty : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } /** * Removes a single property for the given containerList. * * @param containerListID identifer of the containerList * @param propertyName name of the property to be deleted. * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public void removeProperty(int containerListID, String propertyName) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { // composes the query String sqlQuery = "DELETE FROM jahia_ctndef_prop "; sqlQuery += "WHERE id_jahia_ctn_lists = " + containerListID; sqlQuery += " AND name_jahia_ctnlists_prop='" + propertyName + "'"; // 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.removeProperty : " + se.getMessage(); JahiaConsole.println( "JahiaContainerDefPropDB.removeProperty", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot delete container definition property 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.removeProperty : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } //-------------------------------------------------------------------------- /** * return a DOM document of all the properties of all the containers for * a given siteID * * @param siteID the identifier for the siteID for which to extract all the * containerList properties * * @return JahiaDOMObject a DOM representation of the properties of the * containerList * * @throws JahiaException generated if there were problems executing the * query or communicating with the database. */ public JahiaDOMObject getPropertiesAsDOM( int siteID ) throws JahiaException{ Connection dbConn = null; Statement statement = null; String output = null; JahiaDBDOMObject dom = null; try { String sqlQuery = "SELECT DISTINCT jahia_ctndef_prop.id_jahia_ctn_def," +"jahia_ctndef_prop.name_jahia_ctndef_prop,jahia_ctndef_prop.value_jahia_ctndef_prop" +" FROM jahia_ctndef_prop,jahia_ctn_def where " +"jahia_ctndef_prop.id_jahia_ctn_def=jahia_ctn_def.id_jahia_ctn_def AND " +"jahia_ctn_def.jahiaid_jahia_ctn_def="+siteID; dbConn = getDBConnection(0); statement = dbConn.createStatement(); if (statement != null) { ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement, sqlQuery ); if (rs != null) { dom = new JahiaDBDOMObject(); dom.addTable("jahia_ctndef_prop",rs); return dom; } } } catch (SQLException se) { String errorMsg = "Error in JahiaContainerDefPropDB.getPropertiesAsDOM : " + se.getMessage(); JahiaConsole.println( "JahiaContainerDefPropDB.getPropertiesAsDOM", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot load container definition properties from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } return dom; } //------------------------------------------------------------------------- private Connection getDBConnection (int debugInfo) { Connection dbConn = null; try { if ( debugInfo != 0 ){ dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection (debugInfo); } else { dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection (); } } catch (NullPointerException ex) { JahiaConsole.println ("JahiaContainerDefPropDB.getDBConnection", "Null Pointer Exception, DB Pool Service instance might be null!"); } catch (SQLException ex) { JahiaConsole.println ("JahiaContainerDefPropDB.getDBConnection", "SQL Exception: cannot get a connection."); } return dbConn; } //------------------------------------------------------------------------- private void closeDBConnection (Connection dbConn) { if (dbConn != null) { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection (dbConn); } catch (SQLException sqlEx) { // just create an exception without raising it, just to notify it // in the logs. JahiaException je = new JahiaException ("Cannot free resources", "Cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING); } catch (NullPointerException ex) { JahiaConsole.println ("JahiaContainerDefPropDB.closeDBConnection", "Null Pointer Exception, DB Pool Service instance might be null!"); } } } //------------------------------------------------------------------------- private void closeStatement (Statement statement) { // Close the opened statement try { if (statement!=null) { statement.close(); } } catch (SQLException sqlEx) { // just create an exception without raising it, just to notify it // in the logs. JahiaException je = new JahiaException ("Cannot close a statement", "Cannot close a statement", JahiaException.DATABASE_ERROR, JahiaException.WARNING); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -