public SchedulerDto SaveScheduling(ApplicationContext context, SchedulerDto schedule)
        {
            using (var db = new TenantDb(ConnectionString.ForDomain(context.TenantDomainName)))
            {
                var sch = new Schedule
                {
                    Name = schedule.Name,
                    SourceConnectionId = schedule.SourceConnectionId,
                    TargetConnectionId = schedule.TargetConnectionId,
                    RegardingEntityName = schedule.RegardingEntityName,
                    SourceViewId = schedule.SourceViewId,
                    NotificationEmails = schedule.Emails,
                    IsActive = schedule.IsActive,
                    CreatedBy = context.UserId,
                    CreatedOn = DateTime.Now,
                    ModifiedBy = context.UserId,
                    ModifiedOn = DateTime.Now
                };

                db.Schedules.Add(sch);
                db.SaveChanges();

                schedule.ScheduleId = sch.ScheduleId;
                var excecution = new ExecutionDto
                {
                    ScheduleId = schedule.ScheduleId,
                    IsOnDemenad = false,
                    CreatedBy = schedule.CreatedBy
                };
                SaveExecution(context.TenantDomainName, excecution);

                MailService.SendMail(schedule.Emails, MailMessageType.SchedulerCreated);
                return schedule;
            }
        }
        private static ApplicationContext CreateApplicationContext(Dictionary<string, object> cookies)
        {
            var result = new ApplicationContext();
            var contextKeys = typeof(ApplicationContext).GetProperties().Where(x => x.CanWrite);
            var context = HttpContext.Current;

            foreach (PropertyInfo propertyInfo in contextKeys)
            {
                string propertyKey = (context.Request.Cookies[propertyInfo.Name] != null) ? context.Request.Cookies[propertyInfo.Name].Value : String.Empty;
               
                if (!string.IsNullOrEmpty(propertyKey))
                {
                    // in this case whe use TypeDescriptor COnvertion because the GUID cannot be natively translated from string.
                    //http://stackoverflow.com/questions/393731/generic-conversion-function-doesnt-seem-to-work-with-guids
                    object value = TypeDescriptor.GetConverter(propertyInfo.PropertyType).ConvertFromInvariantString(propertyKey);
                    result.GetType().GetProperty(propertyInfo.Name).SetValue(result, value, null);
                }
            }

            return result;
        }
 public void SaveConnection(ApplicationContext context, ConnectionDto connection)
 {
     using (var db=new TenantDb(ConnectionString.ForDomain(context.TenantDomainName)))
     {
         var conn = new Connection
         {
             Description = connection.Name,
             ConnectionType = connection.ConnectionType,
             UserName = connection.UserName,
             Password = connection.Password,
             Url = connection.Url,
             ValidationStatus = connection.ValidationStatus,
             ValidationMessage = connection.ValidationMessage ?? String.Empty,
             CreatedOn = DateTime.Now,
             CreatedBy = context.UserId,
             ModifiedBy = context.UserId,
             ModifiedOn = DateTime.Now,
             IsActive = true
         };
         db.Connections.Add(conn);
         db.SaveChanges();
     }
 }
        public void RunNow(ApplicationContext context, SchedulerDto schedule)
        {
            schedule.Name = "On Demand";
            schedule.IsActive = false;
            schedule = SaveScheduling(context, schedule);

            var crmConnection = GetConnectionById(context.TenantDomainName, schedule.SourceConnectionId);
            var sharepointConnection = GetConnectionById(context.TenantDomainName, schedule.TargetConnectionId);
            var scheduler = new ScheduleExecutionJobInformation
            {
                SourceViewId = schedule.SourceViewId,
                Source = new Connection
                {
                    Login = crmConnection.UserName,
                    Password = crmConnection.Password,
                    Url = crmConnection.Url
                },
                Target = new Connection
                {
                    Login = sharepointConnection.UserName,
                    Password = sharepointConnection.Password,
                    Url = sharepointConnection.Url
                }
            };
            var excecution = new ExecutionDto
            {
                ScheduleId = schedule.ScheduleId,
                IsOnDemenad = true,
                CreatedBy = schedule.CreatedBy
            };
            SaveExecution(context.TenantDomainName, excecution);
            AzureAdapterService.RunNow(scheduler, context.TenantDomainName);
            scheduler.ExecutionId = excecution.ExecutionId;
        }
        public void UserRegister(ApplicationContext context, RegisterModel model)
        {
            using (var db = new AdministrationDb())
            {
                var user = db.ApplicationUsers.FirstOrDefault(x => x.EmailAddress == model.UserName);
                if (user != null)
                {
                    throw new BusinessException("");
                }
                Logger.Info("Creating Membership");
                MembershipCreateStatus createStatus;
                var membershipUser = Membership.CreateUser(model.UserName,
                    model.Password,
                    model.UserName, "¿?", "!!", true, Guid.NewGuid(), out createStatus);
                Logger.Info("Membership created");
                var created = false;

                if (membershipUser != null && membershipUser.ProviderUserKey != null)
                {
                    var userId = (Guid) membershipUser.ProviderUserKey;
                    var appUser = new ApplicationUser
                    {
                        UserId = userId,
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        EmailAddress = model.UserName,
                        IsEmailVerified = true,
                        EmailVerificationToken = string.Empty,
                        IsActive = true,
                        CreatedOn = DateTime.Now,
                        CreatedBy = context.UserId,
                        ModifiedOn = DateTime.Now,
                        ModifiedBy = context.UserId,
                        
                    };

                    db.ApplicationUsers.Add(appUser);
                    db.SaveChanges();
                    var customerUser = new CustomerUser
                    {
                        ApplicationUserId = userId,
                        CustomerId = context.TenantId,
                        IsCreator = false,
                        IsAdmin = false,
                        IsRelationActive = true
                    };
                    db.CustomerUsers.Add(customerUser);
                    db.SaveChanges();

                }
            }
        }