Search in sources :

Example 1 with MonitorPlugin

use of com.xpn.xwiki.monitor.api.MonitorPlugin in project xwiki-platform by xwiki.

the class XWikiHibernateBaseStore method updateSchema.

/**
 * Runs the update script on the current database
 *
 * @param createSQL
 * @param inputxcontext
 */
public void updateSchema(String[] createSQL, XWikiContext inputxcontext) throws HibernateException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    // Updating the schema for custom mappings
    Session session;
    Connection connection;
    Statement stmt = null;
    boolean bTransaction = true;
    MonitorPlugin monitor = Util.getMonitorPlugin(context);
    String sql = "";
    try {
        bTransaction = beginTransaction(context);
        session = getSession(context);
        connection = session.connection();
        setDatabase(session, context);
        stmt = connection.createStatement();
        // Start monitoring timer
        if (monitor != null) {
            monitor.startTimer("sqlupgrade");
        }
        for (String element : createSQL) {
            sql = element;
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Update Schema sql: [" + sql + "]");
            }
            stmt.executeUpdate(sql);
        }
        connection.commit();
    } catch (Exception e) {
        throw new HibernateException("Failed updating schema while executing query [" + sql + "]", e);
    } finally {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (Exception e) {
        }
        try {
            if (bTransaction) {
                endTransaction(context, true);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
        // End monitoring timer
        if (monitor != null) {
            monitor.endTimer("sqlupgrade");
        }
    }
}
Also used : HibernateException(org.hibernate.HibernateException) Statement(java.sql.Statement) MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) Connection(java.sql.Connection) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session)

Example 2 with MonitorPlugin

use of com.xpn.xwiki.monitor.api.MonitorPlugin in project xwiki-platform by xwiki.

the class XWikiHibernateStore method exists.

/**
 * Verifies if a wiki document exists
 */
@Override
public boolean exists(XWikiDocument doc, XWikiContext inputxcontext) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        boolean bTransaction = true;
        MonitorPlugin monitor = Util.getMonitorPlugin(context);
        try {
            doc.setStore(this);
            checkHibernate(context);
            // Start monitoring timer
            if (monitor != null) {
                monitor.startTimer(HINT);
            }
            bTransaction = bTransaction && beginTransaction(null, context);
            Session session = getSession(context);
            String fullName = doc.getFullName();
            String sql = "select doc.fullName from XWikiDocument as doc where doc.fullName=:fullName";
            if (!doc.getLocale().equals(Locale.ROOT)) {
                sql += " and doc.language=:language";
            }
            if (monitor != null) {
                monitor.setTimerDesc(HINT, sql);
            }
            Query query = session.createQuery(sql);
            query.setString("fullName", fullName);
            if (!doc.getLocale().equals(Locale.ROOT)) {
                query.setString("language", doc.getLocale().toString());
            }
            Iterator<String> it = query.list().iterator();
            while (it.hasNext()) {
                if (fullName.equals(it.next())) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            Object[] args = { doc.getDocumentReferenceWithLocale() };
            throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_CHECK_EXISTS_DOC, "Exception while reading document {0}", e, args);
        } finally {
            // End monitoring timer
            if (monitor != null) {
                monitor.endTimer(HINT);
            }
            try {
                if (bTransaction) {
                    endTransaction(context, false);
                }
            } catch (Exception e) {
            }
        }
    } finally {
        restoreExecutionXContext();
    }
}
Also used : Query(org.hibernate.Query) MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) 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 3 with MonitorPlugin

use of com.xpn.xwiki.monitor.api.MonitorPlugin in project xwiki-platform by xwiki.

the class XWikiHibernateStore method search.

@Override
public <T> List<T> search(String sql, int nb, int start, Object[][] whereParams, List<?> parameterValues, XWikiContext inputxcontext) throws XWikiException {
    boolean bTransaction = true;
    if (sql == null) {
        return null;
    }
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    MonitorPlugin monitor = Util.getMonitorPlugin(context);
    try {
        // Start monitoring timer
        if (monitor != null) {
            monitor.startTimer(HINT);
        }
        checkHibernate(context);
        bTransaction = beginTransaction(false, context);
        Session session = getSession(context);
        if (whereParams != null) {
            sql += generateWhereStatement(whereParams);
        }
        Query query = session.createQuery(filterSQL(sql));
        // Add values for provided HQL request containing "?" characters where to insert real
        // values.
        int parameterId = injectParameterListToQuery(0, query, parameterValues);
        if (whereParams != null) {
            for (Object[] whereParam : whereParams) {
                query.setString(parameterId++, (String) whereParam[1]);
            }
        }
        if (start != 0) {
            query.setFirstResult(start);
        }
        if (nb != 0) {
            query.setMaxResults(nb);
        }
        List<T> list = new ArrayList<T>();
        list.addAll(query.list());
        return list;
    } catch (Exception e) {
        Object[] args = { sql };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SEARCH, "Exception while searching documents with sql {0}", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
        // End monitoring timer
        if (monitor != null) {
            monitor.endTimer(HINT);
        }
    }
}
Also used : Query(org.hibernate.Query) MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) 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) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 4 with MonitorPlugin

use of com.xpn.xwiki.monitor.api.MonitorPlugin in project xwiki-platform by xwiki.

the class XWikiHibernateStore method search.

public List search(Query query, int nb, int start, XWikiContext inputxcontext) throws XWikiException {
    boolean bTransaction = true;
    if (query == null) {
        return null;
    }
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    MonitorPlugin monitor = Util.getMonitorPlugin(context);
    try {
        // Start monitoring timer
        if (monitor != null) {
            monitor.startTimer(HINT, query.getQueryString());
        }
        checkHibernate(context);
        bTransaction = beginTransaction(false, context);
        if (start != 0) {
            query.setFirstResult(start);
        }
        if (nb != 0) {
            query.setMaxResults(nb);
        }
        Iterator it = query.list().iterator();
        List list = new ArrayList();
        while (it.hasNext()) {
            list.add(it.next());
        }
        if (bTransaction) {
            // The session is closed here, too.
            endTransaction(context, false, false);
            bTransaction = false;
        }
        return list;
    } catch (Exception e) {
        Object[] args = { query.toString() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SEARCH, "Exception while searching documents with sql {0}", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
        // End monitoring timer
        if (monitor != null) {
            monitor.endTimer(HINT);
        }
    }
}
Also used : MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) List(java.util.List) ArrayList(java.util.ArrayList) 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)

Example 5 with MonitorPlugin

use of com.xpn.xwiki.monitor.api.MonitorPlugin in project xwiki-platform by xwiki.

the class XWikiHibernateStore method saveXWikiDoc.

@Override
public void saveXWikiDoc(XWikiDocument doc, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        MonitorPlugin monitor = Util.getMonitorPlugin(context);
        try {
            // Start monitoring timer
            if (monitor != null) {
                monitor.startTimer(HINT);
            }
            doc.setStore(this);
            // Make sure the database name is stored
            doc.setDatabase(context.getWikiId());
            // If the comment is larger than the max size supported by the Storage, then abbreviate it
            String comment = doc.getComment();
            if (comment != null && comment.length() > 1023) {
                doc.setComment(StringUtils.abbreviate(comment, 1023));
            }
            if (bTransaction) {
                checkHibernate(context);
                SessionFactory sfactory = injectCustomMappingsInSessionFactory(doc, context);
                bTransaction = beginTransaction(sfactory, context);
            }
            Session session = getSession(context);
            session.setFlushMode(FlushMode.COMMIT);
            // These informations will allow to not look for attachments and objects on loading
            doc.setElement(XWikiDocument.HAS_ATTACHMENTS, !doc.getAttachmentList().isEmpty());
            doc.setElement(XWikiDocument.HAS_OBJECTS, !doc.getXObjects().isEmpty());
            // Let's update the class XML since this is the new way to store it
            // TODO If all the properties are removed, the old xml stays?
            BaseClass bclass = doc.getXClass();
            if (bclass != null) {
                if (bclass.getFieldList().isEmpty()) {
                    doc.setXClassXML("");
                } else {
                    // Don't format the XML to reduce the size of the stored data as much as possible
                    doc.setXClassXML(bclass.toXMLString(false));
                }
                bclass.setDirty(false);
            }
            if (doc.hasElement(XWikiDocument.HAS_ATTACHMENTS)) {
                saveAttachmentList(doc, context);
            }
            // Remove attachments planned for removal
            if (!doc.getAttachmentsToRemove().isEmpty()) {
                for (XWikiAttachmentToRemove attachmentToRemove : doc.getAttachmentsToRemove()) {
                    XWikiAttachment attachment = attachmentToRemove.getAttachment();
                    XWikiAttachmentStoreInterface store = getXWikiAttachmentStoreInterface(attachment);
                    store.deleteXWikiAttachment(attachment, false, context, false);
                }
                doc.clearAttachmentsToRemove();
            }
            // Handle the latest text file
            if (doc.isContentDirty() || doc.isMetaDataDirty()) {
                Date ndate = new Date();
                doc.setDate(ndate);
                if (doc.isContentDirty()) {
                    doc.setContentUpdateDate(ndate);
                    doc.setContentAuthorReference(doc.getAuthorReference());
                }
                doc.incrementVersion();
                if (context.getWiki().hasVersioning(context)) {
                    context.getWiki().getVersioningStore().updateXWikiDocArchive(doc, false, context);
                }
                doc.setContentDirty(false);
                doc.setMetaDataDirty(false);
            } else {
                if (doc.getDocumentArchive() != null) {
                    // This is especially needed if we load a document from XML
                    if (context.getWiki().hasVersioning(context)) {
                        context.getWiki().getVersioningStore().saveXWikiDocArchive(doc.getDocumentArchive(), false, context);
                        // If the version does not exist it means it's a new version so add it to the history
                        if (!containsVersion(doc, doc.getRCSVersion(), context)) {
                            context.getWiki().getVersioningStore().updateXWikiDocArchive(doc, false, context);
                        }
                    }
                } else {
                    // with a valid context
                    try {
                        if (context.getWiki().hasVersioning(context)) {
                            doc.getDocumentArchive(context);
                            // history
                            if (!containsVersion(doc, doc.getRCSVersion(), context)) {
                                context.getWiki().getVersioningStore().updateXWikiDocArchive(doc, false, context);
                            }
                        }
                    } catch (XWikiException e) {
                    // this is a non critical error
                    }
                }
            }
            // Verify if the document already exists
            Query query = session.createQuery("select xwikidoc.id from XWikiDocument as xwikidoc where xwikidoc.id = :id");
            query.setLong("id", doc.getId());
            if (query.uniqueResult() == null) {
                if (doc.isContentDirty() || doc.isMetaDataDirty()) {
                    // Reset the creationDate to reflect the date of the first save, not the date of the object
                    // creation
                    doc.setCreationDate(new Date());
                }
                session.save(doc);
            } else {
                session.update(doc);
            // TODO: this is slower!! How can it be improved?
            // session.saveOrUpdate(doc);
            }
            // Remove objects planned for removal
            if (doc.getXObjectsToRemove().size() > 0) {
                for (BaseObject removedObject : doc.getXObjectsToRemove()) {
                    deleteXWikiCollection(removedObject, context, false, false);
                }
                doc.setXObjectsToRemove(new ArrayList<BaseObject>());
            }
            if (bclass != null) {
                bclass.setDocumentReference(doc.getDocumentReference());
                // Store this XWikiClass in the context so that we can use it in case of recursive usage of classes
                context.addBaseClass(bclass);
            }
            if (doc.hasElement(XWikiDocument.HAS_OBJECTS)) {
                // TODO: Delete all objects for which we don't have a name in the Map
                for (List<BaseObject> objects : doc.getXObjects().values()) {
                    for (BaseObject obj : objects) {
                        if (obj != null) {
                            obj.setDocumentReference(doc.getDocumentReference());
                            /* If the object doesn't have a GUID, create it before saving */
                            if (StringUtils.isEmpty(obj.getGuid())) {
                                obj.setGuid(null);
                            }
                            saveXWikiCollection(obj, context, false);
                        }
                    }
                }
            }
            if (context.getWiki().hasBacklinks(context)) {
                try {
                    saveLinks(doc, context, true);
                } catch (Exception e) {
                    this.logger.error("Failed to save links for document [{}]", doc.getDocumentReferenceWithLocale(), e);
                }
            }
            // Update space table
            updateXWikiSpaceTable(doc, session);
            if (bTransaction) {
                endTransaction(context, true);
            }
            doc.setNew(false);
            // We need to ensure that the saved document becomes the original document
            doc.setOriginalDocument(doc.clone());
        } catch (Exception e) {
            Object[] args = { this.defaultEntityReferenceSerializer.serialize(doc.getDocumentReference()) };
            throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_DOC, "Exception while saving 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) Query(org.hibernate.Query) MonitorPlugin(com.xpn.xwiki.monitor.api.MonitorPlugin) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) 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) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiAttachmentToRemove(com.xpn.xwiki.doc.XWikiDocument.XWikiAttachmentToRemove) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)11 MonitorPlugin (com.xpn.xwiki.monitor.api.MonitorPlugin)11 InitializationException (org.xwiki.component.phase.InitializationException)10 XWikiContext (com.xpn.xwiki.XWikiContext)9 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)8 SQLException (java.sql.SQLException)8 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)8 Session (org.hibernate.Session)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 QueryException (org.xwiki.query.QueryException)8 UnexpectedException (org.xwiki.store.UnexpectedException)8 BaseObject (com.xpn.xwiki.objects.BaseObject)6 Query (org.hibernate.Query)6 ArrayList (java.util.ArrayList)4 SessionFactory (org.hibernate.SessionFactory)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 List (java.util.List)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2