public virtual object Deserialize(Cell _data, ConversionContext _context = null) { if (_data == null || String.IsNullOrWhiteSpace(_data.Data)) { return null; } string strData = _data.Data; switch (DataType.Name) { case "String": return strData; case "Int32": int result; if (Int32.TryParse(strData, out result)) { return result; } return null; case "DateTime": DateTime dt; if (DateTime.TryParse(strData, out dt)) { return dt; } return null; case "Boolean": bool b; if (Boolean.TryParse(strData, out b)) { return b; } return null; default: return ParseXml(strData, DataType); } }
public override object Deserialize(Cell _data, ConversionContext _context = null) { object data = base.Deserialize(_data); CheckBoxList chkList; if (data == null) { chkList = new CheckBoxList(); } else { chkList = data as CheckBoxList; } if (_context == null || _context.Field == null || _context.Field.Metadata == null) { return chkList; } if (!_context.Field.Metadata.IsLoaded) { _context.Field.Metadata.Load(); } var metadata = _context.Field.Metadata.FirstOrDefault(m => m.Key == ModuleRepository.CheckBoxListMetadataKey); if (metadata != null && !String.IsNullOrWhiteSpace(metadata.Value)) { var json = new JavaScriptSerializer(); var items = json.Deserialize<string[]>(metadata.Value); if (items != null) { foreach (var i in items) { // avoid adding empty entries to the dictionary if (!String.IsNullOrWhiteSpace(i)) { var newItem = new CheckBoxItem { Key = i.EscapeName(), Value = i }; // checked options will already be present in the list if(!chkList.Any(it => it.Value == i)) { chkList.Add(newItem); } } } } } return chkList; }
public override object Deserialize(Cell _data, ConversionContext _context = null) { object data = base.Deserialize(_data); DropdownButton ddButton; if (data == null) { ddButton = new DropdownButton(); } else { ddButton = data as DropdownButton; } if (_context == null || _context.Field == null || _context.Field.Metadata == null) { return ddButton; } if (!_context.Field.Metadata.IsLoaded) { _context.Field.Metadata.Load(); } var metadata = _context.Field.Metadata.FirstOrDefault(m => m.Key == ModuleRepository.DropdownButtonMetadataKey); if (metadata != null && !String.IsNullOrWhiteSpace(metadata.Value)) { var json = new JavaScriptSerializer(); var items = json.Deserialize<string[]>(metadata.Value); if (items != null) { foreach (var i in items) { if (!String.IsNullOrWhiteSpace(i)) { // avoid adding empty entries to the dictionary ddButton.ValidValues.Add(i.EscapeName(), i); } } } } return ddButton; }
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); } }
/*[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); }
public void Test_Password_Should_Set_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() }) }; 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); }
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); }
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); }
/// <summary> /// Create a new Cell object. /// </summary> /// <param name="id">Initial value of the ID property.</param> public static Cell CreateCell(global::System.Int32 id) { Cell cell = new Cell(); cell.ID = id; return cell; }
/// <summary> /// Deprecated Method for adding a new object to the Cell EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCell(Cell cell) { base.AddObject("Cell", cell); }
public void AddCell(Cell _Cell) { db.AddToCell(_Cell); }
public void AddCell(Cell _field) { throw new NotImplementedException(); }
public ActionResult Edit(ModulesViewModel _model) { if (!ModelState.IsValid) { _model.DataTypeList = GetDataTypeList(); return View(_model); } var modToEdit = ModRepository.GetModuleById(_model.Id, m => m.Fields.Include<Field, FieldDataType>(f => f.FieldDataType), m => m.Fields.Include<Field, FieldMetadata>(f => f.Metadata), m => m.Rows.Include<Row, Cell>(r => r.Cells)); if (modToEdit == null) { TempData["Message"] = "Módulo não encontrado."; _model.DataTypeList = GetDataTypeList(); return View(_model); } modToEdit.DisplayName = _model.DisplayName; if (_model.Fields.Count(f => f.ShowInListMode) == 0) { TempData["Message"] = "Pelo menos um dos campos deve ser visível na listagem."; _model.DataTypeList = GetDataTypeList(); return View(_model); } // handle modified and added fields foreach (ModuleField f in _model.Fields) { Field dbField = modToEdit.Fields.FirstOrDefault(fld => fld.ID == f.ID); bool bNeedToAdd = (dbField == null); if (dbField == null) { dbField = new Field(); } if (!bNeedToAdd) { dbField.FieldDataTypeReference.Load(); dbField.Metadata.Load(); if (dbField.FieldDataType.Name != f.DataType) { // field type changed. set it to null on all matching rows modToEdit.Rows.ToList().ForEach(r => { r.Cells.Load(); r.Cells .ToList() .ForEach(rf => { rf.FieldReference.Load(); if (rf.Field.FieldName == dbField.FieldName) { rf.Data = null; } }); }); } } dbField.DisplayName = f.FieldDisplayName; dbField.FieldName = f.FieldDisplayName.EscapeName(); dbField.FieldDataType = ModRepository.GetDataTypeByName(f.DataType); dbField.IsReadOnly = f.IsReadOnly; dbField.IsRequired = f.IsRequired; dbField.ShowInListMode = f.ShowInListMode; dbField.Module = modToEdit as Module; if (f.Metadata != null) { dbField.AddMetadata(f.Metadata); } if (bNeedToAdd) { ModRepository.AddField(dbField); modToEdit.Rows.ToList() .ForEach(row => { Cell rf = new Cell() { Field = dbField, Row = row }; ModRepository.AddCell(rf); }); } } ModRepository.Save(); TempData["Message"] = "Módulo salvo com sucesso."; return RedirectToAction("List", "DynamicModule", new { moduleid = _model.Id, modulename = modToEdit.ModuleName }); }