Search in sources :

Example 1 with BaseElement

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

the class XWikiHibernateStore method deleteXWikiCollection.

/**
 * @deprecated This is internal to XWikiHibernateStore and may be removed in the future.
 */
@Deprecated
public void deleteXWikiCollection(BaseCollection object, XWikiContext inputxcontext, boolean bTransaction, boolean evict) throws XWikiException {
    if (object == null) {
        return;
    }
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        if (bTransaction) {
            checkHibernate(context);
            bTransaction = beginTransaction(context);
        }
        Session session = getSession(context);
        // Let's check if the class has a custom mapping
        BaseClass bclass = object.getXClass(context);
        List<String> handledProps = new ArrayList<String>();
        if ((bclass != null) && (bclass.hasCustomMapping()) && context.getWiki().hasCustomMappings()) {
            handledProps = bclass.getCustomMappingPropertyList(context);
            Session dynamicSession = session.getSession(EntityMode.MAP);
            Object map = dynamicSession.get(bclass.getName(), object.getId());
            if (map != null) {
                if (evict) {
                    dynamicSession.evict(map);
                }
                dynamicSession.delete(map);
            }
        }
        if (object.getXClassReference() != null) {
            for (BaseElement property : (Collection<BaseElement>) object.getFieldList()) {
                if (!handledProps.contains(property.getName())) {
                    if (evict) {
                        session.evict(property);
                    }
                    if (session.get(property.getClass(), property) != null) {
                        session.delete(property);
                    }
                }
            }
        }
        // In case of custom class we need to force it as BaseObject to delete the xwikiobject row
        if (!"".equals(bclass.getCustomClass())) {
            BaseObject cobject = new BaseObject();
            cobject.setDocumentReference(object.getDocumentReference());
            cobject.setClassName(object.getClassName());
            cobject.setNumber(object.getNumber());
            if (object instanceof BaseObject) {
                cobject.setGuid(((BaseObject) object).getGuid());
            }
            cobject.setId(object.getId());
            if (evict) {
                session.evict(cobject);
            }
            session.delete(cobject);
        } else {
            if (evict) {
                session.evict(object);
            }
            session.delete(object);
        }
        if (bTransaction) {
            endTransaction(context, true);
        }
    } catch (Exception e) {
        Object[] args = { object.getName() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_DELETING_OBJECT, "Exception while deleting object {0}", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false);
            }
        } catch (Exception e) {
        }
        restoreExecutionXContext();
    }
}
Also used : BaseElement(com.xpn.xwiki.objects.BaseElement) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) BaseCollection(com.xpn.xwiki.objects.BaseCollection) Collection(java.util.Collection) BaseObject(com.xpn.xwiki.objects.BaseObject) 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) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 BaseCollection (com.xpn.xwiki.objects.BaseCollection)1 BaseElement (com.xpn.xwiki.objects.BaseElement)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)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