use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class AbstractPropChangeAction method action.
/**
* Tries to change the specified property, and redirect back to the class editor (or the specified {@code xredirect}
* location). If the property does not exist, forward to the exception page.
*
* @param context the current request context
* @return {@code false} if the operation succeeded and the response is finished, {@code true} if the response must
* be rendered by {@link #render(XWikiContext)}
* @throws XWikiException if saving the document fails
*/
@Override
public boolean action(XWikiContext context) throws XWikiException {
XWikiResponse response = context.getResponse();
XWikiDocument doc = context.getDoc();
PropChangeForm form = (PropChangeForm) context.getForm();
String propertyName = form.getPropertyName();
BaseClass xclass = doc.getXClass();
if (propertyName != null && xclass.get(propertyName) != null) {
// CSRF prevention
if (!csrfTokenCheck(context)) {
return false;
}
changePropertyDefinition(xclass, propertyName, context);
} else {
return true;
}
if (Utils.isAjaxRequest(context)) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
response.setContentLength(0);
} else {
String redirect = Utils.getRedirect("edit", "editor=class", context);
sendRedirect(response, redirect);
}
return false;
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class AdminAction method render.
@Override
public String render(XWikiContext context) throws XWikiException {
XWikiRequest request = context.getRequest();
String content = request.getParameter("content");
XWikiDocument doc = context.getDoc();
XWikiForm form = context.getForm();
synchronized (doc) {
XWikiDocument tdoc = (XWikiDocument) context.get("tdoc");
EditForm peform = (EditForm) form;
String parent = peform.getParent();
if (parent != null) {
doc.setParent(parent);
}
String creator = peform.getCreator();
if (creator != null) {
doc.setCreator(creator);
}
String defaultTemplate = peform.getDefaultTemplate();
if (defaultTemplate != null) {
doc.setDefaultTemplate(defaultTemplate);
}
String defaultLanguage = peform.getDefaultLanguage();
if ((defaultLanguage != null) && !defaultLanguage.equals("")) {
doc.setDefaultLanguage(defaultLanguage);
}
if (doc.getDefaultLanguage().equals("")) {
doc.setDefaultLanguage(context.getWiki().getLanguagePreference(context));
}
String language = context.getWiki().getLanguagePreference(context);
String languagefromrequest = context.getRequest().getParameter("language");
String languagetoedit = ((languagefromrequest == null) || (languagefromrequest.equals(""))) ? language : languagefromrequest;
if ((languagetoedit == null) || (languagetoedit.equals("default"))) {
languagetoedit = "";
}
if (doc.isNew() || (doc.getDefaultLanguage().equals(languagetoedit))) {
languagetoedit = "";
}
if (languagetoedit.equals("")) {
// In this case the created document is going to be the default document
tdoc = doc;
context.put("tdoc", doc);
if (doc.isNew()) {
doc.setDefaultLanguage(language);
doc.setLanguage("");
}
} else {
// this means the translated doc did not exists so we need to create it
if ((tdoc == doc)) {
tdoc = new XWikiDocument(doc.getDocumentReference());
tdoc.setLanguage(languagetoedit);
tdoc.setContent(doc.getContent());
tdoc.setSyntax(doc.getSyntax());
tdoc.setAuthor(context.getUser());
tdoc.setStore(doc.getStore());
context.put("tdoc", tdoc);
}
}
XWikiDocument tdoc2 = tdoc.clone();
if (content != null && !content.isEmpty()) {
tdoc2.setContent(content);
}
context.put("tdoc", tdoc2);
try {
tdoc2.readFromTemplate(peform, context);
} catch (XWikiException e) {
if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) {
context.put("exception", e);
return "docalreadyexists";
}
}
/* Setup a lock */
try {
XWikiLock lock = tdoc.getLock(context);
if ((lock == null) || (lock.getUserName().equals(context.getUser())) || (peform.isLockForce())) {
tdoc.setLock(context.getUser(), context);
}
} catch (Exception e) {
// Lock should never make XWiki fail
// But we should log any related information
LOGGER.error("Exception while setting up lock", e);
}
}
return "admin";
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultRatingsManager method getRating.
@Override
public Rating getRating(DocumentReference documentRef, int id) throws RatingsException {
try {
int skipped = 0;
XWikiDocument doc = getXWiki().getDocument(documentRef, getXWikiContext());
List<BaseObject> bobjects = doc.getObjects(getRatingsClassName());
if (bobjects != null) {
for (BaseObject bobj : bobjects) {
if (bobj != null) {
if (skipped < id) {
skipped++;
} else {
return getDefaultRating(documentRef, bobj);
}
}
}
}
} catch (XWikiException e) {
throw new RatingsException(e);
}
return null;
}
use of com.xpn.xwiki.doc.XWikiDocument 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 com.xpn.xwiki.doc.XWikiDocument 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);
}
}
Aggregations