Search in sources :

Example 36 with Document

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

the class DefaultUserAvatarAttachmentExtractor method getUserAvatar.

@Override
public Attachment getUserAvatar(DocumentReference userReference, int width, int height, String fileName) {
    // FIXME: Unfortunately, the ImagePlugin is too much request-oriented and not generic enough to be reused
    // without rewriting. In the end, that might be the right way to go and rewriting it might be inevitable.
    Attachment result = null;
    InputStream sourceImageInputStream = null;
    try {
        XWikiContext context = xwikiContextProvider.get();
        XWiki wiki = context.getWiki();
        XWikiAttachment realAvatarAttachment;
        XWikiAttachment fakeAvatarAttachment = null;
        // Use a second variable to be able to reassign it on the else branch below.
        DocumentReference actualUserReference = userReference;
        if (actualUserReference != null) {
            // Registered user.
            XWikiDocument userProfileDocument = wiki.getDocument(userReference, context);
            DocumentReference usersClassReference = wiki.getUserClass(context).getDocumentReference();
            String avatarFileName = userProfileDocument.getStringValue(usersClassReference, "avatar");
            realAvatarAttachment = userProfileDocument.getAttachment(avatarFileName);
            if (realAvatarAttachment != null && realAvatarAttachment.isImage(context)) {
                // Valid avatar, use the real attachment (and image), but make sure to use a clone as to not impact
                // the
                // real document.
                fakeAvatarAttachment = (XWikiAttachment) realAvatarAttachment.clone();
                sourceImageInputStream = realAvatarAttachment.getContentInputStream(context);
                result = new Attachment(new Document(userProfileDocument, context), fakeAvatarAttachment, context);
            } else {
                // No or invalid avatar, treat the user reference as it did not exist (guest user) so it can be
                // handled below for both cases.
                actualUserReference = null;
            }
        }
        if (actualUserReference == null) {
            // Guest user.
            // No avatar. Return a fake attachment with the "noavatar.png" standard image.
            fakeAvatarAttachment = new XWikiAttachment();
            sourceImageInputStream = environment.getResourceAsStream("/resources/icons/xwiki/noavatar.png");
            result = new Attachment(null, fakeAvatarAttachment, context);
        }
        // In both cases, set an empty attachment content that will be filled with the resized image. This way we
        // also avoid a request to the DB for the attachment content, since it will already be available.
        fakeAvatarAttachment.setAttachment_content(new XWikiAttachmentContent(fakeAvatarAttachment));
        // Resize the image and write it to the fake attachment.
        int resizedWidth = 50;
        int resizedHeight = 50;
        resizeImageToAttachment(sourceImageInputStream, resizedWidth, resizedHeight, fakeAvatarAttachment);
        // Set a fixed name for the user avatar file so that it is easy to work with in a template, for example.
        fakeAvatarAttachment.setFilename(fileName);
    } catch (Exception e) {
        logger.error("Failed to retrieve the avatar for the user {}", userReference, e);
        return null;
    } finally {
        // Close the source image input stream since we are done reading from it.
        if (sourceImageInputStream != null) {
            IOUtils.closeQuietly(sourceImageInputStream);
        }
    }
    return result;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) InputStream(java.io.InputStream) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) 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) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) IOException(java.io.IOException)

Example 37 with Document

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

the class ArticleEngineHQL method getHQL.

private String getHQL(String blogArticleSpace, String language, List<String> subscribedBlogs, boolean withSubscribable) throws XWikiException {
    String useInt = " ";
    String subscribableHQL = "";
    String subscribedBlogsStr = "";
    LOGGER.debug("if params: (" + subscribedBlogs + "!= null) (" + ((subscribedBlogs != null) ? subscribedBlogs.size() : "null") + " > 0) (withSubscribable = " + withSubscribable + ")");
    if ((subscribedBlogs != null) && (subscribedBlogs.size() > 0) && withSubscribable) {
        // useInt = ", IntegerProperty as int ";
        subscribableHQL = /*
                         * to slow with this query part "and (obj.id = int.id.id " +
                         * "and int.id.name = 'isSubscribable' " + "and int.value='1')
                         */
        ")";
        for (Iterator<String> blogIter = subscribedBlogs.iterator(); blogIter.hasNext(); ) {
            String blogSpace = blogIter.next();
            Document blogDoc = blogService.getBlogPageByBlogSpace(blogSpace).newDocument(getContext());
            com.xpn.xwiki.api.Object obj = blogDoc.getObject("Celements2.BlogConfigClass");
            Property prop = obj.getProperty("is_subscribable");
            LOGGER.debug("blogDoc is '" + blogDoc.getFullName() + "' and obj is '" + obj + "' the is_subscribable property is '" + prop + "'");
            if (prop != null) {
                int isSubscribable = Integer.parseInt(prop.getValue().toString());
                LOGGER.debug("is_subscribable property exists and its value is: '" + isSubscribable + "'");
                if (isSubscribable == 1) {
                    if (subscribedBlogsStr.length() > 0) {
                        subscribedBlogsStr += "or ";
                    } else {
                        subscribedBlogsStr = "or ((";
                    }
                    subscribedBlogsStr += "doc.space='" + blogSpace + "' ";
                }
            }
        }
        if (subscribedBlogsStr.length() > 0) {
            subscribedBlogsStr += ") " + subscribableHQL;
        }
    }
    String hql = "select doc.fullName from XWikiDocument as doc, BaseObject as obj, " + "DateProperty as date, StringProperty as lang" + useInt;
    hql += "where obj.name=doc.fullName ";
    hql += "and obj.className='XWiki.ArticleClass' ";
    hql += "and (doc.space = '" + blogArticleSpace + "' " + subscribedBlogsStr + ") ";
    hql += "and lang.id.id=obj.id ";
    hql += "and lang.id.name='lang' ";
    hql += "and lang.value = '" + language + "' ";
    hql += "and obj.id = date.id.id ";
    hql += "and date.id.name='publishdate' ";
    hql += "order by date.value desc, doc.name asc ";
    LOGGER.debug("hql built: " + hql);
    return hql;
}
Also used : Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Property(com.xpn.xwiki.api.Property)

Example 38 with Document

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

the class ArticleEngineHQLTest method testGetBlogArticles_getArchivedArticles.

@Test
public void testGetBlogArticles_getArchivedArticles() throws XWikiException {
    String artSpace = "ArtSpace";
    DocumentReference articleDocRef = new DocumentReference("xwikidb", artSpace, "Article");
    XWikiDocument xdoc = createMock(XWikiDocument.class);
    Document doc = new Document(xdoc, context);
    List<Object> artName = new ArrayList<>();
    String articleFN = artSpace + ".Article";
    artName.add(articleFN);
    expect(xwiki.search(eq(getAllArticlesHQL(artSpace)), same(context))).andReturn(artName).once();
    DocumentReference blogConfigDocRef = new DocumentReference(context.getDatabase(), artSpace, "BlogConfig");
    XWikiDocument confxDoc = createMock(XWikiDocument.class);
    Document confDoc = createMock(Document.class);
    expect(xwiki.getDocument(eq(blogConfigDocRef), same(context))).andReturn(confxDoc).atLeastOnce();
    expect(xwiki.getDocument(eq(articleDocRef), same(context))).andReturn(xdoc).atLeastOnce();
    expect(xwiki.getDocument(eq(articleFN), same(context))).andReturn(xdoc).atLeastOnce();
    expect(blogServiceMock.getBlogPageByBlogSpace(eq("ArtSpace"))).andReturn(confxDoc).atLeastOnce();
    expect(blogServiceMock.getBlogDocRefByBlogSpace(eq("ArtSpace"))).andReturn(blogConfigDocRef).atLeastOnce();
    expect(xwiki.getSpacePreference(eq("default_language"), eq(artSpace), eq(""), same(context))).andReturn("de").once();
    expect(confDoc.hasProgrammingRights()).andReturn(true).atLeastOnce();
    expect(confDoc.hasAccessLevel((String) anyObject())).andReturn(true).atLeastOnce();
    expect(confxDoc.newDocument(same(context))).andReturn(confDoc).atLeastOnce();
    expect(xdoc.newDocument(same(context))).andReturn(doc).atLeastOnce();
    BaseObject obj = new BaseObject();
    obj.setDocumentReference(articleDocRef);
    obj.setStringValue("lang", "de");
    obj.setDateValue("publishdate", new Date(0, 11, 31));
    obj.setDateValue("archivedate", new Date(10, 0, 1));
    obj.setStringValue("title", "the title");
    obj.setStringValue("content", "the content");
    Vector<BaseObject> objVec = new Vector<>();
    objVec.add(obj);
    expect(xdoc.getSpace()).andReturn(artSpace).atLeastOnce();
    DocumentReference articleClassRef = new DocumentReference("xwikidb", "XWiki", "ArticleClass");
    expect(xdoc.clone()).andReturn(xdoc).atLeastOnce();
    expect(xdoc.resolveClassReference(eq("XWiki.ArticleClass"))).andReturn(articleClassRef);
    expect(xdoc.getXObjects(eq(articleClassRef))).andReturn(objVec);
    expect(xdoc.getDocumentReference()).andReturn(articleDocRef).atLeastOnce();
    replayDefault(confDoc, confxDoc, xdoc);
    List<Article> result = engine.getBlogArticles(artSpace, Collections.<String>emptyList(), "de", true, false, false, true, true, true, true, false, true, true);
    assertEquals(1, result.size());
    verifyDefault(confDoc, confxDoc, xdoc);
}
Also used : ArrayList(java.util.ArrayList) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Date(java.util.Date) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) Vector(java.util.Vector) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 39 with Document

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

the class ArticleEngineHQLTest method testGetBlogArticles_getArchivedArticles_archiveBeforePublish.

@Test
public void testGetBlogArticles_getArchivedArticles_archiveBeforePublish() throws XWikiException {
    String artSpace = "ArtSpace";
    DocumentReference articleDocRef = new DocumentReference("xwikidb", artSpace, "Article");
    XWikiDocument xdoc = createMock(XWikiDocument.class);
    Document doc = new Document(xdoc, context);
    List<Object> artName = new ArrayList<>();
    String articleFN = artSpace + ".Article";
    artName.add(articleFN);
    expect(xwiki.search(eq(getAllArticlesHQL(artSpace)), same(context))).andReturn(artName).once();
    DocumentReference blogConfigDocRef = new DocumentReference(context.getDatabase(), artSpace, "BlogConfig");
    XWikiDocument confxDoc = createMock(XWikiDocument.class);
    Document confDoc = createMock(Document.class);
    expect(xwiki.getDocument(eq(blogConfigDocRef), same(context))).andReturn(confxDoc).atLeastOnce();
    expect(xwiki.getDocument(eq(articleDocRef), same(context))).andReturn(xdoc).atLeastOnce();
    expect(xwiki.getDocument(eq(articleFN), same(context))).andReturn(xdoc).atLeastOnce();
    expect(blogServiceMock.getBlogPageByBlogSpace(eq("ArtSpace"))).andReturn(confxDoc).atLeastOnce();
    expect(blogServiceMock.getBlogDocRefByBlogSpace(eq("ArtSpace"))).andReturn(blogConfigDocRef).atLeastOnce();
    expect(xwiki.getSpacePreference(eq("default_language"), eq(artSpace), eq(""), same(context))).andReturn("de").once();
    expect(confDoc.hasProgrammingRights()).andReturn(true).atLeastOnce();
    expect(confDoc.hasAccessLevel((String) anyObject())).andReturn(true).atLeastOnce();
    expect(confxDoc.newDocument(same(context))).andReturn(confDoc).atLeastOnce();
    expect(xdoc.newDocument(same(context))).andReturn(doc);
    BaseObject obj = new BaseObject();
    obj.setDocumentReference(articleDocRef);
    obj.setStringValue("lang", "de");
    obj.setDateValue("publishdate", new Date(8099, 11, 31));
    obj.setDateValue("archivedate", new Date(0, 0, 1));
    obj.setStringValue("title", "the title");
    obj.setStringValue("content", "the content");
    Vector<BaseObject> objVec = new Vector<>();
    objVec.add(obj);
    DocumentReference articleClassRef = new DocumentReference("xwikidb", "XWiki", "ArticleClass");
    expect(xdoc.clone()).andReturn(xdoc).atLeastOnce();
    expect(xdoc.resolveClassReference(eq("XWiki.ArticleClass"))).andReturn(articleClassRef);
    expect(xdoc.getXObjects(eq(articleClassRef))).andReturn(objVec);
    expect(xdoc.getDocumentReference()).andReturn(articleDocRef).atLeastOnce();
    expect(xdoc.getSpace()).andReturn(artSpace).atLeastOnce();
    replayDefault(confDoc, confxDoc, xdoc);
    List<Article> result = engine.getBlogArticles(artSpace, Collections.<String>emptyList(), "de", true, false, false, true, true, true, true, false, true, true);
    assertEquals(1, result.size());
    verifyDefault(confDoc, confxDoc, xdoc);
}
Also used : ArrayList(java.util.ArrayList) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Date(java.util.Date) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) Vector(java.util.Vector) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 40 with Document

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

the class ArticleEngineHQLTest method testGetBlogArticles_getArchivedArticles_noArchiveDateSet.

@Test
public void testGetBlogArticles_getArchivedArticles_noArchiveDateSet() throws XWikiException {
    String artSpace = "ArtSpace";
    DocumentReference articleDocRef = new DocumentReference("xwikidb", artSpace, "Article");
    XWikiDocument xdoc = createMock(XWikiDocument.class);
    Document doc = new Document(xdoc, context);
    List<Object> artName = new ArrayList<>();
    String articleFN = artSpace + ".Article";
    artName.add(articleFN);
    expect(xwiki.search(eq(getAllArticlesHQL(artSpace)), same(context))).andReturn(artName).once();
    DocumentReference blogConfigDocRef = new DocumentReference(context.getDatabase(), artSpace, "BlogConfig");
    XWikiDocument confxDoc = createMock(XWikiDocument.class);
    Document confDoc = createMock(Document.class);
    expect(xwiki.getDocument(eq(articleFN), same(context))).andReturn(xdoc).atLeastOnce();
    expect(blogServiceMock.getBlogPageByBlogSpace(eq("ArtSpace"))).andReturn(confxDoc).atLeastOnce();
    expect(xwiki.getSpacePreference(eq("default_language"), eq(artSpace), eq(""), same(context))).andReturn("de").once();
    expect(confxDoc.newDocument(same(context))).andReturn(confDoc).atLeastOnce();
    expect(xdoc.newDocument(same(context))).andReturn(doc);
    BaseObject obj = new BaseObject();
    obj.setDocumentReference(articleDocRef);
    obj.setStringValue("lang", "de");
    obj.setDateValue("publishdate", new Date(9999, 11, 31));
    obj.setDateValue("archivedate", null);
    obj.setStringValue("title", "the title");
    obj.setStringValue("content", "the content");
    Vector<BaseObject> objVec = new Vector<>();
    objVec.add(obj);
    DocumentReference articleClassRef = new DocumentReference("xwikidb", "XWiki", "ArticleClass");
    expect(xdoc.clone()).andReturn(xdoc).atLeastOnce();
    expect(xdoc.resolveClassReference(eq("XWiki.ArticleClass"))).andReturn(articleClassRef);
    expect(xdoc.getXObjects(eq(articleClassRef))).andReturn(objVec);
    expect(xdoc.getDocumentReference()).andReturn(articleDocRef).atLeastOnce();
    expect(xdoc.getSpace()).andReturn(artSpace).atLeastOnce();
    replayDefault(confDoc, confxDoc, xdoc);
    List<Article> result = engine.getBlogArticles(artSpace, Collections.<String>emptyList(), "de", true, false, false, true, true, true, true, false, true, true);
    assertEquals(0, result.size());
    verifyDefault(confDoc, confxDoc, xdoc);
}
Also used : ArrayList(java.util.ArrayList) Document(com.xpn.xwiki.api.Document) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Date(java.util.Date) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObject(com.xpn.xwiki.objects.BaseObject) Vector(java.util.Vector) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

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