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 24 Feb 2009 10:38:26 -0000 @@ -58,10 +58,11 @@ public class FilteredContentsInputData extends AbstractLogEnabled implements InputData, Serviceable, ThreadSafe, PluginAware { /** - * Constant value for parameter "escape".
Used by private method _escape() to escape characters. + * Constant value for parameter "escape".
Used by private method + * _escape() to escape characters. */ public static final String ESCAPE = "true"; - + /** * Plugin name constant to use in resolveURI() methods as we are here in an * InputData @@ -70,13 +71,13 @@ /** The context handler */ protected CMSContextHandler _contextHandler; - + /** The source resolver */ protected SourceResolver _sourceResolver; - + /** The repository */ protected org.ametys.anycontent.plugins.web.repository.Repository _repository; - + public void service(ServiceManager mgr) throws ServiceException { _contextHandler = (CMSContextHandler) mgr.lookup(CMSContextHandler.ROLE); @@ -88,11 +89,11 @@ { _pluginName = pluginName; } - + public void toSAX(Map objectModel, ContentHandler contentHandler) throws SAXException, ProcessingException { CMSContext context = _contextHandler.peek(); - + if (context == null) { return; @@ -103,20 +104,20 @@ try { String filterName = "default"; - + if (page != null) { filterName = page.getMetadataHolder().getMetadataAsString(TagsHelper.TAGS_FILTER_METADATA); } - + if (filterName == null) { filterName = "default"; } - + // Parsing XML definition of the filter String configFileName = "template://filters/" + filterName + ".xml"; - + Source configSource = null; InputStream configStream = null; try @@ -126,26 +127,26 @@ { configStream = configSource.getInputStream(); Configuration configFileConfiguration = new DefaultConfigurationBuilder().build(configStream); - + if (getLogger().isDebugEnabled()) { getLogger().debug("Importing filter definition from file '" + configFileName + ".xml'."); } - + Configuration[] filters = configFileConfiguration.getChildren(); - + contentHandler.startDocument(); XMLUtils.startElement(contentHandler, "Model"); _initXMLOutput(contentHandler, page); - + // Launching search for each filter definition for (Configuration filterConf : filters) { Collection all_contents = new ArrayList(); Filter filter = new Filter(filterConf); - - // If filter is not targeting contents, we skip it + + // If filter is not targeting contents, we skip it if (!filter.getTarget().equals(Filter.TARGET_CONTENT)) { continue; @@ -162,14 +163,32 @@ { all_contents.addAll(search(objectModel, filter, context, page, filter.getParentDepth(), filter.getChildSearch())); } - + all_contents = filterBySpeficficConditions(all_contents); all_contents = filterByLabels(all_contents, context); - + + // After all filter we can do the limitation + Collection finalContents = new ArrayList(); + + // 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) 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"); contentHandler.endDocument(); } @@ -178,7 +197,7 @@ if (getLogger().isDebugEnabled()) { getLogger().debug("The file '" + configFileName + ".xml' could not be found. No filter imported."); - } + } } } catch (Exception e) @@ -219,7 +238,7 @@ throw new ProcessingException("Error while retrieving filtered contents", t); } } - + /** * Performs a global search with a filter * @@ -231,31 +250,32 @@ protected Collection searchAllContents(Filter filter, CMSContext context) throws RepositoryException { 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()); - + Expression finalExpr = type_expression; if (keyExpr != null) { finalExpr = query.createExpression(finalExpr, org.ametys.anycontent.repository.Query.BINARY_OPERATOR_AND, keyExpr); } - + String siteName = org.ametys.anycontent.context.CMSContextHelper.getSiteName(context); String lang = org.ametys.anycontent.context.CMSContextHelper.getSiteSitemapLanguageCode(context); return query.execute(siteName, lang, finalExpr); } - + /** * Performs a recursive search with a filter * * @param objectModel The cocoon object model * @param filter the filter to test * @param context the cms context (current site, sitemap, ...) - * @param page The page where to start the search. Can be null to search everywhere. - * @param recursivity The max number of recursivity (recursivity means reload on parent if no result) + * @param page The page where to start the search. Can be null to search + * everywhere. + * @param recursivity The max number of recursivity (recursivity means + * reload on parent if no result) * @param downlimitation The max number of children to limit search * @return the contents that match the filter * @throws RepositoryException @@ -266,21 +286,21 @@ { // Build the content expression ContentQuery query = _repository.createContentQuery(); - query.setLimit(filter.getMaxNumberOfResults()); - + // query.setLimit(filter.getMaxNumberOfResults()); + Expression keyExpr = TagsHelper.createExpressionFromStrings(query, filter.getAllowedKeys(), Query.BINARY_OPERATOR_AND); Expression type_expression = TagsHelper.createContentTypeExpression(query, filter.getAllowedTypes()); - + Expression finalExpr = type_expression; if (keyExpr != null) { finalExpr = query.createExpression(finalExpr, org.ametys.anycontent.repository.Query.BINARY_OPERATOR_AND, keyExpr); } - + // Build the jcr query JCRRepository jcrRepository = (JCRRepository) _repository; javax.jcr.Repository javaxRepository = jcrRepository.getRepository(); - + String login; if (UserHelper.isAdministrator(objectModel)) { @@ -295,10 +315,9 @@ } } Session session = javaxRepository.login(new SimpleCredentials(login, new char[0])); - + String predicats = finalExpr != null ? finalExpr.build() : ""; - String siteName = org.ametys.anycontent.context.CMSContextHelper.getSiteName(context); String sitemapName = org.ametys.anycontent.context.CMSContextHelper.getSiteSitemapName(context); String lang = org.ametys.anycontent.context.CMSContextHelper.getSiteSitemapLanguageCode(context); @@ -307,15 +326,21 @@ Collection results = new ArrayList(); String requestBegin = "/jcr:root/root/" + siteName + "/pages/" + sitemapName + "/" + lang + (page != null ? ("/" + _encode(page.getPath())) : ""); - String requestEnd = "/element(*, anycontent:page)" - + "/jcr:deref(@" + org.ametys.anycontent.plugins.anycontent.repository.jcr.JCRRepository.NAMESPACE_PREFIX_INTERNAL + "pageContent, '*')[@anycontent:template]/default" // predicat template obligatoire à cause d'un bug jackrabbit - + "/jcr:deref(@anycontent:content, '*')" - + (predicats != null && predicats.length() > 0 ? "[" + predicats + "]" : ""); - + String requestEnd = "/element(*, anycontent:page)" + "/jcr:deref(@" + org.ametys.anycontent.plugins.anycontent.repository.jcr.JCRRepository.NAMESPACE_PREFIX_INTERNAL + + "pageContent, '*')[@anycontent:template]/default" // predicat + // template + // obligatoire + // à + // cause + // d'un + // bug + // jackrabbit + + "/jcr:deref(@anycontent:content, '*')" + (predicats != null && predicats.length() > 0 ? "[" + predicats + "]" : ""); + if (downlimitation == -1) { String xpathQuery = requestBegin + "/" + requestEnd; - + if (getLogger().isInfoEnabled()) { getLogger().info(xpathQuery); @@ -323,7 +348,7 @@ javax.jcr.query.Query jvaxQuery = session.getWorkspace().getQueryManager().createQuery(xpathQuery, javax.jcr.query.Query.XPATH); NodeIterator nIt = jvaxQuery.execute().getNodes(); - while (nIt.hasNext() && (filter.getMaxNumberOfResults() <= 0 || results.size() < filter.getMaxNumberOfResults())) + while (nIt.hasNext()) { Node node = nIt.nextNode(); results.add(new JCRContent(jcrRepository, siteName, node, null)); @@ -332,11 +357,11 @@ else { StringBuffer requestMiddle = new StringBuffer(""); - - for (int i = 0; i < downlimitation && (filter.getMaxNumberOfResults() <= 0 || results.size() < filter.getMaxNumberOfResults()); i++) + + for (int i = 0; i < downlimitation; i++) { String xpathQuery = requestBegin + requestMiddle + requestEnd; - + if (getLogger().isInfoEnabled()) { getLogger().info(xpathQuery); @@ -344,15 +369,18 @@ javax.jcr.query.Query jvaxQuery = session.getWorkspace().getQueryManager().createQuery(xpathQuery, javax.jcr.query.Query.XPATH); NodeIterator nIt = jvaxQuery.execute().getNodes(); - while (nIt.hasNext() && (filter.getMaxNumberOfResults() <= 0 || results.size() < filter.getMaxNumberOfResults())) + while (nIt.hasNext()) { Node node = nIt.nextNode(); results.add(new JCRContent(jcrRepository, siteName, node, null)); } - requestMiddle.append("/*[@jcr:uuid]"); // predicat obligatoire à cause d'un bug jackrabbit + requestMiddle.append("/*[@jcr:uuid]"); // predicat + // obligatoire à + // cause d'un bug + // jackrabbit } } - + // ending... if (page != null && (recursivity == -1 || recursivity > 1) && results.size() == 0) { @@ -374,7 +402,7 @@ throw new RepositoryException(errorMsg, e); } } - + return search(objectModel, filter, context, parentPage, recursivity > 0 ? recursivity - 1 : -1, downlimitation); } else @@ -403,7 +431,9 @@ } /** - * Returns the list of matching contents after they have been filtered by type + * Returns the list of matching contents after they have been filtered by + * type + * * @param page the page to get the content from * @param allowedTypes the list of allowed types * @return the list of matching contents @@ -412,13 +442,13 @@ protected Map _filterTypes(Page page, List allowedTypes) throws RepositoryException { Map tempResults = new HashMap(); - + for (String area : page.getPageContent().getAreas()) { if (page.getPageContent().getType(area) == PageContent.TYPE_CONTENT) { Content content = page.getPageContent().getContent(area); - + if (allowedTypes == null || allowedTypes.isEmpty()) { tempResults.put(content.getName(), content); @@ -431,7 +461,8 @@ { if (content.getType().equals("org.ametys.anycontent.plugins.structured.Content")) { - if (content.getMetadataHolder().getMetadataAsString(TagsHelper.TAGS_METADATA_STRUCTURED_CONTENT_TYPE).equals(type.substring(type.lastIndexOf(".") + 1))) + if (content.getMetadataHolder().getMetadataAsString(TagsHelper.TAGS_METADATA_STRUCTURED_CONTENT_TYPE).equals( + type.substring(type.lastIndexOf(".") + 1))) { tempResults.put(content.getName(), content); break; @@ -449,10 +480,11 @@ } return tempResults; } - /** - * Returns the list of matching contents after they have been filtered by type + * Returns the list of matching contents after they have been filtered by + * type + * * @param page the page to get the content from * @param allowedKeys the list of allowed keys * @return the map of matching contents with their names as keys @@ -475,14 +507,14 @@ fullAllowedKeys.add(TagsHelper.TAGS_METADATA_PREFIX + tag); } } - + // Testing content's tags for (String area : page.getPageContent().getAreas()) { if (page.getPageContent().getType(area) == PageContent.TYPE_CONTENT) { Content content = page.getPageContent().getContent(area); - + if (allowedKeys == null || allowedKeys.isEmpty()) { tempResults.put(content.getName(), content); @@ -490,7 +522,7 @@ else { List metadatas = Arrays.asList(content.getMetadataHolder().getMetadataNames()); - + if (metadatas.containsAll(fullAllowedKeys)) { tempResults.put(content.getName(), content); @@ -515,14 +547,15 @@ if (page != null) { AttributesImpl atts = new AttributesImpl(); - + atts.addCDATAAttribute("path", page.getPath()); XMLUtils.createElement(contentHandler, "page", atts); } } - + /** * Entry point to change filter preview + * * @param content Content to present * @return url. May start with cocoon:// * @throws RepositoryException @@ -531,14 +564,14 @@ { return "cocoon://_plugins/" + _pluginName + "/content/" + content.getName() + ".source"; } - + private void _buildXMLOutput(ContentHandler contentHandler, Collection contents, Filter filter, CMSContext context) throws SAXException { if (contents.isEmpty()) { return; } - + XMLUtils.startElement(contentHandler, filter.getName()); String siteName = org.ametys.anycontent.context.CMSContextHelper.getSiteName(context); @@ -576,7 +609,7 @@ _sourceResolver.release(contentSrc); } } - + try { // Generating content's refering pages @@ -611,6 +644,7 @@ /** * Filter content by label + * * @param contents The contents to filter * @param context the cms context (current site, sitemap, ...) * @return The filtered contents as a Collection @@ -638,9 +672,10 @@ return contents; } } - + /** * Filter content by project specific conditions + * * @param contents The contents to filter * @param contentVersion * @return The filtered contents as a Collection @@ -650,7 +685,7 @@ { return contents; } - + private boolean _isValid(Content content, CMSContext context) throws ResourceNotFoundException, RepositoryException { for (PageContent referer : ((Repository) content.getRepository()).getReferers(content)) @@ -663,10 +698,10 @@ } } } - + return false; } - + private boolean _isValid(Page page, CMSContext context) throws ResourceNotFoundException, RepositoryException { boolean validParent = true; @@ -676,10 +711,10 @@ Page parent = page.getSitemap().getPage(parent_path); validParent = _isValid(parent, context); } - + return _hasValidData(page, context) && validParent; } - + private boolean _hasLabel(Content content, CMSContext context) throws RepositoryException { String contentVersion = org.ametys.anycontent.context.CMSContextHelper.getContentVersion(context); @@ -691,16 +726,16 @@ return true; } } - + return false; } - + private boolean _hasValidData(Page page, CMSContext context) throws RepositoryException { if (page.getType() == Page.TYPE_PAGE_CONTENT) { PageContent pageContent = page.getPageContent(); - + for (String area : pageContent.getAreas()) { if (pageContent.getType(area) == PageContent.TYPE_CONTENT) @@ -713,7 +748,7 @@ } } } - + return true; } }