public void CreateProviderTemplatedLicense(int appId, int schemaId, BuildLicenseModel model, LoggedInUserDetails user)
        {
            // Check access
            _security.CheckBasicAccess(user);

            // Check whether organisation is active
            if (!user.Organization.IsActive)
            {
                throw new BaseException(orgIsInactiveError);
            }

            // Get published license template
            var publishedTemplate = _licenseTemplates.FirstOrDefault(i => i.Status == (int)TemplateStatus.Active);

            if (publishedTemplate == null)
            {
                throw new BaseException("Published template not found");
            }

            // Create provider license
            SaveProviderLicense(publishedTemplate.ID, schemaId, appId, model.Sections, user);
        }
예제 #2
0
        public ActionResult Create(int appId, int schemaId, BuildLicenseModel model)
        {
            // Check whether posted data has selected sections
            if (model == null || !model.Sections.Any())
            {
                throw new BaseException("Invalid data.");
            }

            // Check whether it's a request for license preview
            if (Request.Form["preview"] != null)
            {
                // Get file result
                var result = _licenseFiles.GetTemplatedLicenseForPreview(model.Sections, LoggedInUser.Organization.ID, schemaId, LoggedInUser);

                // Setup stream
                var stream = new MemoryStream(result.Content);

                // Return file
                return(File(stream, result.MimeType, result.FileName));
            }

            // Setup default redirect url
            var redirectUrl = Url.Action("Index", "Licenses", new { appId, schemaId });

            // Check whether processing request for provider
            if (!model.IsProvider)
            {
                throw new BaseException("Only provider can create licenses");
            }

            _licenseService.CreateProviderTemplatedLicense(appId, schemaId, model, LoggedInUser);

            // Setup status message
            Toastr.Success("License for schema was successfully created.");

            // Return result
            return(Redirect(redirectUrl));
        }
        public BuildLicenseModel SetupBuildLicenseModel(int applicationId, int schemaId, LoggedInUserDetails user)
        {
            // Check whether user has appropriate access
            var application = _security.CheckAccessToApplication(user, applicationId);

            // Get published provider licenses
            var providerLicenses = _service.Where(i => i.Status == (int)PublishStatus.Published);

            // Filter provider licenses
            if (!application.IsProvider)
            {
                providerLicenses = providerLicenses.Where(i => i.ProviderEndpointID != 0).ToList();
            }

            // Check whether any provider has published license to match
            var isPublishedLicensePresent = providerLicenses.Any();

            // Setup schema name
            var schemaName = _dataSchemaService.FirstOrDefault(i => i.ID == schemaId).Name;

            // Setup basic model details
            var result = new BuildLicenseModel()
            {
                SchemaName = schemaName,
                IsProvider = application.IsProvider,
                IsPublishedProviderLicensePresent = isPublishedLicensePresent,
                Providers = new List <DataProvider>()
            };

            // Setup data providers with custom licenses for consumer
            if (!application.IsProvider)
            {
                var customProviderLicenses = providerLicenses.Where(i => i.CustomLicenseID != null);
                result.Providers = GetCustomProviders(customProviderLicenses);
            }

            // Get published clause templates
            var publishedClauseTemplates = _clauseTemplateService.Where(i => i.Status == (int)TemplateStatus.Active).ToList();

            // Select clause template identifiers
            var publishedTemplateIds = publishedClauseTemplates.Select(i => i.LicenseClauseID).Distinct();

            // Get license clauses for each template
            var licenseClauses = publishedTemplateIds.Select(i => _clauseService.FirstOrDefault(p => p.ID == i));

            // Get active section identifiers
            var sectionIdentifiers = licenseClauses.Select(i => i.LicenseSectionID).Distinct();

            // Get sections
            var sections = sectionIdentifiers.Select(i => _sectionService.FirstOrDefault(p => p.ID == i)).ToList();

            // Setup result model
            var sectionModels = new List <SectionsWithClauses>();

            // Setup sections
            foreach (var section in sections)
            {
                section.Title = section.Title.Replace("_additional", " (Optional)");
                sectionModels.Add(new SectionsWithClauses
                {
                    Section       = section.ToModel(),
                    ApplicationId = applicationId,
                    IsForProvider = application.IsProvider,
                    Clauses       = new List <ClauseModel>()
                });
            }

            // Return error if model does not have any items
            if (!sectionModels.Any())
            {
                throw new BaseException("There are no sections in license");
            }

            // Setup clauses for each section
            foreach (var item in sectionModels)
            {
                item.Clauses = GetClauseModelsForSection(publishedClauseTemplates.AsReadOnly(), item.Section.ID);
            }

            // Setup sections
            result.Sections = sectionModels;

            // Return result
            return(result);
        }
        public static MvcHtmlString ClauseForProvider <TModel, TValue>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TValue> > expression, BuildLicenseModel model, int i, int j, object htmlAttributes = null)
        {
            var templateType            = (ClauseType)model.Sections[i].Clauses[j].Type;
            var isOptional              = model.Sections[i].Section.Title.Contains("Optional");
            var htmlAttributeDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

            // Build radio button for clause
            var radioBtn = htmlHelper.RadioButtonFor(m => model.Sections[i].SelectedClause, model.Sections[i].Clauses[j].ClauseTemplateId, htmlAttributeDictionary);

            // Display optional as checkbox
            if (isOptional)
            {
                //Disable required: htmlAttributes = new { @data_val = true };
                radioBtn = htmlHelper.CheckBoxFor(m => model.Sections[i].Clauses[j].IsSelectedByConsumer, htmlAttributeDictionary);
            }
            // Get collection name to build element's id and name
            var clauseCollection = GetPropertyName(() => model.Sections[i].Clauses);

            switch (templateType)
            {
            case ClauseType.Text:
            {
                var clauseText = htmlHelper.DisplayFor(p => model.Sections[i].Clauses[j].LegalText);
                var res        = $"{radioBtn} {clauseText}";
                return(new MvcHtmlString(res));
            }

            case ClauseType.Input:
            {
                var inputModel = model.Sections[i].Clauses[j];
                var inputValue = GetPropertyName(() => inputModel.InputValue);

                // Build input
                var input   = new TagBuilder("input");
                var inputId = $"Sections[{i}].{clauseCollection}[{j}].{inputValue}";
                input.MergeAttributes(new RouteValueDictionary(new { name = inputId, id = inputId }));
                var textBox = new MvcHtmlString(input.ToString(TagRenderMode.Normal));

                // Get clause text to display with input
                var indexOfOpenBracket = model.Sections[i].Clauses[j].LegalText.IndexOf('{');
                var clauseText         = htmlHelper.DisplayFor(p => inputModel.LegalText);

                // Remove brackets from text
                var textWithInput = new string(clauseText.ToString().Where(p => p != '{' && p != '}').ToArray());

                // Insert input on a brackets location
                textWithInput = textWithInput.Insert(indexOfOpenBracket, $" {textBox} ");

                return(new MvcHtmlString($"{radioBtn} {textWithInput}"));
            }

            case ClauseType.InputAndDropDown:
            {
                var inputDropDownModel = model.Sections[i].Clauses[j];
                var inputValue         = GetPropertyName(() => inputDropDownModel.InputValue);
                var dropDownValue      = GetPropertyName(() => inputDropDownModel.SelectedItem);

                // Build input
                var input      = new TagBuilder("input");
                var inputId    = $"Sections[{i}].{clauseCollection}[{j}].{inputValue}";
                var dropDownId = $"Sections[{i}].{clauseCollection}[{j}].{dropDownValue}";
                input.MergeAttributes(new RouteValueDictionary(new { name = inputId, id = inputId }));
                var textBox = new MvcHtmlString(input.ToString(TagRenderMode.Normal));

                // Build DropDown
                var dropDown = htmlHelper.DropDownList(dropDownId, inputDropDownModel.Source
                                                       .Select(item => new SelectListItem {
                        Text = item.Text, Value = item.Value, Selected = item.IsSelected
                    })
                                                       .ToList(), new { id = dropDownId, name = dropDownId });

                // get location of first '{' - input, and second '{','}' - start and end of drop down items
                var inputLocation = inputDropDownModel.LegalText.IndexOf('{');
                var dropDownStart = inputDropDownModel.LegalText.LastIndexOf('{');
                var dropDownEnd   = inputDropDownModel.LegalText.LastIndexOf('}');

                // Get clause text to display
                var clauseText = htmlHelper.DisplayFor(p => inputDropDownModel.LegalText);

                // remove dropdown list items from text
                var textWithInputAndDropDown = clauseText.ToString().Remove(dropDownStart, dropDownEnd - dropDownStart);

                // Remove brackets from text
                textWithInputAndDropDown = new string(textWithInputAndDropDown.Where(p => p != '{' && p != '}').ToArray());

                // Insert input element into input location
                textWithInputAndDropDown = textWithInputAndDropDown.Insert(inputLocation, $" {textBox} ");

                // Insert DropDown into drop down location(+ move as input inserted)
                textWithInputAndDropDown = textWithInputAndDropDown.Insert(dropDownStart + textBox.ToString().Length, $" {dropDown} ");

                return(new MvcHtmlString($"{radioBtn} {textWithInputAndDropDown}"));
            }

            default:
                throw new Exception("Unknown clause type.");
            }
        }