use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class DefaultWikiUserConfigurationHelper method saveConfiguration.
@Override
public void saveConfiguration(WikiUserConfiguration configuration, String wikiId) throws WikiUserManagerException {
XWikiContext context = xcontextProvider.get();
// Get the document
XWikiDocument document = getDocument(wikiId);
// Fill the object
BaseObject object = document.getXObject(WikiUserClassDocumentInitializer.CONFIGURATION_CLASS, true, context);
object.setStringValue(WikiUserClassDocumentInitializer.FIELD_USERSCOPE, configuration.getUserScope().name().toLowerCase());
if (configuration.getMembershipType() != null) {
object.setStringValue(WikiUserClassDocumentInitializer.FIELD_MEMBERSHIPTYPE, configuration.getMembershipType().name().toLowerCase());
}
// Save the document
try {
XWiki xwiki = context.getWiki();
document.setHidden(true);
// The document must have a creator
if (document.getCreatorReference() == null) {
document.setCreatorReference(context.getUserReference());
}
// The document must have an author
if (document.getAuthorReference() == null) {
document.setAuthorReference(context.getUserReference());
}
xwiki.saveDocument(document, "Changed configuration.", context);
} catch (XWikiException e) {
throw new WikiUserManagerException(String.format("Fail to save the confguration document for wiki [%s].", wikiId), e);
}
}
use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class DefaultWikiUserConfigurationHelper method getDocument.
private XWikiDocument getDocument(String wikiId) throws WikiUserManagerException {
try {
XWikiContext context = xcontextProvider.get();
XWiki xwiki = context.getWiki();
DocumentReference reference = new DocumentReference(wikiId, CONFIGURATION_SPACE_NAME, CONFIGURATION_PAGE_NAME);
return xwiki.getDocument(reference, context);
} catch (XWikiException e) {
throw new WikiUserManagerException(String.format("Fail to get the configuration document for wiki [%s].", wikiId));
}
}
use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class XWikiContextInitializationFilter method initializeXWikiContext.
/**
* Initializes the XWiki context.
*
* @param request the request being processed
* @param response the response
* @throws ServletException if the initialization fails
*/
protected void initializeXWikiContext(ServletRequest request, ServletResponse response) throws ServletException {
try {
// Not all request types specify an action (e.g. GWT-RPC) so we default to the empty string.
String action = "";
XWikiServletContext xwikiEngine = new XWikiServletContext(this.filterConfig.getServletContext());
XWikiServletRequest xwikiRequest = new XWikiServletRequest((HttpServletRequest) request);
XWikiServletResponse xwikiResponse = new XWikiServletResponse((HttpServletResponse) response);
// Create the XWiki context.
XWikiContext context = Utils.prepareContext(action, xwikiRequest, xwikiResponse, xwikiEngine);
// parameter is specified.
if (this.mode >= 0) {
context.setMode(this.mode);
}
// Initialize the Container component which is the new way of transporting the Context in the new component
// architecture. Further initialization might require the Container component.
initializeContainerComponent(context);
// Initialize the XWiki database. XWiki#getXWiki(XWikiContext) calls XWikiContext.setWiki(XWiki).
XWiki xwiki = XWiki.getXWiki(context);
// Initialize the URL factory.
context.setURLFactory(xwiki.getURLFactoryService().createURLFactory(context.getMode(), context));
// Prepare the localized resources, according to the selected language.
xwiki.prepareResources(context);
// Initialize the current user.
XWikiUser user = context.getWiki().checkAuth(context);
if (user != null) {
DocumentReferenceResolver<String> documentReferenceResolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "explicit");
SpaceReference defaultUserSpace = new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(context.getWikiId()));
DocumentReference userReference = documentReferenceResolver.resolve(user.getUser(), defaultUserSpace);
context.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null : userReference);
}
} catch (XWikiException e) {
throw new ServletException("Failed to initialize the XWiki context.", e);
}
}
use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class DefaultWikiComponentBuilder method getDocumentReferences.
@Override
public List<DocumentReference> getDocumentReferences() {
List<DocumentReference> results = new ArrayList<DocumentReference>();
// Note that the query is made to work with Oracle which treats empty strings as null.
String query = ", BaseObject as obj, StringProperty as role where obj.className=? and obj.name=doc.fullName " + "and role.id.id=obj.id and role.id.name=? " + "and (role.value <> '' or (role.value is not null and '' is null))";
List<String> parameters = new ArrayList<String>();
parameters.add(COMPONENT_CLASS);
parameters.add(COMPONENT_ROLE_TYPE_FIELD);
try {
XWikiContext xcontext = xcontextProvider.get();
results.addAll(xcontext.getWiki().getStore().searchDocumentReferences(query, parameters, xcontext));
} catch (XWikiException e) {
this.logger.error("Failed to search for existing wiki components [{}]", e.getMessage());
}
return results;
}
use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.
the class DefaultWikiComponentBuilderEventListener method installOrUpdateComponentXClass.
/**
* Verify that the {@link #COMPONENT_CLASS} exists and is up-to-date (act if not).
*
* @throws XWikiException on failure
*/
private void installOrUpdateComponentXClass() throws XWikiException {
XWikiContext xcontext = getXWikiContext();
XWikiDocument doc = xcontext.getWiki().getDocument(COMPONENT_CLASS_REFERENCE, xcontext);
BaseClass bclass = doc.getXClass();
bclass.setDocumentReference(doc.getDocumentReference());
boolean needsUpdate = false;
needsUpdate |= initializeXClassDocumentMetadata(doc, "Wiki Component XWiki Class");
needsUpdate |= bclass.addTextField(COMPONENT_ROLE_TYPE_FIELD, "Component Role Type", 30);
needsUpdate |= bclass.addTextField(COMPONENT_ROLE_HINT_FIELD, "Component Role Hint", 30);
needsUpdate |= bclass.addStaticListField(COMPONENT_SCOPE_FIELD, "Component Scope", 1, false, "wiki=Current Wiki|user=Current User|global=Global", "select");
if (needsUpdate) {
this.update(doc);
}
}
Aggregations