Index: core/xwiki-core/src/main/java/com/xpn/xwiki/api/Api.java =================================================================== --- core/xwiki-core/src/main/java/com/xpn/xwiki/api/Api.java (revision 25284) +++ core/xwiki-core/src/main/java/com/xpn/xwiki/api/Api.java (working copy) @@ -25,6 +25,7 @@ import com.xpn.xwiki.XWikiContext; import com.xpn.xwiki.XWikiException; import com.xpn.xwiki.doc.XWikiDocument; +import com.xpn.xwiki.doc.XWikiAttachment; /** * Base class for all API Objects. API Objects are the Java Objects that can be manipulated from Velocity or Groovy in @@ -128,4 +129,30 @@ { return xdoc == null ? null : xdoc.newDocument(this.context); } + + /** + * Convert an internal representation of an attachment to the public api Attachment. + * + * @param xattach The internal XWikiAttachment object + * @return The public api Attachment object + */ + protected Attachment convert(XWikiAttachment xattach) + { + return xattach == null ? null : new Attachment(convert(xattach.getDoc()), xattach, this.context); + } + + /** + * Convert a list of attachments in their internal form to a list of public api Attachments. + * + * @param xattaches The List of XWikiAttachment objects + * @return A List of Attachment objects + */ + protected List convertAttachments(List xattaches) + { + List outList = new ArrayList(xattaches.size()); + for (XWikiAttachment xattach : xattaches) { + outList.add(convert(xattach)); + } + return outList; + } } 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 25284) +++ core/xwiki-core/src/main/java/com/xpn/xwiki/api/XWiki.java (working copy) @@ -36,6 +36,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 +566,85 @@ } /** + * 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 + { + return convertAttachments(this.xwiki.searchAttachments(parametrizedSqlClause, nb, start, parameterValues, this.context)); + } + + /** + * 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 + { + return this.xwiki.countAttachments(parametrizedSqlClause, parameterValues, this.context); + } + + /** + * 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 Index: core/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java =================================================================== --- core/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java (revision 25284) +++ core/xwiki-core/src/main/java/com/xpn/xwiki/XWiki.java (working copy) @@ -6913,4 +6913,74 @@ { return "1".equals(Param("xwiki.title.compatibility", "0")); } + + /** + * Search attachments by passing HQL where clause values as parameters. + * You can specify properties of the "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 (?) + * @param XWikiContext The underlying context used for running the database query + * @return A List of {@link XWikiAttachment} objects. + * @throws XWikiException in case of error while performing the query + * @see com.xpn.xwiki.store.XWikiStoreInterface#searchDocuments(String, int, int, List) + */ + public List searchAttachments(String parametrizedSqlClause, int nb, int start, List< ? > parameterValues, XWikiContext context) + throws XWikiException + { + // Get the attachment filenames and document fullNames + List results = this.getStore().search( + "select attach.filename, doc.fullName from XWikiAttachment attach, XWikiDocument doc where doc.id = attach.docId and " + + parametrizedSqlClause, nb, start, parameterValues, context); + + HashMap> filenamesByDocFullName = new HashMap>(); + + // Put each attachment name with the document name it belongs to + for (int i=0; i()); + } + filenamesByDocFullName.get(docFullName).add((String) filename); + } + + List out = new ArrayList(); + + // Index through the document names, get relivent attachments + for (String fullName : filenamesByDocFullName.keySet()) { + XWikiDocument doc = getDocument(fullName, context); + List returnedAttachmentNames = filenamesByDocFullName.get(fullName); + for (XWikiAttachment attach : doc.getAttachmentList()) { + if (returnedAttachmentNames.contains(attach.getFilename())) { + out.add(attach); + } + } + } + + return out; + } + + /** + * Count attachments returned by a given parameterized query + * + * @param parametrizedSqlClause Everything which would follow the "WHERE" in HQL + * @param parameterValues A {@link java.util.List} of the where clause values that replace the question marks (?) + * @param XWikiContext The underlying context used for running the database query + * @return int number of attachments found. + * @throws XWikiException in event of an exception querying the database + * @see #searchAttachments(String, int, int, List, XWikiContext) + */ + public int countAttachments(String parametrizedSqlClause, List< ? > parameterValues, XWikiContext context) + throws XWikiException + { + List l = getStore().search( + "select count(attach) from XWikiAttachment attach, XWikiDocument doc where doc.id = attach.docId and " + + parametrizedSqlClause, 0, 0, parameterValues, context); + return ((Number) l.get(0)).intValue(); + } + }