📄 jahiafielddefinitionsdb.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// JahiaFieldDefinitionsDB// EV 05.01.2001//// db_load_field_definition( defID ) -> JahieFieldDefinition// db_create_field_definition( theFieldDefinition )// db_update_field_definition( theFieldDefinition )// db_delete_field_definition( defID )//package org.jahia.services.fields;import java.io.*;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.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;public class JahiaFieldDefinitionsDB{ public static final String defaultValueFlatFileMarker = "<defvalue_in_flat_file>"; /*** * constructor * */ public JahiaFieldDefinitionsDB() { } // end constructor /*** * loads a field definition by its id * * @param theDefID the field definition ID * @return a JahiaFieldDefinition object * @see org.jahia.data.fields.JahiaFieldDefinition * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public JahiaFieldDefinition db_load_field_definition( int theDefID ) throws JahiaException { Connection dbConn = null; Statement stmt = null; ResultSet rs = null; ResultSet rs2 = null; JahiaFieldDefinition theFieldDef = null; try { // creates connexion dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(40); stmt = dbConn.createStatement(); // prepares sqlQuery String sqlQuery = "SELECT * FROM jahia_fields_def "; sqlQuery += "WHERE id_jahia_fields_def = " + theDefID; rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); if (rs.next()) { int ID = rs.getInt ( "id_jahia_fields_def" ); int jahiaID = rs.getInt ( "jahiaid_jahia_fields_def" ); String name = rs.getString ( "name_jahia_fields_def" ); sqlQuery = "SELECT * FROM jahia_fields_def_prop "; sqlQuery += "WHERE flddefid_jahia_fields_def_prop=" + ID; rs2 = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); Hashtable subDefs = new Hashtable(); while (rs2.next()) { int subDefID = rs2.getInt ( "id_jahia_fields_def_prop" ); int templateID = rs2.getInt ( "pdefid_jahia_fields_def_prop" ); String title = rs2.getString ( "title_jahia_fields_def_prop" ); int type = rs2.getInt ( "type_jahia_fields_def_prop" ); String defaultValue = rs2.getString ( "default_jahia_fields_def_prop" ); if (defaultValue.equals("<empty>")) { defaultValue = ""; } else { String filePath = ServicesRegistry.getInstance() .getJahiaFieldService() .composeFieldDefDefaultValueFilePath(jahiaID,name); File f = new File(filePath); if ( f.exists() ){ try { defaultValue = FileUtils.getInstance().readFile(f.getAbsolutePath()); } catch ( Throwable t ){ t.printStackTrace(); } } } subDefs.put( new Integer(templateID), new JahiaFieldSubDefinition( subDefID, ID, templateID, title, type, defaultValue ) ); } theFieldDef = new JahiaFieldDefinition( ID, jahiaID, name, subDefs ); } } catch (SQLException se) { String errorMsg = "Error in db_load_field_definition : " + se.getMessage(); JahiaConsole.println ("JahiaFieldDefinitionsDB", errorMsg + " -> BAILING OUT"); throw new JahiaException ("Cannot load fields 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_field_definition : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } return theFieldDef; } // end db_load_field_definition /*** * loads a field definition by its name and its site id * * @param siteID the site id * @param name the field name * @return a JahiaFieldDefinition object * @see org.jahia.data.fields.JahiaFieldDefinition * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public JahiaFieldDefinition db_load_field_definition( int siteID, String name ) throws JahiaException { Connection dbConn = null; Statement stmt = null; ResultSet rs = null; ResultSet rs2 = null; JahiaFieldDefinition theFieldDef = null; try { // creates connexion dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(40); stmt = dbConn.createStatement(); // prepares sqlQuery String sqlQuery = "SELECT * FROM jahia_fields_def "; sqlQuery += "WHERE jahiaid_jahia_fields_def = " + siteID + " and name_jahia_fields_def='" + name +"'"; rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); if (rs.next()) { int ID = rs.getInt ( "id_jahia_fields_def" ); int jahiaID = rs.getInt ( "jahiaid_jahia_fields_def" ); name = rs.getString ( "name_jahia_fields_def" ); sqlQuery = "SELECT * FROM jahia_fields_def_prop "; sqlQuery += "WHERE flddefid_jahia_fields_def_prop=" + ID; rs2 = ServicesRegistry.getInstance().getDBPoolService().executeQuery( stmt,sqlQuery ); Hashtable subDefs = new Hashtable(); while (rs2.next()) { int subDefID = rs2.getInt ( "id_jahia_fields_def_prop" ); int templateID = rs2.getInt ( "pdefid_jahia_fields_def_prop" ); String title = rs2.getString ( "title_jahia_fields_def_prop" ); int type = rs2.getInt ( "type_jahia_fields_def_prop" ); String defaultValue = rs2.getString ( "default_jahia_fields_def_prop" ); if (defaultValue.equals("<empty>")) { defaultValue = ""; } else if ( defaultValue.equals(this.defaultValueFlatFileMarker) ){ String filePath = ServicesRegistry.getInstance() .getJahiaFieldService() .composeFieldDefDefaultValueFilePath(siteID,name); File f = new File(filePath); if ( f.exists() ){ try { defaultValue = FileUtils.getInstance().readFile(f.getAbsolutePath()); } catch ( Throwable t ){ t.printStackTrace(); } } } subDefs.put( new Integer(templateID), new JahiaFieldSubDefinition( subDefID, ID, templateID, title, type, defaultValue ) ); } theFieldDef = new JahiaFieldDefinition( ID, jahiaID, name, subDefs ); } } catch (SQLException se) { String errorMsg = "Error in db_load_field_definition : " + se.getMessage(); JahiaConsole.println ("JahiaFieldDefinitionsDB", errorMsg + " -> BAILING OUT"); throw new JahiaException ("Cannot load fields 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_field_definition : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } return theFieldDef; } // end db_load_field_definition /*** * creates a field definition, and assignes it a new ID * * @param theFieldDef the Field Definition * @see org.jahia.data.fields.JahiaFieldDefinition * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public void db_create_field_definition( JahiaFieldDefinition theFieldDef ) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { // opens connection dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(41); stmt = dbConn.createStatement(); // gets the field id ServicesRegistry sr = ServicesRegistry.getInstance(); int theFieldDefID = sr.getJahiaIncrementorsDBService().autoIncrement ("jahia_fields_def"); theFieldDef.setID( theFieldDefID ); // saves definition base String sqlQuery = "INSERT INTO jahia_fields_def ("; sqlQuery += "id_jahia_fields_def,"; sqlQuery += "jahiaid_jahia_fields_def,"; sqlQuery += "name_jahia_fields_def) VALUES("; sqlQuery += theFieldDef.getID() + ", "; sqlQuery += theFieldDef.getJahiaID() + ", "; sqlQuery += "'" + theFieldDef.getName() + "')"; ServicesRegistry.getInstance().getDBPoolService().executeUpdate (stmt,sqlQuery); try { ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); if ( stmt != null ) { stmt.close(); } } catch (SQLException ex) { JahiaException je = new JahiaException ("Cannot free resources", "create_jahia_field_definition : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } // enters values with update_jahia_field db_update_field_definition (theFieldDef); } // catches error if cannot execute insert query catch (SQLException se) { String errorMsg = "Error in create_jahia_field_definition : " + se.getMessage(); JahiaConsole.println( "JahiaFieldDefinitionsDB", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot insert new fields definitions in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } } // end db_create_field_definition /*** * updates a field definition * * @param theFieldDef the Field Definition * @see org.jahia.data.fields.JahiaFieldDefinition * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public void db_update_field_definition( JahiaFieldDefinition theFieldDef ) throws JahiaException { Connection dbConn = null; Statement stmt = null; String sqlQuery = ""; try { // creates the connexion dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(42); stmt = dbConn.createStatement(); // deletes all precedent sub definitions sqlQuery = "DELETE FROM jahia_fields_def_prop "; sqlQuery += "WHERE flddefid_jahia_fields_def_prop=" + theFieldDef.getID(); ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); // saves all sub definitions Hashtable subDefs = theFieldDef.getSubDefs(); Enumeration subKeys = subDefs.keys();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -