Search in sources :

Example 6 with BaseCollection

use of com.xpn.xwiki.objects.BaseCollection in project xwiki-platform by xwiki.

the class XWikiHibernateStore method saveXWikiPropertyInternal.

private void saveXWikiPropertyInternal(final PropertyInterface property, final XWikiContext context, final boolean runInOwnTransaction) throws XWikiException {
    // Clone runInOwnTransaction so the value passed is not altered.
    boolean bTransaction = runInOwnTransaction;
    try {
        if (bTransaction) {
            this.checkHibernate(context);
            bTransaction = this.beginTransaction(context);
        }
        final Session session = this.getSession(context);
        Query query = session.createQuery("select prop.classType from BaseProperty as prop " + "where prop.id.id = :id and prop.id.name= :name");
        query.setLong("id", property.getId());
        query.setString("name", property.getName());
        String oldClassType = (String) query.uniqueResult();
        String newClassType = ((BaseProperty) property).getClassType();
        if (oldClassType == null) {
            session.save(property);
        } else if (oldClassType.equals(newClassType)) {
            session.update(property);
        } else {
            // The property type has changed. We cannot simply update its value because the new value and the old
            // value are stored in different tables (we're using joined-subclass to map different property types).
            // We must delete the old property value before saving the new one and for this we must load the old
            // property from the table that corresponds to the old property type (we cannot delete and save the new
            // property or delete a clone of the new property; loading the old property from the BaseProperty table
            // doesn't work either).
            query = session.createQuery("select prop from " + oldClassType + " as prop where prop.id.id = :id and prop.id.name= :name");
            query.setLong("id", property.getId());
            query.setString("name", property.getName());
            session.delete(query.uniqueResult());
            session.save(property);
        }
        ((BaseProperty) property).setValueDirty(false);
        if (bTransaction) {
            endTransaction(context, true);
        }
    } catch (Exception e) {
        // Something went wrong, collect some information.
        final BaseCollection obj = property.getObject();
        final Object[] args = { (obj != null) ? obj.getName() : "unknown", property.getName() };
        // Try to roll back the transaction if this is in it's own transaction.
        try {
            if (bTransaction) {
                this.endTransaction(context, false);
            }
        } catch (Exception ee) {
        // Not a lot we can do here if there was an exception committing and an exception rolling back.
        }
        // Throw the exception.
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while saving property {1} of object {0}", e, args);
    }
}
Also used : Query(org.hibernate.Query) BaseProperty(com.xpn.xwiki.objects.BaseProperty) BaseCollection(com.xpn.xwiki.objects.BaseCollection) 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 7 with BaseCollection

use of com.xpn.xwiki.objects.BaseCollection in project xwiki-platform by xwiki.

the class XWikiHibernateStore method loadXWikiProperty.

private void loadXWikiProperty(PropertyInterface property, XWikiContext context, boolean bTransaction) throws XWikiException {
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(false, context);
        }
        Session session = getSession(context);
        try {
            session.load(property, (Serializable) property);
            // safe to assume that a retrieved NULL value should actually be an empty string.
            if (property instanceof BaseStringProperty) {
                BaseStringProperty stringProperty = (BaseStringProperty) property;
                if (stringProperty.getValue() == null) {
                    stringProperty.setValue("");
                }
            }
            ((BaseProperty) property).setValueDirty(false);
        } catch (ObjectNotFoundException e) {
            // Let's accept that there is no data in property tables but log it
            this.logger.error("No data for property [{}] of object id [{}]", property.getName(), property.getId());
        }
        // Without this test ViewEditTest.testUpdateAdvanceObjectProp fails
        if (property instanceof ListProperty) {
            ((ListProperty) property).getList();
        }
        if (bTransaction) {
            endTransaction(context, false, false);
        }
    } catch (Exception e) {
        BaseCollection obj = property.getObject();
        Object[] args = { (obj != null) ? obj.getName() : "unknown", property.getName() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_LOADING_OBJECT, "Exception while loading property {1} of object {0}", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false, false);
            }
        } catch (Exception e) {
        }
    }
}
Also used : ListProperty(com.xpn.xwiki.objects.ListProperty) BaseStringProperty(com.xpn.xwiki.objects.BaseStringProperty) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) BaseProperty(com.xpn.xwiki.objects.BaseProperty) BaseCollection(com.xpn.xwiki.objects.BaseCollection) 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)

Aggregations

BaseCollection (com.xpn.xwiki.objects.BaseCollection)7 XWikiException (com.xpn.xwiki.XWikiException)5 BaseProperty (com.xpn.xwiki.objects.BaseProperty)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)4 SQLException (java.sql.SQLException)4 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)4 Session (org.hibernate.Session)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 InitializationException (org.xwiki.component.phase.InitializationException)4 QueryException (org.xwiki.query.QueryException)4 UnexpectedException (org.xwiki.store.UnexpectedException)4 Collection (java.util.Collection)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 BaseStringProperty (com.xpn.xwiki.objects.BaseStringProperty)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)2 ArrayList (java.util.ArrayList)2 Query (org.hibernate.Query)2 PropertyClassProvider (com.xpn.xwiki.internal.objects.classes.PropertyClassProvider)1 BaseElement (com.xpn.xwiki.objects.BaseElement)1