public static void SetSaved <T>(this EntityServices @this, IEntity <T> entity, T id)
        {
            ((dynamic)entity).IsNew = false;

            entity.ID = id;
            @this.ResetOriginalId(entity);
        }
Beispiel #2
0
        private static IEntityServices SetupConnection(int connectionId, string domainName)
        {
            ConnectionDto connection;

            using (var db = new TenantDb(ConnectionString.ForDomain(domainName)))
            {
                var conn = db.Connections.Single(x => x.ConnectionId == connectionId && x.IsActive);
                connection = new ConnectionDto
                {
                    ConnectionId = conn.ConnectionId,
                    Url = conn.Url.Trim(),
                    UserName = conn.UserName.Trim(),
                    Password = conn.Password.Trim()
                };
            }
            IAuthenticator authenticator = new Authentication();
            IOrganizationService organizationService = authenticator.Authenticate(new AuthenticationInformation
            {
                OrganizationUri = connection.Url,
                UserName = connection.UserName,
                Password = connection.Password
            });

            IEntityServices entityService = new EntityServices(organizationService);
            return entityService;
        }
Beispiel #3
0
        public static async Task RaiseOnSaving(this EntityServices @this, IEntity record, CancelEventArgs e)
        {
            if (record == null)
            {
                throw new ArgumentNullException(nameof(record));
            }

            await GlobalEntityEvents.OnInstanceSaving(e);

            if (e.Cancel)
            {
                return;
            }

            await(record as Entity).OnSaving(e);

            foreach (var item in Context.Current.GetServices <IEntitySavingInterceptor>())
            {
                var task = item.Process(record);
                if (task != null)
                {
                    await task;
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new clone of an entity. This will work in a polymorphic way.
        /// </summary>
        public static T CloneAsNew <T>(this EntityServices @this, T entity, Action <T> changes = null) where T : Entity
        {
            var result = (T)entity.Clone();

            result.IsNew = true;

            if (result is GuidEntity)
            {
                (result as GuidEntity).ID = Guid.NewGuid();
            }
            // TODO: the following line need to be reviewed and fixed.
            // if (result is IntEntity) (result as IntEntity).ID = IntEntity.NewIdGenerator(result.GetType());

            // Setting the value of AutoNumber properties to zero
            foreach (var propertyInfo in result.GetType().GetProperties())
            {
                if (AutoNumberAttribute.IsAutoNumber(propertyInfo))
                {
                    propertyInfo.SetValue(result, 0);
                }
            }

            result.Initialize();

            // Re attach Documents:
            changes?.Invoke(result);

            return(result);
        }
        /// <summary>
        /// Sets the ID of an object explicitly.
        /// </summary>
        public static void ResetOriginalId <T>(this EntityServices @this, IEntity <T> entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            ((dynamic)entity).OriginalId = entity.ID;
        }
Beispiel #6
0
        public static async Task RaiseOnDeleting(this EntityServices @this, IEntity record, CancelEventArgs args)
        {
            await GlobalEntityEvents.OnInstanceDeleting(args);

            if (args.Cancel)
            {
                return;
            }
            await((Entity)record).OnDeleting(args);
        }
Beispiel #7
0
 public static void SetOriginalId(this EntityServices @this, IEntity entity)
 {
     if (entity is IOriginalIdHolder h)
     {
         h.SetOriginalId();
     }
     else
     {
         entity.GetType().GetProperty("OriginalId").SetValue(entity, entity.GetId());
     }
 }
        public IHttpActionResult UpdateServiceById(EnrolledServiceModel serviceModel)
        {
            var response = new DataResponse <EntityServices>();

            if (ModelState.IsValid)
            {
                serviceModel.CreatedBy  = CurrentUserId;
                serviceModel.BusinessId = CurrentBusinessId.Value;

                EntityServices entityService = new EntityServices();
                entityService.Id                = serviceModel.Id;
                entityService.BusinessId        = serviceModel.BusinessId;
                entityService.ServiceName       = serviceModel.ServiceName;
                entityService.ServiceDecription = serviceModel.ServiceDecription;
                entityService.ServiceColor      = "#" + serviceModel.ServiceColor;
                entityService.IsActive          = true;
                entityService.ImportMode        = serviceModel.ImportMode;
                entityService.FtpInfo           = new EntityServiceFtpInfo();
                entityService.BoxUrl            = serviceModel.BoxUrl;
                if (serviceModel.FtpInfo != null)
                {
                    entityService.FtpInfo.Host       = serviceModel.FtpInfo.Host;
                    entityService.FtpInfo.Username   = serviceModel.FtpInfo.Username;
                    entityService.FtpInfo.Passsword  = serviceModel.FtpInfo.Passsword;
                    entityService.FtpInfo.Protocol   = serviceModel.FtpInfo.Protocol;
                    entityService.FtpInfo.RemotePath = serviceModel.FtpInfo.RemotePath;
                    entityService.FtpInfo.PortNumber = serviceModel.FtpInfo.PortNumber;
                }

                response = new RepositoryEnrolledServices().Update(entityService);
                if (response.Status == DataResponseStatus.OK)
                {
                    string        rootPath = HttpContext.Current.Server.MapPath("~/Assets");
                    string        path     = Path.Combine(rootPath, CurrentBusinessId.ToString(), "Sales", response.Model.ServiceName.Replace(" ", "").ToString());
                    DirectoryInfo dir      = new DirectoryInfo(path);
                    if (!dir.Exists)
                    {
                        dir.Create();
                    }
                }

                return(Ok <DataResponse>(response));
            }
            else
            {
                var errorList = ModelState.Where(a => a.Value.Errors.Any()).Select(s => new
                {
                    Key     = s.Key.Split('.').Last(),
                    Message = s.Value.Errors[0].ErrorMessage
                });
                return(Ok <dynamic>(new { Status = HttpStatusCode.BadRequest, Model = errorList }));
            }
        }
Beispiel #9
0
 public static async Task RaiseOnLoaded(this EntityServices @this, IEntity record)
 {
     await((Entity)record).OnLoaded();
     foreach (var item in Context.Current.GetServices <IEntityLoadedInterceptor>())
     {
         var task = item.Process(record);
         if (task != null)
         {
             await task;
         }
     }
 }
Beispiel #10
0
 public static async Task RaiseOnDeleted(this EntityServices @this, IEntity record)
 {
     await((Entity)record).OnDeleted(EventArgs.Empty);
 }
Beispiel #11
0
        public DataResponse <EntityServices> Insert(EntityServices entity)
        {
            var response = new DataResponse <EntityServices>();

            try
            {
                base.DBInit();
                if ("#FFFFFF" == "#" + entity.ServiceColor)
                {
                    response.Id      = entity.Id;
                    response.Status  = DataResponseStatus.InternalServerError;
                    response.Message = "please select any other color!";
                }
                else if (DBEntity.LookupEnrolledServices.Count(a => a.ServiceColor == "#" + entity.ServiceColor) > 0)
                {
                    response.Id      = entity.Id;
                    response.Status  = DataResponseStatus.InternalServerError;
                    response.Message = "Service Color already exists";
                }
                else
                {
                    var model = new Database.LookupEnrolledService
                    {
                        BusinessId        = entity.BusinessId,
                        ServiceName       = entity.ServiceName,
                        ServiceDecription = entity.ServiceDecription,
                        CreatedBy         = entity.CreatedBy,
                        CreatedOn         = entity.CreatedOn,
                        ServiceColor      = "#" + entity.ServiceColor,
                        IsActive          = true,
                        ImportMode        = entity.ImportMode,
                        BoxUrl            = entity.ImportMode == (int)EBP.Business.Enums.ServiceReportImportModes.BoxAPI ? entity.BoxUrl : null,
                    };
                    if (base.DBSave(model) > 0)
                    {
                        #region FTP
                        if (model.ImportMode == (int)EBP.Business.Enums.ServiceReportImportModes.Ftp)
                        {
                            DBEntity.ServiceFtpInfoes.Add(new ServiceFtpInfo
                            {
                                HostName   = entity.FtpInfo.Host,
                                Protocol   = entity.FtpInfo.Protocol,
                                Username   = entity.FtpInfo.Username,
                                Password   = entity.FtpInfo.Passsword,
                                RemotePath = entity.FtpInfo.RemotePath,
                                PortNumber = entity.FtpInfo.PortNumber,
                                ServiceId  = model.Id
                            });
                            DBEntity.SaveChanges();
                        }
                        #endregion
                        var responseModel = GetServiceById(model.Id);
                        responseModel.Status = DataResponseStatus.OK;
                        return(responseModel);
                    }
                    else
                    {
                        response.CreateResponse(DataResponseStatus.InternalServerError);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Log();
            }
            finally
            {
                base.DBClose();
            }
            return(response);
        }
Beispiel #12
0
 /// <summary>
 /// Sets the state of an entity instance to saved.
 /// </summary>
 public static void SetSaved(this EntityServices @this, IEntity entity) => (entity as Entity).IsNew = false;
Beispiel #13
0
 /// <summary>
 /// Marks the specified object as immutable.
 /// </summary>
 public static void MarkImmutable(this EntityServices @this, IEntity entity)
 {
     ((Entity)entity).IsImmutable = true;
 }
Beispiel #14
0
 /// <summary>
 /// Determines whether the specified record is immutable, or closed for changes.
 /// An object marked as immutable is shared in the application cache. Therefore it must not be changed.
 /// </summary>
 public static bool IsImmutable(this EntityServices @this, IEntity entity)
 {
     return(((Entity)entity).IsImmutable && !entity.IsNew);
 }
Beispiel #15
0
        /// <summary>
        /// Sets the state of an entity instance to saved.
        /// </summary>
        public static void SetSaved(this EntityServices @this, IEntity entity, bool saved = true)
        {
            (entity as Entity).IsNew = !saved;

            entity.GetType().GetProperty("OriginalId").SetValue(entity, entity.GetId());
        }
Beispiel #16
0
 public static async Task RaiseOnSaved(this EntityServices @this, IEntity record, SaveEventArgs e)
 {
     await((Entity)record).OnSaved(e);
 }
 public static Task RaiseOnDeleted(this EntityServices @this, IEntity record)
 {
     return(((Entity)record).OnDeleted(EventArgs.Empty));
 }
Beispiel #18
0
        public DataResponse <EntityServices> Update(EntityServices entityServices)
        {
            var response = new DataResponse <EntityServices>();

            try
            {
                base.DBInit();
                if ("#FFFFFF" == "#" + entityServices.ServiceColor)
                {
                    response.Id      = entityServices.Id;
                    response.Status  = DataResponseStatus.InternalServerError;
                    response.Message = "please select any other color!";
                }
                else if (DBEntity.LookupEnrolledServices.Count(a => a.Id != entityServices.Id && a.ServiceColor == "#" + entityServices.ServiceColor) > 0)
                {
                    response.Id      = entityServices.Id;
                    response.Status  = DataResponseStatus.InternalServerError;
                    response.Message = "Service Color already exists";
                }
                else
                {
                    var model = DBEntity.LookupEnrolledServices.FirstOrDefault(a => a.Id == entityServices.Id);
                    entityServices.OldServiceName = model.ServiceName;
                    model.ServiceName             = entityServices.ServiceName;
                    model.ServiceDecription       = entityServices.ServiceDecription;
                    model.IsActive     = entityServices.IsActive;
                    model.UpdatedOn    = entityServices.UpdatedOn;
                    model.UpdatedBy    = entityServices.UpdatedBy;
                    model.ServiceColor = entityServices.ServiceColor;
                    model.ImportMode   = entityServices.ImportMode;
                    model.BoxUrl       = entityServices.ImportMode == (int)EBP.Business.Enums.ServiceReportImportModes.BoxAPI ? entityServices.BoxUrl : null;

                    if (base.DBSaveUpdate(model) > 0)
                    {
                        #region FTP
                        if (model.ImportMode == (int)EBP.Business.Enums.ServiceReportImportModes.Ftp)
                        {
                            var ftpModel = model.ServiceFtpInfoes.FirstOrDefault(a => a.ServiceId == entityServices.Id);
                            if (ftpModel != null)
                            {
                                ftpModel.Protocol   = entityServices.FtpInfo.Protocol;
                                ftpModel.HostName   = entityServices.FtpInfo.Host;
                                ftpModel.Username   = entityServices.FtpInfo.Username;
                                ftpModel.Password   = entityServices.FtpInfo.Passsword;
                                ftpModel.PortNumber = entityServices.FtpInfo.PortNumber;
                                ftpModel.RemotePath = entityServices.FtpInfo.RemotePath;

                                DBEntity.SaveChanges();
                            }
                            else
                            {
                                DBEntity.ServiceFtpInfoes.Add(new ServiceFtpInfo
                                {
                                    HostName   = entityServices.FtpInfo.Host,
                                    Protocol   = entityServices.FtpInfo.Protocol,
                                    Username   = entityServices.FtpInfo.Username,
                                    Password   = entityServices.FtpInfo.Passsword,
                                    RemotePath = entityServices.FtpInfo.RemotePath,
                                    PortNumber = entityServices.FtpInfo.PortNumber,
                                    ServiceId  = entityServices.Id
                                });
                                DBEntity.SaveChanges();
                            }
                            #endregion
                        }
                        var responseModel = GetServiceById(model.Id);
                        responseModel.Model.OldServiceName = model.ServiceName;
                        responseModel.Status = DataResponseStatus.OK;
                        return(responseModel);
                    }
                    else
                    {
                        response.CreateResponse(DataResponseStatus.InternalServerError);
                    }
                }
            }

            catch (Exception ex)
            {
                ex.Log();
            }
            finally
            {
                base.DBClose();
            }
            return(response);
        }
Beispiel #19
0
        public static async Task RaiseOnValidating(this EntityServices @this, IEntity record, EventArgs args)
        {
            await GlobalEntityEvents.OnInstanceValidating(args);

            await((Entity)record).OnValidating(args);
        }
 public static Task RaiseOnSaved(this EntityServices @this, IEntity record, SaveEventArgs e)
 {
     return(((Entity)record).OnSaved(e));
 }