Beispiel #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CollectionSaveException&lt;T&gt;"/> class.
 /// </summary>
 /// <param name="results">The results.</param>
 /// <param name="innerException">The inner exception.</param>
 public CollectionSaveException(CollectionSaveResult <T> results, Exception innerException)
     : base(DEFAULT_ERROR_MESSAGE, innerException)
 {
     Results = results;
 }
Beispiel #2
0
        /// <summary>
        /// Saves the collection.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="criteria">The criteria of type <see cref="Zonkey.UpdateCriteria"/>.</param>
        /// <param name="affect">The <see cref="Zonkey.UpdateAffect"/> value that determines which rows to affect.</param>
        /// <param name="selectBack">The <see cref="Zonkey.SelectBack"/> value that determines whether to select back the changed rows.</param>
        /// <returns>A value of type <see cref="T:Zonkey.CollectionSaveResult"/></returns>
        public CollectionSaveResult <T> TrySaveCollection(ICollection <T> collection, UpdateCriteria criteria, UpdateAffect affect, SelectBack selectBack)
        {
            var colResult = new CollectionSaveResult <T>();

            var bindCol = collection as ITrackDeletedItems <T>;

            if (bindCol != null)
            {
                foreach (T obj in bindCol.DeletedItems)
                {
                    var objSV = obj as ISavable;
                    if (objSV != null)
                    {
                        if (objSV.DataRowState == DataRowState.Deleted)
                        {
                            DeleteItem(obj);
                            colResult.Deleted.Add(obj);
                        }
                        else
                        {
                            colResult.Skipped.Add(obj);
                        }
                    }
                    else
                    {
                        colResult.Failed.Add(obj);
                    }
                }
            }

            foreach (T obj in collection)
            {
                var objSV = obj as ISavable;
                if (objSV != null)
                {
                    if (objSV.DataRowState != DataRowState.Unchanged)
                    {
                        switch (TrySave(obj, criteria, affect, selectBack))
                        {
                        case SaveResult.Skipped:
                            colResult.Skipped.Add(obj);
                            break;

                        case SaveResult.Conflict:
                            colResult.Conflicted.Add(obj);
                            break;

                        case SaveResult.Fail:
                            colResult.Failed.Add(obj);
                            break;

                        case SaveResult.Success:
                            if (_lastSaveSuccessType == 1)
                            {
                                colResult.Inserted.Add(obj);
                            }
                            else
                            {
                                colResult.Updated.Add(obj);
                            }

                            break;
                        }
                    }
                    else
                    {
                        colResult.Skipped.Add(obj);
                    }
                }
                else
                {
                    colResult.Failed.Add(obj);
                }
            }

            return(colResult);
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CollectionSaveException&lt;T&gt;"/> class.
 /// </summary>
 /// <param name="results">The results.</param>
 public CollectionSaveException(CollectionSaveResult <T> results)
     : base(DEFAULT_ERROR_MESSAGE)
 {
     Results = results;
 }
Beispiel #4
0
        /// <summary>
        /// Saves the collection.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="criteria">The criteria of type <see cref="Zonkey.UpdateCriteria"/>.</param>
        /// <param name="affect">The <see cref="Zonkey.UpdateAffect"/> value that determines which rows to affect.</param>
        /// <param name="selectBack">The <see cref="Zonkey.SelectBack"/> value that determines whether to select back the changed rows.</param>
        /// <param name="continueOnError">if set to <c>true</c> continues past errors.</param>
        /// <returns>
        /// A value of type <see cref="Zonkey.CollectionSaveResult{T}"/>
        /// </returns>
        public CollectionSaveResult <T> TrySaveCollection(ICollection <T> collection, UpdateCriteria criteria, UpdateAffect affect, SelectBack selectBack, bool continueOnError)
        {
            var colResult = new CollectionSaveResult <T>();

            var bindCol = collection as ITrackDeletedItems <T>;

            if (bindCol != null)
            {
                foreach (T obj in bindCol.DeletedItems)
                {
                    var objSV = obj as ISavable;
                    if (objSV != null)
                    {
                        if (objSV.DataRowState == DataRowState.Deleted)
                        {
                            try
                            {
                                DeleteItem(obj);
                            }
                            catch (Exception ex)
                            {
                                colResult.Exceptions.Add(new CollectionSaveExceptionItem <T>(obj, ex));
                                if (!continueOnError)
                                {
                                    throw;
                                }
                            }

                            colResult.Deleted.Add(obj);
                        }
                        else
                        {
                            colResult.Skipped.Add(obj);
                        }
                    }
                    else
                    {
                        colResult.Failed.Add(obj);
                    }
                }
            }

            foreach (T obj in collection)
            {
                var objSV = obj as ISavable;
                if (objSV != null)
                {
                    if (objSV.DataRowState != DataRowState.Unchanged)
                    {
                        try
                        {
                            SaveResult saveResult = TrySave(obj, criteria, affect, selectBack);
                            switch (saveResult.Status)
                            {
                            case SaveResultStatus.Skipped:
                                colResult.Skipped.Add(obj);
                                break;

                            case SaveResultStatus.Conflict:
                                colResult.Conflicted.Add(obj);
                                break;

                            case SaveResultStatus.Fail:
                                colResult.Failed.Add(obj);
                                break;

                            case SaveResultStatus.Success:
                                if (saveResult.SaveType == SaveType.Insert)
                                {
                                    colResult.Inserted.Add(obj);
                                }
                                else
                                {
                                    colResult.Updated.Add(obj);
                                }

                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            colResult.Exceptions.Add(new CollectionSaveExceptionItem <T>(obj, ex));
                            if (!continueOnError)
                            {
                                throw;
                            }
                        }
                    }
                    else
                    {
                        colResult.Skipped.Add(obj);
                    }
                }
                else
                {
                    colResult.Failed.Add(obj);
                }
            }

            return(colResult);
        }