Example #1
0
        protected virtual void InsertCreated(object forObj, int exceptionLimit)
        {
            this.Context.LogManager.Debug(this, "Inserting objects that are up for creation", "");               // do not localize

            try
            {
                long      cnt;
                int       cntStale      = 0;
                bool      noCheck       = false;
                IList     stillDirty    = new ArrayList();
                ArrayList insertObjects = new ArrayList();
                cnt = m_listCreated.Count;
                IObjectManager     om = this.Context.ObjectManager;
                IPersistenceEngine pe = this.Context.PersistenceEngine;
                while (cnt > 0)
                {
                    try
                    {
                        insertObjects.Clear();
                        foreach (object obj in m_listCreated)
                        {
                            try
                            {
                                if (forObj != null)
                                {
                                    if (obj == forObj)
                                    {
                                        insertObjects.Add(obj);
                                    }
                                }
                                else
                                {
                                    if (noCheck)
                                    {
                                        if (MayInsert(obj, true, true))
                                        {
                                            insertObjects.Add(obj);
                                            noCheck = false;
                                        }
                                    }
                                    else
                                    {
                                        if (MayInsert(obj, true, false))
                                        {
                                            insertObjects.Add(obj);
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (exceptionLimit > 0 && exceptions.Count >= exceptionLimit - 1)
                                {
                                    throw ex;
                                }
                                exceptions.Add(ex);
                            }
                        }
                        foreach (object obj in insertObjects)
                        {
                            //this should be the only necessary try block in this
                            //method (and it ought only to be around the call to the
                            //persistence engine, at that) but we add the other tries
                            //to ensure that some exception from the framework doesn't
                            //ruin the beautiful concept with collecting exceptions :-)
                            try
                            {
                                m_listCreated.Remove(obj);
                                m_listInserted.Add(obj);
                                stillDirty.Clear();
                                pe.InsertObject(obj, stillDirty);
                                om.SetObjectStatus(obj, ObjectStatus.Clean);
                                this.Context.LogManager.Debug(this, "Inserted object", "Type: " + obj.GetType().ToString() + " Still dirty: " + stillDirty.ToString());                                  // do not localize
                                if (stillDirty.Count > 0)
                                {
                                    IList cloneList = new ArrayList();
                                    foreach (object clone in stillDirty)
                                    {
                                        cloneList.Add(clone);
                                    }
                                    m_hashStillDirty[obj] = cloneList;
                                }
                            }
                            catch (Exception ex)
                            {
                                if (exceptionLimit > 0 && exceptions.Count >= exceptionLimit - 1)
                                {
                                    throw ex;
                                }
                                exceptions.Add(ex);
                            }
                        }
                        if (m_listCreated.Count == cnt)
                        {
                            noCheck = true;
                            cntStale++;
                            if (cntStale > 1)
                            {
                                throw new NPersistException("Cyclic dependency among objects up for creation could not be resolved!");                                 // do not localize
                            }
                        }
                        else
                        {
                            cntStale = 0;
                            noCheck  = false;
                        }
                        if (forObj != null)
                        {
                            cnt = 0;
                        }
                        else
                        {
                            cnt = m_listCreated.Count;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (exceptionLimit > 0 && exceptions.Count >= exceptionLimit - 1)
                        {
                            throw ex;
                        }
                        exceptions.Add(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                if (exceptionLimit > 0 && exceptions.Count >= exceptionLimit - 1)
                {
                    throw ex;
                }
                exceptions.Add(ex);
            }
        }