Search in sources :

Example 21 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class ObjectAddAction method action.

@Override
public boolean action(XWikiContext context) throws XWikiException {
    // CSRF prevention
    if (!csrfTokenCheck(context)) {
        return false;
    }
    XWiki xwiki = context.getWiki();
    XWikiResponse response = context.getResponse();
    DocumentReference userReference = context.getUserReference();
    XWikiDocument doc = context.getDoc();
    ObjectAddForm oform = (ObjectAddForm) context.getForm();
    String className = oform.getClassName();
    EntityReference classReference = this.relativeResolver.resolve(className, EntityType.DOCUMENT);
    BaseObject object = doc.newXObject(classReference, context);
    BaseClass baseclass = object.getXClass(context);
    // The request parameter names that correspond to object fields must NOT specify the object number because the
    // object number is not known before the object is added. The following is a good parameter name:
    // Space.Class_property. As a consequence we use only the class name to extract the object from the request.
    Map<String, String[]> objmap = oform.getObject(className);
    // We need to have a string in the map for each field for the object to be correctly created.
    // Otherwise, queries using the missing properties will fail to return this object.
    @SuppressWarnings("unchecked") Collection<PropertyClass> fields = baseclass.getFieldList();
    for (PropertyClass property : fields) {
        String name = property.getName();
        if (objmap.get(name) == null) {
            objmap.put(name, EMPTY_PROPERTY);
        }
    }
    // Load the object properties that are defined in the request.
    baseclass.fromMap(objmap, object);
    doc.setAuthorReference(userReference);
    if (doc.isNew()) {
        doc.setCreatorReference(userReference);
    }
    xwiki.saveDocument(doc, localizePlainOrKey("core.comment.addObject"), true, context);
    // If this is an ajax request, no need to redirect.
    if (Utils.isAjaxRequest(context)) {
        context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
        return false;
    }
    // forward to edit
    String redirect = Utils.getRedirect("edit", "editor=object", "xcontinue", "xredirect");
    // If the redirect URL contains the xobjectNumber parameter then inject the number of the added object as its
    // value so that the target page knows which object was added.
    redirect = XOBJECT_NUMBER_PARAMETER.matcher(redirect).replaceFirst("$1xobjectNumber=" + object.getNumber() + "$2");
    sendRedirect(response, redirect);
    return false;
}
Also used : XWiki(com.xpn.xwiki.XWiki) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 22 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class ObjectRemoveAction method getObject.

protected BaseObject getObject(XWikiDocument doc, XWikiContext context) {
    ObjectRemoveForm form = (ObjectRemoveForm) context.getForm();
    BaseObject obj = null;
    String className = form.getClassName();
    int classId = form.getClassId();
    if (StringUtils.isBlank(className)) {
        getCurrentScriptContext().setAttribute("message", localizePlainOrKey("platform.core.action.objectRemove.noClassnameSpecified"), ScriptContext.ENGINE_SCOPE);
    } else if (classId < 0) {
        getCurrentScriptContext().setAttribute("message", localizePlainOrKey("platform.core.action.objectRemove.noObjectSpecified"), ScriptContext.ENGINE_SCOPE);
    } else {
        obj = doc.getObject(className, classId);
        if (obj == null) {
            getCurrentScriptContext().setAttribute("message", localizePlainOrKey("platform.core.action.objectRemove.invalidObject"), ScriptContext.ENGINE_SCOPE);
        }
    }
    return obj;
}
Also used : BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 23 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class ObjectRemoveAction method action.

@Override
public boolean action(XWikiContext context) throws XWikiException {
    // CSRF prevention
    if (!csrfTokenCheck(context)) {
        return false;
    }
    XWiki xwiki = context.getWiki();
    XWikiResponse response = context.getResponse();
    String username = context.getUser();
    XWikiDocument doc = context.getDoc();
    BaseObject obj = getObject(doc, context);
    if (obj == null) {
        return true;
    }
    doc.removeObject(obj);
    doc.setAuthor(username);
    xwiki.saveDocument(doc, localizePlainOrKey("core.comment.deleteObject"), true, context);
    if (Utils.isAjaxRequest(context)) {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        response.setContentLength(0);
    } else {
        // forward to edit
        String redirect = Utils.getRedirect("edit", context);
        sendRedirect(response, redirect);
    }
    return false;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWiki(com.xpn.xwiki.XWiki) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 24 with BaseObject

use of com.xpn.xwiki.objects.BaseObject 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 25 with BaseObject

use of com.xpn.xwiki.objects.BaseObject 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

BaseObject (com.xpn.xwiki.objects.BaseObject)484 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)309 DocumentReference (org.xwiki.model.reference.DocumentReference)225 Test (org.junit.Test)137 XWikiException (com.xpn.xwiki.XWikiException)102 XWikiContext (com.xpn.xwiki.XWikiContext)99 ArrayList (java.util.ArrayList)92 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)69 EntityReference (org.xwiki.model.reference.EntityReference)42 XWiki (com.xpn.xwiki.XWiki)41 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)40 List (java.util.List)30 Date (java.util.Date)24 BaseProperty (com.xpn.xwiki.objects.BaseProperty)23 Document (com.xpn.xwiki.api.Document)22 HashMap (java.util.HashMap)21 QueryException (org.xwiki.query.QueryException)18 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)16 IOException (java.io.IOException)16 Vector (java.util.Vector)16