/// /// Copyright © 2003-2008 JetBrains s.r.o. /// You may distribute under the terms of the GNU General Public License, as published by the Free Software Foundation, version 2 (see License.txt in the repository root folder). /// using System; using System.ComponentModel; using JetBrains.Omea.OpenAPI; namespace JetBrains.Omea.OpenApiEx { /// /// A base class that wraps a resource. /// public abstract class ResourceObject : IResourceObject { /// /// Stores the resource that is wrapped by this business object. /// private IResource _resource; /// /// Stores whether all the write-operations to the resource properties should be asynchronous. /// private bool _async = true; /// /// In case is True, specifies the async operations priority. /// private JobPriority _priority = JobPriority.Normal; /// /// Inits the instance by attaching to an existing resource. /// /// The resource to back the resource object, must not be Null. public ResourceObject(IResource resource) { if(resource == null) throw new ArgumentNullException("resource"); _resource = resource; } /// /// Gets the underlying resource storing the object data. /// [Browsable(false)] public virtual IResource Resource { get { return _resource; } } /// /// Gets the ID of the resource. /// [Browsable(true)] [Category("Database")] [Description("Omea database resource identifier.")] public virtual int Id { get { return Resource.Id; } } /// /// Gets or sets whether all the write-operations to the resource properties should be asynchronous. /// [Browsable(false)] public virtual bool Async { get { return _async; } set { _async = value; } } /// /// Gets or sets the async operations priority (applies only when is True). /// [Browsable(false)] public virtual JobPriority AsyncPriority { get { return _priority; } set { _priority = value; } } /// /// Writes a property value to the resource wrapped by the object, respecting the trigger. /// public virtual void WriteProp(string sPropName, object value) { if(Async) new ResourceProxy(Resource, AsyncPriority).SetPropAsync(sPropName, value); else new ResourceProxy(Resource).SetProp(sPropName, value); } /// /// Writes a property value to the resource wrapped by the object, respecting the trigger. /// public virtual void WriteProp(int nPropId, object value) { if(Async) new ResourceProxy(Resource, AsyncPriority).SetPropAsync(nPropId, value); else new ResourceProxy(Resource).SetProp(nPropId, value); } /// ///Compares the current instance with another object of the same type. /// /// /// ///A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance is less than obj. Zero This instance is equal to obj. Greater than zero This instance is greater than obj. /// /// ///An object to compare with this instance. ///obj is not the same type as this instance. 2 public int CompareTo(object obj) { IResourceObject other = obj as IResourceObject; if(other == null) return -1; return Resource.Id.CompareTo(other.Resource.Id); } public override bool Equals(object obj) { IResourceObject other = obj as IResourceObject; if(other == null) return false; return Resource.Id.Equals(other.Resource.Id); } /// ///Serves as a hash function for a particular type. is suitable for use in hashing algorithms and data structures like a hash table. /// /// /// ///A hash code for the current . /// ///2 public override int GetHashCode() { return Resource.Id.GetHashCode(); } /// /// Marks the resource as deleted. /// public virtual void Delete() { new ResourceProxy(Resource, JobPriority.BelowNormal).SetPropAsync(Core.Props.IsDeleted, true); } /// /// Unmarks the resource as deleted. /// public virtual void Undelete() { new ResourceProxy(Resource, JobPriority.BelowNormal).DeletePropAsync(Core.Props.IsDeleted); } public static bool operator ==(ResourceObject α, ResourceObject β) { if((ReferenceEquals(α, null)) && (ReferenceEquals(β, null))) return true; if((ReferenceEquals(α, null)) || (ReferenceEquals(β, null))) return false; return α.Resource == β.Resource; } public static bool operator !=(ResourceObject α, ResourceObject β) { if((ReferenceEquals(α, null)) && (ReferenceEquals(β, null))) return false; if((ReferenceEquals(α, null)) || (ReferenceEquals(β, null))) return true; return α.Resource != β.Resource; } } }