📄 jahiafieldsdb.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaFieldsDB// EV 05.01.2001//// db_load_field( fieldID )// db_create_field( theField )// db_update_field( theField )// db_delete_field( theField )//package org.jahia.services.fields;import java.sql.*; // ResultSetimport java.util.*; // Vectorimport org.jahia.utils.*; // JahiaConsoleimport org.jahia.settings.*; // SettingsBeanimport org.jahia.data.*;import org.jahia.data.fields.*; // JahiaField, FieldTypes, LoadFlagsimport org.jahia.params.*; // ParamBeanimport org.jahia.sharing.*; // JahiaDataSourceManagerimport org.jahia.registries.*; // ServicesRegistryimport org.jahia.services.files.*; // Jahia Files Servicesimport org.jahia.services.fields.*; // Jahia Fields Servicesimport org.jahia.services.applications.*; // Jahia Applications Serviceimport org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;public class JahiaFieldsDB{ private SettingsBean jSettings; private JahiaFieldPropertiesDB props; /*** * constructor * */ public JahiaFieldsDB( SettingsBean jSettings ) { this.jSettings = jSettings; this.props = new JahiaFieldPropertiesDB(); } // end constructor /*** * loads all page fields from the database * * @param fieldID thefield ID * @return a Vector of JahiaField object * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public Vector db_load_all_fields_info_from_page( int pageID ) throws JahiaException { Vector result = new Vector();// String tmpValue = ""; Connection dbConn = null; Statement stmt = null; ResultSet rs = null; JahiaField theField = null; try { String sqlQuery = "SELECT * FROM jahia_fields_data " + "WHERE (pageid_jahia_fields_data=" + pageID + ")"; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(518); stmt = dbConn.createStatement(); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); if (rs != null) { while (rs.next()) { int jahiaID = rs.getInt ( "jahiaid_jahia_fields_data" ); int fieldID = rs.getInt ( "id_jahia_fields_data" ); int ctnid = rs.getInt ( "ctnid_jahia_fields_data" ); int fieldDefID = rs.getInt ( "fielddefid_jahia_fields_data" ); int typeField = rs.getInt ( "type_jahia_fields_data" ); int connectType = rs.getInt ( "connecttype_jahia_fields_data"); String fieldValue = rs.getString ( "value_jahia_fields_data" ); int rank = rs.getInt ( "rank_jahia_fields_data" ); int rights = rs.getInt ( "rights_jahia_fields_data" ); if (fieldValue.equals("<empty>")) { fieldValue = ""; } theField = ServicesRegistry.getInstance(). getJahiaFieldService().createJahiaField( fieldID, jahiaID, pageID, ctnid, fieldDefID, typeField, connectType, fieldValue, rank, rights ); props.db_load_field_properties( theField ); result.add (theField); } } } catch (SQLException se) { String errorMsg = "Error in db_load_all_fields_info_from_page : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> BAILING OUT" ); JahiaException je = new JahiaException( "Cannot load page's fields from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "db_load_all_fields_info_from_page : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } return result; } // end db_load_field /*** * loads a field from its id * * @param fieldID thefield ID * @return a JahiaField object, or null if the object could not * be found. * @see org.jahia.data.fields.LoadFlags * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public JahiaField db_load_field( int fieldID ) throws JahiaException {// String tmpValue = ""; Connection dbConn = null; Statement stmt = null; ResultSet rs = null; JahiaField theField = null; try { String sqlQuery = "SELECT * FROM jahia_fields_data " + "WHERE (id_jahia_fields_data=" + fieldID + ")"; dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(51); stmt = dbConn.createStatement(); rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); if (rs != null) { if (rs.next()) { int jahiaID = rs.getInt ( "jahiaid_jahia_fields_data" ); int pageID = rs.getInt ( "pageid_jahia_fields_data" ); int ctnid = rs.getInt ( "ctnid_jahia_fields_data" ); int fieldDefID = rs.getInt ( "fielddefid_jahia_fields_data" ); int typeField = rs.getInt ( "type_jahia_fields_data" ); int connectType = rs.getInt ( "connecttype_jahia_fields_data"); String fieldValue = rs.getString ( "value_jahia_fields_data" ); int rank = rs.getInt ( "rank_jahia_fields_data" ); int rights = rs.getInt ( "rights_jahia_fields_data" ); if (fieldValue.equals("<empty>")) { fieldValue = ""; } //theField = new JahiaField( fieldID, jahiaID, pageID, ctnid, fieldDefID, typeField, // connectType, fieldValue, rank, rights ); theField = ServicesRegistry.getInstance(). getJahiaFieldService().createJahiaField( fieldID, jahiaID, pageID, ctnid, fieldDefID, typeField, connectType, fieldValue, rank, rights );// if (!tmpValue.equals("")) {// theField.setObject( tmpValue );// } props.db_load_field_properties( theField ); } } } catch (SQLException se) { String errorMsg = "Error in db_load_field : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> BAILING OUT" ); JahiaException je = new JahiaException( "Cannot load fields from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.ERROR ); } finally { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) stmt.close(); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "db_load_field : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } return theField; } // end db_load_field /*** * creates a field entry in the database * do not call updatefield anymore, should do that in the fieldservice! * * @param theField theJahiaField object * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public void db_create_field( JahiaField theField ) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { // opens connection dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(52); stmt = dbConn.createStatement(); // creates empty line String sqlQuery = "INSERT INTO jahia_fields_data(id_jahia_fields_data) VALUES(" + theField.getID() + ")"; // 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", "db_create_field : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } // catches error if cannot execute insert query catch (SQLException se) { String errorMsg = "Error in create_jahia_field : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot insert new fields in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } } // end db_create_field /*** * updates a field entry in the database * * @param theField theJahiaField object * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public void db_update_field( JahiaField theField ) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { String theValue = theField.getValue(); if (theField.getValue().equals("")) { theValue = "<empty>"; } // composes the query String sqlQuery = "UPDATE jahia_fields_data SET "; sqlQuery += "jahiaid_jahia_fields_data = " + theField.getJahiaID() + ","; sqlQuery += "pageid_jahia_fields_data = " + theField.getPageID() + ","; sqlQuery += "ctnid_jahia_fields_data = " + theField.getctnid() + ","; sqlQuery += "fielddefid_jahia_fields_data = " + theField.getFieldDefID() + ","; sqlQuery += "type_jahia_fields_data = " + theField.getType() + ","; sqlQuery += "connecttype_jahia_fields_data = " + theField.getConnectType() + ","; sqlQuery += "value_jahia_fields_data = '" + theValue + "',"; sqlQuery += "rank_jahia_fields_data = " + theField.getRank() + ","; sqlQuery += "rights_jahia_fields_data = " + theField.getAclID() + " "; sqlQuery += "WHERE id_jahia_fields_data = " + theField.getID(); // executes the query dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(53); stmt = dbConn.createStatement(); ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); // updates field properties
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -