Example #1
0
        public void CreateModule(IModule _dbModule, DynamicModuleViewModel _moduleData)
        {
            var row = new Row()
            {
                Module = (Module)_dbModule
            };

            foreach (DynamicModuleField field in _moduleData.Fields)
            {
                var converter = FieldValueConversion.GetConverter(field.DataType);
                var rowField  = new Cell {
                    Field = _dbModule.Fields.FirstOrDefault(f => f.FieldName == field.PropertyName),
                    Data  = converter.Serialize(field, new ConversionContext {
                        Cell = null
                    })
                };

                row.Cells.Add(rowField);
            }

            try {
                ModRepository.AddModuleEntry(_dbModule.Id, row);
            }
            catch (Exception e) {
                throw new CreateModuleFailedException("Ocorreu um erro ao incluir o seu registro. Tente novamente mais tarde.", e);
            }
        }
Example #2
0
        public void TestGetDefaultConverter()
        {
            var converter = FieldValueConversion.GetConverter(typeof(HtmlText));

            Assert.IsNotNull(converter);
            Assert.IsInstanceOfType(converter, typeof(DefaultFieldValueConverter));
        }
Example #3
0
        public void TestGetCustomConverter()
        {
            var converter = FieldValueConversion.GetConverter(typeof(ModuleReference));

            Assert.IsNotNull(converter);
            Assert.IsInstanceOfType(converter, typeof(ModuleReferenceConverter));
        }
Example #4
0
        public void Test_Password_Should_Change_Value()
        {
            var samplePassord = "some secret data";
            var data          = new Password {
                Value = samplePassord
            };
            var converter = FieldValueConversion.GetConverter(data.GetType());
            var field     = new DynamicModuleField {
                Data = data
            };
            var rf = new Cell()
            {
                Data = converter.Serialize(field, new ConversionContext {
                    Cell = new Cell {
                        Data = "<Password>9b6ca654c29750544f6c4fba45a7c0b7f5fdf0f2</Password>"
                    }
                })
            };
            var retData = converter.Deserialize(rf);

            Assert.IsNotNull(retData);
            Assert.IsInstanceOfType(retData, typeof(Password));

            var pwdResult = retData as Password;

            Assert.AreNotEqual(pwdResult.Value, samplePassord);

            var hash     = new Hash(Hash.Provider.SHA1);
            var hashData = hash.Calculate(new Data(samplePassord), new Data("|)0ver3ncrypt10n_k3y")).Base64;

            Assert.AreEqual(pwdResult.Value, hashData);
        }
Example #5
0
        public void Test_Password_Should_Keep_Current_Value()
        {
            var samplePassord = "some secret data";
            var data          = new Password {
                Value = Password.BogusText
            };
            var converter = FieldValueConversion.GetConverter(data.GetType());
            var field     = new DynamicModuleField {
                Data = data
            };
            var rf = new Cell()
            {
                Data = converter.Serialize(field, new ConversionContext {
                    Cell = new Cell {
                        Data = "<Password>" + samplePassord + "</Password>"
                    }
                })
            };
            var retData = converter.Deserialize(rf);

            Assert.IsNotNull(retData);
            Assert.IsInstanceOfType(retData, typeof(Password));

            var pwdResult = retData as Password;

            Assert.AreEqual(pwdResult.Value, samplePassord);
        }
Example #6
0
        public void SetModuleData(IModule _dbModule, int?id, DynamicModuleViewModel _moduleData, bool partialData = false)
        {
            if (_dbModule.ModuleType == (int)ModuleType.SingleEntry &&
                _dbModule.Rows.FirstOrDefault() == null)
            {
                // We have a single entry module and its row hasn't been created yet.
                // No big deal, we'll do it.
                CreateModule(_dbModule as Module, _moduleData);
            }

            var rowToEdit = (_dbModule.ModuleType == (int)ModuleType.Dynamic)
                                ? _dbModule.Rows.FirstOrDefault(row => row.ID == id)
                                : _dbModule.Rows.FirstOrDefault();

            if (rowToEdit == null)
            {
                throw new ModuleRowNotFoundException("Registro não encontrado.");
            }

            if (!partialData)
            {
                // Update all the row fields, i.e. set null to missing form fields.
                foreach (Cell rowField in rowToEdit.Cells)
                {
                    var formField = _moduleData.Fields.FirstOrDefault(rf => rf.PropertyName == rowField.Field.FieldName);
                    var converter = FieldValueConversion.GetConverter(formField.DataType);

                    rowField.Data = converter.Serialize(formField, new ConversionContext {
                        Cell       = rowField,
                        Field      = rowField.Field,
                        Module     = _dbModule,
                        Repository = ModRepository
                    });
                }
            }
            else
            {
                // Update only the row fields that were received as form fields. Do not touch others.
                foreach (var formField in _moduleData.Fields)
                {
                    var rowField  = rowToEdit.Cells.FirstOrDefault(rf => rf.Field.FieldName == formField.PropertyName);
                    var converter = FieldValueConversion.GetConverter(formField.DataType);

                    rowField.Data = converter.Serialize(formField, new ConversionContext {
                        Cell       = rowField,
                        Field      = rowField.Field,
                        Module     = _dbModule,
                        Repository = ModRepository
                    });
                }
            }

            ModRepository.Save();
        }
Example #7
0
        /*[TestMethod]
         * [Ignore]
         * public void Test_Convert_Data_To_Xml() {
         *      using (var db = new DoverEntities(ConnectionString)) {
         *              foreach (var rf in db.Cell
         *                      .Include("Field.FieldDataType")
         *                      .Include("Field.Metadata")
         *                      .Include("Field.Module")) {
         *                      Type dataType = Type.GetType(rf.Field.FieldDataType.Name);
         *                      if (dataType == null) {
         *                              dataType = Type.GetType(rf.Field.FieldDataType.Name + ", Com.Dover., Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
         *                      }
         *                      var converter = FieldValueConversion.GetConverter(dataType);
         *                      var cc = new ConversionContext {
         *                              Repository = new ModuleRepository(new EntityConnection(ConnectionString)),
         *                              Field = rf.Field,
         *                              Module = rf.Field.Module
         *                      };
         *                      DefaultFieldValueConverter.SerializationMethod = FieldSerializationMethod.Binary;
         *                      object obj = converter.Deserialize(rf, cc);
         *                      DefaultFieldValueConverter.SerializationMethod = FieldSerializationMethod.Xml;
         *                      rf.Data = converter.Serialize(new DynamicModuleField() {
         *                              Data = obj,
         *                              DataType = dataType
         *                      }, cc);
         *              }
         *              db.SaveChanges();
         *      }
         * }
         *
         * [TestMethod]
         * [Ignore]
         * public void Test_Migrate_ModuleReference() {
         *      var dataType = Type.GetType("Com.Dover.Models.DataTypes.ModuleReference, Com.Dover., Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
         *      var converter = FieldValueConversion.GetConverter(dataType);
         *
         *      using (var db = new DoverEntities(ConnectionString)) {
         *              foreach (var rf in db.Cell
         *                      .Include("Field.FieldDataType")
         *                      .Include("Field.Metadata")
         *                      .Include("Field.Module")) {
         *
         *                      if (rf.Field.FieldDataType.Name == "Com.Dover.Models.DataTypes.ModuleReference") {
         *                              var cc = new ConversionContext {
         *                                      Repository = new ModuleRepository(new EntityConnection(ConnectionString)),
         *                                      Field = rf.Field,
         *                                      Module = rf.Field.Module
         *                              };
         *                              if (rf.Data != null) {
         *                                      var modRef = converter.Deserialize(rf, cc) as ModuleReference;
         *
         *                                      Assert.IsNotNull(modRef);
         *
         *                                      var row = db.Row.FirstOrDefault(r => r.Cells.Any(rf1 => rf1.ID == modRef.Id));
         *
         *                                      Assert.IsNotNull(row);
         *
         *                                      modRef.Id = row.ID;
         *
         *                                      rf.Data = converter.Serialize(new DynamicModuleField { Data = modRef }, cc);
         *                              }
         *                      }
         *              }
         *              db.SaveChanges();
         *      }
         * }
         *
         * [TestMethod]
         * [Ignore]
         * public void Migrate_Binary_To_String() {
         *
         *      using (var db = new DoverEntities(ConnectionString)) {
         *              foreach (var record in db.GetRowFieldBytes()) {
         *                      var bytes = record.Data;
         *                      if (bytes == null || bytes.Length == 0) {
         *                              continue;
         *                      }
         *
         *                      var id = record.ID;
         *                      string data = null;
         *                      if ((int)bytes[bytes.Length - 1] == 0) {
         *                              var newArray = bytes.TakeWhile((b, i) => i < bytes.Length - 1);
         *                              data = Encoding.UTF8.GetString(newArray.ToArray());
         *                      }
         *                      else {
         *                              data = Encoding.UTF8.GetString(bytes);
         *                      }
         *
         *                      var rowField = db.Cell.FirstOrDefault(rf => rf.ID == id);
         *
         *                      Assert.IsNotNull(rowField);
         *
         *                      rowField.Data = data;
         *              }
         *              db.SaveChanges();
         *      }
         * }*/

        private static object SerializeAndDeserialize(object data)
        {
            var converter = FieldValueConversion.GetConverter(data.GetType());
            var field     = new DynamicModuleField {
                Data = data
            };
            var rf = new Cell()
            {
                Data = converter.Serialize(field)
            };

            return(converter.Deserialize(rf));
        }
Example #8
0
        private ActionResult ViewModule(Module _module, int?id, bool creating = false)
        {
            if (id != null &&
                _module.Rows.FirstOrDefault(row => row.ID == id) == null)
            {
                TempData["Message"] = "Registro não encontrado.";
                return(this.RedirectToAction("List"));
            }

            DynamicModuleViewModel modelRow = new DynamicModuleViewModel()
            {
                ID          = id ?? -1,
                ModuleName  = _module.ModuleName,
                DisplayName = _module.DisplayName
            };

            foreach (Field field in _module.Fields)
            {
                Cell rowField = null;

                if (!creating)
                {
                    rowField = (id != null)
                                                ? field.Cells.FirstOrDefault(rf => rf.Row.ID == id)
                                                : field.Cells.FirstOrDefault();
                }

                var fieldDataTpe = Type.GetType(field.FieldDataType.Name);
                var converter    = FieldValueConversion.GetConverter(fieldDataTpe);
                var modelField   = new DynamicModuleField()
                {
                    DisplayName  = field.DisplayName,
                    PropertyName = field.FieldName,
                    IsReadOnly   = field.IsReadOnly,
                    IsRequired   = field.IsRequired,
                    DataType     = fieldDataTpe
                };

                modelField.Data = converter.Deserialize(
                    rowField,
                    new ConversionContext {
                    Field      = field,
                    Module     = _module,
                    Repository = ModRepository
                });

                modelRow.Fields.Add(modelField);
            }

            return(View(modelRow));
        }
Example #9
0
        public ActionResult List(object module)
        {
            var modToList = module as Module;

            ViewData["ModuleName"]  = modToList.ModuleName;
            ViewData["DisplayName"] = modToList.DisplayName;
            ViewData["Fields"]      = modToList.Fields.Where(f => f.ShowInListMode).Select(f => f.DisplayName);

            List <DynamicModuleViewModel> viewModel = new List <DynamicModuleViewModel>();

            foreach (Row _row in modToList.Rows.OrderBy(r => r.SortIndex))
            {
                DynamicModuleViewModel modelRow = new DynamicModuleViewModel()
                {
                    ID = _row.ID
                };

                foreach (Field field in modToList.Fields.Where(f => f.ShowInListMode))
                {
                    var rowField     = field.Cells.FirstOrDefault(rf => rf.Row.ID == _row.ID);
                    var fieldDataTpe = Type.GetType(field.FieldDataType.Name);
                    var converter    = FieldValueConversion.GetConverter(fieldDataTpe);

                    var modelField = new DynamicModuleField()
                    {
                        DisplayName  = field.DisplayName,
                        PropertyName = field.FieldName,
                        IsReadOnly   = field.IsReadOnly,
                        IsRequired   = field.IsRequired,
                        DataType     = fieldDataTpe
                    };

                    modelField.Data = converter.Deserialize(
                        rowField,
                        new ConversionContext {
                        Field      = field,
                        Module     = modToList,
                        Repository = ModRepository
                    });

                    modelRow.Fields.Add(modelField);
                }

                viewModel.Add(modelRow);
            }

            return(View(viewModel));
        }
Example #10
0
        public void Test_Deserialize_ModuleReference()
        {
            // Arrange
            var fakeRepo = new FakeModuleRepository();
            var rowField = fakeRepo.GetRandomCell();

            rowField.Field.Metadata.Add(new FieldMetadata {
                Key   = ModuleRepository.ModuleReferenceMetadataKey,
                Value = "\"" + rowField.Field.Module.Id + "\""
            });

            var value = new ModuleReference()
            {
                Id = rowField.ID
            };
            var converter = FieldValueConversion.GetConverter(value.GetType());
            var field     = new DynamicModuleField {
                Data = value
            };
            var rf = new Cell()
            {
                Data = converter.Serialize(field)
            };

            var retData = converter.Deserialize(rf, new ConversionContext {
                Repository = fakeRepo,
                Field      = rowField.Field,
                Module     = rowField.Field.Module
            });

            // Assert
            Assert.IsNotNull(retData);
            Assert.IsInstanceOfType(retData, typeof(ModuleReference));

            ModuleReference result = (ModuleReference)retData;

            Assert.AreEqual(rowField.Row.ID, result.Id);
        }
Example #11
0
        /// <summary>
        /// Returns module records based on the provided module module
        /// and filtering parameters.
        /// </summary>
        /// <param name="module">The module to be queried</param>
        /// <param name="id">Optional row id for filtering</param>
        /// <returns>The matching rows in the specified module</returns>
        public DynamicModuleApiResult GetModuleData(IModule module, int?id, NameValueCollection _filters)
        {
            if (module == null)
            {
                return(null);
            }

            var moduleData = new DynamicModuleApiResult()
            {
                ModuleName = module.ModuleName,
                ModuleType = module.ModuleType,
                ModuleId   = module.Id
            };

            var rows         = module.Rows;
            var filteredRows = (id != null) ? rows.Where(r => r.ID == id) : rows.AsEnumerable();

            // Search filter
            string reqFilter = _filters["_Filter"];

            if (!String.IsNullOrWhiteSpace(reqFilter))
            {
                filteredRows = filteredRows.Where(r => r.Cells.Any(rf => rf.Data != null && rf.Data.IndexOf(reqFilter, StringComparison.CurrentCultureIgnoreCase) != -1));
            }

            // Random entry filter
            string randomEntry = _filters["_Random"];
            int    randomTotal;

            if (!String.IsNullOrWhiteSpace(randomEntry) && Int32.TryParse(randomEntry, out randomTotal))
            {
                filteredRows = filteredRows.OrderBy(r => Guid.NewGuid()).Take(randomTotal);
            }

            foreach (var key in _filters.AllKeys.Except(new string[] { "_Filter", "_Random" }))
            {
                var fieldName = key;
                var value     = _filters[fieldName];
                filteredRows = filteredRows.Where(r => r.Cells.Any(rf => rf.Field.FieldName == fieldName && rf.Data != null && rf.Data.IndexOf(value, StringComparison.CurrentCultureIgnoreCase) != -1));
            }

            foreach (var row in filteredRows.OrderBy(r => r.SortIndex))
            {
                DynamicModuleViewModel modelRow = new DynamicModuleViewModel()
                {
                    ID = row.ID
                };

                foreach (Cell rowField in row.Cells)
                {
                    var field          = rowField.Field;
                    var sFieldDataType = field.FieldDataType.Name;
                    var fieldDataTpe   = Type.GetType(sFieldDataType);

                    var modelField = new DynamicModuleField()
                    {
                        DisplayName  = field.DisplayName,
                        PropertyName = field.FieldName,
                        IsReadOnly   = field.IsReadOnly,
                        IsRequired   = field.IsRequired,
                        DataType     = fieldDataTpe
                    };

                    var converter = FieldValueConversion.GetConverter(fieldDataTpe);
                    modelField.Data = converter.Deserialize(
                        rowField,
                        new ConversionContext {
                        Field      = field,
                        Module     = module,
                        Repository = ModRepository
                    });

                    modelRow.Fields.Add(modelField);
                }

                moduleData.Rows.Add(modelRow);
            }

            return(moduleData);
        }
Example #12
0
 public void TestInvalidDataType()
 {
     FieldValueConversion.GetConverter(typeof(UInt32));
 }
Example #13
0
        public FakeModuleRepository()
        {
            AllModules = new List <IModule>();
            AllRows    = new List <Row>();
            var converter = FieldValueConversion.GetConverter(Type.GetType("System.String"));
            int j         = 100;
            int k         = 200;
            int l         = 300;
            int m         = 400;

            for (int i = 0; i < 101; i++)
            {
                var module = new Module()
                {
                    DisplayName = "Test Module " + i,
                    ModuleName  = "The-test-module-" + i,
                    Id          = i,
                    ModuleType  = i % 3,
                    User        = new User()
                    {
                        UserId = FakeModuleMembership.FakeUserId
                    }
                };

                var field = new Field()
                {
                    DisplayName    = "Test field " + i,
                    FieldName      = "TestField" + i,
                    ID             = k--,
                    ShowInListMode = true,
                    IsRequired     = true,
                    FieldDataType  = new FieldDataType()
                    {
                        Name         = "System.String",
                        ID           = l--,
                        FriendlyName = "Test data type " + i
                    }
                };

                module.Fields.Add(field);

                for (int z = 0; z < 20; z++)
                {
                    module.Rows.Add(new Row()
                    {
                        ID    = m--,
                        Cells = new EntityCollection <Cell>()
                        {
                            new Cell()
                            {
                                Data = converter.Serialize(new DynamicModuleField {
                                    Data = "Some data " + z
                                }),
                                ID    = j--,
                                Field = field
                            }
                        }
                    });
                }

                AllModules.Add(module);
                AllRows.AddRange(AllModules.FirstOrDefault().Rows);
            }
        }