Search in sources :

Example 96 with Document

use of com.xpn.xwiki.api.Document in project celements-blog by celements.

the class NewsletterAttachmentServiceTest method testClearAttachmentList.

@Test
public void testClearAttachmentList() throws Exception {
    DocumentReference docRef = new DocumentReference(getContext().getDatabase(), "Test", "Img");
    XWikiDocument doc = createMockAndAddToDefault(XWikiDocument.class);
    XWikiAttachment att = new XWikiAttachment();
    expect(attService.getAttachmentNameEqual(same(doc), eq("file.pdf"))).andReturn(att).anyTimes();
    expect(attService.getApiAttachment(same(att))).andReturn(new Attachment(new Document(doc, getContext()), att, getContext()));
    expect(xwiki.getDocument(eq(docRef), same(getContext()))).andReturn(doc).once();
    replayDefault();
    service.addAttachment("Test.Img;file.pdf");
    service.clearAttachmentList();
    verifyDefault();
    List<Attachment> atts = service.getAttachmentList(false);
    assertTrue("clearAttachmentList must clear the list.", (atts == null) || atts.isEmpty());
    List<Attachment> atts2 = service.getAttachmentList(true);
    assertTrue("clearAttachmentList must clear both list.", (atts2 == null) || atts2.isEmpty());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 97 with Document

use of com.xpn.xwiki.api.Document in project celements-blog by celements.

the class ArticleEngineHQL method filterRightsAndSubscription.

// checkRights = false braucht programmingrights auf blogdoc
private void filterRightsAndSubscription(List<Article> articles, String blogArticleSpace, String language, boolean withUnsubscribed, boolean withUndecided, boolean withSubscribed, boolean subscribableOnly, boolean checkRights) throws XWikiException {
    List<Article> deleteArticles = new ArrayList<>();
    XWikiDocument spaceBlogDoc = blogService.getBlogPageByBlogSpace(blogArticleSpace);
    if (spaceBlogDoc == null) {
        LOGGER.debug("Missing Blog Configuration! (Blog space: '" + blogArticleSpace + "')");
        deleteArticles.addAll(articles);
    } else {
        Document origBlogDoc = spaceBlogDoc.newDocument(getContext());
        for (Iterator<Article> artIter = articles.iterator(); artIter.hasNext(); ) {
            Article article = artIter.next();
            try {
                XWikiDocument articleDoc = getContext().getWiki().getDocument(article.getDocumentReference(), getContext());
                DocumentReference blogDocRef = blogService.getBlogDocRefByBlogSpace(articleDoc.getDocumentReference().getLastSpaceReference().getName());
                LOGGER.debug("articleDoc='" + articleDoc + "', " + blogDocRef);
                Document blogDoc = getContext().getWiki().getDocument(blogDocRef, getContext()).newDocument(getContext());
                boolean hasRight = false;
                boolean hasEditOnBlog = false;
                if (checkRights || !blogDoc.hasProgrammingRights()) {
                    LOGGER.info("'" + article.getDocName() + "' - Checking rights. Reason: " + "checkRights='" + checkRights + "' || !programming='" + !blogDoc.hasProgrammingRights() + "'");
                    Date publishdate = article.getPublishDate(language);
                    if ((publishdate != null) && (publishdate.after(new Date()))) {
                        if (blogDoc.hasAccessLevel("edit")) {
                            hasRight = true;
                        }
                    } else if (blogDoc.hasAccessLevel("view")) {
                        hasRight = true;
                    }
                    LOGGER.debug("'" + articleDoc.getSpace() + "' != '" + blogArticleSpace + "' && origBlogDoc.hasAccessLevel('edit') => '" + origBlogDoc.hasAccessLevel("edit") + "'");
                    if (!articleDoc.getSpace().equals(blogArticleSpace) && origBlogDoc.hasAccessLevel("edit")) {
                        hasEditOnBlog = true;
                    }
                } else {
                    LOGGER.info("'" + article.getDocName() + "' - Saved with programming rights " + "and not checking for rights.");
                    hasRight = true;
                    hasEditOnBlog = true;
                }
                LOGGER.info("'" + article.getDocName() + "' - hasRight: '" + hasRight + "' " + "hasEditOnBlog: '" + hasEditOnBlog + "'");
                if (hasRight) {
                    if (!articleDoc.getSpace().equals(blogArticleSpace)) {
                        Boolean isSubscribed = article.isSubscribed(spaceBlogDoc.getDocumentReference());
                        if (isSubscribed == null) {
                            if (!withUndecided || !hasEditOnBlog) {
                                LOGGER.info("'" + article.getDocName() + "' - Removed reason: from " + "subscribed blog && isUndecided && (!withUndecided='" + !withUndecided + "' || !hasEditOnBlog='" + !hasEditOnBlog + "')");
                                deleteArticles.add(article);
                            }
                        } else {
                            if (!isSubscribed && (!withUnsubscribed || !hasEditOnBlog)) {
                                LOGGER.info("'" + article.getDocName() + "' - Removed reason: from " + "subscribed blog && isDecided && ((!isSubscribed='" + !isSubscribed + "' && !withUnsubscribed='" + !withUnsubscribed + "') || " + "!hasEditOnBlog='" + !hasEditOnBlog + "')");
                                deleteArticles.add(article);
                            } else if (isSubscribed && !withSubscribed) {
                                LOGGER.info("'" + article.getDocName() + "' - Removed reason: from " + "subscribed blog && isDecided && (isSubscribed='" + isSubscribed + "' && !withSubscribed='" + !withSubscribed + "')");
                                deleteArticles.add(article);
                            }
                        }
                    } else if (subscribableOnly) {
                        LOGGER.info("'" + article.getDocName() + "' - Removed reason: from own " + "blog, but subscribableOnly='" + subscribableOnly + "'");
                        deleteArticles.add(article);
                    }
                } else {
                    LOGGER.info("'" + article.getDocName() + "' - Removed reason: has no rights");
                    deleteArticles.add(article);
                }
            } catch (XWikiException exp) {
                LOGGER.error("filterRightsAndSubscription: Failed to check rights on: " + article.getDocumentReference(), exp);
            }
        }
    }
    for (Iterator<Article> delIter = deleteArticles.iterator(); delIter.hasNext(); ) {
        articles.remove(delIter.next());
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

Document (com.xpn.xwiki.api.Document)97 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)60 XWikiException (com.xpn.xwiki.XWikiException)41 XWikiRestException (org.xwiki.rest.XWikiRestException)40 DocumentReference (org.xwiki.model.reference.DocumentReference)37 BaseObject (com.xpn.xwiki.objects.BaseObject)24 Test (org.junit.Test)23 WebApplicationException (javax.ws.rs.WebApplicationException)22 ArrayList (java.util.ArrayList)16 Attachment (com.xpn.xwiki.api.Attachment)14 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 Vector (java.util.Vector)12 Link (org.xwiki.rest.model.jaxb.Link)10 XWikiContext (com.xpn.xwiki.XWikiContext)9 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)7 Date (java.util.Date)7 XWiki (com.xpn.xwiki.api.XWiki)6 RangeIterable (org.xwiki.rest.internal.RangeIterable)6 Object (org.xwiki.rest.model.jaxb.Object)6 XWiki (com.xpn.xwiki.XWiki)5