📄 jahiacontainerstructuresdb.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .///*** * JahiaContainerStructuresDB * @author Eric Vassalli * * Holds all the methods enabling container structures load, update and delete. * *///// db_load_container_structure( ctndefid )// db_create_container_structure( theContainerDef )// db_update_container_structure( theContainerDef )// db_delete_container_structure( ctndefid )//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;public class JahiaContainerStructuresDB{ /*** * constructor * */ public JahiaContainerStructuresDB() { } // end constructor /*** * loads a container structure from its container definition id * * @param subDefID the sub definition ID * @return returns a Vector of structures * * @exception throws a critical JahiaException if a data access occurs * @exception throws a warning JahiaException if cannot free resources * */ public Vector db_load_container_structure( int subDefID ) throws JahiaException { Connection dbConn = null; Statement stmt = null; ResultSet rs = null; Vector theStructure = new Vector(); try { String sqlQuery = "SELECT * FROM jahia_ctn_struct"; sqlQuery += " WHERE ctnsubdefid_jahia_ctn_struct=" + subDefID; sqlQuery += " ORDER BY rank_jahia_ctn_struct ASC"; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(27); stmt = dbConn.createStatement(); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); while (rs.next()) { int ctnsubdefid = rs.getInt( "ctnsubdefid_jahia_ctn_struct" ); int objTypeID = rs.getInt( "objtype_jahia_ctn_struct" ); int objDefID = rs.getInt( "objdefid_jahia_ctn_struct" ); int rank = rs.getInt( "rank_jahia_ctn_struct" ); theStructure.add( new JahiaContainerStructure( ctnsubdefid, objTypeID, objDefID, rank) ); } } catch (SQLException se) { String errorMsg = "Error in db_load_container_structure : " + se.getMessage() + " -> BAILING OUT"; JahiaConsole.println( "JahiaContainerStructuresDB", errorMsg ); throw new JahiaException( "Cannot load structures 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", "db_load_container_structure : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } if (theStructure.size() > 0) { return theStructure; } else { return null; } } // end db_load_container_structure /*** * creates a container structure from its container definition * * @param theDef the JahiaContainerDefinition object * @see org.jahia.data.containers.JahiaContainerDefinition * */ public void db_create_container_structure( JahiaContainerSubDefinition theSubDef ) throws JahiaException { db_update_container_structure( theSubDef ); } // end db_create_container_structure /*** * updates a container structure from its container definition * * @param theDef the JahiaContainerDefinition object * @see org.jahia.data.containers.JahiaContainerDefinition * * @exception throws a critical JahiaException if a data access occurs * @exception throws a warning JahiaException if cannot free resources * */ public void db_update_container_structure( JahiaContainerSubDefinition theSubDef ) throws JahiaException { Connection dbConn = null; Statement stmt = null; String sqlQuery = ""; try { // connects to the database dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(28); stmt = dbConn.createStatement(); // clears old structure db_delete_container_structure( theSubDef.getID() ); int pageDefID = theSubDef.getPageDefID(); // inserts new structure Vector structList = theSubDef.getStructure(); if (structList != null) { Enumeration structure = structList.elements(); while (structure.hasMoreElements()) { JahiaContainerStructure theStruct = (JahiaContainerStructure) structure.nextElement(); theStruct.setSubctndefid( theSubDef.getID() ); sqlQuery = "INSERT INTO jahia_ctn_struct ("; sqlQuery += "ctnsubdefid_jahia_ctn_struct, "; sqlQuery += "objtype_jahia_ctn_struct, "; sqlQuery += "objdefid_jahia_ctn_struct, "; sqlQuery += "rank_jahia_ctn_struct) "; sqlQuery += "VALUES("; sqlQuery += theSubDef.getID() + ", "; sqlQuery += theStruct.getObjectType() + ", "; sqlQuery += theStruct.getObjectDefID() + ", "; sqlQuery += theStruct.getRank() + ")"; ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); } } } catch (SQLException se) { String errorMsg = "Error in db_update_container_structure : " + se.getMessage() + " -> BAILING OUT"; JahiaConsole.println( "JahiaContainerStructuresDBManager", errorMsg ); throw new JahiaException( "Cannot update structures 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", "db_update_container_structure : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end db_update_container_structure /*** * deletes a container structure from its container definition * * @param theDefID the JahiaContainerDefinition ID * @see org.jahia.data.containers.JahiaContainerDefinition * * @exception throws a critical JahiaException if a data access occurs * @exception throws a warning JahiaException if cannot free resources * */ public void db_delete_container_structure( int theSubDefID ) throws JahiaException { Connection dbConn = null; Statement stmt = null; String sqlQuery = ""; try { // connects to the database dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(29); stmt = dbConn.createStatement(); sqlQuery = "DELETE FROM jahia_ctn_struct "; sqlQuery += "WHERE ctnsubdefid_jahia_ctn_struct=" + theSubDefID; ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); } catch (SQLException se) { String errorMsg = "Error in db_delete_container_structure : " + se.getMessage() + " -> BAILING OUT"; JahiaConsole.println( "JahiaContainerStructuresDBManager", errorMsg ); throw new JahiaException( "Cannot delete structures 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", "db_delete_container_structure : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end db_delete_container_structure //-------------------------------------------------------------------------- /** * return a DOM document of all container strucctures of a site * * @param int the site id * * @return JahiaDOMObject a DOM representation of this object * * @author NK */ public JahiaDOMObject getContainerStructsAsDOM( int siteID ) throws JahiaException{ Connection dbConn = null; Statement statement = null; String output = null; JahiaDBDOMObject dom = null; try { String sqlQuery = "SELECT DISTINCT jahia_ctn_struct.ctnsubdefid_jahia_ctn_struct," +"jahia_ctn_struct.objtype_jahia_ctn_struct,jahia_ctn_struct.objdefid_jahia_ctn_struct," +"jahia_ctn_struct.rank_jahia_ctn_struct FROM jahia_ctn_struct,jahia_fields_def,jahia_ctn_def WHERE " +"( jahia_ctn_struct.objtype_jahia_ctn_struct=1 AND jahia_ctn_struct.objdefid_jahia_ctn_struct=" +"jahia_fields_def.id_jahia_fields_def AND jahia_fields_def.jahiaid_jahia_fields_def="+siteID+" ) " +"OR ( jahia_ctn_struct.objtype_jahia_ctn_struct=2 AND jahia_ctn_struct.objdefid_jahia_ctn_struct=" +"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_ctn_struct",rs); return dom; } } } catch (SQLException se) { String errorMsg = "Error in getContainerStructsAsDOM(int siteID) : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> B AILING OUT" ); throw new JahiaException( "Cannot load container structures 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 ("JahiaPagesDB", "Null Pointer Exception, DB Pool Service instance might be null!"); } catch (SQLException ex) { JahiaConsole.println ("JahiaPagesDB", "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 ("JahiaPagesDB", "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); } }} // end JahiaContainerStructuresDB
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -