public object Initialize(object newEntity)
        {
            PutInitializeInterventionRequest ptr = (PutInitializeInterventionRequest)newEntity;
            MEPatientIntervention            pi  = null;

            try
            {
                pi = new MEPatientIntervention(this.UserId)
                {
                    Id            = ObjectId.GenerateNewId(),
                    PatientGoalId = ObjectId.Parse(ptr.PatientGoalId),
                    TTLDate       = System.DateTime.UtcNow.AddDays(_initializeDays),
                    StatusDate    = DateTime.UtcNow,
                    //LastUpdatedOn = DateTime.UtcNow,
                    //UpdatedBy = ObjectId.Parse(this.UserId),
                    Version = ptr.Version
                };

                using (PatientGoalMongoContext ctx = new PatientGoalMongoContext(_dbName))
                {
                    ctx.PatientInterventions.Collection.Insert(pi);

                    AuditHelper.LogDataAudit(this.UserId,
                                             MongoCollectionName.PatientIntervention.ToString(),
                                             pi.Id.ToString(),
                                             Common.DataAuditType.Insert,
                                             ptr.ContractNumber);
                }
                return(pi.Id.ToString());
            }
            catch (Exception) { throw; }
        }
 public object FindByID(string entityID)
 {
     try
     {
         PatientInterventionData interventionData = null;
         List <IMongoQuery>      queries          = new List <IMongoQuery>();
         queries.Add(Query.EQ(MEPatientIntervention.IdProperty, ObjectId.Parse(entityID)));
         queries.Add(Query.EQ(MEPatientIntervention.DeleteFlagProperty, false));
         queries.Add(Query.EQ(MEPatientIntervention.TTLDateProperty, BsonNull.Value));
         IMongoQuery mQuery = Query.And(queries);
         using (PatientGoalMongoContext ctx = new PatientGoalMongoContext(_dbName))
         {
             MEPatientIntervention b = ctx.PatientInterventions.Collection.Find(mQuery).FirstOrDefault();
             if (b != null)
             {
                 interventionData = new PatientInterventionData
                 {
                     Id            = b.Id.ToString(),
                     Description   = b.Description,
                     PatientGoalId = b.PatientGoalId.ToString(),
                     CategoryId    = b.CategoryId == null ? null : b.CategoryId.ToString(),
                     AssignedToId  = b.AssignedToId == null ? null : b.AssignedToId.ToString(),
                     BarrierIds    = Helper.ConvertToStringList(b.BarrierIds),
                     StatusId      = ((int)b.Status),
                     StatusDate    = b.StatusDate,
                     StartDate     = b.StartDate,
                     DueDate       = b.DueDate,
                     ClosedDate    = b.ClosedDate,
                     CreatedById   = b.RecordCreatedBy.ToString(),
                     DeleteFlag    = b.DeleteFlag,
                     Details       = b.Details
                 };
                 var mePG = ctx.PatientGoals.Collection.Find(Query.EQ(MEPatientGoal.IdProperty, ObjectId.Parse(interventionData.PatientGoalId))).SetFields(MEPatientGoal.PatientIdProperty, MEPatientGoal.NameProperty).FirstOrDefault();
                 if (mePG != null)
                 {
                     interventionData.PatientId = mePG.PatientId.ToString();
                     interventionData.GoalName  = mePG.Name;
                 }
             }
         }
         return(interventionData);
     }
     catch (Exception) { throw; }
 }
        public object Update(object entity)
        {
            bool result = false;
            PutUpdateInterventionRequest ir = (PutUpdateInterventionRequest)entity;
            PatientInterventionData      pi = ir.Intervention;

            try
            {
                using (PatientGoalMongoContext ctx = new PatientGoalMongoContext(_dbName))
                {
                    var q = MB.Query <MEPatientIntervention> .EQ(b => b.Id, ObjectId.Parse(pi.Id));

                    // Set the StatusDate to Now if the status is changed.
                    MEPatientIntervention existingPI = ctx.PatientInterventions.Collection.Find(q).SetFields(MEPatientIntervention.StatusProperty).FirstOrDefault();
                    if (existingPI != null)
                    {
                        if ((int)existingPI.Status != pi.StatusId)
                        {
                            pi.StatusDate = DateTime.UtcNow;
                        }
                        if ((pi.StatusId == (int)InterventionStatus.Removed || pi.StatusId == (int)InterventionStatus.Completed))
                        {
                            if (existingPI.Status != (InterventionStatus)pi.StatusId)
                            {
                                pi.ClosedDate = DateTime.UtcNow;
                            }
                        }
                        else
                        {
                            pi.ClosedDate = null;
                        }
                    }

                    var uv = new List <MB.UpdateBuilder>();
                    uv.Add(MB.Update.Set(MEPatientIntervention.VersionProperty, ir.Version));
                    uv.Add(MB.Update.Set(MEPatientIntervention.LastUpdatedOnProperty, System.DateTime.UtcNow));
                    uv.Add(MB.Update.Set(MEPatientIntervention.UpdatedByProperty, ObjectId.Parse(this.UserId)));

                    if (pi.TemplateId != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.TemplateIdProperty, ObjectId.Parse(pi.TemplateId)));
                    }

                    if (pi.Description != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.DescriptionProperty, pi.Description));
                    }
                    if (pi.Details != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.DetailProperty, pi.Details));
                    }
                    if (pi.StartDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.StartDateProperty, pi.StartDate));
                    }
                    else
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.StartDateProperty, BsonNull.Value));
                    }
                    if (pi.DueDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.DueDateProperty, pi.DueDate));
                    }
                    else
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.DueDateProperty, BsonNull.Value));
                    }
                    if (pi.StatusDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.StatusDateProperty, pi.StatusDate));
                    }
                    if (pi.StatusId != 0)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.StatusProperty, pi.StatusId));
                    }
                    if (pi.CategoryId != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.CategoryProperty, ObjectId.Parse(pi.CategoryId)));
                    }
                    if (pi.AssignedToId != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.AssignedToProperty, ObjectId.Parse(pi.AssignedToId)));
                    }
                    if (pi.BarrierIds != null)
                    {
                        uv.Add(MB.Update.SetWrapped <List <ObjectId> >(MEPatientIntervention.BarriersProperty, DTOUtil.ConvertObjectId(pi.BarrierIds)));
                    }
                    if (pi.PatientGoalId != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.PatientGoalIdProperty, ObjectId.Parse(pi.PatientGoalId)));
                    }
                    if (pi.ClosedDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.ClosedDateProperty, pi.ClosedDate));
                    }
                    else
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.ClosedDateProperty, BsonNull.Value));
                    }
                    uv.Add(MB.Update.Set(MEPatientIntervention.DeleteFlagProperty, pi.DeleteFlag));
                    DataAuditType type;
                    if (pi.DeleteFlag)
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.TTLDateProperty, System.DateTime.UtcNow.AddDays(_expireDays)));
                        type = Common.DataAuditType.Delete;
                    }
                    else
                    {
                        uv.Add(MB.Update.Set(MEPatientIntervention.TTLDateProperty, BsonNull.Value));
                        type = Common.DataAuditType.Update;
                    }
                    IMongoUpdate update = MB.Update.Combine(uv);
                    ctx.PatientInterventions.Collection.Update(q, update);

                    AuditHelper.LogDataAudit(this.UserId,
                                             MongoCollectionName.PatientIntervention.ToString(),
                                             pi.Id.ToString(),
                                             type,
                                             ir.ContractNumber);

                    result = true;
                }
                return(result as object);
            }
            catch (Exception) { throw; }
        }