Ejemplo n.º 1
0
        private bool Save(ThisEntity entity, int parentId, ref SaveStatus status)
        {
            bool isValid = true;

            if (parentId == 0)
            {
                status.AddError("CostProfileItemManager.Save() - Error: the parent cost profile id was not provided.");
                return(false);
            }

            int count = 0;

            DBEntity efEntity = new DBEntity();

            using (var context = new EntityContext())
            {
                if (ValidateProfile(entity, ref status) == false)
                {
                    return(false);
                }

                try
                {
                    //just in case
                    entity.CostProfileId = parentId;

                    if (entity.Id == 0)
                    {
                        //add
                        efEntity = new DBEntity();
                        MapToDB(entity, efEntity);

                        efEntity.RowId   = Guid.NewGuid();
                        efEntity.Created = efEntity.LastUpdated = DateTime.Now;
                        context.Entity_CostProfileItem.Add(efEntity);
                        count = context.SaveChanges();
                        //update profile record so doesn't get deleted
                        entity.Id    = efEntity.Id;
                        entity.RowId = efEntity.RowId;
                        if (count == 0)
                        {
                            status.AddError(string.Format(" Unable to add Cost Item for CostProfileId: {0}, CostTypeId: {1}  ", parentId, entity.CostTypeId));
                            isValid = false;
                        }
                        else
                        {
                            UpdateParts(entity, ref status);
                        }
                    }
                    else
                    {
                        context.Configuration.LazyLoadingEnabled = false;

                        efEntity = context.Entity_CostProfileItem.SingleOrDefault(s => s.Id == entity.Id);
                        if (efEntity != null && efEntity.Id > 0)
                        {
                            //update
                            MapToDB(entity, efEntity);
                            //has changed?
                            if (HasStateChanged(context))
                            {
                                efEntity.LastUpdated = System.DateTime.Now;

                                count = context.SaveChanges();
                            }
                            //always check parts
                            entity.RowId = efEntity.RowId;
                            UpdateParts(entity, ref status);
                        }
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    string message = HandleDBValidationError(dbex, "CostProfileItemManager.Save()", string.Format("CostProfileId: 0 , CostTypeId: {1}  ", parentId, entity.CostTypeId));

                    status.AddError(message);
                    isValid = false;
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, string.Format("CostProfileItemManager.Save(), CostProfileId: 0 , CostTypeId: {1}  ", parentId, entity.CostTypeId));
                    isValid = false;
                }
            }

            return(isValid);
        }
Ejemplo n.º 2
0
        public static List <ThisEntity> Search(int topParentTypeId, int topParentEntityBaseId, string pFilter, string pOrderBy, int pageNumber, int pageSize, ref int pTotalRows)
        {
            string            connectionString = DBConnectionRO();
            ThisEntity        item             = new ThisEntity();
            CostProfile       cp   = new CostProfile();
            List <ThisEntity> list = new List <ThisEntity>();
            var result             = new DataTable();

            using (SqlConnection c = new SqlConnection(connectionString))
            {
                c.Open();

                if (string.IsNullOrEmpty(pFilter))
                {
                    pFilter = "";
                }

                using (SqlCommand command = new SqlCommand("[CostProfileItems_search]", c))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(new SqlParameter("@condProfParentEntityTypeId", topParentTypeId));
                    command.Parameters.Add(new SqlParameter("@condProfParentEntityBaseId", topParentEntityBaseId));
                    command.Parameters.Add(new SqlParameter("@Filter", pFilter));
                    command.Parameters.Add(new SqlParameter("@SortOrder", pOrderBy));
                    command.Parameters.Add(new SqlParameter("@StartPageIndex", pageNumber));
                    command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

                    SqlParameter totalRows = new SqlParameter("@TotalRows", pTotalRows);
                    totalRows.Direction = ParameterDirection.Output;
                    command.Parameters.Add(totalRows);

                    using (SqlDataAdapter adapter = new SqlDataAdapter())
                    {
                        adapter.SelectCommand = command;
                        adapter.Fill(result);
                    }
                    string rows = command.Parameters[4].Value.ToString();
                    try
                    {
                        pTotalRows = Int32.Parse(rows);
                    }
                    catch
                    {
                        pTotalRows = 0;
                    }
                }
                //determine if we want to return data as a list of costprofiles or costProfileItems
                //

                foreach (DataRow dr in result.Rows)
                {
                    //cp = new CostProfile();
                    item    = new ThisEntity();
                    item.Id = GetRowColumn(dr, "Entity_CostProfileId", 0);
                    //include parent entity type somewhere
                    item.ParentEntityType = GetRowColumn(dr, "EntityType", "");

                    item.ProfileName = GetRowColumn(dr, "CostProfileName", "Cost Profile");

                    item.CostTypeName = GetRowColumn(dr, "CostType", "");

                    item.Currency       = GetRowColumn(dr, "Currency", "");
                    item.CurrencySymbol = GetRowColumn(dr, "CurrencySymbol", "");
                    item.Price          = GetRowPossibleColumn(dr, "Price", 0M);

                    //item.Description = string.Format( "{0} {1} ({2})", item.CurrencySymbol, item.Price, item.ParentEntityType );
                    list.Add(item);
                }

                return(list);
            }
        }         //