📄 jahiafielddefinitionsdb.java
字号:
while (subKeys.hasMoreElements()) { int pageDefID = ((Integer)subKeys.nextElement()).intValue(); JahiaFieldSubDefinition theSubDef = (JahiaFieldSubDefinition) subDefs.get( new Integer(pageDefID) ); // sets the subdef id if (theSubDef.getID() == 0) { int theSubDefID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService(). autoIncrement ( "jahia_fields_def_prop" ); theSubDef.setID( theSubDefID ); theSubDef.setFieldDefID(theFieldDef.getID()); } String defaultVal = theFieldDef.getDefaultValue( pageDefID ); if (defaultVal == null || "".equals(defaultVal)) { defaultVal = "<empty>"; // FIXME : Small text size should not be hardcoded } else if ( defaultVal.length() > 250 ) { String filePath = ServicesRegistry.getInstance() .getJahiaFieldService() .composeFieldDefDefaultValueFilePath(theFieldDef.getJahiaID(),theFieldDef.getName()); try { File f = new File(filePath); f.createNewFile(); FileUtils.getInstance() .writeFile(f.getAbsolutePath(),theFieldDef.getDefaultValue(pageDefID)); defaultVal = this.defaultValueFlatFileMarker; } catch ( Throwable t ){ t.printStackTrace(); } } if ((!theSubDef.getTitle().equals("")) && (theSubDef.getType() != -1)) { // composes the query sqlQuery = "INSERT INTO jahia_fields_def_prop ("; sqlQuery += "id_jahia_fields_def_prop,"; sqlQuery += "flddefid_jahia_fields_def_prop,"; sqlQuery += "pdefid_jahia_fields_def_prop,"; sqlQuery += "title_jahia_fields_def_prop,"; sqlQuery += "type_jahia_fields_def_prop,"; sqlQuery += "default_jahia_fields_def_prop) VALUES("; sqlQuery += theSubDef.getID() + ","; sqlQuery += theFieldDef.getID() + ","; sqlQuery += pageDefID + ","; sqlQuery += "'" + theSubDef.getTitle() + "',"; sqlQuery += theSubDef.getType() + ","; sqlQuery += "'" + defaultVal + "')"; ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); } } } // catches error if cannot execute update query catch (SQLException se) { String errorMsg = "Error in db_update_field_definition : " + se.getMessage(); JahiaConsole.println( "JahiaFieldDefinitionsDB", errorMsg + " -> BAILING OUT"); throw new JahiaException( "Cannot update fields definitions in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { if ( stmt != null ) stmt.close(); ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "db_update_field_definition : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end db_update_field_definition /*** * Deletes a field Definition * Warning: a same field def can be shared by different page definition * within a same site !!!! * * @param fieldDefID the field definition id * @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_delete_field_definition( int fieldDefID ) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { // composes the query String sqlQuery = "DELETE FROM "+jahia_fields_def+ " WHERE "+FIELD_FIELD_ID+"=" + fieldDefID; // executes the query dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(43); stmt = dbConn.createStatement(); ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); } // catches error if cannot execute update query catch (SQLException se) { String errorMsg = "Error in db_delete_field_definition : " + se.getMessage(); JahiaConsole.println( "JahiaFieldDefinitionsDB", errorMsg + " -> BAILING OUT"); throw new JahiaException( "Cannot delete fields definitions in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { if ( stmt != null ) stmt.close(); ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "db_delete_field_definition : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end db_delete_field_definition /*** * Deletes all field sub def of a page definition * * @param pageDefID the page definition id * * @exception throws a critical JahiaException if SQL error * @exception throws a warning JahiaException if cannot free resources * */ public void db_delete_field_sub_definition( int pageDefID ) throws JahiaException { Connection dbConn = null; Statement stmt = null; try { // composes the query String sqlQuery = "DELETE FROM jahia_fields_def_prop " + " WHERE pdefid_jahia_fields_def_prop="+ pageDefID; // executes the query dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection(43); stmt = dbConn.createStatement(); ServicesRegistry.getInstance().getDBPoolService().executeUpdate( stmt,sqlQuery ); } // catches error if cannot execute update query catch (SQLException se) { String errorMsg = "Error in db_delete_field_sub_definition : " + se.getMessage(); JahiaConsole.println( "JahiaFieldDefinitionsDB", errorMsg + " -> BAILING OUT"); throw new JahiaException( "Cannot delete fields sub definitions in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { try { if ( stmt != null ) stmt.close(); ServicesRegistry.getInstance().getDBPoolService().freeConnection(dbConn); } catch ( SQLException ex ) { JahiaException je = new JahiaException( "Cannot free resources", "db_delete_field_sub_definition : cannot free resources", JahiaException.DATABASE_ERROR, JahiaException.WARNING ); } } } // end db_delete_field_sub_definition //-------------------------------------------------------------------------- /** * return a DOM document of all field def of a site * * @param int the site id * * @return JahiaDOMObject a DOM representation of this object * * @author NK */ public JahiaDOMObject getFieldDefsAsDOM( int siteID ) throws JahiaException{ Connection dbConn = null; Statement statement = null; String output = null; JahiaDBDOMObject dom = null; try { String sqlQuery = "SELECT * FROM jahia_fields_def where jahiaid_jahia_fields_def=" + siteID; dbConn = getDBConnection(0); statement = dbConn.createStatement(); if (statement != null) { ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,sqlQuery ); if (rs != null) { dom = new JahiaDBDOMObject(); dom.addTable("jahia_fields_def",rs); return dom; } } } catch (SQLException se) { String errorMsg = "Error in getFieldDefsAsDOM(int siteID) : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> B AILING OUT" ); throw new JahiaException( "Cannot load field def from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } return dom; } //-------------------------------------------------------------------------- /** * return a DOM document of all field def props of a site * * @param int the site id * * @return JahiaDOMObject a DOM representation of this object * * @author NK */ public JahiaDOMObject getFieldDefPropsAsDOM( int siteID ) throws JahiaException{ Connection dbConn = null; Statement statement = null; String output = null; JahiaDBDOMObject dom = null; try { String sqlQuery = "SELECT DISTINCT jahia_fields_def_prop.id_jahia_fields_def_prop," +"jahia_fields_def_prop.flddefid_jahia_fields_def_prop,jahia_fields_def_prop.pdefid_jahia_fields_def_prop," +"jahia_fields_def_prop.title_jahia_fields_def_prop,jahia_fields_def_prop.type_jahia_fields_def_prop," +"jahia_fields_def_prop.default_jahia_fields_def_prop" +" FROM jahia_fields_def_prop,jahia_fields_def" +" where jahia_fields_def_prop.flddefid_jahia_fields_def_prop=" + " jahia_fields_def.id_jahia_fields_def AND jahia_fields_def.jahiaid_jahia_fields_def="+ siteID ; dbConn = getDBConnection(0); statement = dbConn.createStatement(); if (statement != null) { ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,sqlQuery ); if (rs != null) { dom = new JahiaDBDOMObject(); dom.addTable("jahia_fields_def_prop",rs); return dom; } } } catch (SQLException se) { String errorMsg = "Error in getFieldDefPropsAsDOM(int siteID) : " + se.getMessage(); JahiaConsole.println( "JahiaFieldsDB", errorMsg + " -> B AILING OUT" ); throw new JahiaException( "Cannot load field def props from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } return dom; } //------------------------------------------------------------------------- private Connection getDBConnection (int debugInfo) { Connection dbConn = null; try { if ( debugInfo != 0 ){ dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection (debugInfo); } else { dbConn = ServicesRegistry.getInstance().getDBPoolService().getConnection (); } } catch (NullPointerException ex) { JahiaConsole.println ("JahiaPagesDB", "Null Pointer Exception, DB Pool Service instance might be null!"); } catch (SQLException ex) { JahiaConsole.println ("JahiaPagesDB", "SQL Exception: cannot get a connection."); } return dbConn; } //------------------------------------------------------------------------- private void closeDBConnection (Connection dbConn) { if (dbConn != null) { try { ServicesRegistry.getInstance().getDBPoolService().freeConnection (dbConn); } 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 ("JahiaPagesDB", "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(); } } 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); } } /////////////////////////////////////////////////////////////////////////// // PRIVATE DEFINITIONS /////////////////////////////////////////////////////////////////////////// private static final String jahia_fields_def = "jahia_fields_def"; private static final String FIELD_FIELD_ID = "id_"+jahia_fields_def; private static final String FIELD_JAHIA_ID = "jahiaid_"+jahia_fields_def; private static final String FIELD_PAGE_DEF_ID = "pagedefid_"+jahia_fields_def; private static final String FIELD_NAME = "name_"+jahia_fields_def; private static final String FIELD_TITLE = "title_"+jahia_fields_def; private static final String FIELD_TYPE = "type_"+jahia_fields_def; private static final String FIELD_DEFAULT_VALUE = "defaultvalue_"+jahia_fields_def;} // end FieldDefinitionsDBManager
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -