// GET: CustomFieldType
        public ActionResult Index()
        {
            var customFieldTypes = _customFieldTypeService.Queryable();

            if (!customFieldTypes.Any())
            {
                return(View(new List <CustomFieldType>()));
            }
            return(View(customFieldTypes.ToList()));
        }
        public ActionResult GetCustomFieldSelectable()
        {
            var selectable = new CustomFieldSelectable
            {
                CustomLists      = _customListService.Queryable().OrderBy(l => l.CustomListName).ToList(),
                CustomFieldTypes = _customFieldTypeService.Queryable().OrderBy(t => t.FieldTypeName).ToList(),
                CustomGroups     = _customGroupService.Queryable().OrderBy(g => g.GroupName).ToList()
            };

            return(Json(selectable, JsonRequestBehavior.AllowGet));
        }
Esempio n. 3
0
        public ActionResult InstallLicense()
        {
            var acResponse = new ActivityResponse();

            try
            {
                if (Request.Files != null)
                {
                    var file = Request.Files[0];

                    if (file == null || file.ContentLength < 1)
                    {
                        acResponse.Code    = -1;
                        acResponse.Message = "The provided Site License File could not be correctly accessed. Please provide all required fields and try again later";
                        return(Json(acResponse, JsonRequestBehavior.AllowGet));
                    }

                    const string folderPath = "~/TempProject";

                    var mainPath = Server.MapPath(folderPath);

                    if (!Directory.Exists(mainPath))
                    {
                        Directory.CreateDirectory(mainPath);
                        var dInfo     = new DirectoryInfo(mainPath);
                        var dSecurity = dInfo.GetAccessControl();
                        dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
                        dInfo.SetAccessControl(dSecurity);
                    }

                    var fileName = file.FileName;

                    var existingFiles = Directory.GetFiles(mainPath);
                    if (existingFiles.Any())
                    {
                        existingFiles.ForEach(System.IO.File.Delete);
                    }

                    var path = Path.Combine(mainPath, fileName);

                    file.SaveAs(path);

                    var projectLicense = JsonConvert.DeserializeObject <ProjectLicense>(System.IO.File.ReadAllText(path));

                    var customGroups        = new List <CustomGroup>(projectLicense.CustomGroups);
                    var customFieldTypes    = new List <CustomFieldType>(projectLicense.CustomFieldTypes);
                    var customFields        = new List <CustomField>(projectLicense.CustomFields);
                    var customLists         = new List <CustomList>(projectLicense.CustomLists);
                    var customListData      = new List <CustomListData>(projectLicense.CustomListData);
                    var projectCustomGroups = new List <ProjectCustomGroup>(projectLicense.ProjectCustomGroups);
                    var projectCustomFields = new List <ProjectCustomField>(projectLicense.ProjectCustomFields);

                    if (string.IsNullOrEmpty(projectLicense?.ProjectCode) || !customFieldTypes.Any() || !customGroups.Any() || !customFields.Any() || !customLists.Any() || !customListData.Any() || !projectCustomGroups.Any() || !projectCustomFields.Any())
                    {
                        acResponse.Code    = -1;
                        acResponse.Message = "The provided Project setup File could not be correctly accessed. Please provide all required fields and try again later";
                        return(Json(acResponse, JsonRequestBehavior.AllowGet));
                    }

                    var processedProjectTableId = 0;

                    var projects = _projectService.Query(p => p.ProjectCode == projectLicense.ProjectCode).Select().ToList();
                    if (projects.Any())
                    {
                        var project = projects[0];
                        project.ProjectName        = projectLicense.ProjectName;
                        project.ProjectDescription = projectLicense.ProjectDescription;
                        project.ActivationCode     = projectLicense.ActivationCode;
                        project.OnlineMode         = projectLicense.OnlineMode;
                        project.LicenseExpiryDate  = projectLicense.LicenseExpiryDate;
                        _projectService.Update(project);
                        _unitOfWork.SaveChanges();
                        processedProjectTableId = project.TableId;
                    }
                    else
                    {
                        var project = new Project
                        {
                            ProjectName        = projectLicense.ProjectName,
                            ProjectDescription = projectLicense.ProjectDescription,
                            ProjectCode        = projectLicense.ProjectCode,
                            DateCreated        = DateTime.Now,
                            LicenceCode        = projectLicense.LicenceCode,
                            ActivationCode     = projectLicense.ActivationCode,
                            OnlineMode         = projectLicense.OnlineMode,
                            LicenseExpiryDate  = projectLicense.LicenseExpiryDate
                        };
                        _projectService.Insert(project);
                        processedProjectTableId = _unitOfWork.SaveChanges();
                    }

                    if (processedProjectTableId < 1)
                    {
                        acResponse.Code    = -1;
                        acResponse.Message = "Project Licensing failed. Please try again later of contact support.";
                        return(Json(acResponse, JsonRequestBehavior.AllowGet));
                    }
                    var oldFieldTypes = _customFieldTypeService.Queryable().ToList();
                    if (oldFieldTypes.Any())
                    {
                        customFieldTypes.ForEach(g =>
                        {
                            var fieldTypes = oldFieldTypes.Where(x => x.FieldTypeId == g.FieldTypeId).ToList();
                            if (fieldTypes.Any())
                            {
                                var f           = fieldTypes[0];
                                f.FieldTypeName = g.FieldTypeName;
                                _customFieldTypeService.Update(f);
                                _unitOfWork.SaveChanges();
                            }
                            else
                            {
                                _customFieldTypeService.Insert(g);
                                _unitOfWork.SaveChanges();
                            }
                        });
                    }
                    else
                    {
                        _customFieldTypeService.InsertRange(customFieldTypes);
                    }

                    var projectCustomGroupEntities = _projectCustomGroupService.Query(g => g.ProjectCode == projectLicense.ProjectCode).Select().ToList();
                    if (projectCustomGroupEntities.Any())
                    {
                        projectCustomGroups.ForEach(g =>
                        {
                            var group = projectCustomGroupEntities.Find(d => d.CustomGroupId == g.CustomGroupId);
                            if (!string.IsNullOrEmpty(@group?.CustomGroupId))
                            {
                                group.TabIndex = g.TabIndex;
                                _projectCustomGroupService.Update(group);
                                _unitOfWork.SaveChanges();
                            }
                            else
                            {
                                _projectCustomGroupService.Insert(g);
                                _unitOfWork.SaveChanges();
                            }
                        });
                    }
                    else
                    {
                        _projectCustomGroupService.InsertRange(projectCustomGroups);
                    }


                    var projectCustomFieldEntities = _projectCustomFieldService.Query(f => f.ProjectCode == projectLicense.ProjectCode).Select().ToList();
                    if (projectCustomFieldEntities.Any())
                    {
                        projectCustomFields.ForEach(g =>
                        {
                            var field = projectCustomFieldEntities.Find(d => d.CustomFieldId == g.CustomFieldId);
                            if (string.IsNullOrEmpty(field?.CustomFieldId))
                            {
                                _projectCustomFieldService.Insert(g);
                                _unitOfWork.SaveChanges();
                            }
                        });
                    }
                    else
                    {
                        _projectCustomFieldService.InsertRange(projectCustomFields);
                    }

                    var customGroupEntities = _customGroupService.Queryable().ToList();
                    if (customGroupEntities.Any())
                    {
                        customGroups.ForEach(g =>
                        {
                            var cGroup = customGroupEntities.Find(d => d.CustomGroupId == g.CustomGroupId);
                            if (!string.IsNullOrEmpty(cGroup?.CustomGroupId))
                            {
                                cGroup.GroupName = g.GroupName;
                                cGroup.TabIndex  = g.TabIndex;
                                _customGroupService.Update(cGroup);
                                _unitOfWork.SaveChanges();
                            }
                            else
                            {
                                g.TableId = 0;
                                _customGroupService.Insert(g);
                                _unitOfWork.SaveChanges();
                            }
                        });
                    }
                    else
                    {
                        _customGroupService.InsertRange(customGroups);
                    }

                    var customFieldEntities = _customFieldService.Queryable().ToList();
                    if (customFieldEntities.Any())
                    {
                        customFields.ForEach(g =>
                        {
                            var cField = customFieldEntities.Find(d => d.CustomFieldId == g.CustomFieldId);
                            if (!string.IsNullOrEmpty(cField?.CustomFieldId))
                            {
                                cField.CustomFieldName = g.CustomFieldName;
                                cField.TabIndex        = g.TabIndex;
                                cField.CustomFieldSize = g.CustomFieldSize;
                                cField.CustomListId    = g.CustomListId;
                                cField.ParentFieldId   = g.ParentFieldId;
                                cField.Required        = g.Required;
                                cField.FieldTypeId     = g.FieldTypeId;
                                cField.CustomGroupId   = g.CustomGroupId;
                                _customFieldService.Update(cField);
                                _unitOfWork.SaveChanges();
                            }
                            else
                            {
                                g.TableId = 0;
                                _customFieldService.Insert(g);
                                _unitOfWork.SaveChanges();
                            }
                        });
                    }
                    else
                    {
                        _customFieldService.InsertRange(customFields);
                    }

                    var customListEntities = _customListService.Queryable().ToList();
                    if (customListEntities.Any())
                    {
                        customLists.ForEach(g =>
                        {
                            var cList = customListEntities.Find(d => d.CustomListId == g.CustomListId);
                            if (!string.IsNullOrEmpty(cList?.CustomListId))
                            {
                                cList.CustomListName = g.CustomListName;
                                cList.CustomListName = g.CustomListName;
                                cList.ParentListId   = g.ParentListId;
                                _customListService.Update(cList);
                                _unitOfWork.SaveChanges();
                            }
                            else
                            {
                                g.TableId = 0;
                                _customListService.Insert(g);
                                _unitOfWork.SaveChanges();
                            }
                        });
                    }
                    else
                    {
                        _customListService.InsertRange(customLists);
                    }

                    var customListDataEntities = _customListDataService.Queryable().ToList();
                    if (customListDataEntities.Any())
                    {
                        customListData.ForEach(g =>
                        {
                            var cListData = customListDataEntities.Find(d => d.CustomListId == g.CustomListId);
                            if (!string.IsNullOrEmpty(cListData?.CustomListId))
                            {
                                cListData.CustomListId = g.CustomListId;
                                cListData.ListDataName = g.ListDataName;
                                cListData.ParentNodeId = g.ParentNodeId;
                                _customListDataService.Update(cListData);
                                _unitOfWork.SaveChanges();
                            }
                            else
                            {
                                g.TableId = 0;
                                _customListDataService.Insert(g);
                                _unitOfWork.SaveChanges();
                            }
                        });
                    }
                    else
                    {
                        _customListDataService.InsertRange(customListData);
                    }

                    _unitOfWork.SaveChanges();

                    //Necesary to initialise and construct the Identity Tables in the created database
                    var usermanager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();
                    usermanager?.FindByEmail("*****@*****.**");

                    acResponse.Code    = 5;
                    acResponse.Message = "The Project License was successfully installed.";
                    return(Json(acResponse, JsonRequestBehavior.AllowGet));
                }
                acResponse.Code    = -1;
                acResponse.Message = "The provided Site License File could not be correctly accessed. Please provide all required fields and try again later";
                return(Json(acResponse, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                ErrorLogger.LogError(e.StackTrace, e.Source, e.Message);
                acResponse.Code    = -1;
                acResponse.Message = e.Message;
                return(Json(acResponse, JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult GenerateLicense(string projectCode)
        {
            var acResponse = new ActivityResponse();

            try
            {
                if (string.IsNullOrEmpty(projectCode))
                {
                    acResponse.Code    = -1;
                    acResponse.Message = "The selected Project information could not be accessed. Please try again later";
                    return(Json(acResponse, JsonRequestBehavior.AllowGet));
                }

                var projects = _projectService.Query(p => p.ProjectCode == projectCode).Select().ToList();

                if (!projects.Any())
                {
                    acResponse.Code    = -1;
                    acResponse.Message = "The selected Project information could not be accessed. Please try again later";
                    return(Json(acResponse, JsonRequestBehavior.AllowGet));
                }

                var fieldTypes = _customFieldTypeService.Queryable().ToList();
                if (!fieldTypes.Any())
                {
                    acResponse.Code    = -1;
                    acResponse.Message = "Project Custom Fields could not be retrieved. Please ensure all required setups are handled appropriately before proceeding.";
                    return(Json(acResponse, JsonRequestBehavior.AllowGet));
                }

                var projectCustomGroups = _projectCustomGroupService.Query(g => g.ProjectCode == projectCode).Select().ToList();
                if (!projectCustomGroups.Any())
                {
                    acResponse.Code    = -1;
                    acResponse.Message = "Project Custom Groups could not be retrieved. Please ensure all required setups are handled appropriately before proceeding.";
                    return(Json(acResponse, JsonRequestBehavior.AllowGet));
                }

                var projectCustomFields = _projectCustomFieldService.Query(g => g.ProjectCode == projectCode).Select().ToList();
                if (!projectCustomFields.Any())
                {
                    acResponse.Code    = -1;
                    acResponse.Message = "Project Custom Fields could not be retrieved. Please ensure all required setups are handled appropriately before proceeding.";
                    return(Json(acResponse, JsonRequestBehavior.AllowGet));
                }

                var project = projects[0];

                var projectLicense = new ProjectLicense
                {
                    ProjectName         = project.ProjectName,
                    ProjectDescription  = project.ProjectDescription,
                    ProjectCode         = project.ProjectCode,
                    LicenseExpiryDate   = project.LicenseExpiryDate,
                    DateCreated         = project.DateCreated,
                    ProjectCustomGroups = projectCustomGroups.ToArray(),
                    ProjectCustomFields = projectCustomFields.ToArray()
                };

                var customGroups   = new List <CustomGroup>();
                var customFields   = new List <CustomField>();
                var customList     = new List <CustomList>();
                var customListData = new List <CustomListData>();

                projectCustomGroups.ForEach(c =>
                {
                    var groups = _customGroupService.Query(l => l.CustomGroupId == c.CustomGroupId).Select().ToList();

                    if (groups.Any())
                    {
                        if (!customGroups.Exists(g => g.CustomGroupId == groups[0].CustomGroupId))
                        {
                            customGroups.Add(groups[0]);
                        }
                    }
                });

                projectCustomFields.ForEach(c =>
                {
                    var fields = _customFieldService.Query(l => l.CustomFieldId == c.CustomFieldId).Select().ToList();

                    if (!fields.Any())
                    {
                        return;
                    }

                    var customField = fields[0];
                    var customLists = _customListService.Query(l => l.CustomListId == customField.CustomListId).Select().ToList();
                    var cGroups     = _customGroupService.Query(l => l.CustomGroupId == customField.CustomGroupId).Select().ToList();

                    if (cGroups.Any())
                    {
                        if (!customGroups.Exists(a => a.CustomGroupId == cGroups[0].CustomGroupId))
                        {
                            customGroups.Add(cGroups[0]);
                        }

                        if (customLists.Any())
                        {
                            var cListItem = customLists[0];

                            if (!customList.Exists(a => a.CustomListId == cListItem.CustomListId))
                            {
                                customList.Add(cListItem);
                                var list = _customListDataService.Query(d => d.CustomListId == cListItem.CustomListId).Select().ToList();
                                if (list.Any())
                                {
                                    list.ForEach(l =>
                                    {
                                        if (!customListData.Exists(a => a.CustomListDataId == l.CustomListDataId))
                                        {
                                            customListData.Add(l);
                                        }
                                    });
                                }
                            }
                        }

                        customFields.Add(customField);
                    }
                });

                projectLicense.CustomFields     = customFields.ToArray();
                projectLicense.CustomGroups     = customGroups.ToArray();
                projectLicense.CustomLists      = customList.ToArray();
                projectLicense.CustomListData   = customListData.ToArray();
                projectLicense.CustomFieldTypes = fieldTypes.ToArray();

                const string folderPath = "~/TempProjectSetUp";

                var filePath = Server.MapPath(folderPath + "/" + projectLicense.ProjectName.Replace(" ", "-") + ".json");

                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }

                System.IO.File.WriteAllText(filePath, JsonConvert.SerializeObject(projectLicense));

                acResponse.Code         = 5;
                acResponse.DownloadLink = GenericHelpers.MapPath(filePath);
                acResponse.Message      = "Project License was successfully generated.";
                return(Json(acResponse, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                ErrorLogger.LogError(e.StackTrace, e.Source, e.Message);
                acResponse.Code    = -1;
                acResponse.Message = "The selected Project information could not be accessed. Please try again later";
                return(Json(acResponse, JsonRequestBehavior.AllowGet));
            }
        }