Esempio n. 1
0
        public static bool Save <T>(IEntity entity, bool bypassConflicts) where T : IEntity, new()
        {
            bool ret = true;

            try
            {
                IEntity copy = new T();
                copy.ConnectionString = entity.ConnectionString;
                entity.__Elements.Where(ebd => ebd.UseForMatching).ToList().ForEach(ebd => copy.__Elements.Find(c => c.Name.Equals(ebd.Name)).Data = ebd.Data);
                //copy.Elements[0].Data = entity.Elements[0].Data;

                CRUDActions.Retrieve <T>(copy);

                if (copy.Notifications.Count == 0)
                {
                    if (entity.__Memento.Count == 0)
                    {
                        long fieldsToSave = entity.Compare(copy);
                        CRUDActions.Update <T>(entity, fieldsToSave);
                    }
                    else
                    {
                        long fieldsToSave = entity.Compare();
                        if (fieldsToSave == 0)
                        {
                            return(false);
                        }

                        Utility.SetWarnings(entity, copy);

                        if (!bypassConflicts)
                        {
                            Utility.SetConflicts(entity, copy);
                            ret = entity.CRUDConflict.Fields.Count == 0;
                        }

                        if (ret)
                        {
                            entity.__Elements.ForEach(e => UpdateField(e, copy, fieldsToSave));
                            CRUDActions.Update <T>(copy, fieldsToSave);

                            entity.__Elements.Where(e => !e.IsCollection).ToList().ForEach(e => SaveCollection(e, copy));
                        }
                    }
                }
                else
                {
                    //copy.Notifications.ForEach(n => getNLogger().Info(n.Message));
                    CRUDActions.Create <T>(entity);
                }
            }
            catch (System.Exception ex)
            {
                entity.Exceptions.Add(new EntityException(1, ex.Message, ex));
            }
            return(ret);
        }
Esempio n. 2
0
        public static int Add <T>(IEntity entity, Collection <T> entities, bool bypassConflicts) where T : IEntity
        {
            int ret = 0;

            //Collection<T> newEntities = new Collection<T>();
            Collection <T> copies = new Collection <T>();

            Load <T>(copies /*, entity.EntityName*/);
            Collection <T> newEntities = copies.Except(entities);

            if (!bypassConflicts && newEntities.Count > 0 &&
                newEntities.ToList().Find(p => p.__Elements.Where(e => e.UseForMatching).Equals(entity.__Elements.Where(e => e.UseForMatching))) != null)
            {
                List <CRUDOption> options = new List <CRUDOption>();
                options.Add(new CRUDOption()
                {
                    Message    = "Add Anyway",
                    OptionType = CRUDOption.CRUDOptionType.SaveAll
                });
                options.Add(new CRUDOption()
                {
                    Message    = "Cancel Add",
                    OptionType = CRUDOption.CRUDOptionType.Cancel
                });

                //CRUDConflict ex = new CRUDConflict()
                //{
                //Message = "A matching profile was added by another user during your current session.",
                //    Options = options
                //};
                //entity.CRUDConflicts.Add(ex);
            }
            else
            {
                if (newEntities.Count > 0)
                {
                    entities.AddRange(newEntities);
                    ////ret = newEntities.ToList().Last().Id + 1;
                }
                else
                {
                    ////ret = entities.ToList().Last().Id + 1;

                    ////entity.Id = ret;
                    //getNLogger().Info(String.Format("Calling CRUDActions.Create({0})", entity.EntityName));
                    CRUDActions.Create <T>(entity);
                }
            }

            return(ret);
        }
Esempio n. 3
0
        public static bool Create <T>(IEntity entity) where T : IEntity
        {
            bool ret = true;

            try
            {
                //getNLogger().Info(String.Format("Calling CRUDActions.Create<T>({0})", entity.EntityName));
                CRUDActions.Create <T>(entity);
            }
            catch (SystemException ex)
            {
                entity.Exceptions.Add(new EntityException(1, ex.Message, ex));
                ret = false;
            }
            return(ret);
        }
Esempio n. 4
0
        public static bool Save <T>(IEntity entity) where T : IEntity, new()
        {
            //return Save<T>(entity, false);
            //bool ret = true;
            try
            {
                bool isNew = false;
                entity.__Elements.Where(ebd => ebd.SqlDbIsPrimaryKey).ToList().ForEach(ebd =>
                {
                    if ((ebd.TypeName.Equals("int") && int.Parse(ebd.Data.ToString()) == 0) || (ebd.TypeName.Equals("Guid") && Guid.Parse(ebd.Data.ToString()).Equals(Guid.Empty)))
                    {
                        isNew = true;
                    }
                });

                if (isNew)
                {
                    CRUDActions.Create <T>(entity);
                }
                else
                {
                    CRUDActions.Update <T>(entity);
                }

                ProcessExceptions(entity);
                //if (entity.Exceptions.Count > 0)
                //{
                //    string message = entity.Exceptions[0].Print(entity);
                //    File.WriteAllText(@"D:\BluePrint\BluePrint.root\BluePrint\BluePrint.Entity.Constructor\bin\Debug\ExceptionMessenger\Exception.html", message);
                //    System.Diagnostics.Process.Start(@"D:\BluePrint\BluePrint.root\BluePrint\BluePrint.Entity.Constructor\bin\Debug\ExceptionMessenger\Exception.html");
                //}

                return(true);
            }
            catch (System.Exception ex)
            {
                entity.Exceptions.Add(new EntityException(1, ex.Message, ex));
                return(false);
            }
        }
Esempio n. 5
0
        public static int Add <T>(IEntity entity, Collection <T> entities, bool bypassConflicts) where T : IEntity
        {
            int ret = 0;

            Collection <T> copies = new Collection <T>();

            Load <T>(copies);
            Collection <T> newEntities = copies.Except(entities);

            if (!bypassConflicts && newEntities.Count > 0 &&
                newEntities.ToList().Find(p => p.__Elements.Where(e => e.UseForMatching).Equals(entity.__Elements.Where(e => e.UseForMatching))) != null)
            {
                List <CRUDOption> options = new List <CRUDOption>();
                options.Add(new CRUDOption()
                {
                    Message    = "Add Anyway",
                    OptionType = CRUDOption.CRUDOptionType.SaveAll
                });
                options.Add(new CRUDOption()
                {
                    Message    = "Cancel Add",
                    OptionType = CRUDOption.CRUDOptionType.Cancel
                });
            }
            else
            {
                if (newEntities.Count > 0)
                {
                    entities.AddRange(newEntities);
                }
                else
                {
                    CRUDActions.Create <T>(entity);
                }
            }

            return(ret);
        }
Esempio n. 6
0
        public static bool Merge <T>(IEntity entity, bool bypassConflicts = false) where T : IEntity, new()
        {
            bool ret = true;

            try
            {
                bool isNew = true;
                foreach (ElementBaseData ebd in entity.__Elements.Where(ebd => ebd.SqlDbIsPrimaryKey).ToList())
                {
                    if ((ebd.TypeName.Equals("int") && int.Parse(ebd.Data.ToString()) != 0) || (ebd.TypeName.Equals("Guid") && !Guid.Parse(ebd.Data.ToString()).Equals(Guid.Empty)))
                    {
                        isNew = false;
                        break;
                    }
                }

                if (isNew)
                {
                    CRUDActions.Create <T>(entity);
                    return(true);
                }

                IEntity copy = new T();
                copy.ConnectionString = entity.ConnectionString;
                entity.__Elements.Where(ebd => ebd.SqlDbIsPrimaryKey).ToList().ForEach(ebd => copy.__Elements.Find(c => c.Name.Equals(ebd.Name)).Data = ebd.Data);

                CRUDActions.Retrieve <T>(copy);

                if (copy.Notifications.Count == 0)
                {
                    if (entity.__Memento.Count == 0)
                    {
                        long fieldsToSave = entity.Compare(copy);
                        CRUDActions.Merge <T>(entity, fieldsToSave);
                    }
                    else
                    {
                        long fieldsToSave = entity.Compare();
                        if (fieldsToSave == 0)
                        {
                            return(false);
                        }

                        SetWarnings(entity, copy);

                        if (!bypassConflicts)
                        {
                            SetConflicts(entity, copy);
                            ret = entity.CRUDConflict.Fields.Count == 0;
                        }

                        if (ret)
                        {
                            entity.__Elements.ForEach(e => UpdateField(e, copy, fieldsToSave));
                            CRUDActions.Merge <T>(copy, fieldsToSave);

                            entity.__Elements.Where(e => !e.IsCollection).ToList().ForEach(e => SaveCollection(e, copy));
                        }
                    }
                }
                else
                {
                    CRUDActions.Create <T>(entity);
                }
            }
            catch (System.Exception ex)
            {
                entity.Exceptions.Add(new EntityException(1, ex.Message, ex));
            }
            return(ret);
        }