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