/// <summary> /// Handles the ServerClick event of the SaveButton control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected void SaveButton_ServerClick(object sender, EventArgs e) { if (FormatList.Items.Count < 0) { throw new Exception("Format is not specified"); } Page.Validate(); if (!Page.IsValid) { return; } Mediachase.Ibn.Data.Meta.Management.AttributeCollection attr = new Mediachase.Ibn.Data.Meta.Management.AttributeCollection(); string sDefaultValue = String.Empty; if (MainPlaceHolder.Controls.Count > 0) { IManageControl control = MainPlaceHolder.Controls[0] as IManageControl; if (control != null) { sDefaultValue = control.GetDefaultValue(AllowNullsCheckBox.Checked); attr = control.FieldAttributes; } } if (!AllowNullsCheckBox.Checked && sDefaultValue == String.Empty) { ErrorMessage.Text = GetGlobalResourceObject("IbnFramework.GlobalMetaInfo", "ErrorMessage_AllowNulls").ToString(); return; } if (sDefaultValue == null) { sDefaultValue = String.Empty; } string sFriendlyName = FriendlyNameTextBox.Text.Trim(); if (mf == null) { string sName; if (!AutogenerateSystemNames) { sName = NameTextBox.Text.Trim(); } else { // Generate the field name as the number of seconds elapsed since 2000-01-01 sName = String.Format(CultureInfo.InvariantCulture, "Field{0}", CHelper.GetDateDiffInSeconds(DateTime.UtcNow, new DateTime(2000, 1, 1))); } try { string typeName = FormatList.SelectedValue; //NewEnum if (attr.ContainsKey("NewEnum")) { string name = attr["EnumName"].ToString(); string friendlyname = attr["EnumFriendlyName"].ToString(); bool isPrivate = (bool)attr["EnumPrivate"]; DataTable dt = (DataTable)attr["EnumDataSource"]; attr.Remove("NewEnum"); attr.Remove("EnumName"); attr.Remove("EnumFriendlyName"); attr.Remove("EnumPrivate"); attr.Remove("EnumDataSource"); MetaFieldType type = MetaEnum.Create(name, friendlyname, false); if (isPrivate) { type.Attributes.Add(McDataTypeAttribute.EnumPrivate, mc.Name); } SortedList sl = new SortedList(); foreach (DataRow dr in dt.Rows) { sl.Add((int)dr["OrderId"], dr["Name"].ToString().Trim()); } foreach (int i in sl.Keys) { MetaEnum.AddItem(type, sl[i].ToString(), i); } typeName = type.Name; } //NewMultiEnum if (attr.ContainsKey("NewMultiEnum")) { string name = attr["EnumName"].ToString(); string friendlyname = attr["EnumFriendlyName"].ToString(); bool isPrivate = (bool)attr["EnumPrivate"]; DataTable dt = (DataTable)attr["EnumDataSource"]; attr.Remove("NewMultiEnum"); attr.Remove("EnumName"); attr.Remove("EnumFriendlyName"); attr.Remove("EnumPrivate"); attr.Remove("EnumDataSource"); MetaFieldType type = MetaEnum.Create(name, friendlyname, true); if (isPrivate) { type.Attributes.Add(McDataTypeAttribute.EnumPrivate, mc.Name); } SortedList sl = new SortedList(); foreach (DataRow dr in dt.Rows) { sl.Add((int)dr["OrderId"], dr["Name"].ToString().Trim()); } foreach (int i in sl.Keys) { MetaEnum.AddItem(type, sl[i].ToString(), i); } typeName = type.Name; } MetaFieldType mft = DataContext.Current.MetaModel.RegisteredTypes[FormatList.SelectedValue]; if (mft != null && mft.McDataType == McDataType.Enum && attr.ContainsKey("EnumFriendlyName")) { mft.FriendlyName = attr["EnumFriendlyName"].ToString(); attr.Remove("EnumFriendlyName"); } MetaField newField = null; if (FieldTypeList.SelectedValue == McDataType.Reference.ToString()) { newField = MetaDataWrapper.CreateReference(mc, attr, sName, sFriendlyName, AllowNullsCheckBox.Checked); if (attr.ContainsKey(McDataTypeAttribute.ReferenceUseSecurity)) { Mediachase.Ibn.Data.Services.Security.AddObjectRolesFromReference(newField); } } else if (FieldTypeList.SelectedValue == McDataType.ReferencedField.ToString()) { newField = MetaDataWrapper.CreateReferencedField(mc, attr, sName, sFriendlyName); } else if (FieldTypeList.SelectedValue == McDataType.BackReference.ToString()) { newField = MetaDataWrapper.CreateBackReference(mc, attr, sName, sFriendlyName); } else { newField = MetaDataWrapper.CreateMetaField(mc, attr, sName, sFriendlyName, typeName, AllowNullsCheckBox.Checked, sDefaultValue); } //add to the forms if (newField != null) { List <FormDocument> metaForms = mfs.MetaForms; foreach (FormDocument fd in metaForms) { if (HistoryManager.MetaClassIsHistory(fd.MetaClassName) && !HistoryManager.IsSupportedField(mc.Name, newField.Name)) { continue; } FormController.AddMetaPrimitive(fd.MetaClassName, fd.Name, newField.Name); } List <string> profiles = mfs.ListProfiles; foreach (string profId in profiles) { string placeName = "EntityList"; ListViewProfile profile = ListViewProfile.Load(mc.Name, profId, placeName); if (profile != null) { profile.FieldSet.Add(newField.Name); profile.ColumnsUI.Add(new ColumnProperties(newField.Name, "200px", String.Empty)); ListViewProfile.SaveSystemProfile(mc.Name, placeName, Mediachase.Ibn.Data.Services.Security.CurrentUserId, profile); } } //using (MetaClassManagerEditScope editScope = DataContext.Current.MetaModel.BeginEdit()) //{ // List<string> metaViews = mfs.MetaViews; // foreach (string viewName in metaViews) // { // MetaView metaView = DataContext.Current.MetaModel.MetaViews[viewName]; // if (HistoryManager.MetaClassIsHistory(metaView.MetaClassName) && !HistoryManager.IsSupportedField(metaView.MetaClassName, newField.Name)) // continue; // McMetaViewPreference pref = UserMetaViewPreference.Load(metaView, (int)DataContext.Current.CurrentUserId); // if (pref == null || pref.Attributes.Count == 0) // { // McMetaViewPreference.CreateDefaultUserPreference(metaView); // pref = UserMetaViewPreference.Load(metaView, (int)DataContext.Current.CurrentUserId); // } // int counter = metaView.AvailableFields.Count; // metaView.AvailableFields.Add(metaView.MetaClass.Fields[newField.Name]); // pref.SetAttribute<int>(newField.Name, McMetaViewPreference.AttrIndex, counter); // pref.SetAttribute<int>(newField.Name, McMetaViewPreference.AttrWidth, 100); // } // editScope.SaveChanges(); //} } Response.Redirect(String.Format("{0}?class={1}", ReturnUrl, mc.Name), true); } catch (MetaFieldAlreadyExistsException) { ErrorLabel.Text = String.Format(GetGlobalResourceObject("IbnFramework.GlobalMetaInfo", "FieldExistsErrorMessage").ToString(), "'" + sName + "'"); ErrorLabel.Visible = true; } /* catch (SqlException sqlException) * { * if (sqlException.Number == 1505) // Duplication key * ErrorLabel.Text = ex.Message; * else * ErrorLabel.Text = ex.Message; * ErrorLabel.Visible = true; * } */ } else // Update { MetaFieldType mft = DataContext.Current.MetaModel.RegisteredTypes[FormatList.SelectedValue]; if (mft.McDataType == McDataType.Enum && attr.ContainsKey("EnumFriendlyName")) { mft.FriendlyName = attr["EnumFriendlyName"].ToString(); attr.Remove("EnumFriendlyName"); } if (FieldTypeList.SelectedValue == McDataType.Reference.ToString() || FieldTypeList.SelectedValue == McDataType.BackReference.ToString() || FieldTypeList.SelectedValue == McDataType.ReferencedField.ToString()) { MetaDataWrapper.UpdateMetaFieldFriendlyName(mf, sFriendlyName); } else { MetaDataWrapper.UpdateMetaField(mf, attr, sFriendlyName, sDefaultValue); } Response.Redirect(String.Format("{0}?class={1}", ReturnUrl, mc.Name), true); } }