Example #1
0
 public void DeleteMe()
 {
     ObjectStore.DeleteAllObjectReferences(Uuid);
 }
Example #2
0
        /// <summary>
        /// Always returns an updated object. So clients must ensure that they receive
        /// the return value of this method and use it as the updated object.
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        private bool Save(string comment = null)
        {
            bool success = false;

            if (!IsObjectReady())
            {
                return(success);
            }

            if (_IsInsideJob && !_IsDirty)
            {
                return(success);
            }

            bool nascent = !ObjectStore.ObjectExists(Uuid);

            #region << Unique Attribute Check >>

            // Check if unique attribute set:
            var props = GetType().GetProperties().Where(
                prop => Attribute.IsDefined(prop, typeof(UniqueAttribute)));

            PropertyInfo propertyToBeUniquelyIndexed = null;
            foreach (var p in props)
            {
                propertyToBeUniquelyIndexed = p;
                // There is only one unique property allowed per class.
                // So we can safely break:
                break;
            }

            object propertyValueToBeIndexed = propertyToBeUniquelyIndexed.GetValue(this);
            if (propertyValueToBeIndexed == null)
            {
                throw new ArgumentException(
                          string.Format("Value for unique property `{0}.{1}` not set or null.", GetType().Name, propertyToBeUniquelyIndexed.Name)
                          );
            }

            string uuidOfSameIndex = OdCepManager.Indexing.GetUuidForUniqueValue(GetType(), propertyValueToBeIndexed.ToString());

            bool objWithSameUniqueValueAlreadyExists      = uuidOfSameIndex != null;
            bool currentObjIsSameAsOneWithSameUniqueValue = Uuid.Equals(uuidOfSameIndex);

            if (nascent)
            {
                // This is a new object.
                if (objWithSameUniqueValueAlreadyExists)
                {
                    throw new ArgumentException(
                              string.Format("Duplicate value for unique property `{0}.{1}`: `{2}`",
                                            GetType().Name, propertyToBeUniquelyIndexed.Name, propertyValueToBeIndexed)
                              );
                }
            }
            else
            {
                // This is an already existing object.
                if (objWithSameUniqueValueAlreadyExists && !currentObjIsSameAsOneWithSameUniqueValue)
                {
                    // Currently indexed object is not this object.
                    throw new ArgumentException(
                              string.Format("Duplicate value for unique property `{0}.{1}`: `{2}`",
                                            GetType().Name, propertyToBeUniquelyIndexed.Name, propertyValueToBeIndexed)
                              );
                }
            }

            #endregion << Unique Attribute Check >>

            if (nascent)
            {
                success = SaveAsNew(comment ?? string.Empty);
            }
            else
            {
                success = SaveAsExisting(comment ?? string.Empty);
            }

            _IsDirty = _IsInsideJob = false;
            return(success);
        }