public static string SetValue(TaxonomyInfo taxonomy, Attribute attribute, bool autoMapSchema, SchemaAttribute schematus, object value, bool suppressAutoMapMessage = false) { var schemaInfo = taxonomy.SchemaInfos.FirstOrDefault(si => si.Attribute.Equals(attribute)); var newSchemaInfo = false; if (schemaInfo == null) { if (suppressAutoMapMessage) { return(null); } if (!autoMapSchema && MessageBox.Show( String.Format("Attribute [{0}] is not mapped in Node [{1}]. Do you want to map it?", attribute, taxonomy.TaxonomyData.NodeName), @"Add to Schema", MessageBoxButtons.YesNo) == DialogResult.No) { return(null); } schemaInfo = new SchemaInfo { Attribute = attribute }; taxonomy.SchemaInfos.Add(schemaInfo); newSchemaInfo = true; } if (schematus == null) { //Create/Update Schema Info only var currentSchemaData = schemaInfo.SchemaData; var newSchemaData = new SchemaData(); if (value != null && value is SchemaData) { var fromSchemaData = (SchemaData)value; if (currentSchemaData != null && currentSchemaData.Equals(fromSchemaData)) { return(null); } newSchemaData.DataType = fromSchemaData.DataType; newSchemaData.DisplayOrder = fromSchemaData.DisplayOrder; newSchemaData.NavigationOrder = fromSchemaData.NavigationOrder; newSchemaData.InSchema = fromSchemaData.InSchema; } if (currentSchemaData != null) { currentSchemaData.Active = false; } schemaInfo.SchemaDatas.Add(newSchemaData); return(null); } var activeSd = schemaInfo.SchemaData; switch (schematus.AttributeType) { case SchemaAttributeType.Primary: var reallyNewSd = false; var reorderRanks = false; SchemaData newSchemaData; if (activeSd == null) { reallyNewSd = true; newSchemaData = new SchemaData(); } else { //activeSd.DeletedBy = AryaTools.Instance.InstanceData.CurrentUser.ID; activeSd.DeletedByUser = AryaTools.Instance.InstanceData.CurrentUser; //fixes Object not found error activeSd.DeletedOn = DateTime.Now; newSchemaData = new SchemaData { DataType = activeSd.DataType, InSchema = activeSd.InSchema, NavigationOrder = activeSd.NavigationOrder, DisplayOrder = activeSd.DisplayOrder }; } var propertyName = schematus.PrimarySchemaAttribute.Replace(" ", String.Empty); var valueProperty = newSchemaData.GetType().GetProperty(propertyName); var propertyType = valueProperty.PropertyType; if (propertyType == typeof(string)) { var stringValue = value ?? String.Empty; if (propertyName == "DataType") { stringValue = ValidateDataType(stringValue); } if (!(valueProperty.GetValue(newSchemaData, null) ?? String.Empty).Equals(stringValue)) { reallyNewSd = true; valueProperty.SetValue(newSchemaData, stringValue.ToString(), null); } } else if (propertyType == typeof(bool)) { var boolValue = value ?? false; string firstCharacter; try { firstCharacter = boolValue.ToString().Substring(0, 1).ToLower(); } catch (Exception) { firstCharacter = "f"; } var newValue = firstCharacter.Equals("t") || firstCharacter.Equals("y") || firstCharacter.Equals("1"); if (!((bool)valueProperty.GetValue(newSchemaData, null)).Equals(newValue)) { reallyNewSd = true; valueProperty.SetValue(newSchemaData, newValue, null); } } else if (propertyType == typeof(decimal)) { var decimalValue = value ?? 0.0; decimal newDecimalValue; if (Decimal.TryParse(decimalValue.ToString(), out newDecimalValue)) { if (!((decimal)valueProperty.GetValue(newSchemaData, null)).Equals(newDecimalValue)) { reallyNewSd = true; valueProperty.SetValue(newSchemaData, newDecimalValue, null); reorderRanks = SchemaDataGridView.AutoOrderRanks; } } else { return(String.Format("Invalid value [{0}] for [{1}] - [{2}].{3}Expected a decimal value.", decimalValue, taxonomy.NodeName, attribute, Environment.NewLine)); } } else { return(String.Format("Unknown data type for value [{0}] for [{1}] - [{2}]", value ?? String.Empty, taxonomy.NodeName, attribute)); } if (reallyNewSd) { if (activeSd != null) { activeSd.Active = false; } schemaInfo.SchemaDatas.Add(newSchemaData); if (reorderRanks) { AutoOrderRanks(newSchemaData.SchemaInfo.TaxonomyInfo, newSchemaData); } } return(null); case SchemaAttributeType.Meta: var reallyNewSmd = false; if (schematus.DataType == typeof(bool)) { value = value.ToString().StartsWith("y", StringComparison.OrdinalIgnoreCase) ? "Yes" : "No"; } //First check if there is an active SchemaData if (activeSd == null) { if (suppressAutoMapMessage) { return(null); } if (!newSchemaInfo && !autoMapSchema && MessageBox.Show( String.Format("Attribute [{0}] is not mapped in Node [{1}]. Do you want to map it?", attribute, taxonomy.TaxonomyData.NodeName), "Add to Schema", MessageBoxButtons.YesNo) == DialogResult.No) { return(null); } schemaInfo.SchemaDatas.Add(new SchemaData()); reallyNewSmd = true; } var activeSmi = (from mi in schemaInfo.SchemaMetaInfos where mi.Attribute.Equals(schematus.MetaSchemaAttribute) select mi).FirstOrDefault(); if (activeSmi == null) { activeSmi = new SchemaMetaInfo { Attribute = schematus.MetaSchemaAttribute }; schemaInfo.SchemaMetaInfos.Add(activeSmi); reallyNewSmd = true; } var activeSmd = activeSmi.SchemaMetaData; if (activeSmd == null) { reallyNewSmd = true; } else if (value == null || !activeSmd.Value.Equals(value)) { reallyNewSmd = true; activeSmd.Active = false; } if (reallyNewSmd && value != null && !String.IsNullOrEmpty(value.ToString())) { activeSmi.SchemaMetaDatas.Add(new SchemaMetaData { Value = value.ToString() }); SchemaDataGridView.UpdateAutoCompleteSource(schematus.MetaSchemaAttribute, value.ToString()); } return(null); default: return("Unknown Meta-attribute Type! Weird!!!"); } }
public static void AutoOrderRanks(TaxonomyInfo taxonomy, SchemaData schemaData) { if (taxonomy == null) { return; } var sds = (from si in taxonomy.SchemaInfos let sd = si.SchemaData where sd != null && (sd.NavigationOrder > 0 || sd.DisplayOrder > 0) select sd).ToDictionary(sd => sd, sd => new[] { sd.NavigationOrder, sd.DisplayOrder }); if (!sds.Any()) { return; } var baseNavOrder = schemaData != null ? schemaData.NavigationOrder : 0; var baseDispOrder = schemaData != null ? schemaData.DisplayOrder : 0; var navs = (from sd in sds where sd.Key.NavigationOrder > 0 && sd.Key.NavigationOrder >= baseNavOrder && (schemaData == null || !sd.Key.Equals(schemaData)) select sd.Key).ToList(); foreach (var nav in navs) { sds[nav][0] = nav.NavigationOrder + 1; } var iCtr = 0; foreach (var nav in (from sd in sds where sd.Value[0] > 0 orderby sd.Value[0] select sd.Key).ToList()) { sds[nav][0] = ++iCtr; } var disps = (from sd in sds where sd.Key.DisplayOrder > 0 && sd.Key.DisplayOrder >= baseDispOrder && (schemaData == null || !sd.Key.Equals(schemaData)) select sd.Key).ToList(); foreach (var disp in disps) { sds[disp][1] = disp.DisplayOrder + 1; } iCtr = 0; foreach (var disp in (from sd in sds where sd.Value[1] > 0 orderby sd.Value[1] select sd.Key).ToList()) { sds[disp][1] = ++iCtr; } foreach (var kvp in sds) { var schemaInfo = kvp.Key.SchemaInfo; SetValue(schemaInfo.TaxonomyInfo, schemaInfo.Attribute, false, SchemaAttributeNavigationOrder, kvp.Value[0]); SetValue(schemaInfo.TaxonomyInfo, schemaInfo.Attribute, false, SchemaAttributeDisplayOrder, kvp.Value[1]); } }