private static Application BindingToClass(tblApplication exist)
        {
            var app = new Application
            {
                AppId = exist.AppId,
                AcquisitionId = exist.AcquisitionId ?? 0,
                ApplicantId = exist.ApplicantId ?? 0,
                ApplicationStatus = exist.ApplicationStatus,
                ArmyNo = exist.ArmyNo,
                CreatedBy = exist.CreatedBy,
                CreatedDt = exist.CreatedDt,
                LastModifiedBy = exist.LastModifiedBy,
                LastModifiedDt = exist.LastModifiedDt,
                DentalExamine = exist.DentalExamine,
                FinalSelectionInd = exist.FinalSelectionInd,
                FinalSelectionRemark = exist.FinalSelectionRemark,
                FinalShortlistedInd = exist.FinalShortlistedInd,
                FirstSelActualAcqLocationId = exist.FirstSelActualAcqLocationId,
                FirstSelectionAttdnInd = exist.FirstSelectionAttdnInd,
                FirstSelectionDate = exist.FirstSelectionDate,
                FirstSelectionInd = exist.FirstSelectionInd,
                FirstShortlistedInd = exist.FirstShortlistedInd,
                InvitationFirstSel = exist.InvitationFirstSel,
                MedicalExamine = exist.MedicalExamine,
                Panel1Score = exist.Panel1Score,
                Panel2Score = exist.Panel2Score,
                Panel3Score = exist.Panel3Score,
                PhysicalExamination = exist.PhysicalExamination,
                PhysicalTest = exist.PhysicalTest,
                ReportDuty = exist.ReportDuty,
                SecurityAADK = exist.SecurityAADK,
                SecurityAADKRemark = exist.SecurityAADKRemark,
                SecurityBSPP = exist.SecurityBSPP,
                SecurityBSPPRemark = exist.SecurityBSPPRemark,
                SecurityJPN = exist.SecurityJPN,
                SecurityJPNRemark = exist.SecurityJPNRemark,
                SecurityKPM = exist.SecurityKPM,
                SecurityKPMRemark = exist.SecurityKPMRemark,
                SecurityPDRM = exist.SecurityPDRM,
                SecurityPDRMRemark = exist.SecurityPDRMRemark,
                SecurityStatus = exist.SecurityStatus,
                SelectedAcqLocationId = exist.SelectedAcqLocationId,
                SelectionCenterId = exist.SelectionCenterId,
                SuppDocumentReview = exist.SuppDocumentReview,
                Urine = exist.Urine,
                WrittenTest = exist.WrittenTest,
                ReportDutyLocId = exist.ReportDutyLocId,
                ReportDutyDate = exist.ReportDutyDate,
                FinalSelectionEndDate = exist.FinalSelectionEndDate,
                FinalSelectionStartDate = exist.FinalSelectionStartDate,
                FinalSelActualAcqLocationId = exist.FinalSelActualAcqLocationId,
                NoKawalan = exist.NoKawalan,
                FinalServiceCd = exist.FinalServiceCd,
                FirstSelectionEndDate = exist.FirstSelectionEndDate,
            };

            if (exist.AcquisitionId.HasValue)
                app.Acquisition =
                    ObjectBuilder.GetObject<IAcquisitionPersistence>("AcquisitionPersistence")
                        .GetAcquisition(exist.AcquisitionId.Value);

            if (exist.ApplicantId.HasValue && exist.AcquisitionId.HasValue)
                app.ApplicantSubmitted =
                    ObjectBuilder.GetObject<IApplicantSubmittedPersistence>("ApplicantSubmittedPersistence")
                        .GetApplicant(exist.ApplicantId.Value, exist.AcquisitionId.Value);

            if (exist.ReportDutyLocId.HasValue)
                if (exist.tblREFReportDutyLoc != null)
                    app.ReportDutyLocation = new Location()
                    {
                        LocationId = exist.tblREFReportDutyLoc.ReportDutyLocId,
                        LocationNm = exist.tblREFReportDutyLoc.ReportDutyLoc
                    };

            if (exist.FinalSelActualAcqLocationId.HasValue)
                app.FinalSelectionLocation = ObjectBuilder.GetObject<IAcquisitionPersistence>("AcquisitionPersistence").GetLocation(exist.FinalSelActualAcqLocationId.Value);

            if (exist.FirstSelActualAcqLocationId.HasValue)
                app.FirstSelectionLocation = ObjectBuilder.GetObject<IAcquisitionPersistence>("AcquisitionPersistence").GetLocation(exist.FirstSelActualAcqLocationId.Value);

            return app;
        }
        public int AddNew(Application application)
        {
            using (var entities = new atmEntities())
            {
                var exist = (from a in entities.tblApplications where a.ApplicantId == application.ApplicantId && a.AcquisitionId == application.AcquisitionId select a).SingleOrDefault();
                if (null != exist)
                {
                    application.AppId = exist.AppId;
                    return Update(application);
                }

                var app = new tblApplication
                {
                    AcquisitionId = application.AcquisitionId,
                    ApplicantId = application.ApplicantId,
                    ApplicationStatus = application.ApplicationStatus,
                    ArmyNo = application.ArmyNo,
                    CreatedBy = application.CreatedBy,
                    CreatedDt = DateTime.Now,
                    DentalExamine = application.DentalExamine,
                    FinalSelectionInd = application.FinalSelectionInd,
                    FinalSelectionRemark = application.FinalSelectionRemark,
                    FinalShortlistedInd = application.FinalShortlistedInd,
                    FirstSelActualAcqLocationId = application.FirstSelActualAcqLocationId,
                    FirstSelectionAttdnInd = application.FirstSelectionAttdnInd,
                    FirstSelectionDate = application.FirstSelectionDate,
                    FirstSelectionInd = application.FirstSelectionInd,
                    FirstShortlistedInd = application.FirstShortlistedInd,
                    InvitationFirstSel = application.InvitationFirstSel,
                    MedicalExamine = application.MedicalExamine,
                    Panel1Score = application.Panel1Score,
                    Panel2Score = application.Panel2Score,
                    Panel3Score = application.Panel3Score,
                    PhysicalExamination = application.PhysicalExamination,
                    PhysicalTest = application.PhysicalTest,
                    ReportDuty = application.ReportDuty,
                    SecurityAADK = application.SecurityAADK,
                    SecurityAADKRemark = application.SecurityAADKRemark,
                    SecurityBSPP = application.SecurityBSPP,
                    SecurityBSPPRemark = application.SecurityBSPPRemark,
                    SecurityJPN = application.SecurityJPN,
                    SecurityJPNRemark = application.SecurityJPNRemark,
                    SecurityKPM = application.SecurityKPM,
                    SecurityKPMRemark = application.SecurityKPMRemark,
                    SecurityPDRM = application.SecurityPDRM,
                    SecurityPDRMRemark = application.SecurityPDRMRemark,
                    SecurityStatus = application.SecurityStatus,
                    SelectedAcqLocationId = application.SelectedAcqLocationId,
                    SelectionCenterId = application.SelectionCenterId,
                    SuppDocumentReview = application.SuppDocumentReview,
                    Urine = application.Urine,
                    WrittenTest = application.WrittenTest,
                    FinalSelActualAcqLocationId = application.FinalSelActualAcqLocationId,
                    FinalSelectionEndDate = application.FinalSelectionEndDate,
                    FinalSelectionStartDate = application.FinalSelectionStartDate,
                    FinalServiceCd = application.FinalServiceCd,
                    FirstSelectionEndDate = application.FirstSelectionEndDate,
                    NoKawalan = application.NoKawalan,
                    ReportDutyDate = application.ReportDutyDate,
                    ReportDutyLocId = application.ReportDutyLocId,

                };
                entities.tblApplications.Add(app);
                if (entities.SaveChanges() > 0) return app.AppId;
            }
            return 0;
        }