Esempio n. 1
0
        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 }));
        }