Beispiel #1
0
        /// <summary>
        /// propose the editable panel for a table, if table will probably not be self-editable
        /// (such as an M2N mapping), return null
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns>Panel (or null)</returns>
        public Panel ProposeForTable(string tableName)
        {
            // don`t care for indexes for now

            DataColumnCollection cols   = stats.ColumnTypes[tableName];
            List <FK>            FKs    = stats.FKs[tableName];
            List <string>        PKCols = stats.PKs[tableName];

            List <IField> fields = new List <IField>();


            foreach (M2NMapping mapping in mappings)    // find mappings related to this table
            {
                if (mapping.myTable != tableName)
                {
                    continue;
                }
                List <string> displayColOrder = stats.ColumnsToDisplay[mapping.refTable];
                mapping.displayColumn = displayColOrder[0];
                fields.Add(new M2NMappingField(mapping, "Mapping " + mapping.myTable + " to " + mapping.refTable));
            }

            List <IColumnFieldFactory> factories = (List <IColumnFieldFactory>)(System.Web.HttpContext.Current.Application["ColumnFieldFactories"]);

            foreach (DataColumn col in cols)
            {
                if (col.AutoIncrement)
                {
                    continue;
                }
                IColumnFieldFactory leadingFactory = (from f in factories
                                                      where f.CanHandle(col) && !(f is ICustomizableColumnFieldFactory)
                                                      orderby f.Specificity descending
                                                      select f).FirstOrDefault();
                if (leadingFactory == null && !col.AllowDBNull)
                {
                    return(null);
                }
                IColumnField field = leadingFactory.Create(col);
                field.Required = !col.AllowDBNull;
                field.Unique   = col.Unique;
                fields.Add(field);
            }

            PropertyCollection controlProps = new PropertyCollection();
            PropertyCollection viewProps    = new PropertyCollection();
            string             panelName    = tableName + " Editation";

            List <Control> controls = new List <Control>();

            // all the controls
            controls.Add(new Control(0, UserAction.Insert.ToString(), UserAction.Insert));
            controls.Add(new Control(0, UserAction.Update.ToString(), UserAction.Update));
            controls.Add(new Control(0, UserAction.Update.ToString(), UserAction.Delete));

            List <Control> controlsAsControl = new List <Control>(controls);

            Panel res = new Panel(tableName, 0, PanelTypes.Editable,
                                  new List <Panel>(), fields, controlsAsControl, PKCols);

            return(res);
        }
Beispiel #2
0
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            // extract the data for fields from the table
            List <IField> fields = new List <IField>();
            int           i      = 1;

            Dictionary <DataColumn, Dictionary <string, object> > customs = new Dictionary <DataColumn, Dictionary <string, object> >();

            foreach (DataColumn col in mm.Stats.ColumnTypes[actPanel.tableName])
            {       // standard fields
                TableRow r = tbl.Rows[i++];
                if (!((CheckBox)r.Cells[1].Controls[0]).Checked)
                {
                    continue;
                }
                // label, present, type, valid, caption

                IColumnFieldFactory factory = factories[Int32.Parse(((DropDownList)r.Cells[2].Controls[0]).SelectedValue)];


                // cell 3 is there for FK display column dropList

                bool required = false;
                bool unique   = false;
                if (r.Cells[4].Controls.Count == 4)
                {
                    CheckBox reqChb = (CheckBox)r.Cells[4].Controls[1];
                    CheckBox uniChb = (CheckBox)r.Cells[4].Controls[3];
                    required = reqChb.Checked;
                    unique   = uniChb.Checked;
                }

                string caption = ((TextBox)r.Cells[5].Controls[0]).Text;
                if (caption == "")
                {
                    caption = null;
                }

                if (factory is ICustomizableColumnFieldFactory)
                {
                    customs[col]             = new Dictionary <string, object>();
                    customs[col]["factory"]  = factory.Clone();     // so that each field gets its factory even if there more custom fields of the same type
                    customs[col]["required"] = required;
                    customs[col]["unique"]   = unique;
                    customs[col]["caption"]  = caption;
                    continue;
                }

                IField newField;

                //PBPR

                // react to the changes to the displaycolumn for the FK
                if (col.ExtendedProperties.Contains("FK"))
                {
                    FK colFK = (FK)(col.ExtendedProperties["FK"]);
                    colFK.displayColumn          = ((DropDownList)(r.Cells[3].Controls[0])).SelectedValue;
                    col.ExtendedProperties["FK"] = colFK;
                }
                newField          = factory.Create(col);
                newField.Caption  = caption;
                newField.Required = required;
                if (newField is IColumnField)
                {
                    ((IColumnField)newField).Unique = unique;
                }
                fields.Add(newField);
            }

            i = 1;
            foreach (M2NMapping mapping in mappings)
            {                // mappings
                TableRow r = mappingsTbl.Rows[i++];
                // label, present, type (mappingType), valid (req?), caption
                if (!((CheckBox)r.Cells[1].Controls[0]).Checked)
                {
                    continue;
                }

                // must be mappingType...

                List <ValidationRules> rules = new List <ValidationRules>();
                // no validation for a mapping

                mapping.displayColumn = ((DropDownList)(r.Cells[3].Controls[0])).SelectedValue;

                string caption = ((TextBox)r.Cells[5].Controls[0]).Text;

                M2NMappingField m2nf = new M2NMappingField(mapping, caption);
                fields.Add(m2nf);
            }

            // crate a control for each checked action
            List <_min.Models.Control> controls = new List <_min.Models.Control>();           // controls

            bool          valid     = true;
            List <string> errorMsgs = new List <string>();

            if (actions.RetrieveStringData().Count == 0)
            {
                valid = false;
                errorMsgs.Add("Choose at least one action for the panel, please.");
            }

            foreach (string actionString in actions.RetrieveStringData())
            {
                _min.Models.Control c = new _min.Models.Control(0, actionString,
                                                                (UserAction)Enum.Parse(typeof(UserAction), actionString));
                c.targetPanel = actPanel.controls[0].targetPanel;

                c.targetPanelId = actPanel.controls[0].targetPanelId;       //  a miserable way to find out the target panel...really

                controls.Add(c);
            }

            MPanel resPanel = new MPanel(actPanel.tableName, actPanel.panelId, PanelTypes.Editable, new List <MPanel>(),
                                         fields, controls, actPanel.PKColNames, null, actPanel.parent);

            resPanel.panelName = panelName.Text;


            valid = valid && mm.Architect.checkPanelProposal(resPanel, out errorMsgs, customs);

            // validate the Panel using Architect`s validator - don`t edit PKs, unique columns must have the constraint, must contain all collumns except Nullable
            // and AI and more rules
            validationResult.Items.Clear();
            if (valid)
            {
                if (customs.Count == 0)
                {
                    validationResult.Items.Add(new ListItem("Valid"));

                    actPanel = resPanel;
                    mm.SysDriver.BeginTransaction();
                    mm.SysDriver.UpdatePanel(actPanel);
                    Session.Clear();
                    mm.SysDriver.IncreaseVersionNumber();
                    mm.SysDriver.CommitTransaction();

                    validationResult.Items.Add(new ListItem("Saved"));
                    Response.Redirect(Page.Request.RawUrl);
                }
                else
                {
                    Session["interPanel"] = resPanel;
                    Session["customs"]    = customs;
                    Response.RedirectToRoute("ArchitectEditEditableCustomRoute", new { projectName = CE.project.Name, panelId = actPanel.panelId });
                    // for the sake of unity (and SysDriver)
                }
            }
            else
            {
                foreach (string s in errorMsgs)
                {
                    validationResult.Items.Add(new ListItem(s));
                }
            }
        }