예제 #1
0
        public IActionResult OnGet()
        {
            Init();

            var entMan = new EntityManager();
            var relMan = new EntityRelationManager();

            #region << InitPage >>
            int           pager     = 0;
            string        sortBy    = "";
            QuerySortType sortOrder = QuerySortType.Ascending;
            PageUtils.GetListQueryParams(PageContext.HttpContext, out pager, out sortBy, out sortOrder);
            Pager     = pager;
            SortBy    = sortBy;
            SortOrder = sortOrder;

            ErpEntity = entMan.ReadEntity(ParentRecordId ?? Guid.Empty).Object;

            if (ErpEntity == null)
            {
                return(NotFound());
            }

            if (string.IsNullOrWhiteSpace(ReturnUrl))
            {
                ReturnUrl = $"/sdk/objects/entity/r/{ErpEntity.Id}/";
            }

            var entityRelations = relMan.Read().Object.Where(x => x.TargetEntityId == ErpEntity.Id || x.OriginEntityId == ErpEntity.Id).ToList();

            #region << Apply filters >>
            var submittedFilters = PageUtils.GetPageFiltersFromQuery(PageContext.HttpContext);
            if (submittedFilters.Count > 0)
            {
                foreach (var filter in submittedFilters)
                {
                    switch (filter.Name)
                    {
                    case "name":
                        if (filter.Type == FilterType.CONTAINS)
                        {
                            entityRelations = entityRelations.FindAll(x => x.Name.ToLowerInvariant().Contains(filter.Value.ToLowerInvariant())).ToList();
                        }
                        break;
                    }
                }
            }
            #endregion

            entityRelations = entityRelations.OrderBy(x => x.Name).ToList();
            TotalCount      = entityRelations.Count;

            ReturnUrlEncoded = HttpUtility.UrlEncode(PageUtils.GetCurrentUrl(PageContext.HttpContext));

            PageDescription = PageUtils.GenerateListPageDescription(PageContext.HttpContext, "", TotalCount);
            #endregion

            #region << Create Columns >>

            Columns = new List <GridColumn>()
            {
                new GridColumn()
                {
                    Name  = "action",
                    Width = "1%"
                },
                new GridColumn()
                {
                    Label      = "Name",
                    Name       = "name",
                    Sortable   = true,
                    Searchable = true
                },
                new GridColumn()
                {
                    Label      = "Origin",
                    Name       = "origin",
                    Sortable   = false,
                    Searchable = false,
                    Width      = "25%"
                },
                new GridColumn()
                {
                    Label      = "Target",
                    Name       = "target",
                    Sortable   = false,
                    Searchable = false,
                    Width      = "25%",
                },
            };

            #endregion

            #region << Records >>
            //Apply sort
            if (!String.IsNullOrWhiteSpace(SortBy))
            {
                switch (SortBy)
                {
                case "name":
                    if (SortOrder == QuerySortType.Descending)
                    {
                        entityRelations = entityRelations.OrderByDescending(x => x.Name).ToList();
                    }
                    else
                    {
                        entityRelations = entityRelations.OrderBy(x => x.Name).ToList();
                    }
                    break;

                default:
                    break;
                }
            }

            //Apply pager
            var skipPages = (Pager - 1) * PagerSize;
            entityRelations = entityRelations.Skip(skipPages).Take(PagerSize).ToList();

            const string OneToOne   = " <span class='badge badge-primary badge-inverse' title='One to One' style='margin-left:5px;'>1 : 1</span>";
            const string OneToMany  = " <span class='badge badge-primary badge-inverse' title='One to Many' style='margin-left:5px;'>1 : N</span>";
            const string ManyToMany = " <span class='badge badge-primary badge-inverse' title='Many to Many' style='margin-left:5px;'>N : N</span>";

            foreach (var relation in entityRelations)
            {
                var nameColumnText = relation.Name + (relation.System ? " <i class='fa fa-fw fa-lock'></i>" : "");
                switch (relation.RelationType)
                {
                case EntityRelationType.OneToOne:
                    nameColumnText = nameColumnText + OneToOne;
                    break;

                case EntityRelationType.OneToMany:
                    nameColumnText = nameColumnText + OneToMany;
                    break;

                case EntityRelationType.ManyToMany:
                    nameColumnText = nameColumnText + ManyToMany;
                    break;
                }


                var originColumnText = $"<div><span class='go-gray'>Entity: </span> {relation.OriginEntityName}</div><div><span class='go-gray'>Field: </span> {relation.OriginFieldName}</div>";
                var targetColumnText = $"<div><span class='go-gray'>Entity: </span> {relation.TargetEntityName}</div><div><span class='go-gray'>Field: </span> {relation.TargetFieldName}</div>";

                var record = new EntityRecord();
                record["action"] = $"<a class='btn btn-sm btn-white' title='view relation details' href='/sdk/objects/entity/r/{ErpEntity.Id}/rl/relations/r/{relation.Id}?returnUrl={ReturnUrlEncoded}'><span class='ti-eye'></span></a>";
                record["name"]   = nameColumnText;
                record["origin"] = originColumnText;
                record["target"] = targetColumnText;
                Records.Add(record);
            }
            #endregion

            HeaderToolbar.AddRange(AdminPageUtils.GetEntityAdminSubNav(ErpEntity, "relations"));

            ErpRequestContext.PageContext = PageContext;

            return(Page());
        }
예제 #2
0
        public void TaskCreateRecordAction(dynamic data)
        {
            var record         = (EntityRecord)data.record;
            var createResult   = (QueryResponse)data.result;
            var controller     = (Controller)data.controller;
            var createdRecord  = createResult.Object.Data[0];
            var patchObject    = new EntityRecord();
            var priorityString = "";

            if ((string)record["priority"] == "high")
            {
                priorityString = "<span class='go-red'> [high] </span>";
            }
            using (SecurityContext.OpenSystemScope())
            {
                #region << Add creator in watch list >>
                {
                    var targetRelation             = relMan.Read("user_n_n_task_watchers").Object;
                    var createRelationNtoNResponse = recMan.CreateRelationManyToManyRecord(targetRelation.Id, (Guid)record["created_by"], (Guid)record["id"]);
                    if (!createRelationNtoNResponse.Success)
                    {
                        throw new Exception("Could not create watch relation" + createRelationNtoNResponse.Message);
                    }
                }
                #endregion

                #region << Add owner in watch list >>
                {
                    if ((Guid)record["created_by"] != (Guid)record["owner_id"])
                    {
                        var targetRelation             = relMan.Read("user_n_n_task_watchers").Object;
                        var createRelationNtoNResponse = recMan.CreateRelationManyToManyRecord(targetRelation.Id, (Guid)record["owner_id"], (Guid)record["id"]);
                        if (!createRelationNtoNResponse.Success)
                        {
                            throw new Exception("Could not create watch relation" + createRelationNtoNResponse.Message);
                        }
                    }
                }
                #endregion

                #region << Generate the code field value >>
                {
                    var filterObj = EntityQuery.QueryEQ("id", (Guid)createdRecord["project_id"]);
                    var query     = new EntityQuery("wv_project", "*", filterObj, null, null, null);
                    var result    = recMan.Find(query);
                    if (result.Success && result.Object.Data.Any())
                    {
                        patchObject         = new EntityRecord();
                        patchObject["id"]   = (Guid)createdRecord["id"];
                        patchObject["code"] = result.Object.Data[0]["code"] + "-T" + createdRecord["number"];
                        record["code"]      = (string)patchObject["code"];                    // for use in regenerating the fts
                        var patchResult = recMan.UpdateRecord("wv_task", patchObject);
                        if (!patchResult.Success)
                        {
                            throw new Exception(patchResult.Message);
                        }

                        #region << Check if project owner is different than owner and add it in watch list >>
                        {
                            if ((Guid)result.Object.Data[0]["owner_id"] != (Guid)record["owner_id"] && (Guid)result.Object.Data[0]["owner_id"] != (Guid)record["created_by"])
                            {
                                var targetRelation             = relMan.Read("user_n_n_task_watchers").Object;
                                var createRelationNtoNResponse = recMan.CreateRelationManyToManyRecord(targetRelation.Id, (Guid)result.Object.Data[0]["owner_id"], (Guid)record["id"]);
                                if (!createRelationNtoNResponse.Success)
                                {
                                    throw new Exception("Could not create watch relation" + createRelationNtoNResponse.Message);
                                }
                            }
                        }
                        #endregion
                    }
                }
                #endregion
            }
            var activityDescription = Utils.RemoveHtml((string)createdRecord["description"]);
            activityDescription = Utils.TextLength(activityDescription, "short");
            Utils.CreateActivity(recMan, "created", "created a <i class='fa fa-fw fa-tasks go-purple'></i> task</br></br>[" + patchObject["code"] + "] " + priorityString + " <a href='/#/areas/projects/wv_task/view-general/sb/general/" + createdRecord["id"] + "'>" + System.Net.WebUtility.HtmlEncode((string)createdRecord["subject"]) + "</a>", activityDescription, (Guid)createdRecord["project_id"], SecurityContext.CurrentUser.Id, (Guid)createdRecord["id"], null);
            var creatorUsername = "";
            #region << Get username of the creator>>
            {
                EntityQuery   query  = new EntityQuery("user", "username", EntityQuery.QueryEQ("id", (Guid)createdRecord["created_by"]), null, null, null);
                QueryResponse result = recMan.Find(query);
                if (!result.Success)
                {
                    throw new Exception("Cannot get the username of the commentator");
                }
                creatorUsername = (string)result.Object.Data[0]["username"];
            }
            #endregion

            #region << Sent email notification>>
            {
                //At this moment only if the project manager is different than the item creator should get an email
                if ((Guid)createdRecord["created_by"] != (Guid)createdRecord["owner_id"])
                {
                    var emailService           = new EmailService();
                    var emailSubjectParameters = new Dictionary <string, string>();
                    emailSubjectParameters["code"]    = (string)patchObject["code"];
                    emailSubjectParameters["subject"] = (string)createdRecord["subject"];

                    var subject = Regex.Replace(EmailTemplates.NewBugOrTaskNotificationSubject, @"\{(.+?)\}", m => emailSubjectParameters[m.Groups[1].Value]);

                    var emailContentParameters = new Dictionary <string, string>();
                    emailContentParameters["baseUrl"]              = controller.HttpContext.Request.Scheme + "://" + controller.HttpContext.Request.Host.Value;
                    emailContentParameters["subject"]              = subject;
                    emailContentParameters["type"]                 = "task";
                    emailContentParameters["creator"]              = creatorUsername;
                    emailContentParameters["taskOrBugUrl"]         = emailContentParameters["baseUrl"] + "/#/areas/projects/wv_task/view-general/sb/general/" + createdRecord["id"];
                    emailContentParameters["taskOrBugDescription"] = (string)createdRecord["description"];

                    var content = Regex.Replace(EmailTemplates.NewBugOrTaskNotificationContent, @"\{(.+?)\}", m => emailContentParameters[m.Groups[1].Value]);

                    var resepients = new List <string>();

                    #region << Get pm email>>
                    {
                        EntityQuery   query  = new EntityQuery("user", "email", EntityQuery.QueryEQ("id", (Guid)createdRecord["owner_id"]), null, null, null);
                        QueryResponse result = recMan.Find(query);
                        if (!result.Success)
                        {
                            throw new Exception("Cannot get the username of the commentator");
                        }
                        emailService.SendEmail((string)result.Object.Data[0]["email"], subject, content);
                    }
                    #endregion
                }
            }
            #endregion

            #region << Regen FTS >>
            Utils.RegenAndUpdateTaskFts((EntityRecord)record, recMan);
            #endregion
        }