Exemplo n.º 1
0
        public Business ModelToEntity(UserSessionModel admin, BusinessModel model, bool businessEdit)
        {
            var entity = GetEntity(admin, model);

            if (this.Response.HasError)
            {
                return(null);
            }

            entity.BusinessName = Utilities.Trim(model.BusinessName);

            entity.AccountId = Utilities.Trim(model.AccountId);

            if (model.BusinessTypeId != null)
            {
                entity.BusinessTypeId = (BusinessTypeEnum)model.BusinessTypeId;
            }

            if (businessEdit)
            {
                if (this.Context.Entry(entity).State == EntityState.Added)
                {
                    if (entity.IsWebServiceImport)
                    {
                        model.Enabled = true;
                    }
                }

                entity.ShowPricing = model.ShowPricing;

                entity.Enabled = model.Enabled;

                Db.PermissionsUpdate(EntityEnum.CityArea, (long)PermissionTypeEnum.CityArea, EntityEnum.Business, model.BusinessId, CheckBoxListModel.ToPermissionListModel(model.CityAreas), PermissionTypeEnum.CityArea);
                Db.PermissionsUpdate(EntityEnum.Brand, (long)PermissionTypeEnum.Brand, EntityEnum.Business, model.BusinessId, CheckBoxListModel.ToPermissionListModel(model.Brands), PermissionTypeEnum.Brand);
                Db.PermissionsUpdate(EntityEnum.ProductFamily, (long)PermissionTypeEnum.ProductFamily, EntityEnum.Business, model.BusinessId, CheckBoxListModel.ToPermissionListModel(model.ProductFamilies), PermissionTypeEnum.ProductFamily);
                Db.PermissionsUpdate(EntityEnum.Tool, (long)PermissionTypeEnum.Tool, EntityEnum.Business, model.BusinessId, CheckBoxListModel.ToPermissionListModel(model.Tools), PermissionTypeEnum.Tool);

                contactService.BeginPropertyReference(this, "Contact");
                entity.Contact = contactService.ModelToEntity(model.Contact);
                contactService.EndPropertyReference();

                if (model.ParentBusinessId != 0 && model.ParentBusinessId != null)
                {
                    UpdateParentBusiness(model);
                }
            }

            addressService.BeginPropertyReference(this, "Address");
            entity.Address = addressService.ModelToEntity(model.Address);
            addressService.EndPropertyReference();

            return(entity);
        }
Exemplo n.º 2
0
        public ServiceResponse GetUserModel(UserSessionModel user, UserModel templateModel, bool isEditing = false)
        {
            UserModel model = null;

            if (templateModel != null)
            {
                var userId = templateModel.UserId;

                if (userId.HasValue)
                {
                    var query = from u in this.Db.QueryUserViewableByUserId(user, userId.Value)
                                select
                                new UserModel
                    {
                        UserId     = u.UserId,
                        Email      = u.Email,
                        FirstName  = u.FirstName,
                        MiddleName = u.MiddleName,
                        LastName   = u.LastName,
                        Enabled    = u.Enabled,
                        Business   = new BusinessModel {
                            BusinessId = u.BusinessId
                        },
                        // AddressId = user.AddressId,
                        //ContactId = user.ContactId,
                        ShowPricing        = u.ShowPricing,
                        UseBusinessAddress = u.UseBusinessAddress,
                        UserTypeId         = (UserTypeEnum?)u.UserTypeId,
                        Approved           = u.Approved,
                        Timestamp          = u.Timestamp,
                        Address            = new AddressModel
                        {
                            AddressId = u.AddressId,
                        },
                        Contact = new ContactModel
                        {
                            ContactId = u.ContactId,
                        }
                    };



                    model = query.FirstOrDefault();

                    //Get Parent Business if available // Moved to GetBusinessModel()
                    //var businessLink = this.Db.BusinessLinkQueryByBusinessId(model.Business.BusinessId).FirstOrDefault();
                    //if (businessLink != null)
                    //{
                    //    SearchBusiness businessSearch = new SearchBusiness
                    //    {
                    //        BusinessId = businessLink.ParentBusinessId,
                    //        ReturnTotals = true
                    //    };
                    //    var resp = businessService.GetBusinessModel(user, businessSearch);
                    //    if (resp != null && resp.IsOK && resp.Model != null)
                    //    {
                    //        var parentBusiness = resp.Model as BusinessModel;
                    //        model.Business.ParentBusinessId = (long)parentBusiness.BusinessId;
                    //        model.Business.ParentBusinessName = parentBusiness.BusinessName;
                    //    }

                    //}

                    if (model == null)
                    {
                        this.Response.AddError(Resources.DataMessages.DM004);
                    }
                }
            }

            model = model ?? new UserModel();

            var userTypeChanged = false;

            // Setup from templateModel
            if (templateModel != null)
            {
                //if (model.UserTypeId != templateModel.UserTypeId) userTypeChanged = true;   this line causing the problem, comment by aaron
                if (templateModel.UserTypeId.HasValue)
                {
                    model.UserTypeId = templateModel.UserTypeId;
                }
                model.IsRegistering = templateModel.IsRegistering;
            }

            // Load Model Children
            addressService.BeginPropertyReference(this, "Address");
            model.Address = addressService.GetAddressModel(user, model.Address);
            addressService.EndPropertyReference();

            contactService.BeginPropertyReference(this, "Contact");
            model.Contact = contactService.GetContactModel(user, model.Contact);
            contactService.EndPropertyReference();

            businessService.BeginPropertyReference(this, "Business");
            model.Business = businessService.GetBusinessModel(user, model.Business.BusinessId, true).Model as BusinessModel;
            businessService.EndPropertyReference();

            FinaliseModel(user, model, isEditing, userTypeChanged);

            this.Response.Model = model;

            return(this.Response);
        }