use of com.xpn.xwiki.doc.XWikiLock 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";
}
use of com.xpn.xwiki.doc.XWikiLock in project xwiki-platform by xwiki.
the class XWikiHibernateStore method loadLock.
// ---------------------------------------
// Locks
// ---------------------------------------
@Override
public XWikiLock loadLock(long docId, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
XWikiContext context = getExecutionXContext(inputxcontext, true);
XWikiLock lock = null;
try {
if (bTransaction) {
checkHibernate(context);
bTransaction = beginTransaction(false, context);
}
Session session = getSession(context);
Query query = session.createQuery("select lock.docId from XWikiLock as lock where lock.docId = :docId");
query.setLong("docId", docId);
if (query.uniqueResult() != null) {
lock = new XWikiLock();
session.load(lock, Long.valueOf(docId));
}
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_LOCK, "Exception while loading lock", e);
} finally {
try {
if (bTransaction) {
endTransaction(context, false, false);
}
} catch (Exception e) {
}
restoreExecutionXContext();
}
return lock;
}
use of com.xpn.xwiki.doc.XWikiLock in project xwiki-platform by xwiki.
the class SaveAction method save.
/**
* Saves the current document, updated according to the parameters sent in the request.
*
* @param context The current request {@link XWikiContext context}.
* @return <code>true</code> if there was an error and the response needs to render an error page,
* <code>false</code> if the document was correctly saved.
* @throws XWikiException If an error occured: cannot communicate with the storage module, or cannot update the
* document because the request contains invalid parameters.
*/
public boolean save(XWikiContext context) throws XWikiException {
XWiki xwiki = context.getWiki();
XWikiRequest request = context.getRequest();
XWikiDocument doc = context.getDoc();
EditForm form = (EditForm) context.getForm();
// Check save session
int sectionNumber = 0;
if (request.getParameter("section") != null && xwiki.hasSectionEdit(context)) {
sectionNumber = Integer.parseInt(request.getParameter("section"));
}
// We need to clone this document first, since a cached storage would return the same object for the
// following requests, so concurrent request might get a partially modified object, or worse, if an error
// occurs during the save, the cached object will not reflect the actual document at all.
doc = doc.clone();
String language = form.getLanguage();
// FIXME Which one should be used: doc.getDefaultLanguage or
// form.getDefaultLanguage()?
// String defaultLanguage = ((EditForm) form).getDefaultLanguage();
XWikiDocument tdoc;
if (doc.isNew() || (language == null) || (language.equals("")) || (language.equals("default")) || (language.equals(doc.getDefaultLanguage()))) {
// Saving the default document translation.
// Need to save parent and defaultLanguage if they have changed
tdoc = doc;
} else {
tdoc = doc.getTranslatedDocument(language, context);
if ((tdoc == doc) && xwiki.isMultiLingual(context)) {
// Saving a new document translation.
tdoc = new XWikiDocument(doc.getDocumentReference());
tdoc.setLanguage(language);
tdoc.setStore(doc.getStore());
} else if (tdoc != doc) {
// Saving an existing document translation (but not the default one).
// Same as above, clone the object retrieved from the store cache.
tdoc = tdoc.clone();
}
}
if (doc.isNew()) {
doc.setLocale(Locale.ROOT);
if (doc.getDefaultLocale() == Locale.ROOT) {
doc.setDefaultLocale(LocaleUtils.toLocale(context.getWiki().getLanguagePreference(context), Locale.ROOT));
}
}
try {
tdoc.readFromTemplate(form.getTemplate(), context);
} catch (XWikiException e) {
if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) {
context.put("exception", e);
return true;
}
}
if (sectionNumber != 0) {
XWikiDocument sectionDoc = tdoc.clone();
sectionDoc.readFromForm(form, context);
String sectionContent = sectionDoc.getContent() + "\n";
String content = tdoc.updateDocumentSection(sectionNumber, sectionContent);
tdoc.setContent(content);
tdoc.setComment(sectionDoc.getComment());
tdoc.setMinorEdit(sectionDoc.isMinorEdit());
} else {
tdoc.readFromForm(form, context);
}
// TODO: handle Author
String username = context.getUser();
tdoc.setAuthor(username);
if (tdoc.isNew()) {
tdoc.setCreator(username);
}
// Make sure we have at least the meta data dirty status
tdoc.setMetaDataDirty(true);
// Validate the document if we have xvalidate=1 in the request
if ("1".equals(request.getParameter("xvalidate"))) {
boolean validationResult = tdoc.validate(context);
// If the validation fails we should show the "Inline form" edit mode
if (validationResult == false) {
// Set display context to 'edit'
context.put("display", "edit");
// Set the action used by the "Inline form" edit mode as the context action. See #render(XWikiContext).
context.setAction(tdoc.getDefaultEditMode(context));
// Set the document in the context
context.put("doc", doc);
context.put("cdoc", tdoc);
context.put("tdoc", tdoc);
// Force the "Inline form" edit mode.
getCurrentScriptContext().setAttribute("editor", "inline", ScriptContext.ENGINE_SCOPE);
return true;
}
}
// redirect place-holders that are created when we move pages around.
if (tdoc.getXObject(REDIRECT_CLASS) != null && request.getParameter("XWiki.RedirectClass_0_location") == null) {
tdoc.removeXObjects(REDIRECT_CLASS);
}
// We get the comment to be used from the document
// It was read using readFromForm
xwiki.saveDocument(tdoc, tdoc.getComment(), tdoc.isMinorEdit(), context);
Job createJob = startCreateJob(tdoc.getDocumentReference(), form);
if (createJob != null) {
if (isAsync(request)) {
if (Utils.isAjaxRequest(context)) {
// Redirect to the job status URL of the job we have just launched.
sendRedirect(context.getResponse(), String.format("%s/rest/jobstatus/%s?media=json", context.getRequest().getContextPath(), serializeJobId(createJob.getRequest().getId())));
}
// else redirect normally and the operation will eventually finish in the background.
// Note: It is preferred that async mode is called in an AJAX request that can display the progress.
} else {
// Sync mode, default, wait for the work to finish.
try {
createJob.join();
} catch (InterruptedException e) {
throw new XWikiException(String.format("Interrupted while waiting for template [%s] to be processed when creating the document [%s]", form.getTemplate(), tdoc.getDocumentReference()), e);
}
}
} else {
// Nothing more to do, just unlock the document.
XWikiLock lock = tdoc.getLock(context);
if (lock != null) {
tdoc.removeLock(context);
}
}
return false;
}
use of com.xpn.xwiki.doc.XWikiLock in project xwiki-platform by xwiki.
the class InlineAction method render.
@Override
public String render(XWikiContext context) throws XWikiException {
XWikiDocument doc = context.getDoc();
synchronized (doc) {
XWikiForm form = context.getForm();
XWikiDocument cdoc = (XWikiDocument) context.get("cdoc");
if (cdoc == null) {
cdoc = doc;
}
EditForm peform = (EditForm) form;
XWikiDocument doc2 = doc.clone();
context.put("doc", doc2);
String parent = peform.getParent();
if (parent != null) {
doc2.setParent(parent);
}
String creator = peform.getCreator();
if (creator != null) {
doc2.setCreator(creator);
}
String defaultLanguage = peform.getDefaultLanguage();
if ((defaultLanguage != null) && !defaultLanguage.equals("")) {
doc2.setDefaultLanguage(defaultLanguage);
}
if (doc2.getDefaultLanguage().equals("")) {
doc2.setDefaultLanguage(context.getWiki().getLanguagePreference(context));
}
try {
doc2.readFromTemplate(peform, context);
} catch (XWikiException e) {
if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) {
return "docalreadyexists";
}
}
if (doc == cdoc) {
context.put("cdoc", doc2);
} else {
XWikiDocument cdoc2 = cdoc.clone();
cdoc2.readFromTemplate(peform, context);
context.put("cdoc", cdoc2);
}
doc2.readFromForm((EditForm) form, context);
// scripts in newly created documents even if the user creating the document has the right.
if (doc2.isNew()) {
doc2.setCreatorReference(context.getUserReference());
doc2.setAuthorReference(context.getUserReference());
doc2.setContentAuthorReference(context.getUserReference());
}
/* Setup a lock */
try {
XWikiLock lock = doc.getLock(context);
if ((lock == null) || (lock.getUserName().equals(context.getUser())) || (peform.isLockForce())) {
doc.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);
}
}
// Make sure object property fields are displayed in edit mode.
// See XWikiDocument#display(String, BaseObject, XWikiContext)
context.put("display", "edit");
return "inline";
}
use of com.xpn.xwiki.doc.XWikiLock in project xwiki-platform by xwiki.
the class LockAction method action.
@Override
public boolean action(XWikiContext context) throws XWikiException {
XWikiRequest request = context.getRequest();
XWikiResponse response = context.getResponse();
XWikiDocument doc = context.getDoc();
XWikiForm form = context.getForm();
String language = ((EditForm) form).getLanguage();
XWikiDocument tdoc = getTranslatedDocument(doc, language, context);
String username = context.getUser();
XWikiLock lock = tdoc.getLock(context);
if ((lock == null) || (username.equals(lock.getUserName()))) {
if ("inline".equals(request.get("action"))) {
doc.setLock(username, context);
} else {
tdoc.setLock(username, context);
}
}
// forward to view
if (Utils.isAjaxRequest(context)) {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
response.setContentLength(0);
} else {
String redirect = Utils.getRedirect("view", context);
sendRedirect(response, redirect);
}
return false;
}
Aggregations