📄 containersorterbean.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////////////// 29.05.2002 NK added in Jahiapackage org.jahia.data.containers;import java.sql.*;import java.util.BitSet;import java.util.Vector;import java.util.Collections;import java.util.Date;import java.util.TimeZone;import java.util.Calendar;import java.util.Comparator;import org.jahia.params.*; // ParamBeanimport org.jahia.registries.*; // JahiaRegistriesimport org.jahia.exceptions.*;import org.jahia.registries.ServicesRegistry;import org.jahia.utils.JahiaConsole;import org.jahia.utils.JahiaTools;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseException;/** * Jahia Standard Containers Sort Handler for a given container list. * * @see ContainerSorterBean * @see JahiaContainerSet * @author Khue Nguyen <a href="mailto:khue@jahia.org">khue@jahia.org</a> */public class ContainerSorterBean { private static final String CLASS_NAME = ContainerSorterBean.class.getName(); private int ctnListID = -1; private String fieldName; private boolean updated = false; private long lastSortingTime = -1; private boolean numberSort = false; private boolean isValid = false; private boolean ASC_Ordering = true; // by default ASCENDANT ORDER. //** sorted ctnids **/ private Vector result; //-------------------------------------------------------------------------- /** * Constructor * * @param int ctnListID, the container list id. * @param String the field name, the field on which to sort. */ public ContainerSorterBean(int ctnListID, String fieldName) throws JahiaException { if ( ctnListID >0 && fieldName != null && !fieldName.trim().equals("") ) { this.ctnListID = ctnListID; this.fieldName = fieldName; this.isValid = true; } } //-------------------------------------------------------------------------- /** * Constructor * * @param int ctnListID, the container list id. * @param String the field name, the field on which to sort. * @param boolean , force field values to be converted to long representation before sorting ( if true ). */ public ContainerSorterBean(int ctnListID, String fieldName, boolean numberSort) throws JahiaException { if ( ctnListID >0 && fieldName != null && !fieldName.trim().equals("") ) { this.ctnListID = ctnListID; this.fieldName = fieldName; this.numberSort = numberSort; this.isValid = true; } } //-------------------------------------------------------------------------- /** * Constructor * * @param String containerListName, the container list name. * @param ParamBean, the param bean. * @param String the field name, the field on which to sort. */ public ContainerSorterBean(String containerListName, ParamBean params, String fieldName) throws JahiaException { JahiaConsole.println("ContainerSorterBean","Created container sort for : " + containerListName); if ( containerListName != null ){ int clistID = ServicesRegistry.getInstance().getJahiaContainersService(). getContainerListID( containerListName, params.getPage().getID() ); if ( clistID >0 && fieldName != null && !fieldName.trim().equals("") ) { this.ctnListID = clistID; this.fieldName = fieldName; this.isValid = true; } } } //-------------------------------------------------------------------------- /** * Constructor * * @param String containerListName, the container list name. * @param ParamBean, the param bean. * @param String the field name, the field on which to sort. * @param boolean , force field values to be converted to long representation before sorting ( if true ). */ public ContainerSorterBean(String containerListName, ParamBean params, String fieldName, boolean numberSort) throws JahiaException { JahiaConsole.println("ContainerSorterBean","Created container sort for clist: " + containerListName + " on the field " + fieldName); if ( containerListName != null ){ int clistID = ServicesRegistry.getInstance().getJahiaContainersService(). getContainerListID( containerListName, params.getPage().getID() ); if ( clistID >0 && fieldName != null && !fieldName.trim().equals("") ) { this.ctnListID = clistID; this.fieldName = fieldName; this.numberSort = numberSort; this.isValid = true; //JahiaConsole.println("ContainerSorterBean","Constructor sorter is valid "); } } } //-------------------------------------------------------------------------- /** * Do the sort. Optionally, you can provide a BitSet where each bit set correspond the a container id you want in the result. * If you want all containers in the result, give a null BitSet. * * @param BitSet bits */ public Vector doSort(BitSet bits) { JahiaConsole.println("ContainerSorterBean.doSort", "Started"); this.result = null; try { Vector ctnIds = null; if ( this.isValid ){ // get all container ids JahiaConsole.println("ContainerSorterBean.doSort", "Sorting : On field : " + getSortingFieldName()); if ( this.numberSort ) { this.result = doNumberSort(bits); } else { ctnIds = ServicesRegistry.getInstance() .getJahiaContainersService().getctnidsInList(getCtnListID(),getSortingFieldName(),isAscOrdering()); if ( bits == null ){ this.result = ctnIds; } else { this.result = new Vector(); int size = ctnIds.size(); for (int i=0; i < size; i++) { int ctnid = ((Integer)ctnIds.elementAt(i)).intValue(); if ( bits.get(ctnid) ){ this.result.add(ctnIds.elementAt(i)); } } } } } } catch ( Throwable t ){ JahiaConsole.println("ContainerSorterBean.doSort", "Exception occured :" + t.getMessage()); t.printStackTrace(); } // Set search time TimeZone tz = TimeZone.getTimeZone("UTC"); Calendar cal = Calendar.getInstance(tz); Date nowDate = cal.getTime(); this.lastSortingTime = nowDate.getTime(); return this.result; } //-------------------------------------------------------------------------- /** * Return the vector of sorted ctnids. * */ public Vector result() { return this.result; } //-------------------------------------------------------------------------- /** * Return the order , true - > ASC, false -> DESC. * */ public boolean isAscOrdering() { return this.ASC_Ordering; } //-------------------------------------------------------------------------- /** * Return true, if the values are converted to number before sorting. * */ public boolean isNumberOrdering() { return this.numberSort; } //-------------------------------------------------------------------------- /** * Force or not value to be converted to number before doing the sort. * */ public boolean setNumberOrdering(boolean val) { return this.numberSort = val; } //-------------------------------------------------------------------------- /** * Set DESC ordering. * */ public void setDescOrdering() { this.ASC_Ordering = false; } //-------------------------------------------------------------------------- /** * Set ASC ordering. * */ public boolean setAscOrdering() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -