📄 jahiasitespersistance.java
字号:
} //-------------------------------------------------------------------------- /** * add a property. Do not check if there is alrady a same * property in db.You must remove it first. * * @param int siteid * @param String the property name * @param String the property value */ void addProperty( int siteID , String key , String value ) throws JahiaException { if ( (key == null) || (key.trim().equals("")) ) return; try { StringBuffer buff = new StringBuffer("INSERT INTO jahia_site_prop VALUES ("); buff.append(siteID); buff.append(",'"); buff.append(JahiaTools.quote(key)); buff.append("','"); buff.append(JahiaTools.quote(value)); buff.append("')"); executeQueryNoResultSet(buff.toString()); } catch (JahiaException je) { String errorMsg = "Error in addProperty(siteID, String key, String value) : " + je.getMessage(); JahiaConsole.println( CLASS_NAME+".addProperty", errorMsg ); throw new JahiaException( "Cannot add a property in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } } //-------------------------------------------------------------------------- /** * delete all properties of a site * * @param int siteID */ void deleteProperties(int siteID) throws JahiaException{ try { StringBuffer buff = new StringBuffer("DELETE FROM jahia_site_prop WHERE id_jahia_site="); buff.append(siteID); executeQueryNoResultSet(buff.toString()); } catch (JahiaException je) { String errorMsg = "Error in deleteProperties(siteID) : " + je.getMessage(); JahiaConsole.println( CLASS_NAME+".deleteProperties(site)", errorMsg ); throw new JahiaException( "Cannot delete all site's properties in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } } //-------------------------------------------------------------------------- /** * remove a property of a site * * @param int site id * @param String the property name */ void deleteProperty( int id , String key ) throws JahiaException { if ( (key == null) || (key.trim().equals("")) ) return; try { StringBuffer buff = new StringBuffer("DELETE FROM jahia_site_prop WHERE id_jahia_site="); buff.append(id); buff.append(" AND name_jahia_site_prop='"); buff.append(JahiaTools.quote(key)); buff.append("'"); executeQueryNoResultSet(buff.toString()); } catch (JahiaException je) { String errorMsg = "Error in deleteProperty(int id, String key) : " + je.getMessage(); JahiaConsole.println( CLASS_NAME+".deleteProperty", errorMsg ); throw new JahiaException( "Cannot delete a site's property in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } } //------------------------------------------------------------------------- private void executeQueryNoResultSet(String queryStr) throws JahiaException { Connection dbConn = null; Statement statement = null; try { dbConn = getDBConnection(); statement = dbConn.createStatement(); if (statement != null) { ServicesRegistry.getInstance().getDBPoolService().executeUpdate(statement,queryStr); } } catch (SQLException se) { String errorMsg = "Error in executeQueryNoResultSet(String queryStr) : " + se.getMessage(); JahiaConsole.println( "JahiaSitesPersistance", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot execute query" + queryStr, errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } } //------------------------------------------------------------------------- private Connection getDBConnection () { Connection dbConn = null; try { dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection (); } catch (NullPointerException ex) { JahiaConsole.println ("JahiaSitesPersistance", "Null Pointer Exception, DB Pool Service instance might be null!"); } catch (SQLException ex) { JahiaConsole.println ("JahiaSitesPersistance", "SQL Exception: cannot get a connection."); } return dbConn; } //------------------------------------------------------------------------- private Connection getDBConnection (int debugInfo) { Connection dbConn = null; try { dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection (debugInfo); } catch (NullPointerException ex) { JahiaConsole.println ("JahiaSitesPersistance", "Null Pointer Exception, DB Pool Service instance might be null!"); } catch (SQLException ex) { JahiaConsole.println ("JahiaSitesPersistance", "SQL Exception: cannot get a connection."); } return dbConn; } //------------------------------------------------------------------------- private void closeDBConnection (Connection dbConn) { if (dbConn != null) { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection (dbConn); dbConn = null; } 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 ("JahiaSitesPersistance", "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(); statement = null; } } 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); } } //-------------------------------------------------------------------------- /** * return a DOM representation of a site * * @param int the site id * * @return JahiaDOMObjet a DOM representation of this object * * @author NK */ public JahiaDOMObject getSiteAsDOM( int id ) throws JahiaException{ Connection dbConn = null; Statement statement = null; String output = null; JahiaDBDOMObject dom = null; try { String sqlQuery = "SELECT * FROM jahia_sites where id_jahia_sites=" + id; dbConn = getDBConnection(); statement = dbConn.createStatement(); if (statement != null) { ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,sqlQuery ); if (rs != null) { dom = new JahiaDBDOMObject(); dom.addTable("jahia_sites",rs); return dom; } } } catch (SQLException se) { String errorMsg = "Error in getSiteAsDOM(int id) : " + se.getMessage(); JahiaConsole.println( "JahiaSitesPersistance", errorMsg + " -> B AILING OUT" ); throw new JahiaException( "Cannot load site from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } return dom; } //-------------------------------------------------------------------------- /** * return a DOM representation of a site's properties * * @param int the site id * * @return JahiaDOMObjet a DOM representation of this object * * @author NK */ public JahiaDOMObject getSitePropsAsDOM( int id ) throws JahiaException{ Connection dbConn = null; Statement statement = null; String output = null; JahiaDBDOMObject dom = null; try { String sqlQuery = "SELECT * FROM jahia_site_prop where id_jahia_site=" + id; dbConn = getDBConnection(); statement = dbConn.createStatement(); if (statement != null) { ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,sqlQuery ); if (rs != null) { dom = new JahiaDBDOMObject(); dom.addTable("jahia_site_prop",rs); return dom; } } } catch (SQLException se) { String errorMsg = "Error in getSitePropsAsDOM(int id) : " + se.getMessage(); JahiaConsole.println( "JahiaSitesPersistance", errorMsg + " -> B AILING OUT" ); throw new JahiaException( "Cannot load data from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } return dom; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -