Search in sources :

Example 96 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class EntityJobTest method visitDocuments.

@Test
public void visitDocuments() {
    DocumentReference alice = new DocumentReference("foo", "Alice", "WebHome");
    DocumentReference alicePrefs = new DocumentReference("WebPreferences", alice.getLastSpaceReference());
    DocumentReference aliceBio = new DocumentReference("Bio", alice.getLastSpaceReference());
    DocumentReference bob = new DocumentReference("foo", Arrays.asList("Alice", "Bob"), "WebHome");
    DocumentReference bobPrefs = new DocumentReference("WebPreferences", bob.getLastSpaceReference());
    DocumentReference bobBio = new DocumentReference("Bio", bob.getLastSpaceReference());
    DocumentReference carolBio = new DocumentReference("bar", Arrays.asList("Users", "Carol"), "Bio");
    SpaceReference spaceReference = mock(SpaceReference.class);
    when(this.modelBridge.getDocumentReferences(spaceReference)).thenReturn(Arrays.asList(alice, alicePrefs, aliceBio, bob, bobPrefs, bobBio, carolBio));
    NoopEntityJob job = new NoopEntityJob();
    initialize(job, new EntityRequest());
    final List<DocumentReference> documentReferences = new ArrayList<>();
    job.visitDocuments(spaceReference, new Visitor<DocumentReference>() {

        @Override
        public void visit(DocumentReference documentReference) {
            documentReferences.add(documentReference);
        }
    });
    // Space preferences documents are handled after their siblings.
    assertEquals(Arrays.asList(carolBio, aliceBio, bobBio, bob, bobPrefs, alice, alicePrefs), documentReferences);
}
Also used : EntityRequest(org.xwiki.refactoring.job.EntityRequest) SpaceReference(org.xwiki.model.reference.SpaceReference) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 97 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class SeparatePageRating method getPageReference.

/**
 * Generate page name from the container page We add Rating and getUniquePageName will add us a counter to our page.
 *
 * @param documentRef reference to the document with which the rating is associated
 * @return a reference to the document in which the rating is stored
 */
private DocumentReference getPageReference(DocumentReference documentRef) throws XWikiException {
    XWikiDocument doc = context.getWiki().getDocument(documentRef, context);
    String ratingsSpace = ratingsManager.getRatingsSpaceName(documentRef);
    String pageSufix = "R";
    boolean hasRatingsSpaceForeachSpace = ratingsManager.hasRatingsSpaceForeachSpace(documentRef);
    SpaceReference spaceReference = doc.getDocumentReference().getLastSpaceReference();
    spaceReference.replaceParent(spaceReference.getWikiReference(), this.context.getWikiReference());
    if (hasRatingsSpaceForeachSpace) {
        spaceReference = new SpaceReference(spaceReference.getName() + ratingsSpace, spaceReference.getParent());
        String uniqueName = getUniquePageName(ratingsSpace, doc.getName(), pageSufix, true);
        return new DocumentReference(uniqueName, spaceReference);
    } else if (ratingsSpace == null) {
        String uniqueName = getUniquePageName(doc.getSpace(), doc.getName() + pageSufix, "", true);
        return new DocumentReference(uniqueName, spaceReference);
    } else {
        String uniqueName = getUniquePageName(ratingsSpace, doc.getSpace() + "_" + doc.getName(), pageSufix, true);
        return new DocumentReference(context.getWikiId(), ratingsSpace, uniqueName);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 98 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class SeparatePageRating method addDocument.

/**
 * Adds a new document in which to store ratings.
 *
 * @param documentRef reference to the document with which the rating is associated
 * @param author the author of the rating
 * @param date the date when the rating was done
 * @param vote the author's vote
 * @return the newly created document
 * @throws RatingsException when an error occurs while creating the document
 */
private XWikiDocument addDocument(DocumentReference documentRef, DocumentReference author, Date date, int vote) throws RatingsException {
    try {
        DocumentReference pageRef = getPageReference(documentRef);
        String parentDocName = ratingsManager.entityReferenceSerializer.serialize(documentRef);
        XWiki xwiki = context.getWiki();
        XWikiDocument doc = xwiki.getDocument(pageRef, context);
        doc.setParent(parentDocName);
        doc.setHidden(true);
        BaseObject obj = doc.newXObject(RatingsManager.RATINGS_CLASSREFERENCE, context);
        obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_AUTHOR, ratingsManager.entityReferenceSerializer.serialize(author));
        obj.setDateValue(RatingsManager.RATING_CLASS_FIELDNAME_DATE, date);
        obj.setIntValue(RatingsManager.RATING_CLASS_FIELDNAME_VOTE, vote);
        obj.setStringValue(RatingsManager.RATING_CLASS_FIELDNAME_PARENT, parentDocName);
        return doc;
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWiki(com.xpn.xwiki.XWiki) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 99 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class SeparatePageRating method save.

@Override
public void save() throws RatingsException {
    DocumentReference superadmin = new DocumentReference("xwiki", XWiki.SYSTEM_SPACE, XWikiRightService.SUPERADMIN_USER);
    try {
        if (document == null) {
            throw new RatingsException(RatingsException.MODULE_PLUGIN_RATINGS, RatingsException.ERROR_RATINGS_SAVERATING_NULLDOCUMENT, "Cannot save invalid separate page rating, the rating document is null");
        }
        document.setCreatorReference(superadmin);
        document.setAuthorReference(superadmin);
        ContextualLocalizationManager localization = Utils.getComponent(ContextualLocalizationManager.class);
        context.getWiki().saveDocument(getDocument(), localization.getTranslationPlain("rating.saveComment"), true, context);
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
}
Also used : ContextualLocalizationManager(org.xwiki.localization.ContextualLocalizationManager) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Example 100 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class SeparatePageRatingsManager method getRatings.

@Override
public List<Rating> getRatings(DocumentReference documentRef, int start, int count, boolean asc) throws RatingsException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Calling separate page manager code for ratings");
    }
    String sql = ", BaseObject as obj, StringProperty as parentprop where doc.fullName=obj.name and obj.className=?" + " and obj.id=parentprop.id.id and parentprop.id.name=?" + " and parentprop.value=?" + " and obj.name not in (select obj2.name from BaseObject as obj2, StringProperty as statusprop where obj2.className=?" + " and obj2.id=statusprop.id.id and statusprop.id.name=? and (statusprop.value=? or statusprop.value= ?) and obj.id=obj2.id) order by doc.date " + (asc ? "asc" : "desc");
    List<?> params = new ArrayList<String>(Arrays.asList(getRatingsClassName(), RATING_CLASS_FIELDNAME_PARENT, entityReferenceSerializer.serialize(documentRef), getRatingsClassName(), "status", "moderated", "refused"));
    List<Rating> ratings = new ArrayList<Rating>();
    try {
        List<DocumentReference> ratingPageReferenceList = getXWikiContext().getWiki().getStore().searchDocumentReferences(sql, params, getXWikiContext());
        for (DocumentReference ratingPageReference : ratingPageReferenceList) {
            ratings.add(getRatingFromDocument(documentRef, getXWiki().getDocument(ratingPageReference, getXWikiContext())));
        }
    } catch (XWikiException e) {
        throw new RatingsException(e);
    }
    return ratings;
}
Also used : Rating(org.xwiki.ratings.Rating) ArrayList(java.util.ArrayList) RatingsException(org.xwiki.ratings.RatingsException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

DocumentReference (org.xwiki.model.reference.DocumentReference)1324 Test (org.junit.Test)711 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)482 BaseObject (com.xpn.xwiki.objects.BaseObject)250 XWikiContext (com.xpn.xwiki.XWikiContext)186 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)157 ArrayList (java.util.ArrayList)128 WikiReference (org.xwiki.model.reference.WikiReference)127 XWikiException (com.xpn.xwiki.XWikiException)121 EntityReference (org.xwiki.model.reference.EntityReference)113 SpaceReference (org.xwiki.model.reference.SpaceReference)96 XWiki (com.xpn.xwiki.XWiki)82 HashMap (java.util.HashMap)54 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)52 Expectations (org.jmock.Expectations)50 Before (org.junit.Before)50 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)46 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)45 AttachmentReference (org.xwiki.model.reference.AttachmentReference)44 Date (java.util.Date)42