Search in sources :

Example 1 with XWikiStats

use of com.xpn.xwiki.stats.impl.XWikiStats in project xwiki-platform by xwiki.

the class XWikiHibernateStore method saveXWikiCollection.

/**
 * @deprecated This is internal to XWikiHibernateStore and may be removed in the future.
 */
@Deprecated
public void saveXWikiCollection(BaseCollection object, XWikiContext inputxcontext, boolean bTransaction) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        if (object == null) {
            return;
        }
        // We need a slightly different behavior here
        boolean stats = (object instanceof XWikiStats);
        if (!stats) {
            checkObjectClassIsLocal(object, context);
        }
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(context);
        }
        Session session = getSession(context);
        // Verify if the property already exists
        Query query;
        if (stats) {
            query = session.createQuery("select obj.id from " + object.getClass().getName() + " as obj where obj.id = :id");
        } else {
            query = session.createQuery("select obj.id from BaseObject as obj where obj.id = :id");
        }
        query.setLong("id", object.getId());
        if (query.uniqueResult() == null) {
            if (stats) {
                session.save(object);
            } else {
                session.save("com.xpn.xwiki.objects.BaseObject", object);
            }
        } else {
            if (stats) {
                session.update(object);
            } else {
                session.update("com.xpn.xwiki.objects.BaseObject", object);
            }
        }
        /*
             * if (stats) session.saveOrUpdate(object); else
             * session.saveOrUpdate((String)"com.xpn.xwiki.objects.BaseObject", (Object)object);
             */
        BaseClass bclass = object.getXClass(context);
        List<String> handledProps = new ArrayList<>();
        if ((bclass != null) && (bclass.hasCustomMapping()) && context.getWiki().hasCustomMappings()) {
            // save object using the custom mapping
            Map<String, Object> objmap = object.getCustomMappingMap();
            handledProps = bclass.getCustomMappingPropertyList(context);
            Session dynamicSession = session.getSession(EntityMode.MAP);
            query = session.createQuery("select obj.id from " + bclass.getName() + " as obj where obj.id = :id");
            query.setLong("id", object.getId());
            if (query.uniqueResult() == null) {
                dynamicSession.save(bclass.getName(), objmap);
            } else {
                dynamicSession.update(bclass.getName(), objmap);
            }
        // dynamicSession.saveOrUpdate((String) bclass.getName(), objmap);
        }
        if (object.getXClassReference() != null) {
            // Remove all existing properties
            if (object.getFieldsToRemove().size() > 0) {
                for (int i = 0; i < object.getFieldsToRemove().size(); i++) {
                    BaseProperty prop = (BaseProperty) object.getFieldsToRemove().get(i);
                    if (!handledProps.contains(prop.getName())) {
                        session.delete(prop);
                    }
                }
                object.setFieldsToRemove(new ArrayList<BaseProperty>());
            }
            Iterator<String> it = object.getPropertyList().iterator();
            while (it.hasNext()) {
                String key = it.next();
                BaseProperty prop = (BaseProperty) object.getField(key);
                if (!prop.getName().equals(key)) {
                    Object[] args = { key, object.getName() };
                    throw new XWikiException(XWikiException.MODULE_XWIKI_CLASSES, XWikiException.ERROR_XWIKI_CLASSES_FIELD_INVALID, "Field {0} in object {1} has an invalid name", null, args);
                }
                String pname = prop.getName();
                if (pname != null && !pname.trim().equals("") && !handledProps.contains(pname)) {
                    saveXWikiPropertyInternal(prop, context, false);
                }
            }
        }
        if (bTransaction) {
            endTransaction(context, true);
        }
    } catch (XWikiException xe) {
        throw xe;
    } catch (Exception e) {
        Object[] args = { object.getName() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_OBJECT, "Exception while saving object {0}", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, true);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
}
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) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiStats(com.xpn.xwiki.stats.impl.XWikiStats) BaseProperty(com.xpn.xwiki.objects.BaseProperty) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 BaseProperty (com.xpn.xwiki.objects.BaseProperty)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 XWikiStats (com.xpn.xwiki.stats.impl.XWikiStats)1 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)1 Query (org.hibernate.Query)1 Session (org.hibernate.Session)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 InitializationException (org.xwiki.component.phase.InitializationException)1 QueryException (org.xwiki.query.QueryException)1 UnexpectedException (org.xwiki.store.UnexpectedException)1