示例#1
0
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            RmAttributeValue other = obj as RmAttributeValue;

            if (other == null)
            {
                return(false);
            }
            lock (this.attributeValues) {
                lock (other.attributeValues) {
                    if (this.attributeValues.Count != other.attributeValues.Count)
                    {
                        return(false);
                    }
                    this.attributeValues.Sort();
                    other.attributeValues.Sort();
                    for (int i = 0; i < this.attributeValues.Count; i++)
                    {
                        if (this.attributeValues[i].Equals(other.attributeValues[i]) == false)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(true);
        }
示例#2
0
 /// <summary>
 /// Starts tracking changes in the underlying <see cref="RmResource"/>.
 /// </summary>
 public void BeginChanges()
 {
     EnsureNotDisposed();
     lock (rmObject.attributes) {
         this.originalAttributes = new Dictionary <RmAttributeName, RmAttributeValue>();
         foreach (RmAttributeName key in rmObject.attributes.Keys)
         {
             RmAttributeValue value = rmObject.attributes[key];
             this.originalAttributes[key] = value.Clone() as RmAttributeValue;
         }
     }
 }
        /// <summary>
        /// Return a string with the value of the attribute whose name is passed
        /// as format.
        /// If the attribute is multi valued, returns a string with the
        /// concatenation of the values separated by ';'
        /// </summary>
        /// <example>
        /// obj.ToString("DisplayName") -> returns DisplayName of the object.
        /// string.Format("{0:DisplayName}",obj) -> returns DisplayName of the object.
        /// </example>
        public string ToString(
            string format,
            IFormatProvider formatProvider)
        {
            if (string.IsNullOrEmpty(format))
            {
                return(this.ToString());
            }
            RmAttributeName key = new RmAttributeName(format);

            if (!attributes.ContainsKey(key))
            {
                return(string.Empty);
            }
            RmAttributeValue value = attributes[key];

            if (value.IsMultiValue)
            {
                // make an array of string values
                string[] values = value.Values.ConvertAll <string>(x => GetString(x)).ToArray();
                return(string.Join("; ", values));
            }
            return(GetString(value.Value));
        }
示例#4
0
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            RmAttributeValue newValue = new RmAttributeValue(IsMultiValue);

            newValue.attributeValues = new List <IComparable>();
            foreach (IComparable value in this.attributeValues)
            {
                ICloneable cloneValue = value as ICloneable;
                if (cloneValue == null)
                {
                    newValue.attributeValues.Add(value);
                }
                else
                {
                    IComparable cloneInsert = cloneValue.Clone() as IComparable;
                    if (cloneInsert == null)
                    {
                        throw new InvalidOperationException("A comparable, when cloned, returned a non-comparable: " + cloneValue.ToString());
                    }
                    newValue.attributeValues.Add(cloneInsert);
                }
            }
            return(newValue);
        }
示例#5
0
        /// <summary>
        /// Gets the differences from source and destination attributes.
        /// </summary>
        /// <param name="sourceAttributes">The source attributes.</param>
        /// <param name="destinationAttributes">The destination attributes.</param>
        /// <returns></returns>
        private static IList <RmAttributeChange> GetDifference(
            Dictionary <RmAttributeName, RmAttributeValue> sourceAttributes,
            Dictionary <RmAttributeName, RmAttributeValue> destinationAttributes)
        {
            IList <RmAttributeChange> changedAttributes = new List <RmAttributeChange>();

            // iterate source attributes
            foreach (KeyValuePair <RmAttributeName, RmAttributeValue> sourceItem in sourceAttributes)
            {
                RmAttributeName  sourceName  = sourceItem.Key;
                RmAttributeValue sourceValue = sourceItem.Value;
                if (!destinationAttributes.ContainsKey(sourceName))
                {
                    // the destination does not contain the attribute
                    if (sourceValue.IsMultiValue)
                    {
                        // add all the values
                        foreach (IComparable value1 in sourceValue.Values)
                        {
                            changedAttributes.Add(new RmAttributeChange(sourceName, value1, RmAttributeChangeOperation.Add));
                        }
                    }
                    else
                    {
                        changedAttributes.Add(new RmAttributeChange(sourceItem.Key, null, RmAttributeChangeOperation.Replace));
                    }
                }
                else
                {
                    // the destination contains the attribute
                    RmAttributeValue destinationValue = destinationAttributes[sourceItem.Key];
                    if (sourceValue.IsMultiValue)
                    {
                        foreach (IComparable value1 in sourceValue.Values)
                        {
                            if (destinationValue.Values.Contains(value1) == false)
                            {
                                changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value1, RmAttributeChangeOperation.Add));
                            }
                        }
                        foreach (IComparable value2 in destinationValue.Values)
                        {
                            if (sourceItem.Value.Values.Contains(value2) == false)
                            {
                                changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value2, RmAttributeChangeOperation.Delete));
                            }
                        }
                    }
                    else
                    {
                        if (destinationValue.Value != sourceValue.Value)
                        {
                            changedAttributes.Add(new RmAttributeChange(sourceItem.Key, sourceValue.Value, RmAttributeChangeOperation.Replace));
                        }
                    }
                }
            }
            // iterate destination attributes
            foreach (KeyValuePair <RmAttributeName, RmAttributeValue> destinationItem in destinationAttributes)
            {
                if (sourceAttributes.ContainsKey(destinationItem.Key) == false)
                {
                    foreach (IComparable value2 in destinationItem.Value.Values)
                    {
                        changedAttributes.Add(new RmAttributeChange(destinationItem.Key, value2, RmAttributeChangeOperation.Delete));
                    }
                }
            }
            return(changedAttributes);
        }
示例#6
0
 /// <summary>
 /// Creates a new object that is a copy of the current instance.
 /// </summary>
 /// <returns>
 /// A new object that is a copy of this instance.
 /// </returns>
 public object Clone()
 {
     RmAttributeValue newValue = new RmAttributeValue(IsMultiValue);
     newValue.attributeValues = new List<IComparable>();
     foreach (IComparable value in this.attributeValues) {
         ICloneable cloneValue = value as ICloneable;
         if (cloneValue == null) {
             newValue.attributeValues.Add(value);
         } else {
             IComparable cloneInsert = cloneValue.Clone() as IComparable;
             if (cloneInsert == null)
                 throw new InvalidOperationException("A comparable, when cloned, returned a non-comparable: " + cloneValue.ToString());
             newValue.attributeValues.Add(cloneInsert);
         }
     }
     return newValue;
 }
示例#7
0
        /// <summary>
        /// Gets the differences from source and destination attributes.
        /// </summary>
        /// <param name="sourceAttributes">The source attributes.</param>
        /// <param name="destinationAttributes">The destination attributes.</param>
        /// <returns></returns>
        private static IList <RmAttributeChange> GetDifference(
            Dictionary <RmAttributeName, RmAttributeValue> sourceAttributes,
            Dictionary <RmAttributeName, RmAttributeValue> destinationAttributes)
        {
            IList <RmAttributeChange> changedAttributes = new List <RmAttributeChange>();

            // iterate source attributes
            foreach (KeyValuePair <RmAttributeName, RmAttributeValue> sourceItem in sourceAttributes)
            {
                RmAttributeName  sourceName  = sourceItem.Key;
                RmAttributeValue sourceValue = sourceItem.Value;
                if (!destinationAttributes.ContainsKey(sourceName))
                {
                    // the destination does not contain the attribute
                    if (sourceValue.IsMultiValue)
                    {
                        // add all the values
                        foreach (IComparable value1 in sourceValue.Values)
                        {
                            changedAttributes.Add(new RmAttributeChange(sourceName, value1, RmAttributeChangeOperation.Add));
                        }
                    }
                    else
                    {
                        changedAttributes.Add(new RmAttributeChange(sourceItem.Key, null, RmAttributeChangeOperation.Replace));
                    }
                }
                else
                {
                    // the destination contains the attribute
                    RmAttributeValue destinationValue = destinationAttributes[sourceItem.Key];
                    if (sourceValue.IsMultiValue)
                    {
                        foreach (IComparable value1 in sourceValue.Values)
                        {
                            if (destinationValue.Values.Contains(value1) == false)
                            {
                                changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value1, RmAttributeChangeOperation.Add));
                            }
                        }
                        foreach (IComparable value2 in destinationValue.Values)
                        {
                            if (sourceItem.Value.Values.Contains(value2) == false)
                            {
                                changedAttributes.Add(new RmAttributeChange(sourceItem.Key, value2, RmAttributeChangeOperation.Delete));
                            }
                        }
                    }
                    else
                    {
                        if (destinationValue.Value != sourceValue.Value)
                        {
                            RmAttributeChangeOperation operation;
                            if (
                                (
                                    // <[MA] change 24-11-2011> - cleared reference attributes require Delete operation
                                    destinationValue.Value is RmReference
                                    ||
                                    // <[MA] change 05-03-2012> - cleared date attributes require Delete operation
                                    destinationValue.Value is DateTime?
                                ) &&
                                sourceValue.Value == null
                                )
                            {
                                operation = RmAttributeChangeOperation.Delete;
                            }
                            else
                            {
                                operation = RmAttributeChangeOperation.Replace;
                            }
                            changedAttributes.Add(new RmAttributeChange(sourceItem.Key, sourceValue.Value, operation));
                            // </[MA]>
                        }
                    }
                }
            }
            // iterate destination attributes
            foreach (KeyValuePair <RmAttributeName, RmAttributeValue> destinationItem in destinationAttributes)
            {
                if (sourceAttributes.ContainsKey(destinationItem.Key) == false)
                {
                    foreach (IComparable value2 in destinationItem.Value.Values)
                    {
                        changedAttributes.Add(new RmAttributeChange(destinationItem.Key, value2, RmAttributeChangeOperation.Delete));
                    }
                }
            }
            return(changedAttributes);
        }