コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rep"></param>
        /// <param name="entity"></param>
        public void SaveOrUpdate(IRepository rep, T entity)
        {
            // Todo: know how nhibernate to saveorupdate
            IVersionedEntity ve = entity as IVersionedEntity;

            if (ve == null)
            {
                throw new NotSupportedException("SaveOrUpdate only support IVersionedEntity!");
            }
            OperateArgs <T> e;

            if (ve.Version == 0)
            {
                e = new OperateArgs <T>(rep, OperateType.Save, entity);
            }
            else
            {
                e = new OperateArgs <T>(rep, OperateType.Update, entity);
            }

            PreparingEntity(e);

            OnEntityOperating(e);

            DoSaveOrUpdate(rep, entity);

            OnEntityOperated(e);

            PreparedEntity(e);
        }
コード例 #2
0
        /// <summary>
        /// Creates a FHIR reference.
        /// </summary>
        /// <typeparam name="TResource">The type of the t resource.</typeparam>
        /// <param name="targetEntity">The target entity.</param>
        /// <returns>Returns a reference instance.</returns>
        public static Reference CreatePlainReference <TResource>(IVersionedEntity targetEntity, RestOperationContext context) where TResource : DomainResourceBase, new()
        {
            var refer = Reference.CreateResourceReference((DomainResourceBase)DataTypeConverter.CreateResource <TResource>(targetEntity), context.IncomingRequest.Url);

            refer.Display = (targetEntity as Entity)?.Names?.FirstOrDefault()?.ToString();
            return(refer);
        }
コード例 #3
0
 public static IConcurrencyVersion CreateFromEntity(IVersionedEntity entity)
 {
     return(new ConcurrencyVersion
     {
         Version = entity.Version
     });
 }
コード例 #4
0
        public void Raise_sets_RaisedAt_correctly()
        {
            IVersionedEntity versionedEntity = Mock.Of <IVersionedEntity>();
            var sut = new FakeDomainEvent();

            sut.Raise(versionedEntity);

            sut.RaisedAt.Kind.Should().Be(DateTimeKind.Utc);
            sut.RaisedAt.Should().BeCloseTo(DateTime.UtcNow);
        }
コード例 #5
0
        private void HandleCreated(IVersionedEntity entity, SaveOrUpdateEvent @event)
        {
            entity.SetMemberValue(x => x.CreatedDate, DateTime.UtcNow);

            if (IsVersionedEntityWithUser(entity))
            {
                var user = GetCurrentUser(@event.Session);

                entity.SetMemberValue(PropertyName((IVersionedEntityWithUser <TUser> x) => x.CreatedBy), user);
            }
        }
コード例 #6
0
        public void Raise_sets_version_correctly()
        {
            int version = _fixture.Create <int>();
            IVersionedEntity versionedEntity =
                Mock.Of <IVersionedEntity>(x => x.Version == version);
            var sut = new FakeDomainEvent();

            sut.Raise(versionedEntity);

            sut.Version.Should().Be(version + 1);
        }
コード例 #7
0
        public void Raise(IVersionedEntity source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            SourceId = source.Id;
            Version  = source.Version + 1;
            RaisedAt = DateTimeOffset.Now;
        }
コード例 #8
0
        public void Raise_sets_SourceId_correctly()
        {
            var sourceId = Guid.NewGuid();
            IVersionedEntity versionedEntity =
                Mock.Of <IVersionedEntity>(x => x.Id == sourceId);
            var sut = new FakeDomainEvent();

            sut.Raise(versionedEntity);

            sut.SourceId.Should().Be(sourceId);
        }
コード例 #9
0
        /// <summary>
        /// Update the version identifier to the server identifier
        /// </summary>
        public void UpdateToServerCopy(IVersionedEntity newData)
        {
            this.m_tracer.TraceVerbose("Updating to remote version {0}", newData);
            // Update the ETag of the current version
            var idp        = typeof(IDataPersistenceService <>).MakeGenericType(newData.GetType());
            var idpService = ApplicationContext.Current.GetService(idp);

            if (idpService != null)
            {
                idp.GetRuntimeMethod("Update", new Type[] { newData.GetType() }).Invoke(idpService, new object[] { newData });
            }
        }
コード例 #10
0
        /// <summary>
        /// 增加或者修改
        /// </summary>
        /// <param name="entity"></param>
        public override void SaveOrUpdate(T entity)
        {
            if (m_suspendRep == null)
            {
                using (var rep = GenerateRepository())
                {
                    IVersionedEntity ve = entity as IVersionedEntity;
                    if (ve == null)
                    {
                        throw new NotSupportedException("SaveOrUpdate only support IVersionedEntity!");
                    }
                    OperateArgs <T> e;
                    if (ve.Version == 0)
                    {
                        e = new OperateArgs <T>(rep, OperateType.Save, entity);
                    }
                    else
                    {
                        e = new OperateArgs <T>(rep, OperateType.Update, entity);
                    }

                    try
                    {
                        BeginTransaction(rep, e);

                        SaveOrUpdate(rep, entity);

                        CommitTransaction(rep, e);

                        Clear();
                    }
                    catch (InvalidUserOperationException)
                    {
                        // 出错的时候,MemoryBll不清空
                        RollbackTransaction(rep, e);

                        throw;
                    }
                    catch (Exception)
                    {
                        RollbackTransaction(rep, e);

                        Clear();

                        throw;
                    }
                }
            }
            else
            {
                SaveOrUpdate(m_suspendRep, entity);
            }
        }
コード例 #11
0
        private void HandleModified(IVersionedEntity entity, PreUpdateEvent @event)
        {
            entity.SetMemberValue(x => x.LastModifiedDate, DateTime.UtcNow);
            @event.State[GetIndex(@event.Persister.PropertyNames, (IVersionedEntity x) => x.LastModifiedDate)] = entity.LastModifiedDate;

            if (IsVersionedEntityWithUser(entity))
            {
                var user = GetCurrentUser(@event.Session);
                entity.SetMemberValue(PropertyName((IVersionedEntityWithUser <TUser> x) => x.LastModifiedBy), user);
                @event.State[GetIndex(@event.Persister.PropertyNames, (IVersionedEntityWithUser <TUser> x) => x.LastModifiedBy)] = user;
            }
        }
コード例 #12
0
        /// <summary>
        /// Creates a FHIR reference.
        /// </summary>
        /// <typeparam name="TResource">The type of the t resource.</typeparam>
        /// <param name="targetEntity">The target entity.</param>
        /// <returns>Returns a reference instance.</returns>
        public static Reference <TResource> CreateReference <TResource>(IVersionedEntity targetEntity, RestOperationContext context) where TResource : DomainResourceBase, new()
        {
            if (targetEntity == null)
            {
                throw new ArgumentNullException(nameof(targetEntity));
            }
            else if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var refer = Reference.CreateResourceReference(DataTypeConverter.CreateResource <TResource>(targetEntity), context.IncomingRequest.Url);

            refer.Display = (targetEntity as Entity)?.Names?.FirstOrDefault()?.ToString();
            return(refer);
        }
コード例 #13
0
        /// <summary>
        /// Update the version identifier to the server identifier
        /// </summary>
        public void UpdateToServerCopy(IVersionedEntity newData, IVersionedEntity submittedData)
        {
            this.m_tracer.TraceVerbose("Updating to remote version {0}", newData);

            // Update the ETag of the current version
            var idp        = typeof(IDataPersistenceService <>).MakeGenericType(newData.GetType());
            var idpService = ApplicationContext.Current.GetService(idp) as IDataPersistenceService;

            if (idpService != null)
            {
                submittedData.VersionKey         = newData.VersionKey;
                submittedData.VersionSequence    = newData.VersionSequence;
                submittedData.PreviousVersionKey = newData.PreviousVersionKey;
                idpService.Update(submittedData);
            }
        }
コード例 #14
0
        /// <summary>
        /// 增加或修改
        /// </summary>
        /// <param name="entity"></param>
        public override void SaveOrUpdate(T entity)
        {
            IVersionedEntity ve = entity as IVersionedEntity;

            if (ve == null)
            {
                throw new NotSupportedException("SaveOrUpdate only support IVersionedEntity!");
            }
            if (ve.Version == 0)
            {
                Save(entity);
            }
            else
            {
                Update(entity);
            }
        }
コード例 #15
0
        /// <summary>
        /// Ensures a model has been persisted
        /// </summary>
        public static IIdentifiedEntity EnsureExists(this IIdentifiedEntity me, DataContext context, IPrincipal principal)
        {
            if (me == null)
            {
                return(null);
            }

            // Me
            var    vMe  = me as IVersionedEntity;
            String dkey = String.Format("{0}.{1}", me.GetType().FullName, me.Key);

            IIdentifiedEntity existing = me.TryGetExisting(context, principal);
            var idpInstance            = AdoAuditPersistenceService.GetPersister(me.GetType());

            // Existing exists?
            if (existing != null && me.Key.HasValue)
            {
                // Exists but is an old version
                if ((existing as IVersionedEntity)?.VersionKey != vMe?.VersionKey &&
                    vMe?.VersionKey != null && vMe?.VersionKey != Guid.Empty)
                {
                    // Update method
                    IVersionedEntity updated = idpInstance.Update(context, me) as IVersionedEntity;
                    me.Key = updated.Key;
                    if (vMe != null)
                    {
                        vMe.VersionKey = (updated as IVersionedEntity).VersionKey;
                    }
                    return(updated);
                }
                return(existing);
            }
            else if (existing == null) // Insert
            {
                IIdentifiedEntity inserted = idpInstance.Insert(context, me) as IIdentifiedEntity;
                me.Key = inserted.Key;

                if (vMe != null)
                {
                    vMe.VersionKey = (inserted as IVersionedEntity).VersionKey;
                }
                return(inserted);
            }
            return(existing);
        }
コード例 #16
0
        /// <summary>
        /// Ensures a model has been persisted
        /// </summary>
        public static void EnsureExists(this IIdentifiedEntity me, ModelDataContext context, IPrincipal principal)
        {
            // Me
            var    vMe  = me as IVersionedEntity;
            String dkey = String.Format("{0}.{1}", me.GetType().FullName, me.Key);

            IIdentifiedEntity existing = me.TryGetExisting(context, principal);
            var idpType     = typeof(IDataPersistenceService <>).MakeGenericType(me.GetType());
            var idpInstance = ApplicationContext.Current.GetService(idpType);

            // Existing exists?
            if (existing != null && me.Key.HasValue)
            {
                // Exists but is an old version
                if ((existing as IVersionedEntity)?.VersionKey != vMe?.VersionKey &&
                    vMe?.VersionKey != null && vMe?.VersionKey != Guid.Empty)
                {
                    // Update method
                    var updateMethod = idpInstance.GetType().GetRuntimeMethods().SingleOrDefault(o => o.Name == "Update" && o.GetParameters().Length == 3 && o.GetParameters()[0].ParameterType == typeof(ModelDataContext));
                    if (updateMethod != null)
                    {
                        IVersionedEntity updated = updateMethod.Invoke(idpInstance, new object[] { context, me, principal }) as IVersionedEntity;
                        me.Key = updated.Key;
                        if (vMe != null)
                        {
                            vMe.VersionKey = (updated as IVersionedEntity).VersionKey;
                        }
                    }
                }
            }
            else // Insert
            {
                var insertMethod = idpInstance.GetType().GetRuntimeMethods().SingleOrDefault(o => o.Name == "Insert" && o.GetParameters().Length == 3 && o.GetParameters()[0].ParameterType == typeof(ModelDataContext));
                if (insertMethod != null)
                {
                    IIdentifiedEntity inserted = insertMethod.Invoke(idpInstance, new object[] { context, me, principal }) as IIdentifiedEntity;
                    me.Key = inserted.Key;

                    if (vMe != null)
                    {
                        vMe.VersionKey = (inserted as IVersionedEntity).VersionKey;
                    }
                }
            }
        }
コード例 #17
0
ファイル: Repository.cs プロジェクト: sansay61/OMS
        public void Create(T entity)
        {
            Type type = entity.GetType();

            //checking whether an entity is inherited from iversionedentity
            if (typeof(IVersionedEntity).IsAssignableFrom(type))
            {
                IVersionedEntity versionedEntity = (IVersionedEntity)entity;
                versionedEntity.Iscurrent   = 1;
                versionedEntity.Version     = 1;
                versionedEntity.Chtimestamp = DateTime.Now;
                Session.Save(versionedEntity);
                versionedEntity.Inid = versionedEntity.Id;
                Session.Save(versionedEntity);
            }
            else
            {
                Session.Save(entity);
            }
        }
コード例 #18
0
        /// <summary>
        /// Creates the resource.
        /// </summary>
        /// <typeparam name="TResource">The type of the t resource.</typeparam>
        /// <param name="resource">The resource.</param>
        /// <returns>TResource.</returns>
        public static TResource CreateResource <TResource>(IVersionedEntity resource) where TResource : ResourceBase, new()
        {
            var retVal = new TResource();

            retVal.Id        = resource.Key.ToString();
            retVal.VersionId = resource.VersionKey.ToString();

            // metadata
            retVal.Meta = new ResourceMetadata()
            {
                LastUpdated = (resource as IdentifiedData).ModifiedOn.DateTime,
                VersionId   = resource.VersionKey?.ToString(),
                Profile     = new Uri("http://santedb.org/fhir")
            };
            retVal.Meta.Tags = (resource as ITaggable)?.Tags.Select(o => DataTypeConverter.ToFhirTag(o)).ToList();
            // TODO: Configure this namespace / coding scheme
            retVal.Meta.Security = (resource as ISecurable)?.Policies?.Where(o => o.GrantType == Core.Model.Security.PolicyGrantType.Grant).Select(o => new FhirCoding(new Uri("http://santedb.org/security/policy"), o.Policy.Oid)).ToList() ?? new List <FhirCoding>();
            retVal.Meta.Security.Add(new FhirCoding(new Uri("http://santedb.org/security/policy"), PermissionPolicyIdentifiers.ReadClinicalData));
            retVal.Extension = (resource as IExtendable)?.Extensions.Where(o => o.ExtensionTypeKey != ExtensionTypeKeys.JpegPhotoExtension).Select(o => DataTypeConverter.ToExtension(o)).ToList();
            return(retVal);
        }
コード例 #19
0
ファイル: Repository.cs プロジェクト: sansay61/OMS
        public void Update(T entity)
        {
            Type type = entity.GetType();

            //checking whether an entity is inherited from iversionedentity
            if (typeof(IVersionedEntity).IsAssignableFrom(type))
            {
                IVersionedEntity oldentity = (IVersionedEntity)GetById(entity.Id);
                entity.Id = 0;
                IVersionedEntity versionedEntity = (IVersionedEntity)Session.Merge((IVersionedEntity)entity);
                oldentity.Iscurrent = 0;
                versionedEntity.Version++;
                versionedEntity.Chtimestamp = DateTime.Now;
                versionedEntity.Iscurrent   = 1;
                Session.Save(versionedEntity);
                Session.Save(oldentity);
            }
            else
            {
                Session.Update(entity);
            }
        }
コード例 #20
0
        /// <summary>
        /// Import element
        /// </summary>
        private void ImportElement(IdentifiedData data)
        {
            var idpType = typeof(IDataPersistenceService <>).MakeGenericType(data.GetType());
            var svc     = OpenIZ.Mobile.Core.ApplicationContext.Current.GetService(idpType) as IDataPersistenceService;

            try
            {
                IdentifiedData existing = null;
                if (!(data is Bundle))
                {
                    existing = svc.Get(data.Key.Value) as IdentifiedData;
                }

                this.m_tracer.TraceVerbose("Inserting object from inbound queue: {0}", data);
                if (existing == null)
                {
                    svc.Insert(data);
                }
                else
                {
                    IVersionedEntity ver = data as IVersionedEntity;
                    if (ver?.VersionKey == (existing as IVersionedEntity)?.VersionKey) // no need to update
                    {
                        this.m_tracer.TraceVerbose("Object {0} is already up to date", existing);
                    }
                    else
                    {
                        svc.Update(data);
                    }
                }
            }
            catch (Exception e)
            {
                this.m_tracer.TraceError("Error inserting object data: {0}", e);
                throw;
            }
        }
コード例 #21
0
 public void SetOriginalVersion(IVersionedEntity entity, byte[] originalValue)
 {
     Entry(entity).Property("Version").OriginalValue = originalValue;
 }
コード例 #22
0
 /// <summary>
 /// Get the identifier
 /// </summary>
 public static Identifier <Guid> Id(this IVersionedEntity me)
 {
     return(new Identifier <Guid>(me.Key.Value, me.VersionKey.Value));
 }
コード例 #23
0
 public CreateDomainException(IVersionedEntity entity, Exception innerException)
     : base(_formatCreateException(entity.GetType(), entity.Id), innerException)
 {
 }
コード例 #24
0
        /// <summary>
        /// Ensures a model has been persisted
        /// </summary>
        public static IIdentifiedEntity EnsureExists(this IIdentifiedEntity me, DataContext context, IPrincipal principal)
        {
            if (me == null)
            {
                return(null);
            }

            // Me
            var    vMe  = me as IVersionedEntity;
            String dkey = String.Format("{0}.{1}", me.GetType().FullName, me.Key);

            IIdentifiedEntity existing = me.TryGetExisting(context, principal);
            var idpInstance            = AdoPersistenceService.GetPersister(me.GetType());

            // Don't touch the child just return reference
            if (!AdoPersistenceService.GetConfiguration().AutoInsertChildren)
            {
                if (existing != null)
                {
                    if (me.Key != existing.Key ||
                        vMe?.VersionKey != (existing as IVersionedEntity)?.VersionKey)
                    {
                        me.CopyObjectData(existing); // copy data into reference
                    }
                    return(existing);
                }
                else
                {
                    throw new KeyNotFoundException(me.Key.Value.ToString());
                }
            }

            // Existing exists?
            if (existing != null && me.Key.HasValue)
            {
                // Exists but is an old version
                if ((existing as IVersionedEntity)?.VersionKey != vMe?.VersionKey &&
                    vMe?.VersionKey != null && vMe?.VersionKey != Guid.Empty)
                {
                    // Update method
                    IVersionedEntity updated = idpInstance.Update(context, me, principal) as IVersionedEntity;
                    me.Key = updated.Key;
                    if (vMe != null)
                    {
                        vMe.VersionKey = (updated as IVersionedEntity).VersionKey;
                    }
                    return(updated);
                }
                return(existing);
            }
            else if (existing == null) // Insert
            {
                IIdentifiedEntity inserted = idpInstance.Insert(context, me, principal) as IIdentifiedEntity;
                me.Key = inserted.Key;

                if (vMe != null)
                {
                    vMe.VersionKey = (inserted as IVersionedEntity).VersionKey;
                }
                return(inserted);
            }
            return(existing);
        }
コード例 #25
0
        /// <summary>
        /// Ensures a model has been persisted
        /// </summary>
        public static IIdentifiedEntity EnsureExists(this IIdentifiedEntity me, DataContext context, bool createIfNotExists = true, Type ensureType = null)
        {
            if (me == null)
            {
                return(null);
            }

            // Me
            var serviceInstance = ApplicationServiceContext.Current.GetService <AdoPersistenceService>();
            var vMe             = me as IVersionedEntity;

            var idpInstance            = serviceInstance.GetPersister(ensureType ?? me.GetType());
            IIdentifiedEntity existing = me.TryGetExisting(context) ?? idpInstance.Get(context, me.Key.GetValueOrDefault()) as IIdentifiedEntity;

            // Don't touch the child just return reference
            if (!serviceInstance.GetConfiguration().AutoInsertChildren || !createIfNotExists)
            {
                if (existing != null)
                {
                    if (me.Key != existing.Key ||
                        vMe?.VersionKey != (existing as IVersionedEntity)?.VersionKey)
                    {
                        me.CopyObjectData(existing); // copy data into reference
                    }
                    return(existing);
                }
                else
                {
                    throw new KeyNotFoundException(me.Key.Value.ToString());
                }
            }

            // Existing exists?
            if (existing != null && me.Key.HasValue)
            {
                // Exists but is an old version
                if ((existing as IVersionedEntity)?.VersionSequence < vMe?.VersionSequence &&
                    vMe?.VersionKey != null && vMe?.VersionKey != Guid.Empty)
                {
                    // Update method
                    IVersionedEntity updated = idpInstance.Update(context, me) as IVersionedEntity;
                    me.Key = updated.Key;
                    if (vMe != null)
                    {
                        vMe.VersionKey = (updated as IVersionedEntity).VersionKey;
                    }
                    return(updated);
                }
                return(existing);
            }
            else if (existing == null) // Insert
            {
                IIdentifiedEntity inserted = idpInstance.Insert(context, me) as IIdentifiedEntity;
                me.Key = inserted.Key;

                if (vMe != null)
                {
                    vMe.VersionKey = (inserted as IVersionedEntity).VersionKey;
                }
                return(inserted);
            }
            return(existing);
        }
コード例 #26
0
 private bool IsVersionedEntityWithUser(IVersionedEntity entity)
 {
     return(entity.GetType().IsAssignableToGenericType(_genericVersionedEntityType));
 }
コード例 #27
0
 public DeleteDomainException(IVersionedEntity entity, Exception innerException)
     : base($"Unable to delete entity {entity}", innerException)
 {
 }
コード例 #28
0
 public ConcurrencyDomainException(IVersionedEntity entity, Exception innerException)
     : base(_formatConcurrencyException(entity.GetType(), entity.Id, entity.Version), innerException)
 {
 }