📄 containersorterbean.java
字号:
return this.ASC_Ordering = true; } //-------------------------------------------------------------------------- /** * Set ASC ordering. * */ public boolean setAscOrdering(boolean val) { return this.ASC_Ordering = val; } //-------------------------------------------------------------------------- /** * Return the container list id. * * @return int ctnListID, the container list id. */ public int getCtnListID() { return this.ctnListID; } //-------------------------------------------------------------------------- /** * Return the sorting field. * * @return int ctnListID, the container list id. */ public String getSortingFieldName() { return this.fieldName; } //-------------------------------------------------------------------------- /** * Return the last sorting time. * * @return long , the last sorting time */ public long getLastSortingTime() { return this.lastSortingTime; } //-------------------------------------------------------------------------- /** * Return true if the sorter initialited properly * * @return boolean, the valid state value. */ public boolean isValid() { return this.isValid; } //-------------------------------------------------------------------------- /** * Return the update status. Each time the doSort method is called, this update status is set to true. * * @return boolean, the internal updated status value. */ public boolean getUpdateStatus() { return this.updated; } //-------------------------------------------------------------------------- /** * Set the update status to true. * */ public void setUpdateStatus() { this.updated = true; } //-------------------------------------------------------------------------- /** * You can reset the internal update status by setting it to false * */ public void resetUpdateStatus() { this.updated = false; } //-------------------------------------------------------------------------- /** * Containers are sorted after sorting field's data are loaded and converted to * a long representation. * * @param BitSet bits, any bit position sset to true must correspond to a ctn id to include in the result. * if you want all ctn ids in the result, gieve a null BitSet. * @return Vector, vector of sorted ctn ids. */ private Vector doNumberSort(BitSet bits) { Vector datas = new Vector(); Vector results = new Vector(); // retrieve data 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; try { dbConn = getDBConnection(10000); stmt = dbConn.createStatement(); rs = stmt.executeQuery( buff.toString() ); /* JahiaConsole.println ("ContainerSorterBean.getFieldValues", "Field : " + getFieldName() + "\nExecuted Filter query : " + query + "\n"); */ String fieldValue = null; int ctnID = 0; long longValue = 0; while (rs.next()) { try { ctnID = rs.getInt( "ctnid_jahia_fields_data" ); if ( (bits == null) || bits.get(ctnID) ) { fieldValue = rs.getString( "value_jahia_fields_data" ); try { longValue = Long.parseLong(fieldValue); } catch ( Throwable t ){ longValue = -1; // } DataBean dataBean = new DataBean(ctnID,longValue); datas.add(dataBean); } } catch ( Throwable t ){ // bypass undefined values } } // sort the datas if ( datas.size()>1 ){ // a dummy dataBean DataBean dummyDataBean = new DataBean(ASC_Ordering); Collections.sort(datas,dummyDataBean); } // retrieve sorted ids int size = datas.size(); DataBean dataBean = null; for ( int i=0; i<size ; i++ ){ dataBean = (DataBean)datas.get(i); results.add(new Integer(dataBean.ctnID)); } } catch (SQLException se) { String errorMsg = "Error in getFieldValues : " + se.getMessage(); JahiaConsole.println ("ContainerSorterBean.getFieldValues", errorMsg); } finally { closeDBConnection (dbConn); closeStatement (stmt); } return results; } //------------------------------------------------------------------------- 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); } } //-------------------------------------------------------------------------- private class DataBean implements Comparator { int ctnID = 0; long value = -1; boolean ASC_Ordering = true; public DataBean (int ctnID, long value) { this.ctnID = ctnID; this.value = value; } public DataBean (boolean ASC_Ordering) { this.ASC_Ordering = ASC_Ordering; } public int compare(Object obj1, Object obj2) throws ClassCastException { DataBean dataBean1 = (DataBean)obj1; DataBean dataBean2 = (DataBean)obj2; if ( dataBean1.value == dataBean2.value ){ return 0; } else if ( ASC_Ordering && (dataBean1.value > dataBean2.value) ){ return 1; } else if ( !ASC_Ordering && (dataBean1.value < dataBean2.value) ){ return 1; } return -1; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -