use of org.xwiki.script.ScriptContextManager in project xwiki-platform by xwiki.
the class DBTreeListClass method displayTree.
private String displayTree(String name, String prefix, List<String> selectlist, String mode, XWikiContext context) {
ScriptContextManager scriptManager = Utils.getComponent(ScriptContextManager.class);
ScriptContext scontext = scriptManager.getCurrentScriptContext();
Map<String, ListItem> map = getMap(context);
Map<String, List<ListItem>> treemap = getTreeMap(context);
scontext.setAttribute("selectlist", selectlist, ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("fieldname", prefix + name, ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("tree", map, ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("treelist", getTreeList(treemap, map, context), ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("treemap", treemap, ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("mode", mode, ScriptContext.ENGINE_SCOPE);
return context.getWiki().parseTemplate("treeview.vm", context);
}
use of org.xwiki.script.ScriptContextManager in project xwiki-platform by xwiki.
the class XWiki method include.
public String include(String topic, boolean isForm, XWikiContext context) throws XWikiException {
String database = null, incdatabase = null;
String prefixedTopic, localTopic;
// Save current documents in script context
Document currentAPIdoc = null, currentAPIcdoc = null, currentAPItdoc = null;
ScriptContextManager scritContextManager = Utils.getComponent(ScriptContextManager.class);
ScriptContext scontext = scritContextManager.getScriptContext();
String currentDocName = context.getWikiId() + ":" + context.getDoc().getFullName();
if (scontext != null) {
currentAPIdoc = (Document) scontext.getAttribute("doc");
currentAPIcdoc = (Document) scontext.getAttribute("cdoc");
currentAPItdoc = (Document) scontext.getAttribute("tdoc");
}
try {
int i0 = topic.indexOf(':');
if (i0 != -1) {
incdatabase = topic.substring(0, i0);
database = context.getWikiId();
context.setWikiId(incdatabase);
prefixedTopic = topic;
localTopic = topic.substring(i0 + 1);
} else {
prefixedTopic = context.getWikiId() + ":" + topic;
localTopic = topic;
}
XWikiDocument doc = null;
try {
LOGGER.debug("Including Topic " + topic);
try {
@SuppressWarnings("unchecked") Set<String> includedDocs = (Set<String>) context.get("included_docs");
if (includedDocs == null) {
includedDocs = new HashSet<String>();
context.put("included_docs", includedDocs);
}
if (includedDocs.contains(prefixedTopic) || currentDocName.equals(prefixedTopic)) {
LOGGER.warn("Error on too many recursive includes for topic " + topic);
return "Cannot make recursive include";
}
includedDocs.add(prefixedTopic);
} catch (Exception e) {
}
// Get document to include
DocumentReference targetDocumentReference = getCurrentMixedDocumentReferenceResolver().resolve(localTopic);
doc = getDocument(targetDocumentReference, context);
if (checkAccess("view", doc, context) == false) {
throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, "Access to this document is denied: " + doc);
}
} catch (XWikiException e) {
LOGGER.warn("Exception Including Topic " + topic, e);
return "Topic " + topic + " does not exist";
}
XWikiDocument contentdoc = doc.getTranslatedDocument(context);
String result;
if (isForm) {
// We do everything in the context of the including document
if (database != null) {
context.setWikiId(database);
}
// Note: the Script macro in the new rendering checks for programming rights for the document in
// the xwiki context.
result = getRenderedContent(contentdoc, (XWikiDocument) context.get("doc"), context);
} else {
// We stay in the included document context
// Since the Script macro checks for programming rights in the current document, we need to
// temporarily set the contentdoc as the current doc before rendering it.
XWikiDocument originalDoc = null;
try {
originalDoc = context.getDoc();
context.put("doc", doc);
result = getRenderedContent(contentdoc, doc, context);
} finally {
context.put("doc", originalDoc);
}
}
try {
@SuppressWarnings("unchecked") Set<String> includedDocs = (Set<String>) context.get("included_docs");
if (includedDocs != null) {
includedDocs.remove(prefixedTopic);
}
} catch (Exception e) {
}
return result;
} finally {
if (database != null) {
context.setWikiId(database);
}
if (currentAPIdoc != null) {
if (scontext != null) {
scontext.setAttribute("doc", currentAPIdoc, ScriptContext.ENGINE_SCOPE);
}
}
if (currentAPIcdoc != null) {
if (scontext != null) {
scontext.setAttribute("cdoc", currentAPIcdoc, ScriptContext.ENGINE_SCOPE);
}
}
if (currentAPItdoc != null) {
if (scontext != null) {
scontext.setAttribute("tdoc", currentAPItdoc, ScriptContext.ENGINE_SCOPE);
}
}
}
}
use of org.xwiki.script.ScriptContextManager in project xwiki-platform by xwiki.
the class XWikiValidationStatus method addError.
public void addError(String className, String propName, String propPrettyName, String validationMessage, XWikiContext context) {
getErrorObjects().add(className);
getPropertyErrors().add(propName);
if ((validationMessage != null) && (!validationMessage.trim().equals(""))) {
getErrors().add(validationMessage);
} else {
ScriptContextManager scriptManager = Utils.getComponent(ScriptContextManager.class);
ScriptContext scontext = scriptManager.getCurrentScriptContext();
if (scontext == null) {
getErrors().add("Validation error for property " + propPrettyName + " in class " + className);
} else {
scontext.setAttribute("className", className, ScriptContext.ENGINE_SCOPE);
scontext.setAttribute("propName", propPrettyName, ScriptContext.ENGINE_SCOPE);
String message = context.getMessageTool().get("validationerror", Collections.singletonList(propName));
getErrors().add(message);
}
}
}
Aggregations