⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jahiapagetemplatedb.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//package org.jahia.services.pages;import java.sql.*;                      // ResultSetimport java.util.*;                     // Vectorimport org.jahia.utils.JahiaTools;import org.jahia.utils.JahiaConsole;import org.jahia.registries.ServicesRegistry;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.services.pages.JahiaPageDefinition;import org.jahia.services.acl.*;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;public class JahiaPageTemplateDB{        private static final String CLASS_NAME = JahiaPageTemplateDB.class.getName();        private static JahiaPageTemplateDB mObject = null;    //-------------------------------------------------------------------------    /**     * Default constructor     */    private JahiaPageTemplateDB ()    {    }    //-------------------------------------------------------------------------    public static JahiaPageTemplateDB getInstance()    {        if (mObject == null) {            mObject = new JahiaPageTemplateDB ();        }        return mObject;    }    //-------------------------------------------------------------------------    /***                                              * load a page template by its template id        *        * @param        defID               the template id        *        */    public synchronized JahiaPageDefinition loadPageTemplate (int templateID)        throws JahiaException	{  		Connection dbConn = null;        Statement statement = null;        JahiaPageDefinition theTemplate = null;	    try {            String sqlQuery = "SELECT * FROM jahia_pages_def "+                              "WHERE id_jahia_pages_def=" + templateID;	        dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(65);            statement = dbConn.createStatement();            if (statement != null) {                ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,sqlQuery );                if (rs != null) {                    if (rs.next()) {                        theTemplate = readTemplateFromResultSet (rs);                    }                }            }	    } catch (SQLException se) {	        String errorMsg = "Error in db_load_page_definition : " + se.getMessage();            JahiaConsole.println( "JahiaPageTemplateDB", errorMsg + " -> BAILING OUT" );	        throw new JahiaException(   "Cannot load page data from the database",	                                    errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL );	    } finally {			try {				ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn);                if ( statement != null ) statement.close();			} catch ( SQLException ex ) {			    JahiaException je = new JahiaException(   "Cannot free resources",                                        "loadPageTemplate : cannot free resources",	                                    JahiaException.DATABASE_ERROR, JahiaException.WARNING );			}        }				// load the properties		theTemplate.setProperties(JahiaPageDefinitionPropDB.getProperties(theTemplate.getID()));                 return theTemplate;    }    //-------------------------------------------------------------------------    /** Create a new page template entry in the table. The passed page     *  template will receive a new unique identification number.     *     * @param   theDef  The JahiaPageDefinition object to create.     *     * @exception   JahiaException     *      Throws this object if any error occurs while accessing the database.     */    public synchronized boolean insertPageTemplate (JahiaPageDefinition thePageTemplate)        throws JahiaException	{        // Get the database connection.        Connection dbConn = getDBConnection (32001);        if (dbConn == null) {            throw new JahiaException (                    "Page template creation error on database access",                    "Cannot insert the new page template in the database, could not obtain a DB connection.",                    JahiaException.DATABASE_ERROR, JahiaException.CRITICAL );        }        Statement statement = null;        boolean result = false;	    try {	        // creates empty line            String sqlQuery = "INSERT INTO jahia_pages_def(id_jahia_pages_def) "+                              "VALUES(" + thePageTemplate.getID() + ")";            statement = dbConn.createStatement();            if (statement != null) {                // executes the query                statement.execute (sqlQuery);	            closeDBConnection (dbConn);    	        closeStatement (statement);                // enters values with db_update_jahia_field                updatePageTemplate (thePageTemplate);                result = true;            }	    }	    catch (SQLException se)	    {            String errorMsg = "Error in insertPageTemplate : " + se.getMessage();            JahiaConsole.println( "JahiaPageTemplateDB", errorMsg + " -> BAILING OUT" );            throw new JahiaException(   "Cannot insert new page template in the database",	                                    errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL );	    } finally {            closeDBConnection (dbConn);            closeStatement (statement);        }				// save properties		JahiaPageDefinitionPropDB.setProperties(thePageTemplate,thePageTemplate.getProperties());				return result;    }    //-------------------------------------------------------------------------    /***     * updates a page template     *     * @param        theDef              the JahiaPageDefinition to update     *     */    public synchronized void updatePageTemplate (JahiaPageDefinition thePageTemplate)        throws JahiaException	{  		Connection dbConn = null;        Statement statement = null;	    try {	        // checks that there is no null string	        String errorMsg = "";            if (thePageTemplate.getName().equals("")) {    	        errorMsg = "Error in db_update_page_definition : page name value is an empty string";	        }	        if (!errorMsg.equals("")) {                JahiaConsole.println( "JahiaPageTemplateDB", errorMsg );                JahiaConsole.println( "JahiaPageTemplateDB", errorMsg + " -> BAILING OUT" );                throw new JahiaException(   "Cannot update pages template in the database : page name value is an empty string",	                                        errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL );            }	        // composes the query            StringBuffer sqlQuery = new StringBuffer ("UPDATE jahia_pages_def SET ");            sqlQuery.append ("jahiaid_jahia_pages_def = " + thePageTemplate.getJahiaID() + ",");            sqlQuery.append ("name_jahia_pages_def = '" + JahiaTools.quote(thePageTemplate.getName()) + "',");            sqlQuery.append ("sourcepath_jahia_pages_def = '" + JahiaTools.quote(thePageTemplate.getSourcePath()) + "', ");            if ( thePageTemplate.isAvailable() ){               sqlQuery.append ("visible_jahia_pages_def = 1, ");			} else {               sqlQuery.append ("visible_jahia_pages_def = 0, ");			}            // dummy values, no used anymore            sqlQuery.append ("browsable_jahia_pages_def = 1 ,");            sqlQuery.append ("warning_msg_jahia_pages_def = '', ");                        sqlQuery.append ("img_jahia_pages_def = '" + JahiaTools.quote(thePageTemplate.getImage()) + "' ");            sqlQuery.append ("WHERE id_jahia_pages_def=" + thePageTemplate.getID());	        // executes the query	        dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(67);            statement = dbConn.createStatement();            if (statement != null) {            	//System.out.println(" query is " + sqlQuery.toString());                statement.executeUpdate( sqlQuery.toString() );            }	    }	    // catches error if cannot execute update query	    catch (SQLException se)	    {	        String errorMsg = "Error in db_update_page_definition : " + se.getMessage();            JahiaConsole.println( "JahiaPageTemplateDB", errorMsg + " -> BAILING OUT");            throw new JahiaException(   "Cannot update page template in the database",	                                    errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL );	    } finally {            closeDBConnection (dbConn);            closeStatement (statement);        }		// save properties		JahiaPageDefinitionPropDB.setProperties(thePageTemplate,thePageTemplate.getProperties());            }    //-------------------------------------------------------------------------    /***     * deletes a page template     *     * @param        defID               the page template ID to delete     *     */    public synchronized void deletePageTemplate (int templateID)        throws JahiaException	{  		Connection dbConn = null;        Statement statement = null;	    try {	        // composes the query            String sqlQuery = "DELETE from jahia_pages_def "+                              "WHERE id_jahia_pages_def="+templateID;	        // executes the query	        dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(68);            statement = dbConn.createStatement();            if (statement != null) {                ServicesRegistry.getInstance().getDBPoolService().executeUpdate( statement,sqlQuery );            }	    }	    // catches error if cannot execute update query	    catch (SQLException se)	    {	        String errorMsg = "Error in db_delete_page_definition : " + se.getMessage();            JahiaConsole.println( "JahiaPageTemplateDB", errorMsg + " -> BAILING OUT");            throw new JahiaException(   "Cannot delete page template in the database",	                                    errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL );	    } finally {            closeDBConnection (dbConn);            closeStatement (statement);      	}		// delete properties		JahiaPageDefinitionPropDB.removeProperties(templateID);          }    //-------------------------------------------------------------------------    /**     * Check if a page has a same location as the gived path     *     * @param (String) path, the full path including the filename     *                 to the template file.     * @return (JahiaPageDefinition) a JahiaPageDefinition with same source path     *         or null if not found     *     */    public synchronized int getPageTemplateIDMatchingSourcePath (int siteID, String path)        throws JahiaException    {  		Connection dbConn = null;        Statement statement = null;        int templateID = -1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -