Index: plugins/optional/tagged_pages/src/org/ametys/anycontent/modules/tags/ui/inputdatas/FilteredPagesInputData.java
===================================================================
RCS file: /cvs/produits/Ametys/AnyContent_2.9/plugins/optional/tagged_pages/src/org/ametys/anycontent/modules/tags/ui/inputdatas/FilteredPagesInputData.java,v
retrieving revision 1.7
diff -u -r1.7 FilteredPagesInputData.java
--- plugins/optional/tagged_pages/src/org/ametys/anycontent/modules/tags/ui/inputdatas/FilteredPagesInputData.java	6 Oct 2008 15:53:09 -0000	1.7
+++ plugins/optional/tagged_pages/src/org/ametys/anycontent/modules/tags/ui/inputdatas/FilteredPagesInputData.java	4 Aug 2009 12:42:22 -0000
@@ -5,7 +5,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -133,19 +133,19 @@
 
                         if (filter.getChildSearch() == 0 || page == null)
                         {
-                            Collection<Page> pages = search(filter, null, siteName, sitemapName, lang, filter.getParentDepth(), filter.getChildSearch());
+                            Collection<Page> pages = search(filter, null, siteName, sitemapName, lang, filter.getParentDepth(), filter.getChildSearch(), contentVersion);
                             // recherche globale
                             all_pages.addAll(pages);
                         }
                         else 
                         {
                             // recherche devant remonter au parent en cas d'échec
-                            Collection<Page> pages = search(filter, page, siteName, sitemapName, lang, filter.getParentDepth(), filter.getChildSearch());
+                            Collection<Page> pages = search(filter, page, siteName, sitemapName, lang, filter.getParentDepth(), filter.getChildSearch(), contentVersion);
                             all_pages.addAll(pages);
                         }
                         
-                        all_pages = filterBySpecificConditions (all_pages, contentVersion);
-                        all_pages = filterByLabels(all_pages, contentVersion);
+                        // Filter the list of resulting pages.
+                        all_pages = filterBySpecificConditions(all_pages, contentVersion);
                         
                         // Building output on generated list of contents
                         buildXMLOutput(contentHandler, all_pages, filter);
@@ -210,10 +210,12 @@
      * @param lang The lang
      * @param recursivity The max number of recursivity (recursivity means reload on parent if no result)
      * @param downlimitation The max number of child to search for. -1 is infinite. 0 is not child search. n > 0 is the number of levels
+     * @param contentVersion The content version.
      * @return the pages that match the filter
      * @throws RepositoryException
+     * @throws ResourceNotFoundException 
      */
-    protected Collection<Page> search(Filter filter, Page page, String site, String sitemap, String lang, int recursivity, int downlimitation) throws RepositoryException
+    protected Collection<Page> search(Filter filter, Page page, String site, String sitemap, String lang, int recursivity, int downlimitation, String contentVersion) throws RepositoryException, ResourceNotFoundException
     {
         List<String> fullAllowedKeys = filter.getAllowedKeys();
         List<String> allowedKeys = new ArrayList<String>(fullAllowedKeys.size());
@@ -233,7 +235,6 @@
 
         // Childs search
         PageQuery query = _repository.createPageQuery();
-        query.setLimit(filter.getMaxNumberOfResults());
         
         Expression key_expression = null;
 
@@ -282,17 +283,26 @@
         
         if (page != null && (recursivity == 0 || recursivity > 1) && results.size() == 0)
         {
-            return search(filter, page, site, sitemap, lang, recursivity > 0 ? recursivity - 1 : -1, downlimitation);
+            return search(filter, page, site, sitemap, lang, recursivity > 0 ? recursivity - 1 : -1, downlimitation, contentVersion);
         }
         else
         {
-            // on ne garde que getMaxNumberOfResults éléments
-            while (results.size() > filter.getMaxNumberOfResults() && filter.getMaxNumberOfResults() > 0)
+            // Filter the returned pages on a given label and with project-specific conditions.
+            int maxResults = filter.getMaxNumberOfResults();
+            Iterator<Page> resultIt = results.iterator();
+            Collection<Page> filteredResults = new ArrayList<Page>();
+            
+            // Loop while there are results and the maximum number of results is not reached.
+            while (resultIt.hasNext() && (maxResults < 1 || filteredResults.size() < maxResults))
             {
-                results.remove(results.iterator().next());
+                Page resultPage = resultIt.next();
+                if (filterBySpecificConditions(resultPage, contentVersion) && filterByLabel(resultPage, contentVersion))
+                {
+                    filteredResults.add(resultPage);
+                }
             }
             
-            return results;
+            return filteredResults;
         }
     }
 
@@ -353,37 +363,29 @@
     }
     
     /**
-     * Filter pages by label
-     * @param pages The pages to filter
-     * @param contentVersion The content version, can be null
-     * @return The filtered pages as a <code>Collection</code>
+     * Filter page by label.
+     * @param page The content to filter.
+     * @param label the label to test, can be null.
+     * @return true if the page is to be returned by the filter, false otherwise.
      * @throws RepositoryException
      * @throws ResourceNotFoundException
      */
-    protected Collection<Page> filterByLabels(Collection<Page> pages, String contentVersion) throws RepositoryException, ResourceNotFoundException
+    protected boolean filterByLabel(Page page, String label) throws RepositoryException, ResourceNotFoundException
     {
-        if (contentVersion == null)
+        if (label != null)
         {
-            return pages;
+            return _isValid(page, label);
         }
         else
         {
-            HashMap<String, Page> results = new HashMap<String, Page>(pages.size());
-            
-            for (Page page : pages)
-            {
-                if (_isValid(page, contentVersion))
-                {
-                    results.put(page.getPath(), page);
-                }
-            }
-            
-            return results.values();
+            return true;
         }
     }
     
     /**
-     * Use for project specific condition
+     * Filter a list of pages by project specific conditions.
+     * This method is called after the pages are separately filtered. The max number of results are already found, no more pages will be added if more filtering is done.
+     * If one wants to change the way a single page is filtered, he should override filterBySpecificConditions(Page) instead.
      * @param pages The pages to filter
      * @param contentVersion The content version, can be null
      * @return The filtered pages as a <code>Collection</code>
@@ -395,6 +397,18 @@
         return pages;
     }
     
+    /**
+     * Filter a single page by project specific conditions.
+     * @param page the page to test for inclusion in the results.
+     * @param contentVersion The content version, can be null
+     * @return true if the page is to be returned by the filter, false otherwise.
+     * @throws RepositoryException
+     */
+    protected boolean filterBySpecificConditions(Page page, String contentVersion) throws RepositoryException
+    {
+        return true;
+    }
+    
     private boolean _isValid(Page page, String contentVersion) throws ResourceNotFoundException, RepositoryException
     {
         boolean validParent = true;
Index: plugins/optional/tagged_pages/src/org/ametys/anycontent/modules/tags/ui/inputdatas/FilteredContentsInputData.java
===================================================================
RCS file: /cvs/produits/Ametys/AnyContent_2.9/plugins/optional/tagged_pages/src/org/ametys/anycontent/modules/tags/ui/inputdatas/FilteredContentsInputData.java,v
retrieving revision 1.21
diff -u -r1.21 FilteredContentsInputData.java
--- plugins/optional/tagged_pages/src/org/ametys/anycontent/modules/tags/ui/inputdatas/FilteredContentsInputData.java	5 Dec 2008 15:20:53 -0000	1.21
+++ plugins/optional/tagged_pages/src/org/ametys/anycontent/modules/tags/ui/inputdatas/FilteredContentsInputData.java	4 Aug 2009 12:42:22 -0000
@@ -163,11 +163,25 @@
                             all_contents.addAll(search(objectModel, filter, context, page, filter.getParentDepth(), filter.getChildSearch()));
                         }
                         
-                        all_contents = filterBySpeficficConditions(all_contents);
-                        all_contents = filterByLabels(all_contents, context);
+                        // Filter the list of contents.
+                        all_contents = filterBySpecificConditions(all_contents);
+                        
+                        // After all filter we can do the limitation
+                        Collection<Content> finalContents = new ArrayList<Content>();
+                        // If there is no limitation or there is less results than wanted
+                        if (filter.getMaxNumberOfResults() <= 0 || all_contents.size() < filter.getMaxNumberOfResults())
+                        {
+                            // all content is taken
+                            finalContents = all_contents;
+                        }
+                        // else only the max result is taken
+                        else
+                        {
+                            finalContents = ((ArrayList<Content>) all_contents).subList(0, filter.getMaxNumberOfResults());
+                        }
                         
                         // Building output on generated list of contents
-                        _buildXMLOutput(contentHandler, all_contents, filter, context);
+                        _buildXMLOutput(contentHandler, finalContents, filter, context);
                     }
                     
                     XMLUtils.endElement(contentHandler, "Model");
@@ -227,11 +241,11 @@
      * @param context the cms context (current site, sitemap, ...)
      * @return the contents that match the filter
      * @throws RepositoryException
+     * @throws ResourceNotFoundException 
      */
-    protected Collection<Content> searchAllContents(Filter filter, CMSContext context) throws RepositoryException
+    protected Collection<Content> searchAllContents(Filter filter, CMSContext context) throws RepositoryException, ResourceNotFoundException
     {
         ContentQuery query = _repository.createContentQuery();
-        query.setLimit(filter.getMaxNumberOfResults());
         
         Expression keyExpr = TagsHelper.createExpressionFromStrings(query, filter.getAllowedKeys(), Query.BINARY_OPERATOR_AND);
         Expression type_expression = TagsHelper.createContentTypeExpression(query, filter.getAllowedTypes());
@@ -244,8 +258,19 @@
         
         String siteName = org.ametys.anycontent.context.CMSContextHelper.getSiteName(context);
         String lang = org.ametys.anycontent.context.CMSContextHelper.getSiteSitemapLanguageCode(context);
-
-        return query.execute(siteName, lang, finalExpr);
+        
+        Collection<Content> results = query.execute(siteName, lang, finalExpr);
+        Collection<Content> filteredResults = new ArrayList<Content>(results.size());
+        
+        for (Content content : results)
+        {
+            if (filterBySpecificConditions(content) && filterByLabel(content, context))
+            {
+                filteredResults.add(content);
+            }
+        }
+        
+        return filteredResults;
     }
     
     /**
@@ -259,14 +284,14 @@
      * @param downlimitation The max number of children to limit search
      * @return the contents that match the filter
      * @throws RepositoryException
+     * @throws ResourceNotFoundException 
      */
-    protected Collection<Content> search(Map objectModel, Filter filter, CMSContext context, Page page, int recursivity, int downlimitation) throws RepositoryException
+    protected Collection<Content> search(Map objectModel, Filter filter, CMSContext context, Page page, int recursivity, int downlimitation) throws RepositoryException, ResourceNotFoundException
     {
         try
         {
             // Build the content expression
             ContentQuery query = _repository.createContentQuery();
-            query.setLimit(filter.getMaxNumberOfResults());
     
             Expression keyExpr = TagsHelper.createExpressionFromStrings(query, filter.getAllowedKeys(), Query.BINARY_OPERATOR_AND);
             Expression type_expression = TagsHelper.createContentTypeExpression(query, filter.getAllowedTypes());
@@ -326,7 +351,11 @@
                 while (nIt.hasNext() && (filter.getMaxNumberOfResults() <= 0 || results.size() < filter.getMaxNumberOfResults()))
                 {
                     Node node = nIt.nextNode();
-                    results.add(new JCRContent(jcrRepository, siteName, node, null));
+                    Content content = new JCRContent(jcrRepository, siteName, node, null);
+                    if (filterBySpecificConditions(content) && filterByLabel(content, context))
+                    {
+                        results.add(content);
+                    }
                 }
             }
             else
@@ -347,7 +376,11 @@
                     while (nIt.hasNext() && (filter.getMaxNumberOfResults() <= 0 || results.size() < filter.getMaxNumberOfResults()))
                     {
                         Node node = nIt.nextNode();
-                        results.add(new JCRContent(jcrRepository, siteName, node, null));
+                        Content content = new JCRContent(jcrRepository, siteName, node, null);
+                        if (filterBySpecificConditions(content) && filterByLabel(content, context))
+                        {
+                            results.add(content);
+                        }
                     }
                     requestMiddle.append("/*[@jcr:uuid]"); // predicat obligatoire à cause d'un bug jackrabbit
                 }
@@ -610,47 +643,51 @@
     }
 
     /**
-     * Filter content by label
-     * @param contents The contents to filter
+     * Filter content by label.
+     * @param content The content to filter.
      * @param context the cms context (current site, sitemap, ...)
-     * @return The filtered contents as a <code>Collection</code>
+     * @return true if the content is to be returned by the filter, false otherwise.
      * @throws RepositoryException
      * @throws ResourceNotFoundException
      */
-    protected Collection<Content> filterByLabels(Collection<Content> contents, CMSContext context) throws RepositoryException, ResourceNotFoundException
+    protected boolean filterByLabel(Content content, CMSContext context) throws RepositoryException, ResourceNotFoundException
     {
         String contentVersion = org.ametys.anycontent.context.CMSContextHelper.getContentVersion(context);
-
+        
         if (contentVersion != null)
         {
-            List<Content> results = new ArrayList<Content>();
-            for (Content content : contents)
-            {
-                if (_isValid(content, context))
-                {
-                    results.add(content);
-                }
-            }
-            return results;
+            return _isValid(content, context);
         }
         else
         {
-            return contents;
+            return true;
         }
     }
     
     /**
-     * Filter content by project specific conditions
+     * Filter a list of contents by project specific conditions.
+     * This method is called after the contents are separately filtered. The max number of results are already found, no more contents will be added if more filtering is done.
+     * If one wants to change the way a single content is filtered, he should override filterBySpecificConditions(Content) instead.
      * @param contents The contents to filter
-     * @param contentVersion
      * @return The filtered contents as a <code>Collection</code>
      * @throws RepositoryException
      */
-    protected Collection<Content> filterBySpeficficConditions(Collection<Content> contents) throws RepositoryException
+    protected Collection<Content> filterBySpecificConditions(Collection<Content> contents) throws RepositoryException
     {
         return contents;
     }
     
+    /**
+     * Filter a single content by project specific conditions.
+     * @param content the content to test for inclusion in the results.
+     * @return true if the content is to be returned by the filter, false otherwise.
+     * @throws RepositoryException
+     */
+    protected boolean filterBySpecificConditions(Content content) throws RepositoryException
+    {
+        return true;
+    }
+    
     private boolean _isValid(Content content, CMSContext context) throws ResourceNotFoundException, RepositoryException
     {
         for (PageContent referer : ((Repository) content.getRepository()).getReferers(content))
