Beispiel #1
0
        /// <summary>
        /// Get table content from table settings.
        /// </summary>
        /// <param name="settings">Table settings.</param>
        /// <param name="pageContext">Page context.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        /// <returns>Element content.</returns>
        public IElementContent GetContent(IElementSettings settings, IPageContext pageContext, IUnitOfWork unitOfWork = null)
        {
            TableSettings tableSettings = (TableSettings)settings;
            TableContent  content       = new TableContent {
                PartialViewName = "Table", Rows = new List <List <string> >()
            };

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(tableSettings.Rows)))
            {
                using (StreamReader sr = new StreamReader(ms))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        List <string> values = _stringService.GetCsvValues(line);
                        if (tableSettings.ShowHeaders && content.Headers == null)
                        {
                            content.Headers = values;
                        }
                        else
                        {
                            content.Rows.Add(values);
                        }
                    }
                }
            }
            return(content);
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves form for table administration.
        /// </summary>
        /// <param name="context">Form context.</param>
        /// <returns>Form.</returns>
        public Form GetForm(string context)
        {
            // Check permissions
            _authorizationService.AuthorizeUserForFunction(Functions.UpdatePageElements);

            // Get page and element identifiers
            string[] parts     = context.Split('|');
            long     pageId    = Convert.ToInt64(parts[0]);
            long     elementId = Convert.ToInt64(parts[1]);

            // Get current settings
            Guid          elementTypeId = FormId;
            TableSettings settings      = (TableSettings)New(_authenticationService.TenantId);

            settings.ElementId = elementId;
            Read(settings);

            // Construct form
            Form form = new Form {
                Fields = new Dictionary <string, IFormField>(), Id = FormId.ToString(), Context = context
            };

            form.Fields.Add("displayName", new TextField
            {
                Name                  = "displayName",
                Label                 = ElementResource.TableDisplayNameLabel,
                MaxLength             = TableLengths.DisplayNameMaxLength,
                MaxLengthErrorMessage = string.Format(ElementResource.TableDisplayNameMaxLengthMessage, "displayName", TableLengths.DisplayNameMaxLength),
                Value                 = settings.DisplayName
            });
            form.Fields.Add("preamble", new MultiLineTextField
            {
                Name  = "preamble",
                Label = ElementResource.TablePreambleLabel,
                Value = settings.Preamble,
                Rows  = 2
            });
            form.Fields.Add("showHeaders", new BooleanField
            {
                Name  = "showHeaders",
                Label = ElementResource.TableShowHeadersLabel,
                Value = settings.ShowHeaders
            });
            form.Fields.Add("rows", new MultiLineTextField
            {
                Name  = "rows",
                Label = ElementResource.TableRowsLabel,
                Value = settings.Rows,
                Rows  = 8
            });
            form.SubmitLabel = ElementResource.TableButtonLabel;

            // Return result
            return(form);
        }
        /// <summary>
        /// Creates a new table element.
        /// </summary>
        /// <param name="settings">Table settings.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        public void Create(TableSettings settings, IUnitOfWork unitOfWork = null)
        {
            IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork);

            try
            {
                dbm.SetSQL(_sqlManager.GetSql("Sql.CreateTable.sql"));
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.AddParameter("@DisplayName", FieldType.NVarChar, TableLengths.DisplayNameMaxLength, settings.DisplayName);
                dbm.AddParameter("@Preamble", FieldType.NVarChar, -1, settings.Preamble);
                dbm.AddParameter("@ShowHeaders", FieldType.Bit, settings.ShowHeaders);
                dbm.AddParameter("@Rows", FieldType.NVarChar, -1, settings.Rows);
                dbm.ExecuteNonQuery();
            }
            finally
            {
                if (unitOfWork == null)
                {
                    dbm.Dispose();
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Submits form to update table element.
        /// </summary>
        /// <param name="form">View model containing form definition and submitted values.</param>
        /// <returns>Result of form post.</returns>
        public FormResult PostForm(Form form)
        {
            try
            {
                // Check permissions
                _authorizationService.AuthorizeUserForFunction(Functions.UpdatePageElements);

                // Get page and element identifiers
                string[] parts     = form.Context.Split('|');
                long     pageId    = Convert.ToInt64(parts[0]);
                long     elementId = Convert.ToInt64(parts[1]);

                // Get updated settings
                TableSettings settings = (TableSettings)New(_authenticationService.TenantId);
                settings.ElementId   = elementId;
                settings.DisplayName = string.IsNullOrWhiteSpace(((TextField)form.Fields["displayName"]).Value) ? string.Empty : ((TextField)form.Fields["displayName"]).Value;
                settings.Preamble    = string.IsNullOrWhiteSpace(((MultiLineTextField)form.Fields["preamble"]).Value) ? string.Empty : ((MultiLineTextField)form.Fields["preamble"]).Value;
                settings.ShowHeaders = ((BooleanField)form.Fields["showHeaders"]).Value;
                settings.Rows        = string.IsNullOrWhiteSpace(((MultiLineTextField)form.Fields["rows"]).Value) ? string.Empty : ((MultiLineTextField)form.Fields["rows"]).Value;

                // Perform the update
                Update(settings);

                // Return form result with no errors
                return(_formHelperService.GetFormResult());
            }
            catch (ValidationErrorException ex)
            {
                // Return form result containing errors
                return(_formHelperService.GetFormResultWithValidationErrors(ex.Errors));
            }
            catch (Exception)
            {
                // Return form result containing unexpected error message
                return(_formHelperService.GetFormResultWithErrorMessage(ApplicationResource.UnexpectedErrorMessage));
            }
        }
        /// <summary>
        /// Reads a table element.
        /// </summary>
        /// <param name="settings">Table settings.</param>
        /// <param name="unitOfWork">Unit of work.</param>
        public void Read(TableSettings settings, IUnitOfWork unitOfWork = null)
        {
            IDatabaseManager dbm = _databaseManagerFactory.GetDatabaseManager(unitOfWork);

            try
            {
                dbm.SetSQL(_sqlManager.GetSql("Sql.ReadTable.sql"));
                dbm.AddParameter("@TenantId", FieldType.BigInt, settings.TenantId);
                dbm.AddParameter("@ElementId", FieldType.BigInt, settings.ElementId);
                dbm.ExecuteReader();
                dbm.Read();
                settings.DisplayName = (string)dbm.DataReaderValue("DisplayName");
                settings.Preamble    = (string)dbm.DataReaderValue("Preamble");
                settings.ShowHeaders = (bool)dbm.DataReaderValue("ShowHeaders");
                settings.Rows        = (string)dbm.DataReaderValue("Rows");
            }
            finally
            {
                if (unitOfWork == null)
                {
                    dbm.Dispose();
                }
            }
        }