mcardle     2005/05/27 17:04:07 CEST

  Added files:
    core/src/java/org/jahia/data/containers 
                                            
ContainerFilterByContainerDefinitions.java 
  Log:
  A filter used to returns all containers of a single given definition, or with 
a definition defined in a set of given definition names. The former case is 
equivalent to ContainerFilterByContainerDefinition.
  
  Revision  Changes    Path
  1.1       +590 -0    
jahia/core/src/java/org/jahia/data/containers/ContainerFilterByContainerDefinitions.java
 (new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/data/containers/ContainerFilterByContainerDefinitions.java?rev=1.1&content-type=text/plain
  
  
  
  Index: ContainerFilterByContainerDefinitions.java
  ====================================================================
  //
  //                                   ____.
  //                       __/\ ______|    |__/\.     _______
  //            __   .____|    |       \   |    +----+       \
  //    _______|  /--|    |    |    -   \  _    |    :    -   \_________
  //   \\______: :---|    :    :           |    :    |         \________>
  //           |__\---\_____________:______:    :____|____:_____\
  //                                      /_____|
  //
  //                 . . . i n   j a h i a   w e   t r u s t . . .
  //
  //
  //
  //
  //
  // 27.05.2002 NK Creation
  
  
  package org.jahia.data.containers;
  
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.ArrayList;
  import java.util.BitSet;
  
  import org.jahia.exceptions.JahiaException;
  import org.jahia.services.version.EntryLoadRequest;
  import org.jahia.utils.JahiaTools;
  import java.io.Serializable;
  import org.jahia.services.fields.ContentField;
  
  
  /**
   * A filter used to returns all containers of a single given definition, or 
with a definition
   * defined in a set of given definition names. The former case is equivalent 
to
   * [EMAIL PROTECTED] ContainerFilterByContainerDefinition}
   *
   * @see ContainerFilterByContainerDefinition
   * @see FilterClause
   * @see ContainerFilters
   * @see JahiaContainerSet
   * @author Khue Nguyen <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
   * @author MC
   */
  public class ContainerFilterByContainerDefinitions implements Serializable, 
ContainerFilterInterface{
  
      private static org.apache.log4j.Logger logger =
          
org.apache.log4j.Logger.getLogger(ContainerFilterByContainerDefinitions.class);
  
      public static final String VERSION_ID = "version_id";
      public static final String WORKFLOW_STATE = "workflow_state";
  
      private EntryLoadRequest entryLoadRequest = EntryLoadRequest.CURRENT;
  
      private String[] containerDefinitionNames;
  
      private ContainerFilters containerFilters = null;
  
      
//--------------------------------------------------------------------------
      /**
       * Constructor
       *
       * @param containerDefinitionNames, the set of definition names to search 
for
       * @param entryLoadRequest
       */
      public ContainerFilterByContainerDefinitions (String[] 
containerDefinitionNames,
              EntryLoadRequest entryLoadRequest){
  
          this.containerDefinitionNames = containerDefinitionNames;
          if (entryLoadRequest != null) {
              this.entryLoadRequest = entryLoadRequest;
          }
      }
  
      
//--------------------------------------------------------------------------
      /**
       * Return the container definition name
       *
       * @return
       */
      public String getContainerDefinitionName()
      {
          return null;
      }
  
      
//--------------------------------------------------------------------------
      /**
       * 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
      {
          BitSet result = null;
  
          result = doFiltering(ctnListID);
          return result;
      }
  
      
//--------------------------------------------------------------------------
      /**
       * 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 doFiltering (int ctnListID)
      throws JahiaException
      {
          String fieldFilterQuery = getSelect(ctnListID,true);
          if (fieldFilterQuery == null && !fieldFilterQuery.trim().equals("")) {
              return null;
          }
  
          BitSet bits = new BitSet();
  
          Connection dbConn = null;
          Statement stmt = null;
          ResultSet rs = null;
  
          ArrayList deletedCtns = getDeletedContainers(ctnListID);
  
          try
          {
              dbConn = 
org.jahia.services.database.ConnectionDispenser.getConnection();
              stmt = dbConn.createStatement();
              rs = stmt.executeQuery(fieldFilterQuery);
              while (rs.next())
              {
                  int ctnID = rs.getInt(1);
                  int workflowState = rs.getInt(2);
                  if (this.entryLoadRequest.isCurrent()
                      || !deletedCtns.contains(new Integer(ctnID))) {
                      if (workflowState > 
EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
                          workflowState = 
EntryLoadRequest.STAGING_WORKFLOW_STATE;
                      }
                      if (this.entryLoadRequest.isCurrent() &&
                          workflowState == 
EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
                          bits.set(ctnID);
                      } else if (this.entryLoadRequest.isStaging()
                                 && workflowState >= 
EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
                          bits.set(ctnID);
                      }
                  }
              }
          } catch (SQLException se) {
              String errorMsg = "Error in doFiltering : " + se.getMessage();
              logger.error(errorMsg, se);
          } finally {
  
              closeStatement(stmt);
          }
          return bits;
      }
  
      
//--------------------------------------------------------------------------
  
      
//--------------------------------------------------------------------------
      /**
       * Return the select statement, build with the clauses for all container 
list of the site.
       * Do not execute this query !! It's only used for comparison between 
query.
       * It's not a valable sql query.
       *
       * @param int ctnListID, the container list id
       * @return String , the sql statement. Null on error
       */
      public String getSelect (int ctnListID) {
          return getSelect (ctnListID, false);
      }
  
      /**
       * Return the select statement, build with the clauses for all container 
list of the site.
       *
       * @param int ctnListID, the container list id
       * @param ignoreLang boolean, add language in query or not ( should not 
when performing sql query, because containre table do not have lang column
       * @return String , the sql statement. Null on error
       */
      protected String getSelect (int ctnListID, boolean ignoreLang) {
          //JahiaConsole.println("ContainerFilterBean.getSelect","Started");
  
          StringBuffer buff = new StringBuffer("SELECT DISTINCT 
id_jahia_ctn_entries,workflow_state FROM jahia_ctn_entries WHERE 
listid_jahia_ctn_entries=");
          buff.append(ctnListID);
          buff.append(" AND ");
          
buff.append(getMultilangAndStagingFiltering(this.entryLoadRequest,ignoreLang));
          buff.append(" ORDER BY id_jahia_ctn_entries, workflow_state");
          return buff.toString();
      }
  
      
//--------------------------------------------------------------------------
      /**
       * Set reference to a containerFilters
       *
       * @return
       * @throws JahiaException
       */
      public void setContainerFilters (ContainerFilters containerFilters) {
          this.containerFilters = containerFilters;
      }
  
      
//--------------------------------------------------------------------------
      /**
       *
       * @return
       */
      static public String getMultilangAndStagingFiltering(EntryLoadRequest 
entryLoadRequest){
  
          return getMultilangAndStagingFiltering(entryLoadRequest,
                                                 false);
      }
  
      
//--------------------------------------------------------------------------
      /**
       *
       * @return
       */
      static public String getMultilangAndStagingFiltering(EntryLoadRequest 
entryLoadRequest,
          boolean ignoreLang){
  
          StringBuffer strBuf = new StringBuffer(" ");
          if ( entryLoadRequest.isCurrent()){
              strBuf.append(WORKFLOW_STATE);
              strBuf.append("=");
              strBuf.append(EntryLoadRequest.ACTIVE_WORKFLOW_STATE);
          } else if ( entryLoadRequest.isStaging() ) {
              strBuf.append(WORKFLOW_STATE);
              strBuf.append(">");
              strBuf.append(EntryLoadRequest.VERSIONED_WORKFLOW_STATE);
              strBuf.append(" AND ");
              strBuf.append("version_id");
              strBuf.append(" <> -1 ");
          } else {
              strBuf.append("version_id");
              strBuf.append("=");
              strBuf.append(entryLoadRequest.getVersionID());
          }
          if ( !ignoreLang ) {
              String languageCode = entryLoadRequest.getFirstLocale(true).
                  toString();
              strBuf.append(" AND (");
              strBuf.append("language_code");
              strBuf.append("='");
              strBuf.append(JahiaTools.quote(languageCode));
              strBuf.append("' OR ");
              strBuf.append("language_code");
              strBuf.append("='");
              strBuf.append(ContentField.SHARED_LANGUAGE);
              strBuf.append("') ");
          }
          /*
          strBuf.append("') AND ");
          strBuf.append(FIELD_VERSION_ID);
          strBuf.append(">=0 ");*/
          return strBuf.toString();
  
      }
  
      
//--------------------------------------------------------------------------
      /**
       * Returns an array of cntids that are workflow=staging and versionid=-1 
(deleted)
       *
       * @param ctnListID
       * @return
       * @throws JahiaException
       */
      static public ArrayList getDeletedContainers (int ctnListID)
          throws JahiaException {
  
          StringBuffer buff = new StringBuffer("SELECT DISTINCT 
id_jahia_ctn_entries FROM jahia_ctn_entries WHERE workflow_state>1 AND 
version_id=-1 AND listid_jahia_ctn_entries=");
          buff.append(ctnListID);
  
          Connection dbConn = null;
          Statement stmt = null;
          ResultSet rs = null;
  
          ArrayList datas = new ArrayList();
  
          try {
              dbConn = org.jahia.services.database.ConnectionDispenser.
                       getConnection();
              stmt = dbConn.createStatement();
              rs = stmt.executeQuery(buff.toString());
  
              while (rs.next()) {
                  datas.add(new Integer(rs.getInt(1)));
              }
          } catch (SQLException se) {
              String errorMsg = "Error in getDeletedContainers() : " +
                                se.getMessage();
              logger.error(errorMsg, se);
          } finally {
  
              closeStatement(stmt);
          }
          return datas;
      }
  
      
//--------------------------------------------------------------------------
      /**
       * Perform filtering on a given site or all sites
       *
       * The expected result is a bit set of matching container ids.
       *
       * If siteId = -1 , returns results from all sites
       *
       * The containerDefinitionName is ignored here,  it is only kept due to 
the implemented
       * [EMAIL PROTECTED] ContainerFilterInterface} Interface. The internal 
containerDefinitionNames array is
       * used instead and is initialized by
       * [EMAIL PROTECTED] #ContainerFilterByContainerDefinitions (String[] 
containerDefinitionNames,EntryLoadRequest entryLoadRequest)}
       *
       * @param siteId
       * @param containerDefinitionName, IGNORED
       * @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.
       * @throws JahiaException
       */
      public BitSet doFilterBySite(int siteId, String containerDefinitionName, 
int listId)
      throws JahiaException
      {
          BitSet result = null;
          result = doFilteringBySite(siteId, containerDefinitionName, listId);
          return result;
      }
  
      
//--------------------------------------------------------------------------
      /**
       *
       * The expected result is a bit set of matching container ids for a given 
siteId.
       * if siteId = -1 , return result from all sites
       *
       * The containerDefinitionName is ignored here,  it is only kept due to 
the implemented
       * [EMAIL PROTECTED] ContainerFilterInterface} Interface. The internal 
containerDefinitionNames array is
       * used instead and is initialized by
       * [EMAIL PROTECTED] #ContainerFilterByContainerDefinitions (String[] 
containerDefinitionNames,EntryLoadRequest entryLoadRequest)}
       *
       * @param containerDefinitionName, IGNORED
       * @param siteId
       * @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.
       * @throws JahiaException
       */
      private BitSet doFilteringBySite (int siteId,
              String containerDefinitionName, int listId)
      throws JahiaException
      {
          String fieldFilterQuery = this.getSelectBySiteID(siteId, 
containerDefinitionName,true);
          if (fieldFilterQuery == null && !fieldFilterQuery.trim().equals("")) {
              return null;
          }
  
          BitSet bits = new BitSet();
  
          Connection dbConn = null;
          Statement stmt = null;
          ResultSet rs = null;
  
          ArrayList deletedCtns = getDeletedContainersBySite(siteId, 
containerDefinitionName);
  
          try {
              dbConn = org.jahia.services.database.ConnectionDispenser.
                       getConnection();
              stmt = dbConn.createStatement();
              rs = stmt.executeQuery(fieldFilterQuery);
              while (rs.next())
              {
                  int ctnID = rs.getInt(1);
                  int workflowState = rs.getInt(2);
                  if (this.entryLoadRequest.isCurrent()
                      || !deletedCtns.contains(new Integer(ctnID))) {
                      if (workflowState > 
EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
                          workflowState = 
EntryLoadRequest.STAGING_WORKFLOW_STATE;
                      }
                      if (this.entryLoadRequest.isCurrent()
                           && 
workflowState==EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
                          bits.set(ctnID);
                      } else if (this.entryLoadRequest.isStaging()
                                 && workflowState >= 
EntryLoadRequest.ACTIVE_WORKFLOW_STATE ){
                          bits.set(ctnID);
                      }
                  }
              }
          } catch (SQLException se) {
              String errorMsg = "Error in doStringValueFiltering : " +
                                se.getMessage();
              logger.error(errorMsg, se);
          } finally {
  
              closeStatement(stmt);
          }
          return bits;
      }
  
      
//--------------------------------------------------------------------------
      /**
       * Return the select statement, build with the clauses for a given site.
       * If siteId = -1 -> build query for all sites
       *
       * The containerDefinitionName is ignored here,  it is only kept due to 
the implemented
       * [EMAIL PROTECTED] ContainerFilterInterface} Interface. The internal 
containerDefinitionNames array is
       * used instead and is initialized by
       * @see #ContainerFilterByContainerDefinitions (String[] 
containerDefinitionNames,EntryLoadRequest entryLoadRequest)
       *
       * Do not execute this query !! It's only used for comparison between 
query.
       * It's not a valable sql query.
       *
       * @param siteId
       * @param containerDefinitionName, IGNORED
       * @param ignoreLang boolean, add language in query or not ( should not 
when performing sql query, because containre table do not have lang column
       * @return
       */
      public String getSelectBySiteID (int siteId, String 
containerDefinitionName) {
          return getSelectBySiteID (siteId, containerDefinitionName, false);
      }
  
      /**
       * Return the select statement, build with the clauses for a given site.
       * If siteId = -1 -> build query for all sites
       *
       * The containerDefinitionName is ignored here,  it is only kept due to 
the implemented
       * [EMAIL PROTECTED] ContainerFilterInterface} Interface. The internal 
containerDefinitionNames array is
       * used instead and is initialized by
       * @see #ContainerFilterByContainerDefinitions (String[] 
containerDefinitionNames,EntryLoadRequest entryLoadRequest)
       *
       * @param siteId
       * @param containerDefinitionName, IGNORED
       * @param ignoreLang boolean, add language in query or not ( should not 
when performing sql query, because containre table do not have lang column
       * @return
       */
       public String getSelectBySiteID (int siteId, String 
containerDefinitionName,
                                       boolean ignoreLang) {
  
          StringBuffer buff = new StringBuffer("SELECT DISTINCT 
id_jahia_ctn_entries,workflow_state FROM jahia_ctn_entries a, jahia_ctn_def b 
WHERE ");
          
buff.append(getMultilangAndStagingFiltering(this.entryLoadRequest,ignoreLang));
  
          if (siteId != -1) {
              buff.append(" AND ");
              buff.append(" jahiaid_jahia_ctn_entries=");
              buff.append(siteId);
          }
  
          /*WAS: buff.append(" AND ");
          buff.append(" a.ctndefid_jahia_ctn_entries = b.id_jahia_ctn_def ");
          buff.append(" AND (");
          for ( int i=0; i<this.containerDefinitionNames.length ; i++ ){
              buff.append(" b.name_jahia_ctn_def='");
              buff.append(JahiaTools.quote(this.containerDefinitionNames[i]));
              buff.append("'");
              if (i < this.containerDefinitionNames.length - 1) {
                  buff.append(" OR ");
              }
          }
          buff.append(")");
          */
  
          //Only include valid container names
          boolean firstTime = true;
          for (int d = 0; d < this.containerDefinitionNames.length; d++) {
              if (this.containerDefinitionNames[d] != null &&
                  !this.containerDefinitionNames[d].trim().equals("")) {
                  if (firstTime) {
                      buff.append(" AND ");
                      buff.append(" a.ctndefid_jahia_ctn_entries = 
b.id_jahia_ctn_def ");
                      buff.append(" AND ( ");
                      firstTime = false;
                  } else
                      buff.append(" OR ");
                  buff.append(" b.name_jahia_ctn_def='");
                  
buff.append(JahiaTools.quote(this.containerDefinitionNames[d]));
                  buff.append("'");
              } else
                  logger.debug("getSelectBySiteID: A definition was null, and 
therefore ignored, in containerDefinitionNames array");
              if (!firstTime && d == this.containerDefinitionNames.length - 1)
                  buff.append(" )");
          }
  
          buff.append(" ORDER BY id_jahia_ctn_entries,");
          buff.append(WORKFLOW_STATE);
          return buff.toString();
      }
  
      
//--------------------------------------------------------------------------
     /**
      * Returns an array of cntids that are workflow=staging and versionid=-1 
(deleted)
      * for a given site
      * If siteId = -1 , return deleted containers from all sites
      * If containerDefinitionName = null, container definition name is ignored 
in the search
      *
      * @param siteId
      * @param containerDefinitionName, the container definition name to search 
for
      * @return
      * @throws JahiaException
      */
     static public ArrayList getDeletedContainersBySite (int siteId,
         String containerDefinitionName)
         throws JahiaException {
         return getDeletedContainersBySite(siteId,
                                           new String[] 
{containerDefinitionName});
     }
  
     
//--------------------------------------------------------------------------
     /**
      * Returns an array of cntids that are workflow=staging and versionid=-1 
(deleted)
      * for a given site
      * If siteId = -1 , return deleted containers from all sites
      *
      * If containerDefinitionNames = null, container definition names are 
ignored in the search. If the
      * containerDefinitionNames String Array contains null Strings they are 
ignored while any valid Strings
      * are kept.
      *
      * @param siteId
      * @param containerDefinitionNames, the container definition names to 
search for
      * @return
      * @throws JahiaException
      */
     static public ArrayList getDeletedContainersBySite (int siteId,
         String[] containerDefinitionNames)
         throws JahiaException {
  
         StringBuffer buff = new StringBuffer("SELECT DISTINCT 
id_jahia_ctn_entries FROM jahia_ctn_entries, jahia_ctn_def WHERE 
workflow_state>1 AND version_id=-1 ");
         if (siteId != -1) {
             buff.append(" AND jahiaid_jahia_ctn_entries=");
             buff.append(siteId);
         }
  
         //Only include valid container names
         boolean firstTime = true;
         for (int d = 0; d < containerDefinitionNames.length; d++) {
             if (containerDefinitionNames[d] != null &&
                 !containerDefinitionNames[d].trim().equals("")) {
                 if (firstTime) {
                     buff.append(" AND ");
                     buff.append(" ctndefid_jahia_ctn_entries = 
id_jahia_ctn_def ");
                     buff.append(" AND ( ");
                     firstTime = false;
                 }
                 else
                     buff.append(" OR ");
                 buff.append(" name_jahia_ctn_def='");
                 buff.append(JahiaTools.quote(containerDefinitionNames[d]));
                 buff.append("'");
             }
             else
                 logger.debug("getDeletedContainersBySite: A definition was 
null, and therefore ignored, in containerDefinitionNames array");
             if (!firstTime && d == containerDefinitionNames.length-1)
                 buff.append(" )");
         }
         Connection dbConn = null;
         Statement stmt = null;
         ResultSet rs = null;
  
         ArrayList datas = new ArrayList();
  
         try {
             dbConn = org.jahia.services.database.ConnectionDispenser.
                      getConnection();
             stmt = dbConn.createStatement();
             rs = stmt.executeQuery(buff.toString());
  
             while (rs.next()) {
                 datas.add(new Integer(rs.getInt(1)));
             }
         } catch (SQLException se) {
             String errorMsg = "Error in getDeletedContainers() : " +
                               se.getMessage();
             logger.error(errorMsg, se);
         } finally {
  
             closeStatement(stmt);
         }
         return datas;
     }
  
  
      
//-------------------------------------------------------------------------
      static 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.
              logger.error("Error closing statement", sqlEx);
          }
      }
  
  }
  

Reply via email to