/// <summary> /// A method used to find out a field by specified field attribute value. The "DisplayName" is not a unique identifier for a field, so the method return the first match item when using "DisplayName" attribute value. /// </summary> /// <param name="fields">A parameter represents the fields array.</param> /// <param name="expectedAttributeValue">A parameter represents the expected attribute value which is used to match field item.</param> /// <param name="usedAttribute">A parameter represents the attribute type which is used to compare with the expected attribute value.</param> /// <returns>A return value represents the selected field.</returns> private FieldInformation FindOutTheField(FieldInformation[] fields, string expectedAttributeValue, FieldAttributeType usedAttribute) { #region validate parameters if (null == fields) { throw new ArgumentNullException("fields"); } if (string.IsNullOrEmpty(expectedAttributeValue)) { throw new ArgumentNullException("attributeValue"); } if (0 == fields.Length) { throw new ArgumentException("The fields' array should contain at least one item."); } #endregion validate parameters FieldInformation selectedField = null; #region select the field by specified attribute value switch (usedAttribute) { case FieldAttributeType.InternalName: { var selectResult = from fieldItem in fields where expectedAttributeValue.Equals(fieldItem.InternalName, StringComparison.OrdinalIgnoreCase) select fieldItem; selectedField = 1 == selectResult.Count() ? selectResult.ElementAt<FieldInformation>(0) : null; break; } case FieldAttributeType.Id: { Guid expectedGuidValue; if (!Guid.TryParse(expectedAttributeValue, out expectedGuidValue)) { throw new InvalidOperationException( string.Format( @"The attributeValue parameter value should be a valid GUID value when select field by using [Id] attribute. Current attributeValue parameter:[{0}].", expectedAttributeValue)); } var selectResult = from fieldItem in fields where expectedGuidValue.Equals(fieldItem.Id) select fieldItem; selectedField = 1 == selectResult.Count() ? selectResult.ElementAt<FieldInformation>(0) : null; break; } case FieldAttributeType.DisplayName: { var selectResult = from fieldItem in fields where expectedAttributeValue.Equals(fieldItem.InternalName, StringComparison.OrdinalIgnoreCase) select fieldItem; // The DisplayName attribute is not unique. selectedField = selectResult.Count() > 0 ? selectResult.ElementAt<FieldInformation>(0) : null; break; } } #endregion select the field by specified attribute value if (null == selectedField) { string errorMsg = string.Format( "Could not find the expected field by specified vale:[{0}] of [{1}] attribute.", expectedAttributeValue, usedAttribute); #region log the fields information StringBuilder logBuilder = new StringBuilder(); logBuilder.AppendLine(errorMsg); logBuilder.AppendLine("Fields in current field collection:"); foreach (FieldInformation fieldItem in fields) { string fieldInformation = string.Format( @"FieldName:[{0}] InternalName:[{1}] Type:[{2}] Value:[{3}]", fieldItem.DisplayName, string.IsNullOrEmpty(fieldItem.InternalName) ? "Null/Empty" : fieldItem.InternalName, fieldItem.Type, string.IsNullOrEmpty(fieldItem.Value) ? "Null/Empty" : fieldItem.Value); logBuilder.AppendLine(fieldInformation); } logBuilder.AppendLine("=================="); logBuilder.AppendLine(string.Format("Total fields:[{0}]", fields.Length)); this.Site.Log.Add(LogEntryKind.Debug, logBuilder.ToString()); #endregion log the fields information } return selectedField; }
/// <summary> /// A method used to find out a field by specified field attribute value. The "DisplayName" is not a unique identifier for a field, so the method return the first match item when using "DisplayName" attribute value. /// </summary> /// <param name="fields">A parameter represents the fields array.</param> /// <param name="expectedAttributeValue">A parameter represents the expected attribute value which is used to match field item.</param> /// <param name="usedAttribute">A parameter represents the attribute type which is used to compare with the expected attribute value.</param> /// <returns>A return value represents the selected field.</returns> private FieldInformation FindOutTheField(FieldInformation[] fields, string expectedAttributeValue, FieldAttributeType usedAttribute) { #region validate parameters if (null == fields) { throw new ArgumentNullException("fields"); } if (string.IsNullOrEmpty(expectedAttributeValue)) { throw new ArgumentNullException("attributeValue"); } if (0 == fields.Length) { throw new ArgumentException("The fields' array should contain at least one item."); } #endregion validate parameters FieldInformation selectedField = null; #region select the field by specified attribute value switch (usedAttribute) { case FieldAttributeType.InternalName: { var selectResult = from fieldItem in fields where expectedAttributeValue.Equals(fieldItem.InternalName, StringComparison.OrdinalIgnoreCase) select fieldItem; selectedField = 1 == selectResult.Count() ? selectResult.ElementAt <FieldInformation>(0) : null; break; } case FieldAttributeType.Id: { Guid expectedGuidValue; if (!Guid.TryParse(expectedAttributeValue, out expectedGuidValue)) { throw new InvalidOperationException( string.Format( @"The attributeValue parameter value should be a valid GUID value when select field by using [Id] attribute. Current attributeValue parameter:[{0}].", expectedAttributeValue)); } var selectResult = from fieldItem in fields where expectedGuidValue.Equals(fieldItem.Id) select fieldItem; selectedField = 1 == selectResult.Count() ? selectResult.ElementAt <FieldInformation>(0) : null; break; } case FieldAttributeType.DisplayName: { var selectResult = from fieldItem in fields where expectedAttributeValue.Equals(fieldItem.InternalName, StringComparison.OrdinalIgnoreCase) select fieldItem; // The DisplayName attribute is not unique. selectedField = selectResult.Count() > 0 ? selectResult.ElementAt <FieldInformation>(0) : null; break; } } #endregion select the field by specified attribute value if (null == selectedField) { string errorMsg = string.Format( "Could not find the expected field by specified vale:[{0}] of [{1}] attribute.", expectedAttributeValue, usedAttribute); #region log the fields information StringBuilder logBuilder = new StringBuilder(); logBuilder.AppendLine(errorMsg); logBuilder.AppendLine("Fields in current field collection:"); foreach (FieldInformation fieldItem in fields) { string fieldInformation = string.Format( @"FieldName:[{0}] InternalName:[{1}] Type:[{2}] Value:[{3}]", fieldItem.DisplayName, string.IsNullOrEmpty(fieldItem.InternalName) ? "Null/Empty" : fieldItem.InternalName, fieldItem.Type, string.IsNullOrEmpty(fieldItem.Value) ? "Null/Empty" : fieldItem.Value); logBuilder.AppendLine(fieldInformation); } logBuilder.AppendLine("=================="); logBuilder.AppendLine(string.Format("Total fields:[{0}]", fields.Length)); this.Site.Log.Add(LogEntryKind.Debug, logBuilder.ToString()); #endregion log the fields information } return(selectedField); }
/// <summary> /// A method used to select a field by specified field attribute value. The "DisplayName" is not a unique identifier for a field, so the method return the first match item when using "DisplayName" attribute value. If there are no any fields are found, this method will raise an InvalidOperationException. /// </summary> /// <param name="fields">A parameter represents the fields array.</param> /// <param name="expectedAttributeValue">A parameter represents the expected attribute value which is used to match field item.</param> /// <param name="usedAttribute">A parameter represents the attribute type which is used to compare with the expected attribute value.</param> /// <returns>A return value represents the selected field.</returns> protected FieldInformation SelectFieldBySpecifiedAtrribute(FieldInformation[] fields, string expectedAttributeValue, FieldAttributeType usedAttribute) { FieldInformation selectedField = this.FindOutTheField(fields, expectedAttributeValue, usedAttribute); if (null == selectedField) { string errorMsg = string.Format( "Could not find the expected field by specified value:[{0}] of [{1}] attribute.", expectedAttributeValue, usedAttribute); throw new InvalidOperationException(errorMsg); } return selectedField; }
/// <summary> /// A method used to select a field by specified field attribute value. The "DisplayName" is not a unique identifier for a field, so the method return the first match item when using "DisplayName" attribute value. If there are no any fields are found, this method will raise an InvalidOperationException. /// </summary> /// <param name="fields">A parameter represents the fields array.</param> /// <param name="expectedAttributeValue">A parameter represents the expected attribute value which is used to match field item.</param> /// <param name="usedAttribute">A parameter represents the attribute type which is used to compare with the expected attribute value.</param> /// <returns>A return value represents the selected field.</returns> protected FieldInformation SelectFieldBySpecifiedAtrribute(FieldInformation[] fields, string expectedAttributeValue, FieldAttributeType usedAttribute) { FieldInformation selectedField = this.FindOutTheField(fields, expectedAttributeValue, usedAttribute); if (null == selectedField) { string errorMsg = string.Format( "Could not find the expected field by specified value:[{0}] of [{1}] attribute.", expectedAttributeValue, usedAttribute); throw new InvalidOperationException(errorMsg); } return(selectedField); }