public void PendingQuestionFormUpdateForProgram(PendingQuestionForm entity)
        {
            TraceCallEnterEvent.Raise();
              try
              {
            if (entity.ProgramID.IsNull)
              throw new ArgumentNullException("PendingQuestionForm.ProgramID", "A program azonosítója nincs megadva.");
            ProgramService programSrv = new ProgramService(m_DataContext);
            Program program = programSrv.ProgramSelect(entity.ProgramID);
            if (program == null)
              throw new ApplicationException("A megadott azonosítóval nem létezik program.");

            string writerRole = program.OrganisationRef.Value.ToString() + ".Writer";
            PrincipalPermission permWriter = new PrincipalPermission(Thread.CurrentPrincipal.Identity.Name, writerRole);
            permWriter.Demand();

            // required:
            if (entity.TemplateRef.IsNull)
              throw new ArgumentNullException("PendingQuestionForm.TemplateRef", "A sablon azonosítója nincs megadva.");
            if (entity.Status.Length == 0)
              throw new ArgumentNullException("PendingQuestionForm.Status", "A státusz nincs megadva.");

            // logical check:
            TemplateCategoryService templCatSrv = new TemplateCategoryService(m_DataContext);
            TemplateCategory templCat = templCatSrv.TemplateCategorySelect(entity.TemplateRef, program.ProgramCategoryRef);
            if (templCat == null)
              throw new ApplicationException("A megadott programhoz nem tölthetõ ki ez a sablon.");

            PendingQuestionForm selectedPending = base.PendingQuestionFormSelect(entity.ID);
            if (selectedPending == null)
              throw new ApplicationException("A módosítani kívánt kérdõív nem létezik.");

            // set data:
            entity.SentBy = Thread.CurrentPrincipal.Identity.Name;
            entity.SentDate = DBDateTime.Now;
            entity.LastModifiedByUser = DBDateTime.Now;
            if (entity.Status == "UWD" || entity.Status == "NWD")
              entity.IsActual = true;
            else
            {
              entity.IsActual = false;
            }
            PendingQuestionFormDetailService detailService = new PendingQuestionFormDetailService(m_DataContext);
            ProgramPendingQuestionFormService programPendingSrv = new ProgramPendingQuestionFormService(m_DataContext);
            m_DataContext.BeginNestedTran();
            try
            {
              base.PendingQuestionFormUpdate(entity);
              base.DeleteChildrenByDetailOfPendingQuestionForm(entity.ID);
              foreach (PendingQuestionFormDetail detail in entity.Details.All)
              {
            detailService.PendingQuestionFormDetailInsert(detail);
              }
              m_DataContext.CommitNested();
            }
            catch
            {
              m_DataContext.RollbackNested();
              throw;
            }

            // Log success
            BusinessAuditEvent.Success(
              new EventParameter("PendingQuestionFormID", entity.ID.ToString()),
              new EventParameter("ProgramID", entity.ProgramID.ToString())
              );
            TraceCallReturnEvent.Raise();
            return;
              }
              catch (Exception ex)
              {
            ExceptionManager.Publish(ex);
            BusinessAuditEvent.Fail(
              new EventParameter("Exception", ex.ToString()),
              new EventParameter("PendingQuestionFormID", entity.ID.ToString()),
              new EventParameter("ProgramID", entity.ProgramID.ToString())
              );
            TraceCallReturnEvent.Raise(false);
            throw;
              }
        }
        public void PendingQuestionFormInsertForProgram(PendingQuestionForm entity)
        {
            TraceCallEnterEvent.Raise();
              try
              {
            if (entity.ProgramID.IsNull)
              throw new ArgumentNullException("PendingQuestionForm.ProgramID", "A program azonosítója nincs megadva.");
            ProgramService programSrv = new ProgramService(m_DataContext);
            Program program = programSrv.ProgramSelect(entity.ProgramID);
            if (program == null)
              throw new ApplicationException("A megadott azonosítóval nem létezik program.");

            string writerRole = program.OrganisationRef.Value.ToString() + ".Writer";
            PrincipalPermission permWriter = new PrincipalPermission(Thread.CurrentPrincipal.Identity.Name, writerRole);
            permWriter.Demand();

            // required:
            if (entity.TemplateRef.IsNull)
              throw new ArgumentNullException("PendingQuestionForm.TemplateRef", "A sablon azonosítója nincs megadva.");
            if (entity.Status.Length == 0)
              throw new ArgumentNullException("PendingQuestionForm.Status", "A státusz nincs megadva.");

            /* TODO módosításnál ez is kötelezõ:
            if(entity.QuestionFormRef.IsNull)
              throw new ArgumentNullException("PendingQuestionForm.QuestionFormRef", "Az eredeti kérdõív azonosítója nincs megadva.");
              */

            // logical check:
            TemplateCategoryService templCatSrv = new TemplateCategoryService(m_DataContext);
            TemplateCategory templCat = templCatSrv.TemplateCategorySelect(entity.TemplateRef, program.ProgramCategoryRef);
            if (templCat == null)
              throw new ApplicationException("A megadott programhoz nem tölthetõ ki ez a sablon.");

            // ellenõrizzük, hogy nincs folyamatban ilyen kérdõív kitöltése:
            TemplateService srv = new TemplateService(m_DataContext);
            Template tmplCurrentPending =
              srv.TemplateSelectOfProgramPendingQuestionFormByTemplateID(program.ID, entity.TemplateRef);

            if (!tmplCurrentPending.Status.IsNull)
            {
              throw new ApplicationException("A kérdõív nem módosítható, mert a módosítás már folyamatban van.");
            }

            // set data:
            entity.SentBy = Thread.CurrentPrincipal.Identity.Name;
            entity.SentDate = DBDateTime.Now;
            entity.LastModifiedByUser = DBDateTime.Now;
            if (entity.Status == "UWD" || entity.Status == "NWD")
              entity.IsActual = true;
            else
            {
              entity.IsActual = false;
            }

            ProgramPendingQuestionForm programPending = new ProgramPendingQuestionForm(program.ID, entity.ID);

            PendingQuestionFormDetailService detailService = new PendingQuestionFormDetailService(m_DataContext);
            ProgramPendingQuestionFormService programPendingSrv = new ProgramPendingQuestionFormService(m_DataContext);
            m_DataContext.BeginNestedTran();
            try
            {
              base.PendingQuestionFormInsert(entity);
              foreach (PendingQuestionFormDetail detail in entity.Details.All)
              {
            detailService.PendingQuestionFormDetailInsert(detail);
              }
              programPendingSrv.ProgramPendingQuestionFormInsert(programPending);
              m_DataContext.CommitNested();
            }
            catch
            {
              m_DataContext.RollbackNested();
              throw;
            }

            // Log success
            BusinessAuditEvent.Success(
              new EventParameter("PendingQuestionFormID", entity.ID.ToString()),
              new EventParameter("ProgramID", entity.ProgramID.ToString())
              );
            TraceCallReturnEvent.Raise();
            return;
              }
              catch (Exception ex)
              {
            ExceptionManager.Publish(ex);
            BusinessAuditEvent.Fail(
              new EventParameter("Exception", ex.ToString()),
              new EventParameter("PendingQuestionFormID", entity.ID.ToString()),
              new EventParameter("ProgramID", entity.ProgramID.ToString())
              );
            TraceCallReturnEvent.Raise(false);
            throw;
              }
        }
        public void PendingQuestionFormUpdateForOrganisation(PendingQuestionForm entity)
        {
            TraceCallEnterEvent.Raise();
              try
              {
            if (entity.OrganisationID.IsNull)
              throw new ArgumentNullException("PendingQuestionForm.OrganisationID", "A szervezet azonosítója nincs megadva.");
            string writerRole = entity.OrganisationID.Value.ToString() + ".Writer";
            PrincipalPermission permWriter = new PrincipalPermission(Thread.CurrentPrincipal.Identity.Name, writerRole);
            permWriter.Demand();

            OrganisationService orgSrv = new OrganisationService(m_DataContext);
            Organisation organisation = orgSrv.OrganisationSelect(entity.OrganisationID);
            if (organisation == null)
              throw new ApplicationException("A megadott azonosítóval nem létezik szervezet.");

            // required:
            if (entity.TemplateRef.IsNull)
              throw new ArgumentNullException("PendingQuestionForm.TemplateRef", "A sablon azonosítója nincs megadva.");
            if (entity.Status.Length == 0)
              throw new ArgumentNullException("PendingQuestionForm.Status", "A státusz nincs megadva.");

            // logical check:
            TemplateCategoryService templCatSrv = new TemplateCategoryService(m_DataContext);
            TemplateCategory templCat = templCatSrv.TemplateCategorySelect(entity.TemplateRef, "ORG");
            if (templCat == null)
              throw new ApplicationException("Szervezethez nem tölthetõ ki ez a sablon.");

            PendingQuestionForm selectedPending = base.PendingQuestionFormSelect(entity.ID); //Ez van az adatbázisban
            if (selectedPending == null)
              throw new ApplicationException("A módosítani kívánt kérdõív nem létezik.");
            PendingQuestionFormDetailService detailService = new PendingQuestionFormDetailService(m_DataContext);
            PendingQuestionFormDetailContainer selectedPendingDetails = base.SelectChildrenByDetailOfPendingQuestionForm(selectedPending.ID);
            // set data:
            entity.SentBy = Thread.CurrentPrincipal.Identity.Name;
            entity.SentDate = DBDateTime.Now;
            entity.LastModifiedByUser = DBDateTime.Now;
            bool onlyIsActualChanged = true;
            if (entity.Status == "UWD" || entity.Status == "NWD")
            {
              entity.IsActual = true; //Új- jóváhagyásra vár, módosítiott jóváhagyásra vár
              //Kell egy ellenõrzés, hogy csak a bIsActual változott-e
              //onlyIsActualChanged =
              //  entity.QuestionFormRef == selectedPending.QuestionFormRef && entity.TemplateRef == selectedPending.TemplateRef;

              //foreach (PendingQuestionFormDetail detail in entity.Details.All)
              //{
              //   foreach (PendingQuestionFormDetail selectedDetail in selectedPendingDetails.All)
              //   {
              //     if(detail.TemplateDetailRef == selectedDetail.TemplateDetailRef && detail.TemplateRef == selectedDetail.TemplateRef)
              //     {
              //       onlyIsActualChanged &= detail.Answer == selectedDetail.Answer;
              //     }
              //   }
              //}
              //Ha az alapadatok és minden váasz egyforma, akkor csak a bIsActualt akarta menteni, tehát jóv kell hagyni a végén
            }
            else
            {
              entity.IsActual = false;
            }

            m_DataContext.BeginNestedTran();
            try
            {
              base.PendingQuestionFormUpdate(entity);
              base.DeleteChildrenByDetailOfPendingQuestionForm(entity.ID);
              foreach (PendingQuestionFormDetail detail in entity.Details.All)
              {
            detailService.PendingQuestionFormDetailInsert(detail);
              }
              m_DataContext.CommitNested();

            }
            catch
            {
              m_DataContext.RollbackNested();
              throw;
            }

            // Log success
            BusinessAuditEvent.Success(
              new EventParameter("PendingQuestionFormID", entity.ID.ToString()),
              new EventParameter("OrganisationID", entity.OrganisationID.ToString())
              );
            TraceCallReturnEvent.Raise();
            return;
              }
              catch (Exception ex)
              {
            ExceptionManager.Publish(ex);
            BusinessAuditEvent.Fail(
              new EventParameter("Exception", ex.ToString()),
              new EventParameter("PendingQuestionFormID", entity.ID.ToString()),
              new EventParameter("OrganisationID", entity.OrganisationID.ToString())
              );
            TraceCallReturnEvent.Raise(false);
            throw;
              }
        }