public object Update(object entity)
        {
            PutProgramActionProcessingRequest p = (PutProgramActionProcessingRequest)entity;
            ProgramDetail pg = p.Program;

            try
            {
                using (ProgramMongoContext ctx = new ProgramMongoContext(_dbName))
                {
                    var q = MB.Query <MEPatientProgram> .EQ(b => b.Id, ObjectId.Parse(p.ProgramId));

                    List <Module> mods = DTOUtils.CloneAppDomainModules(pg.Modules, this.UserId);

                    var uv = new List <MB.UpdateBuilder>();
                    uv.Add(MB.Update.Set(MEPatientProgram.CompletedProperty, pg.Completed));
                    uv.Add(MB.Update.Set(MEPatientProgram.EnabledProperty, pg.Enabled));
                    uv.Add(MB.Update.Set(MEPatientProgram.OrderProperty, pg.Order));
                    //uv.Add(MB.Update.Set(MEPatientProgram.ProgramStateProperty, (ProgramState)pg.ProgramState)); // depricated - Use Element state instead.
                    uv.Add(MB.Update.Set(MEPatientProgram.LastUpdatedOnProperty, System.DateTime.UtcNow));
                    uv.Add(MB.Update.Set(MEPatientProgram.UpdatedByProperty, ObjectId.Parse(this.UserId)));
                    uv.Add(MB.Update.Set(MEPatientProgram.VersionProperty, pg.Version));
                    if (pg.AssignTo != null && !string.IsNullOrEmpty(pg.AssignTo))
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.AssignToProperty, pg.AssignTo));
                    }
                    else
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.AssignToProperty, BsonNull.Value));
                    }

                    if (pg.ElementState != 0)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.StateProperty, (ElementState)pg.ElementState));
                    }
                    if (pg.StateUpdatedOn != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.StateUpdatedOnProperty, pg.StateUpdatedOn));
                    }
                    if (pg.Status != 0)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.StatusProperty, (Status)pg.Status));
                    }
                    if (pg.AssignBy != null && !string.IsNullOrEmpty(pg.AssignBy))
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.AssignByProperty, pg.AssignBy));
                    }
                    if (pg.AssignDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.AssignDateProperty, pg.AssignDate));
                    }
                    if (pg.AttrEndDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.AttributeEndDateProperty, pg.AttrEndDate));
                    }
                    if (pg.AttrStartDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.AttributeStartDateProperty, pg.AttrStartDate));
                    }
                    if (pg.Client != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.ClientProperty, pg.Client));
                    }
                    if (pg.CompletedBy != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.CompletedByProperty, pg.CompletedBy));
                    }
                    if (pg.ContractProgramId != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.ContractProgramIdProperty, ObjectId.Parse(pg.ContractProgramId)));
                    }
                    if (pg.DateCompleted != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.CompletedOnProperty, pg.DateCompleted));
                    }
                    if (pg.Description != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.DescriptionProperty, pg.Description));
                    }
                    if (pg.EndDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.EndDateProperty, pg.EndDate));
                    }
                    if (pg.Name != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.NameProperty, pg.Name));
                    }
                    if (pg.Next != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.NextProperty, DTOUtils.ParseObjectId(pg.Next)));
                    }
                    if (pg.Previous != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.PreviousProperty, DTOUtils.ParseObjectId(pg.Previous)));
                    }
                    if (pg.ShortName != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.ShortNameProperty, pg.ShortName));
                    }
                    if (pg.SourceId != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.SourceIdProperty, pg.SourceId));
                    }
                    if (pg.StartDate != null)
                    {
                        uv.Add(MB.Update.Set(MEPatientProgram.StartDateProperty, pg.StartDate));
                    }
                    if (mods != null)
                    {
                        uv.Add(MB.Update.SetWrapped <List <Module> >(MEPatientProgram.ModulesProperty, mods));
                    }
                    if (pg.SpawnElement != null)
                    {
                        uv.Add(MB.Update.SetWrapped <List <SpawnElement> >(MEPatientProgram.SpawnProperty, DTOUtils.GetSpawnElements(pg.SpawnElement)));
                    }
                    if (pg.ObjectivesData != null)
                    {
                        uv.Add(MB.Update.SetWrapped <List <Objective> >(MEPatientProgram.ObjectivesInfoProperty, DTOUtils.GetObjectives(pg.ObjectivesData)));
                    }

                    IMongoUpdate update = MB.Update.Combine(uv);
                    ctx.PatientPrograms.Collection.Update(q, update);

                    AuditHelper.LogDataAudit(this.UserId,
                                             MongoCollectionName.PatientProgram.ToString(),
                                             p.ProgramId,
                                             Common.DataAuditType.Update,
                                             _dbName);
                }
                return(pg);
            }
            catch (Exception ex)
            {
                throw new Exception("DD:PatientProgramRepository:Update()::" + ex.Message, ex.InnerException);
            }
        }