Search in sources :

Example 86 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class XWikiHibernateStore method deleteXWikiDoc.

@Override
public void deleteXWikiDoc(XWikiDocument doc, XWikiContext inputxcontext) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        boolean bTransaction = true;
        MonitorPlugin monitor = Util.getMonitorPlugin(context);
        try {
            // Start monitoring timer
            if (monitor != null) {
                monitor.startTimer(HINT);
            }
            checkHibernate(context);
            SessionFactory sfactory = injectCustomMappingsInSessionFactory(doc, context);
            bTransaction = bTransaction && beginTransaction(sfactory, context);
            Session session = getSession(context);
            session.setFlushMode(FlushMode.COMMIT);
            if (doc.getStore() == null) {
                Object[] args = { doc.getDocumentReference() };
                throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_CANNOT_DELETE_UNLOADED_DOC, "Impossible to delete document {0} if it is not loaded", null, args);
            }
            // Let's delete any attachment this document might have
            for (XWikiAttachment attachment : doc.getAttachmentList()) {
                XWikiAttachmentStoreInterface store = getXWikiAttachmentStoreInterface(attachment);
                store.deleteXWikiAttachment(attachment, false, context, false);
            }
            // deleting XWikiLinks
            if (context.getWiki().hasBacklinks(context)) {
                deleteLinks(doc.getId(), context, true);
            }
            // Remove properties planned for removal
            if (!doc.getXObjectsToRemove().isEmpty()) {
                for (BaseObject bobj : doc.getXObjectsToRemove()) {
                    if (bobj != null) {
                        deleteXWikiCollection(bobj, context, false, false);
                    }
                }
                doc.setXObjectsToRemove(new ArrayList<BaseObject>());
            }
            for (List<BaseObject> objects : doc.getXObjects().values()) {
                for (BaseObject obj : objects) {
                    if (obj != null) {
                        deleteXWikiCollection(obj, context, false, false);
                    }
                }
            }
            context.getWiki().getVersioningStore().deleteArchive(doc, false, context);
            session.delete(doc);
            // We need to ensure that the deleted document becomes the original document
            doc.setOriginalDocument(doc.clone());
            // Update space table if needed
            maybeDeleteXWikiSpace(doc, session);
            if (bTransaction) {
                endTransaction(context, true);
            }
        } catch (Exception e) {
            Object[] args = { doc.getDocumentReference() };
            throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_DOC, "Exception while deleting document {0}", e, args);
        } finally {
            try {
                if (bTransaction) {
                    endTransaction(context, false);
                }
            } catch (Exception e) {
            }
            // End monitoring timer
            if (monitor != null) {
                monitor.endTimer(HINT);
            }
        }
    } finally {
        restoreExecutionXContext();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) BaseObject(com.xpn.xwiki.objects.BaseObject) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 87 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class XWikiHibernateStore method loadLinks.

// ---------------------------------------
// Links
// ---------------------------------------
@Override
public List<XWikiLink> loadLinks(long docId, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    List<XWikiLink> links = new ArrayList<XWikiLink>();
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(false, context);
        }
        Session session = getSession(context);
        Query query = session.createQuery(" from XWikiLink as link where link.id.docId = :docId");
        query.setLong("docId", docId);
        links = query.list();
        if (bTransaction) {
            endTransaction(context, false, false);
            bTransaction = false;
        }
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_LINKS, "Exception while loading links", e);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
    return links;
}
Also used : Query(org.hibernate.Query) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) XWikiLink(com.xpn.xwiki.doc.XWikiLink) Session(org.hibernate.Session)

Example 88 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class XWikiHibernateStore method saveAttachment.

private void saveAttachment(XWikiAttachment attachment, XWikiContext context) throws XWikiException {
    try {
        // If the comment is larger than the max size supported by the Storage, then abbreviate it
        String comment = attachment.getComment();
        if (comment != null && comment.length() > 1023) {
            attachment.setComment(StringUtils.abbreviate(comment, 1023));
        }
        // See XWIKI-9421: Attachment version is incremented when a document is restored from recycle bin
        if (attachment.isContentDirty() && !attachment.getDoc().isNew()) {
            attachment.updateContentArchive(context);
        }
        Session session = getSession(context);
        Query query = session.createQuery("select attach.id from XWikiAttachment as attach where attach.id = :id");
        query.setLong("id", attachment.getId());
        boolean exist = query.uniqueResult() != null;
        if (exist) {
            session.update(attachment);
        } else {
            if (attachment.getContentStore() == null) {
                // Set content store
                attachment.setContentStore(getDefaultAttachmentContentStore(context));
            }
            if (attachment.getArchiveStore() == null) {
                // Set archive store
                attachment.setArchiveStore(getDefaultAttachmentArchiveStore(context));
            }
            session.save(attachment);
        }
        // Save the attachment content if it's marked as "dirty" (out of sync with the database).
        if (attachment.isContentDirty()) {
            // updateParent and bTransaction must be false because the content should be saved in the same
            // transaction as the attachment and if the parent doc needs to be updated, this function will do it.
            XWikiAttachmentStoreInterface store = getXWikiAttachmentStoreInterface(attachment);
            store.saveAttachmentContent(attachment, false, context, false);
        }
        // Mark the attachment content and metadata as not dirty.
        // Ideally this would only happen if the transaction is committed successfully but since an unsuccessful
        // transaction will most likely be accompanied by an exception, the cache will not have a chance to save
        // the copy of the document with erronious information. If this is not set here, the cache will return
        // a copy of the attachment which claims to be dirty although it isn't.
        attachment.setMetaDataDirty(false);
        if (attachment.isContentDirty()) {
            attachment.getAttachment_content().setContentDirty(false);
        }
    } catch (Exception e) {
        Object[] args = { attachment.getReference() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_ATTACHMENT, "Exception while saving attachment [{0}]", e, args);
    }
}
Also used : Query(org.hibernate.Query) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 89 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class XWikiHibernateStore method deleteLinks.

@Override
public void deleteLinks(long docId, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(context);
        }
        Session session = getSession(context);
        Query query = session.createQuery("delete from XWikiLink as link where link.id.docId = :docId");
        query.setLong("docId", docId);
        query.executeUpdate();
        if (bTransaction) {
            endTransaction(context, true);
            bTransaction = false;
        }
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_LINKS, "Exception while deleting links", e);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
}
Also used : Query(org.hibernate.Query) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) MigrationRequiredException(com.xpn.xwiki.store.migration.MigrationRequiredException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) QueryException(org.xwiki.query.QueryException) UnexpectedException(org.xwiki.store.UnexpectedException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) SQLException(java.sql.SQLException) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 90 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class XWikiHibernateVersioningStore method getXWikiDocVersions.

@Override
public Version[] getXWikiDocVersions(XWikiDocument doc, XWikiContext context) throws XWikiException {
    try {
        XWikiDocumentArchive archive = getXWikiDocumentArchive(doc, context);
        if (archive == null) {
            return new Version[0];
        }
        Collection<XWikiRCSNodeInfo> nodes = archive.getNodes();
        Version[] versions = new Version[nodes.size()];
        Iterator<XWikiRCSNodeInfo> it = nodes.iterator();
        for (int i = 0; i < versions.length; i++) {
            XWikiRCSNodeInfo node = it.next();
            versions[versions.length - 1 - i] = node.getId().getVersion();
        }
        return versions;
    } catch (Exception e) {
        Object[] args = { doc.getFullName() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_READING_REVISIONS, "Exception while reading document {0} revisions", e, args);
    }
}
Also used : XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) Version(org.suigeneris.jrcs.rcs.Version) XWikiRCSNodeInfo(com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo) XWikiException(com.xpn.xwiki.XWikiException) HibernateException(org.hibernate.HibernateException) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)442 XWikiContext (com.xpn.xwiki.XWikiContext)156 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)147 DocumentReference (org.xwiki.model.reference.DocumentReference)98 BaseObject (com.xpn.xwiki.objects.BaseObject)88 IOException (java.io.IOException)57 QueryException (org.xwiki.query.QueryException)57 ArrayList (java.util.ArrayList)56 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)51 XWiki (com.xpn.xwiki.XWiki)48 XWikiRestException (org.xwiki.rest.XWikiRestException)44 Session (org.hibernate.Session)42 Document (com.xpn.xwiki.api.Document)38 InitializationException (org.xwiki.component.phase.InitializationException)36 WebApplicationException (javax.ws.rs.WebApplicationException)32 SQLException (java.sql.SQLException)31 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)30 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)29 UnexpectedException (org.xwiki.store.UnexpectedException)29 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)25