📄 jahiahomepagespersistance.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaHomepagesPersistance//// NK 17.12.2001//package org.jahia.services.homepages;import java.sql.*;import java.util.Vector;import java.util.Enumeration;import java.util.Hashtable;import org.jahia.services.acl.JahiaBaseACL;import org.jahia.services.acl.JahiaACLException;import org.jahia.registries.ServicesRegistry;import org.jahia.utils.JahiaTools;import org.jahia.utils.JahiaConsole;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;/** * persistance storage of jahia homepages information * * @author Khue ng */class JahiaHomepagesPersistance{ private static final String CLASS_NAME = JahiaHomepagesPersistance.class.getName(); /** unique instance */ private static JahiaHomepagesPersistance m_Instance = null; /** * constructor * */ protected JahiaHomepagesPersistance(){ } //-------------------------------------------------------------------------- /** * return the singleton instance of this class * */ public static synchronized JahiaHomepagesPersistance getInstance(){ if ( m_Instance == null ){ m_Instance = new JahiaHomepagesPersistance(); } return m_Instance; } //-------------------------------------------------------------------------- /** * return the list of all homepages * * @return a Vector of JahiaHomepage bean */ Vector getHomepages() throws JahiaException{ Connection dbConn = null; Statement statement = null; Vector v = new Vector(); JahiaHomepage hp = null; try { String sqlQuery = "SELECT * FROM jahia_homepages"; dbConn = getDBConnection(); statement = dbConn.createStatement(); if (statement != null) { ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,sqlQuery ); if (rs != null) { while (rs.next()) { hp = getHomepageFromResultSet(rs); hp.loadProperties(); v.add(hp); } } } } catch (SQLException se) { JahiaConsole.println( CLASS_NAME+".getHomepages", se.getMessage() ); throw new JahiaException( CLASS_NAME+".getHomepages", "Cannot load homepages from the database" + se.getMessage(), JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } return v; } //-------------------------------------------------------------------------- /** * return the JahiaHomepage bean looking at it id * * @param int the JahiaSite id * @return JahiaSite the JahiaSite bean */ JahiaHomepage load( int id ) throws JahiaException{ Connection dbConn = null; Statement statement = null; JahiaHomepage hp = null; try { String sqlQuery = "SELECT * FROM jahia_homepages where id_jahia_homepages =" + id; dbConn = getDBConnection(); statement = dbConn.createStatement(); if (statement != null) { ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,sqlQuery ); if (rs != null) { if ( rs.next() ){ hp = getHomepageFromResultSet(rs); hp.loadProperties(); } } } } catch (SQLException se) { JahiaConsole.println( CLASS_NAME+".load", se.getMessage() ); throw new JahiaException( CLASS_NAME+".load", "Cannot load homepage from the database" + se.getMessage(), JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } return hp; } //-------------------------------------------------------------------------- /** * save a homepage, create it if not exist * * @param JahiaHomepage the homepage bean */ void save( JahiaHomepage hp ) throws JahiaException{ if ( hp == null ) return; Connection dbConn = null; PreparedStatement pstmt = null; try { if ( hp.getID()>0 ){ update(hp); return; } int id = ServicesRegistry.getInstance() .getJahiaIncrementorsDBService() .autoIncrement("jahia_homepages"); if ( (hp.getSiteKey() == null) || hp.getSiteKey().trim().equals("") ) throw new JahiaException( CLASS_NAME+".save", "Invalid sitekey value", JahiaException.DATA_ERROR, JahiaException.ERROR ); if ( (hp.getName() == null) || hp.getName().trim().equals("") ) hp.setName("no name (" + id +")"); if ( (hp.getDescr() == null) ) hp.setDescr("no description"); dbConn = getDBConnection(); pstmt = dbConn.prepareStatement("INSERT INTO jahia_homepages VALUES(?,?,?,?,?,?)"); pstmt.setInt(1, id); pstmt.setString(2, hp.getName()); pstmt.setString(3, hp.getDescr()); pstmt.setString(4, hp.getSiteKey()); pstmt.setInt(5, hp.getType()); pstmt.setInt(6, hp.getAclID()); pstmt.execute(); hp.setID(id); } catch (java.sql.SQLException sqle) { String errorMsg = "Error in save(Jahiahomepage) : " + sqle.getMessage(); JahiaConsole.println( CLASS_NAME+".save", errorMsg ); throw new JahiaException( "Error saving a homepage in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement ((Statement)pstmt); } } //-------------------------------------------------------------------------- /** * remove a homepage * * @param int the home page id */ void delete( int id ) throws JahiaException{ try { String sqlQuery = "DELETE FROM jahia_homepages WHERE id_jahia_homepages=" + id; executeQueryNoResultSet(sqlQuery); } catch (JahiaException je) { String errorMsg = "Error in delete(int id) : " + je.getMessage(); JahiaConsole.println( CLASS_NAME+".delete", errorMsg ); throw new JahiaException( "Cannot delete homepage in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } } //-------------------------------------------------------------------------- /** * Update a home page definition * * @param JahiaHomepage the homepage bean */ void update( JahiaHomepage hp ) throws JahiaException { if ( hp == null ) return; if ( (hp.getSiteKey() == null) || hp.getSiteKey().trim().equals("") ) throw new JahiaException( CLASS_NAME+".save", "Invalid sitekey value", JahiaException.DATA_ERROR, JahiaException.ERROR ); if ( (hp.getName() == null) || hp.getName().trim().equals("") ) hp.setName("no name (" + hp.getID() +")"); if ( (hp.getDescr() == null) ) hp.setDescr("no description"); Connection dbConn = null; PreparedStatement pstmt = null; try { dbConn = getDBConnection(); pstmt = dbConn.prepareStatement("UPDATE jahia_homepages SET name_jahia_homepages=?, descr_jahia_homepages=?, sitekey_jahia_homepages=?, type_jahia_homepages=?, rights_jahia_homepages=? WHERE id_jahia_homepages=?"); pstmt.setString(1, hp.getName()); pstmt.setString(2, hp.getDescr()); pstmt.setString(3, hp.getSiteKey()); pstmt.setInt(4, hp.getType()); pstmt.setInt(5, hp.getAclID()); pstmt.setInt(6, hp.getID()); pstmt.execute(); } catch (java.sql.SQLException sqle) { String errorMsg = "Error in update : " + sqle.getMessage(); JahiaConsole.println( CLASS_NAME+".update", errorMsg ); throw new JahiaException( "Cannot update homepage in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement ((Statement)pstmt); } } //-------------------------------------------------------------------------- /** * Returns a properties as a String. * * @param JahiaHomepage hp , the homepage bean * @param String the property name */ String getProperty( JahiaHomepage hp , String key ) throws JahiaException { Connection dbConn = null; PreparedStatement pstmt = null; String value = null; if ( (hp == null) || (key == null) || (key.trim().equals("")) ) return null; try { dbConn = getDBConnection(); pstmt = dbConn.prepareStatement("SELECT value_homepages_prop FROM jahia_homepages_prop WHERE id_jahia_homepages=? AND name_homepages_prop=?"); pstmt.setInt(1, hp.getID()); pstmt.setString(2, key); ResultSet rs = pstmt.executeQuery(); if (rs != null) { if ( rs.next() ){ value = rs.getString("value_homepages_prop"); } } } catch (SQLException se) { JahiaConsole.println( CLASS_NAME+".getProperty(hp,key)", se.getMessage() ); throw new JahiaException( CLASS_NAME+".getProperty(hp,key)", "Cannot load homepage prop from the database" + se.getMessage(), JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement ((Statement)pstmt); } return value; } //-------------------------------------------------------------------------- /** * add a property of a home page. Do not check if there is alrady a same * property in db.You must remove it first. * * @param JahiaHomepage hp , the homepage bean * @param String the property name * @param String the property value */ void addProperty( JahiaHomepage hp , String key , String value ) throws JahiaException { if ( (hp == null) || (hp.getSiteKey() == null) || hp.getSiteKey().trim().equals("") || (key == null) || (key.trim().equals("")) ) return; Connection dbConn = null; PreparedStatement pstmt = null; try { dbConn = getDBConnection(); pstmt = dbConn.prepareStatement("INSERT INTO jahia_homepages_prop VALUES (?,?,?,?)"); pstmt.setInt(1, hp.getID()); pstmt.setString(2, hp.getSiteKey()); pstmt.setString(3, key); pstmt.setString(4, value); pstmt.execute();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -