📄 containerfilterbean.java
字号:
*/ public boolean getNumberFiltering() { return this.numberFiltering; } //-------------------------------------------------------------------------- /** * Return the vector of clauses. * * @return Vector, the list of FilterClause bean. */ public Vector getClauses() { return this.clauses; } //-------------------------------------------------------------------------- /** * Perform filtering. * The expected result is a bit set of matching container ids. * * @param int ctnListID, the container list id * @return BitSet bits, the expected result as a bit set of matching ctn ids,each bit position set to true correspond to matching ctn ids. */ public BitSet doFilter(int ctnListID) throws JahiaException { if ( (this.getFieldName()==null) || (this.getFieldName().trim().equals("")) || (this.getClauses()==null) || (this.getClauses().size()==0) ) { return null; } BitSet result = null; if ( !needNumberValueFiltering(ctnListID) ) { result = doStringValueFiltering(ctnListID); } else { result = doNumberValueFiltering(ctnListID); } return result; } //-------------------------------------------------------------------------- /** * Return the select statement, build with the clauses for a given container list id. * * @param int ctnListID, the container list id * @return String , the sql statement. Null on error */ public String getSelect(int ctnListID) { //JahiaConsole.println("ContainerFilterBean.getSelect","Started"); if ( (this.getFieldName()==null) || (this.getFieldName().trim().equals("")) || (this.getClauses()==null) || (this.getClauses().size()==0) ) { return null; } StringBuffer buff = new StringBuffer("SELECT DISTINCT id_jahia_ctn_entries FROM jahia_ctn_entries a, jahia_fields_data b, jahia_fields_def c WHERE listid_jahia_ctn_entries="); buff.append(ctnListID); buff.append(" AND ( a.id_jahia_ctn_entries = b.ctnid_jahia_fields_data AND b.fielddefid_jahia_fields_data = c.id_jahia_fields_def AND c.name_jahia_fields_def='"); buff.append(JahiaTools.quote(this.getFieldName())); buff.append("' AND ("); FilterClause fClause = null; int size = this.clauses.size(); for ( int i=0 ; i<size ; i++ ) { fClause = (FilterClause)this.clauses.get(i); if ( fClause != null && fClause.isValid() ) { StringBuffer clauseBuff = new StringBuffer("("); if ( !fClause.isRangeClause() ) { String[] values = fClause.getValues(); for (int j=0 ; j<values.length; j++) { // TODO : hollis // need to review encode/decode special car String val = FormDataManager.getInstance().encode(values[j]); val = FormDataManager.getInstance().encode(val); clauseBuff.append(FIELD_VALUE); clauseBuff.append(fClause.getComp()); clauseBuff.append("'"); clauseBuff.append(val); clauseBuff.append("'"); if ( j<(values.length-1) ){ clauseBuff.append(" OR "); } } clauseBuff.append(")"); } else { String val = FormDataManager.getInstance().encode(fClause.getLowerValue()); val = FormDataManager.getInstance().encode(val); clauseBuff.append(FIELD_VALUE); clauseBuff.append(fClause.getLowerComp()); clauseBuff.append("'"); clauseBuff.append(val); clauseBuff.append("' AND "); clauseBuff.append(FIELD_VALUE); clauseBuff.append(fClause.getUpperComp()); clauseBuff.append("'"); val = FormDataManager.getInstance().encode(fClause.getUpperValue()); val = FormDataManager.getInstance().encode(val); clauseBuff.append(val); clauseBuff.append("')"); } buff.append(clauseBuff.toString()); if ( i<size-1 ) { buff.append(" OR "); } } } buff.append("))"); //JahiaConsole.println("ContainerFilterBean.getSelect","field filter query : " + buff.toString()); return buff.toString(); } //-------------------------------------------------------------------------- /** * Perform filtering for TEXT like fields. * * The expected result is a bit set of matching container ids. * * @param int ctnListID, the container list id * @return BitSet bits, the expected result as a bit set of matching ctn ids,each bit position set to true correspond to matching ctn ids. */ private BitSet doStringValueFiltering(int ctnListID) throws JahiaException { String fieldFilterQuery = getSelect(ctnListID); if ( fieldFilterQuery == null && !fieldFilterQuery.trim().equals("") ){ return null; } BitSet bits = new BitSet(); Connection dbConn = null; Statement stmt = null; ResultSet rs = null; try { dbConn = getDBConnection(9000); stmt = dbConn.createStatement(); rs = stmt.executeQuery( fieldFilterQuery ); /* JahiaConsole.println ("ContainerFilterBean.doStringValueFiltering", "Field : " + getFieldName() + "\nExecuted Filter query : " + fieldFilterQuery + "\n"); */ while (rs.next()) { bits.set(rs.getInt( "id_jahia_ctn_entries" )); } } catch (SQLException se) { String errorMsg = "Error in doStringValueFiltering : " + se.getMessage(); JahiaConsole.println ("ContainerFilterBean.doStringValueFiltering", errorMsg); } finally { closeDBConnection (dbConn); closeStatement (stmt); } return bits; } //-------------------------------------------------------------------------- /** * Perform filtering for NUMBER like fields. * Container field values are loaded and converted to long representation before * filtering comparison. * * The expected result is a bit set of matching container ids. * * @param int ctnListID, the container list id * @return BitSet bits, the expected result as a bit set of matching ctn ids,each bit position set to true correspond to matching ctn ids. */ private BitSet doNumberValueFiltering(int ctnListID) throws JahiaException { //JahiaConsole.println(CLASS_NAME+".doNumberValueFiltering","Started for ctnlist ["+ctnListID+"]"); BitSet result = new BitSet(); if ( (this.getFieldName()==null) || (this.getFieldName().trim().equals("")) || (this.getClauses()==null) || (this.getClauses().size()==0) ) { return null; } Hashtable datas = getFieldValues(ctnListID, fieldName); //JahiaConsole.println(CLASS_NAME+".doNumberValueFiltering","Nb datas : " + datas.size()); if ( datas == null || datas.size()==0 ){ return result; } FilterClause fClause = null; Enumeration keys = datas.keys(); Integer I = null; String S = null; Long L = null; while ( keys.hasMoreElements() ) { I = (Integer)keys.nextElement(); S = (String)datas.get(I); L = null; try { L = Long.valueOf(S); } catch ( Throwable t ){ } if ( L != null ) { boolean match = false; int size = this.clauses.size(); int i = 0; while ( (i<size) && !match ) { fClause = (FilterClause)this.clauses.get(i); if ( fClause != null && fClause.isValid() ) { match = fClause.compare(L.longValue()); } i++; } if ( match ) { result.set(I.intValue()); } } } return result; } //-------------------------------------------------------------------------- /** * Load an hashtable of pair/value (ctnID,fieldValue) for a given ctnlist and a given fieldName. * * * @param int ctnListID, the container list id * @param String fieldName, the fieldName * @return Hashtable. */ private Hashtable getFieldValues(int ctnListID, String fieldName) throws JahiaException { StringBuffer buff = new StringBuffer("SELECT DISTINCT ctnid_jahia_fields_data,value_jahia_fields_data FROM jahia_ctn_entries a, jahia_fields_data b, jahia_fields_def c WHERE listid_jahia_ctn_entries="); buff.append(ctnListID); buff.append(" AND ( a.id_jahia_ctn_entries = b.ctnid_jahia_fields_data AND b.fielddefid_jahia_fields_data = c.id_jahia_fields_def AND c.name_jahia_fields_def='"); buff.append(JahiaTools.quote(fieldName)); buff.append("' )"); String query = buff.toString(); Connection dbConn = null; Statement stmt = null; ResultSet rs = null; Hashtable datas = new Hashtable(); try { dbConn = getDBConnection(9000); stmt = dbConn.createStatement(); rs = stmt.executeQuery( buff.toString() ); /* JahiaConsole.println ("ContainerFilterBean.getFieldValues", "Field : " + getFieldName() + "\nExecuted Filter query : " + query + "\n"); */ String fieldValue = null; int ctnID = 0; while (rs.next()) { try { ctnID = rs.getInt( "ctnid_jahia_fields_data" ); fieldValue = rs.getString( "value_jahia_fields_data" ); datas.put(new Integer(ctnID),fieldValue); } catch ( Throwable t ){ // bypass undefined values } } } catch (SQLException se) { String errorMsg = "Error in getFieldValues : " + se.getMessage(); JahiaConsole.println ("ContainerFilterBean.getFieldValues", errorMsg); } finally { closeDBConnection (dbConn); closeStatement (stmt); } return datas; } //-------------------------------------------------------------------------- /** * Check if a field values must be converted to long representation before comparison or not * * @param int , the container list id * @return boolean , true is a Long Value comparison is needed */ private boolean needNumberValueFiltering(int ctnListID) throws JahiaException { if ( this.numberFiltering ){ return true; } // TODO: text or number filtering should be defined by the field type. // actually, it is defined by the template designer who can force number filtering or not. return false; } //-------------------------------------------------------------------------- /** * Check if the comparator is valid * * @param String comparator, the comparator */ private boolean checkComparator(String comparator){ if ( comparator == null ) return false; return ( comparator.equals(COMP_EQUAL) || comparator.equals(COMP_SMALLER) || comparator.equals(COMP_SMALLER_OR_EQUAL) || comparator.equals(COMP_BIGGER_OR_EQUAL) || comparator.equals(COMP_BIGGER) ); } //------------------------------------------------------------------------- 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 ("ContainerFilter", "Null Pointer Exception, DB Pool Service instance might be null!"); } catch (SQLException ex) { JahiaConsole.println ("ContainerFilter", "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 ("ContainerFilter", "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); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -