Search in sources :

Example 51 with XWikiDocument

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Example 52 with XWikiDocument

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";
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiException(com.xpn.xwiki.XWikiException) XWikiLock(com.xpn.xwiki.doc.XWikiLock) XWikiException(com.xpn.xwiki.XWikiException)

Example 53 with XWikiDocument

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) RatingsException(org.xwiki.ratings.RatingsException) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 54 with XWikiDocument

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);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 55 with XWikiDocument

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

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23