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);
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations