Index: core/xwiki-core/src/main/java/com/xpn/xwiki/api/XWiki.java
===================================================================
--- core/xwiki-core/src/main/java/com/xpn/xwiki/api/XWiki.java (revision 24111)
+++ core/xwiki-core/src/main/java/com/xpn/xwiki/api/XWiki.java (working copy)
@@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
+import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -36,6 +37,7 @@
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiDeletedDocument;
import com.xpn.xwiki.doc.XWikiDocument;
+import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.objects.meta.MetaClass;
import com.xpn.xwiki.plugin.query.XWikiCriteria;
import com.xpn.xwiki.plugin.query.XWikiQuery;
@@ -565,6 +567,120 @@
}
/**
+ * Search attachments by passing HQL where clause values as parameters. See
+ * {@link #searchDocuments(String, int, int, List)} for more about parameterized hql clauses.
+ * You can specify properties of attach (the attachment) or doc (the document it is attached to)
+ *
+ * @param parametrizedSqlClause The HQL where clause. For example " where doc.fullName
+ * <> ? and (attach.author = ? or (attach.filename = ? and doc.space = ?))"
+ * @param nb The number of rows to return. If 0 then all rows are returned
+ * @param start The number of rows to skip at the beginning.
+ * @param parameterValues A {@link java.util.List} of the where clause values that replace the question marks (?)
+ * @return A List of {@link Attachment} objects.
+ * @throws XWikiException in case of error while performing the query
+ */
+ public List searchAttachments(String parametrizedSqlClause, int nb, int start, List< ? > parameterValues)
+ throws XWikiException
+ {
+ //get the attachment filenames and document fullNames
+ List results = this.xwiki.getStore().search(
+ "select attach.filename, doc.fullName from XWikiAttachment attach, XWikiDocument doc where doc.id = attach.docId and "
+ + parametrizedSqlClause, nb, start, parameterValues, this.context);
+
+ HashMap> filenamesByDocFullName = new HashMap>();
+
+ //put each attachment with the document it belongs to
+ for(int i=0; i());
+ }
+ filenamesByDocFullName.get(DocFullName).add((String) filename);
+ }
+
+ List out = new ArrayList();
+
+ //index through the document names
+ for(String fullName : filenamesByDocFullName.keySet()){
+ //get the documents
+ Document doc = getDocument(fullName);
+ //index through the attachments
+ for(String filename : filenamesByDocFullName.get(fullName)){
+ //get the attachments from the document (they already have their "doc" value set)
+ Attachment attach = doc.getAttachment(filename);
+ //add the attachment to the output
+ out.add(attach);
+ }
+ }
+
+ return out;
+ }
+
+ /**
+ * API allowing to search for attachments matching a query return only a limited number of elements and skipping
+ * the first rows.
+ *
+ * @param whereSql Query to use similar to {@link #searchDocuments(String)}
+ * @param nb Number of rows to return
+ * @param start Skip the first (start) rows
+ * @return A List of {@link Attachment} objects.
+ * @throws XWikiException
+ * @see #searchAttachments(String, int, int, List)
+ */
+ public List searchAttachments(String whereSql, int nb, int start)
+ throws XWikiException
+ {
+ return searchAttachments(whereSql, nb, start, null);
+ }
+
+ /**
+ * API allowing to search for attachments matching a query return only a limited number of elements and skipping
+ * the first rows. The query part is the same as {@link #searchAttachments(String, int, int, List)}
+ *
+ * @param whereSql Query to use similar to {@link #searchDocuments(String)}
+ * @return A List of {@link Attachment} objects.
+ * @throws XWikiException
+ */
+ public List searchAttachments(String whereSql)
+ throws XWikiException
+ {
+ return searchAttachments(whereSql, 0, 0, null);
+ }
+
+ /**
+ * Count attachments returned by a given parameterized query
+ *
+ * @param parametrizedSqlClause Everything which would follow the "WHERE" in HQL see: {@link #searchDocuments(String, int, int, List)}
+ * @param parameterValues A {@link java.util.List} of the where clause values that replace the question marks (?)
+ * @return int number of attachments found.
+ * @throws XWikiException
+ * @see #searchAttachments(String, int, int, List)
+ */
+ public int countAttachments(String parametrizedSqlClause, List< ? > parameterValues)
+ throws XWikiException
+ {
+ List l = this.xwiki.getStore().search(
+ "select count(attach) from XWikiAttachment attach, XWikiDocument doc where doc.id = attach.docId and "
+ + parametrizedSqlClause, 0, 0, parameterValues, this.context);
+ return ((Number) l.get(0)).intValue();
+ }
+
+ /**
+ * Count attachments returned by a given query
+ *
+ * @param whereSql everything which would follow the "WHERE" in HQL
+ * @return int number of attachments found.
+ * @throws XWikiException
+ * @see #searchAttachments(String)
+ */
+ public int countAttachments(String whereSql)
+ throws XWikiException
+ {
+ return countAttachments(whereSql, null);
+ }
+
+ /**
* Function to wrap a list of XWikiDocument into Document objects
*
* @param docs list of XWikiDocument