//***************************** ATTAINS_REPORT_LOG **************************************************
        public static int InsertOrUpdateATTAINS_REPORT_LOG(int? aTTAINS_LOG_IDX, int? aTTAINS_REPORT_IDX,  DateTime? sUBMIT_DT, 
            string sUBMIT_FILE, byte[] rESPONSE_FILE, string rESPONSE_TXT, string cDX_SUBMIT_TRANSID, string cDX_SUBMIT_STATUS, string cREATE_USER = "system")
        {
            using (OpenEnvironmentEntities ctx = new OpenEnvironmentEntities())
            {
                Boolean insInd = false;
                try
                {
                    T_ATTAINS_REPORT_LOG a = new T_ATTAINS_REPORT_LOG();

                    if (aTTAINS_LOG_IDX != null)
                        a = (from c in ctx.T_ATTAINS_REPORT_LOG
                             where c.ATTAINS_LOG_IDX == aTTAINS_LOG_IDX
                             select c).FirstOrDefault();

                    if (aTTAINS_REPORT_IDX == null) //insert case
                    {
                        insInd = true;
                    }

                    if (aTTAINS_REPORT_IDX != null) a.ATTAINS_REPORT_IDX = aTTAINS_REPORT_IDX.ConvertOrDefault<int>();
                    if (sUBMIT_DT != null) a.SUBMIT_DT = sUBMIT_DT.ConvertOrDefault<DateTime>();
                    if (sUBMIT_FILE != null) a.SUBMIT_FILE = sUBMIT_FILE;
                    if (rESPONSE_FILE != null) a.RESPONSE_FILE = rESPONSE_FILE;
                    if (rESPONSE_TXT != null) a.RESPONSE_TXT = rESPONSE_TXT;
                    if (cDX_SUBMIT_TRANSID != null) a.CDX_SUBMIT_TRANSID = cDX_SUBMIT_TRANSID;
                    if (cDX_SUBMIT_STATUS != null) a.CDX_SUBMIT_STATUS = cDX_SUBMIT_STATUS;

                    if (insInd) //insert case
                    {
                        ctx.AddToT_ATTAINS_REPORT_LOG(a);
                    }

                    ctx.SaveChanges();

                    return a.ATTAINS_LOG_IDX ;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }
Example #2
0
        // *************************** IMPORT: RESULT ******************************
        // *****************************************************************************
        public static int InsertOrUpdateWQX_IMPORT_TEMP_RESULT(global::System.Int32? tEMP_RESULT_IDX, int tEMP_SAMPLE_IDX, global::System.Int32? rESULT_IDX, string dATA_LOGGER_LINE, 
            string rESULT_DETECT_CONDITION, global::System.String cHAR_NAME, string mETHOD_SPECIATION_NAME, string rESULT_SAMP_FRACTION, global::System.String rESULT_MSR, global::System.String rESULT_MSR_UNIT,
            string rESULT_MSR_QUAL, string rESULT_STATUS, string sTATISTIC_BASE_CODE, string rESULT_VALUE_TYPE, string wEIGHT_BASIS, string tIME_BASIS, string tEMP_BASIS, string pARTICAL_BASIS,
            string pRECISION_VALUE, string bIAS_VALUE, string cONFIDENCE_INTERVAL_VALUE, string uP_CONFIDENCE_LIMIT, string lOW_CONFIDENCE_LIMIT, string rESULT_COMMENT, string dEPTH_HEIGHT_MSR,
            string dEPTH_HEIGHT_MSR_UNIT, string dEPTHALTITUDEREFPOINT, string bIO_INTENT_NAME, string bIO_INDIVIDUAL_ID, string bIO_SUBJECT_TAXONOMY, string bIO_UNIDENTIFIED_SPECIES_ID,
            string bIO_SAMPLE_TISSUE_ANATOMY, string gRP_SUMM_COUNT_WEIGHT_MSR, string gRP_SUMM_COUNT_WEIGHT_MSR_UNIT, string tAX_DTL_CELL_FORM, string tAX_DTL_CELL_SHAPE, string tAX_DTL_HABIT,
            string tAX_DTL_VOLTINISM, string tAX_DTL_POLL_TOLERANCE, string tAX_DTL_POLL_TOLERANCE_SCALE, string tAX_DTL_TROPHIC_LEVEL, string tAX_DTL_FUNC_FEEDING_GROUP1,
            string tAX_DTL_FUNC_FEEDING_GROUP2, string tAX_DTL_FUNC_FEEDING_GROUP3, string fREQ_CLASS_CODE, string fREQ_CLASS_UNIT, string fREQ_CLASS_UPPER, string fREQ_CLASS_LOWER,
            int? aNALYTIC_METHOD_IDX, string aNALYTIC_METHOD_ID, string aNALYTIC_METHOD_CTX, string aNALYTIC_METHOD_NAME,
            int? lAB_IDX, string lAB_NAME, DateTime? lAB_ANALYSIS_START_DT, DateTime? lAB_ANALYSIS_END_DT, string lAB_ANALYSIS_TIMEZONE, string rESULT_LAB_COMMENT_CODE,
            string mETHOD_DETECTION_LEVEL, string lAB_REPORTING_LEVEL, string pQL, string lOWER_QUANT_LIMIT, string uPPER_QUANT_LIMIT, string dETECTION_LIMIT_UNIT, int? lAB_SAMP_PREP_IDX,
            string lAB_SAMP_PREP_ID, string lAB_SAMP_PREP_CTX, DateTime? lAB_SAMP_PREP_START_DT, DateTime? lAB_SAMP_PREP_END_DT, string dILUTION_FACTOR, string sTATUS_CD, string sTATUS_DESC, bool BioIndicator, string orgID, Boolean autoImportRefDataInd)
        {
            using (OpenEnvironmentEntities ctx = new OpenEnvironmentEntities())
            {
                Boolean insInd = false;
                try
                {
                    T_WQX_IMPORT_TEMP_RESULT a = new T_WQX_IMPORT_TEMP_RESULT();

                    if (tEMP_RESULT_IDX != null)
                        a = (from c in ctx.T_WQX_IMPORT_TEMP_RESULT
                             where c.TEMP_RESULT_IDX == tEMP_RESULT_IDX
                             select c).FirstOrDefault();

                    if (tEMP_RESULT_IDX == null) //insert case
                        insInd = true;

                    a.TEMP_SAMPLE_IDX = tEMP_SAMPLE_IDX;
                    if (rESULT_IDX != null) a.RESULT_IDX = rESULT_IDX;

                    if (!string.IsNullOrEmpty(dATA_LOGGER_LINE))
                    {
                        a.DATA_LOGGER_LINE = dATA_LOGGER_LINE.Trim().SubStringPlus(0, 15);
                    }

                    if (rESULT_DETECT_CONDITION == "DNQ" || rESULT_MSR == "DNQ") { rESULT_DETECT_CONDITION = "Detected Not Quantified"; rESULT_MSR = "DNQ"; }
                    if (rESULT_DETECT_CONDITION == "ND" || rESULT_MSR == "ND") { rESULT_DETECT_CONDITION = "Not Detected"; rESULT_MSR = "ND"; }
                    if (rESULT_DETECT_CONDITION == "NR" || rESULT_MSR == "NR") { rESULT_DETECT_CONDITION = "Not Reported"; rESULT_MSR = "NR"; }
                    if (rESULT_DETECT_CONDITION == "PAQL" || rESULT_MSR == "PAQL") { rESULT_DETECT_CONDITION = "Present Above Quantification Limit"; rESULT_MSR = "PAQL"; }
                    if (rESULT_DETECT_CONDITION == "PBQL" || rESULT_MSR == "PBQL") { rESULT_DETECT_CONDITION = "Present Below Quantification Limit"; rESULT_MSR = "PBQL"; }

                    if (!string.IsNullOrEmpty(rESULT_DETECT_CONDITION))
                    {
                        a.RESULT_DETECT_CONDITION = rESULT_DETECT_CONDITION.Trim().SubStringPlus(0, 35);
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("ResultDetectionCondition", rESULT_DETECT_CONDITION.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Result Detection Condition not valid. "; }
                    }

                    if (!string.IsNullOrEmpty(cHAR_NAME))
                    {
                        a.CHAR_NAME = cHAR_NAME.Trim().SubStringPlus(0, 120);
                        if (db_Ref.GetT_WQX_REF_CHARACTERISTIC_ExistCheck(cHAR_NAME.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Characteristic Name not valid. "; }
                    }

                    if (!string.IsNullOrEmpty(mETHOD_SPECIATION_NAME))
                    {
                        a.METHOD_SPECIATION_NAME = mETHOD_SPECIATION_NAME.Trim().SubStringPlus(0, 20);
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("MethodSpeciation", mETHOD_SPECIATION_NAME.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Method Speciation not valid. "; }
                    }

                    if (!string.IsNullOrEmpty(rESULT_SAMP_FRACTION))
                    {
                        a.RESULT_SAMP_FRACTION = rESULT_SAMP_FRACTION.Trim().SubStringPlus(0, 25);
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("ResultSampleFraction", rESULT_SAMP_FRACTION.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Result Sample Fraction not valid. "; }
                    }
                    else
                    {
                        if (db_Ref.GetT_WQX_REF_CHARACTERISTIC_SampFracReqCheck(cHAR_NAME.Trim()) == true) { sTATUS_CD = "F"; sTATUS_DESC += "Sample Fraction must be reported."; }
                    }

                    if (!string.IsNullOrEmpty(rESULT_MSR))
                    {
                        a.RESULT_MSR = rESULT_MSR.Trim().SubStringPlus(0, 60).Replace(",","");
                    }
                    else
                    {
                        if ( string.IsNullOrEmpty(rESULT_DETECT_CONDITION) ) { sTATUS_CD = "F"; sTATUS_DESC += "Either Result Measure or Result Detection Condition must be reported."; }
                    }

                    if (!string.IsNullOrEmpty(rESULT_MSR_UNIT))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("MeasureUnit", rESULT_MSR_UNIT.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Measurement Unit not valid. "; }
                        a.RESULT_MSR_UNIT = rESULT_MSR_UNIT.Trim().SubStringPlus(0, 12);
                    }

                    if (!string.IsNullOrEmpty(rESULT_MSR_QUAL))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("ResultMeasureQualifier", rESULT_MSR_QUAL.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Measurement Qualifier not valid. "; }
                        a.RESULT_MSR_QUAL = rESULT_MSR_QUAL.Trim().SubStringPlus(0, 5);
                    }

                    if (!string.IsNullOrEmpty(rESULT_STATUS))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("ResultStatus", rESULT_STATUS.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Result Status not valid. "; }
                        a.RESULT_STATUS = rESULT_STATUS.Trim().SubStringPlus(0, 12);
                    }

                    if (!string.IsNullOrEmpty(sTATISTIC_BASE_CODE))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("StatisticalBase", sTATISTIC_BASE_CODE.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Statistic Base Code not valid. "; }
                        a.STATISTIC_BASE_CODE = sTATISTIC_BASE_CODE.Trim().SubStringPlus(0, 25);
                    }

                    if (!string.IsNullOrEmpty(rESULT_VALUE_TYPE))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("ResultValueType", rESULT_VALUE_TYPE.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Result Value Type not valid. "; }
                        a.RESULT_VALUE_TYPE = rESULT_VALUE_TYPE.Trim().SubStringPlus(0, 20);
                    }

                    if (!string.IsNullOrEmpty(wEIGHT_BASIS))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("ResultWeightBasis", wEIGHT_BASIS.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Weight Basis not valid. "; }
                        a.WEIGHT_BASIS = wEIGHT_BASIS.Trim().SubStringPlus(0, 15);
                    }

                    if (!string.IsNullOrEmpty(tIME_BASIS))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("ResultTimeBasis", tIME_BASIS.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Time Basis not valid. "; }
                        a.TIME_BASIS = tIME_BASIS.Trim().SubStringPlus(0, 12);
                    }

                    if (!string.IsNullOrEmpty(tEMP_BASIS))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("ResultTemperatureBasis", tEMP_BASIS.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Temp Basis not valid. "; }
                        a.TEMP_BASIS = tEMP_BASIS.Trim().SubStringPlus(0, 12);
                    }

                    if (!string.IsNullOrEmpty(pARTICAL_BASIS))
                        a.PARTICLESIZE_BASIS = pARTICAL_BASIS.Trim().SubStringPlus(0, 40);

                    if (!string.IsNullOrEmpty(pRECISION_VALUE))
                        a.PRECISION_VALUE = pRECISION_VALUE.Trim().SubStringPlus(0,60);

                    if (!string.IsNullOrEmpty(bIAS_VALUE))
                        a.BIAS_VALUE = bIAS_VALUE.Trim().SubStringPlus(0,60);

                    if (!string.IsNullOrEmpty(cONFIDENCE_INTERVAL_VALUE))
                        a.CONFIDENCE_INTERVAL_VALUE = cONFIDENCE_INTERVAL_VALUE.Trim().SubStringPlus(0,15);

                    if (!string.IsNullOrEmpty(uP_CONFIDENCE_LIMIT))
                        a.UPPER_CONFIDENCE_LIMIT = uP_CONFIDENCE_LIMIT.Trim().SubStringPlus(0, 15);

                    if (!string.IsNullOrEmpty(lOW_CONFIDENCE_LIMIT))
                        a.LOWER_CONFIDENCE_LIMIT = lOW_CONFIDENCE_LIMIT.Trim().SubStringPlus(0, 15);

                    if (!string.IsNullOrEmpty(rESULT_COMMENT))
                        a.RESULT_COMMENT = rESULT_COMMENT.Trim().SubStringPlus(0,4000);

                    if (!string.IsNullOrEmpty(dEPTH_HEIGHT_MSR))
                        a.DEPTH_HEIGHT_MSR = dEPTH_HEIGHT_MSR.Trim().SubStringPlus(0, 12);

                    if (!string.IsNullOrEmpty(dEPTH_HEIGHT_MSR_UNIT))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("MeasureUnit", dEPTH_HEIGHT_MSR_UNIT.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Result Depth Unit not valid. "; }
                        a.DEPTH_HEIGHT_MSR_UNIT = dEPTH_HEIGHT_MSR_UNIT.Trim().SubStringPlus(0, 12);
                    }

                    if (!string.IsNullOrEmpty(dEPTHALTITUDEREFPOINT))
                        a.DEPTHALTITUDEREFPOINT = dEPTHALTITUDEREFPOINT.Trim().SubStringPlus(0, 125);

                    if (BioIndicator == true)
                    {

                        if (!string.IsNullOrEmpty(bIO_INTENT_NAME))
                        {
                            if (db_Ref.GetT_WQX_REF_DATA_ByKey("BiologicalIntent", bIO_INTENT_NAME.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Biological Intent not valid. "; }
                            a.BIO_INTENT_NAME = bIO_INTENT_NAME.Trim().SubStringPlus(0, 35);

                            if (string.IsNullOrEmpty(bIO_SUBJECT_TAXONOMY)) { sTATUS_CD = "F"; sTATUS_DESC += "Taxonomy must be reported when intent is reported. "; }
                        }

                        if (!string.IsNullOrEmpty(bIO_INDIVIDUAL_ID))
                            a.BIO_INDIVIDUAL_ID = bIO_INDIVIDUAL_ID.Trim().SubStringPlus(0, 4);

                        if (!string.IsNullOrEmpty(bIO_SUBJECT_TAXONOMY))
                        {
                            if (db_Ref.GetT_WQX_REF_DATA_ByKey("Taxon", bIO_SUBJECT_TAXONOMY.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Subject Taxonomy not valid. "; }
                            a.BIO_SUBJECT_TAXONOMY = bIO_SUBJECT_TAXONOMY.Trim().SubStringPlus(0, 120);

                            if (string.IsNullOrEmpty(bIO_INTENT_NAME)) { sTATUS_CD = "F"; sTATUS_DESC += "Biological intent must be reported when taxonomy is reported. "; }
                        }

                        if (!string.IsNullOrEmpty(bIO_UNIDENTIFIED_SPECIES_ID))
                            a.BIO_UNIDENTIFIED_SPECIES_ID = bIO_UNIDENTIFIED_SPECIES_ID.Trim().SubStringPlus(0, 120);

                        if (!string.IsNullOrEmpty(bIO_SAMPLE_TISSUE_ANATOMY))
                        {
                            if (db_Ref.GetT_WQX_REF_DATA_ByKey("SampleTissueAnatomy", bIO_SAMPLE_TISSUE_ANATOMY.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Sample Tissue Anatomy not valid. "; }
                            a.BIO_SAMPLE_TISSUE_ANATOMY = bIO_SAMPLE_TISSUE_ANATOMY.Trim().SubStringPlus(0, 30);
                        }

                        if (!string.IsNullOrEmpty(gRP_SUMM_COUNT_WEIGHT_MSR))
                            a.GRP_SUMM_COUNT_WEIGHT_MSR = gRP_SUMM_COUNT_WEIGHT_MSR.Trim().SubStringPlus(0, 12);

                        if (!string.IsNullOrEmpty(gRP_SUMM_COUNT_WEIGHT_MSR_UNIT))
                        {
                            if (db_Ref.GetT_WQX_REF_DATA_ByKey("MeasureUnit", gRP_SUMM_COUNT_WEIGHT_MSR_UNIT.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Group Summary Unit not valid. "; }
                            a.GRP_SUMM_COUNT_WEIGHT_MSR_UNIT = gRP_SUMM_COUNT_WEIGHT_MSR_UNIT.Trim().SubStringPlus(0, 12);
                        }

                        if (!string.IsNullOrEmpty(tAX_DTL_CELL_FORM))
                        {
                            if (db_Ref.GetT_WQX_REF_DATA_ByKey("CellForm", tAX_DTL_CELL_FORM.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Cell Form not valid. "; }
                            a.TAX_DTL_CELL_FORM = tAX_DTL_CELL_FORM.Trim().SubStringPlus(0, 11);
                        }

                        if (!string.IsNullOrEmpty(tAX_DTL_CELL_SHAPE))
                        {
                            if (db_Ref.GetT_WQX_REF_DATA_ByKey("CellShape", tAX_DTL_CELL_SHAPE.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Cell Shape not valid. "; }
                            a.TAX_DTL_CELL_SHAPE = tAX_DTL_CELL_SHAPE.Trim().SubStringPlus(0, 18);
                        }

                        if (!string.IsNullOrEmpty(tAX_DTL_HABIT))
                        {
                            if (db_Ref.GetT_WQX_REF_DATA_ByKey("Habit", tAX_DTL_HABIT.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Habit not valid. "; }
                            a.TAX_DTL_HABIT = tAX_DTL_HABIT.Trim().SubStringPlus(0, 15);
                        }

                        if (!string.IsNullOrEmpty(tAX_DTL_VOLTINISM))
                        {
                            if (db_Ref.GetT_WQX_REF_DATA_ByKey("Voltinism", tAX_DTL_VOLTINISM.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Voltinism not valid. "; }
                            a.TAX_DTL_VOLTINISM = tAX_DTL_VOLTINISM.Trim().SubStringPlus(0, 25);
                        }

                        if (!string.IsNullOrEmpty(tAX_DTL_POLL_TOLERANCE))
                            a.TAX_DTL_POLL_TOLERANCE = tAX_DTL_POLL_TOLERANCE.Trim().SubStringPlus(0, 4);

                        if (!string.IsNullOrEmpty(tAX_DTL_POLL_TOLERANCE_SCALE))
                            a.TAX_DTL_POLL_TOLERANCE_SCALE = tAX_DTL_POLL_TOLERANCE_SCALE.Trim().SubStringPlus(0, 50);

                        if (!string.IsNullOrEmpty(tAX_DTL_TROPHIC_LEVEL))
                            a.TAX_DTL_TROPHIC_LEVEL = tAX_DTL_TROPHIC_LEVEL.Trim().SubStringPlus(0, 4);

                        if (!string.IsNullOrEmpty(tAX_DTL_FUNC_FEEDING_GROUP1))
                            a.TAX_DTL_FUNC_FEEDING_GROUP1 = tAX_DTL_FUNC_FEEDING_GROUP1.Trim().SubStringPlus(0, 6);

                        if (!string.IsNullOrEmpty(tAX_DTL_FUNC_FEEDING_GROUP2))
                            a.TAX_DTL_FUNC_FEEDING_GROUP2 = tAX_DTL_FUNC_FEEDING_GROUP2.Trim().SubStringPlus(0, 6);

                        if (!string.IsNullOrEmpty(tAX_DTL_FUNC_FEEDING_GROUP3))
                            a.TAX_DTL_FUNC_FEEDING_GROUP3 = tAX_DTL_FUNC_FEEDING_GROUP3.Trim().SubStringPlus(0, 6);

                        if (!string.IsNullOrEmpty(fREQ_CLASS_CODE))
                            a.FREQ_CLASS_CODE = fREQ_CLASS_CODE.Trim().SubStringPlus(0, 50);

                        if (!string.IsNullOrEmpty(fREQ_CLASS_UNIT))
                            a.FREQ_CLASS_UNIT = fREQ_CLASS_UNIT.Trim().SubStringPlus(0, 12);

                        if (!string.IsNullOrEmpty(fREQ_CLASS_CODE))
                            a.FREQ_CLASS_UPPER = fREQ_CLASS_UPPER.Trim().SubStringPlus(0, 8);

                        if (!string.IsNullOrEmpty(fREQ_CLASS_CODE))
                            a.FREQ_CLASS_LOWER = fREQ_CLASS_LOWER.Trim().SubStringPlus(0, 8);

                    }

                    //analysis method
                    //first populate the IDX if it is supplied
                    if (aNALYTIC_METHOD_IDX != null)
                        a.ANALYTIC_METHOD_IDX = aNALYTIC_METHOD_IDX;
                    else
                    {
                        //if ID is supplied but Context is not, set context to org id
                        if (!string.IsNullOrEmpty(aNALYTIC_METHOD_ID) && string.IsNullOrEmpty(aNALYTIC_METHOD_CTX))
                            aNALYTIC_METHOD_CTX = orgID;

                        //if we now have values for the ID and context
                        if (!string.IsNullOrEmpty(aNALYTIC_METHOD_ID) && !string.IsNullOrEmpty(aNALYTIC_METHOD_CTX))
                        {
                            //see if matching collection method exists
                            T_WQX_REF_ANAL_METHOD am = db_Ref.GetT_WQX_REF_ANAL_METHODByIDandContext(aNALYTIC_METHOD_ID.Trim(), aNALYTIC_METHOD_CTX.Trim());
                            if (am != null)
                                a.ANALYTIC_METHOD_IDX = am.ANALYTIC_METHOD_IDX;
                            else  //no matching anal method lookup found
                            {
                                if (autoImportRefDataInd == true)
                                {
                                    db_Ref.InsertOrUpdateT_WQX_REF_ANAL_METHOD(null, aNALYTIC_METHOD_ID.Trim(), aNALYTIC_METHOD_CTX.Trim(), aNALYTIC_METHOD_NAME.Trim(), "", true);
                                }
                                else
                                { sTATUS_CD = "F"; sTATUS_DESC += "No matching Analysis Method found - please add it at the Reference Data screen first. "; }
                            }

                            //****************************************
                            a.ANALYTIC_METHOD_ID = aNALYTIC_METHOD_ID.Trim().SubStringPlus(0, 20);
                            a.ANALYTIC_METHOD_CTX = aNALYTIC_METHOD_CTX.Trim().SubStringPlus(0, 120);

                            if (!string.IsNullOrEmpty(aNALYTIC_METHOD_NAME))
                                a.ANALYTIC_METHOD_NAME = aNALYTIC_METHOD_NAME.Trim().SubStringPlus(0, 120);
                        }
                        else
                        {
                            //if IDX, ID, and Context not supplied, lookup the method from the default Org Char reference list
                            T_WQX_REF_CHAR_ORG rco = db_Ref.GetT_WQX_REF_CHAR_ORGByName(orgID, cHAR_NAME.Trim().SubStringPlus(0, 120));
                            if (rco != null)
                            {
                                a.ANALYTIC_METHOD_IDX = rco.DEFAULT_ANAL_METHOD_IDX;
                                if (rco.DEFAULT_ANAL_METHOD_IDX != null)
                                {
                                    T_WQX_REF_ANAL_METHOD anal = db_Ref.GetT_WQX_REF_ANAL_METHODByIDX(rco.DEFAULT_ANAL_METHOD_IDX.ConvertOrDefault<int>());
                                    if (anal != null)
                                    {
                                        a.ANALYTIC_METHOD_ID = anal.ANALYTIC_METHOD_ID;
                                        a.ANALYTIC_METHOD_NAME = anal.ANALYTIC_METHOD_NAME;
                                        a.ANALYTIC_METHOD_CTX = anal.ANALYTIC_METHOD_CTX;
                                    }
                                }
                            }

                        }
                    }

                    //********** LABORATORY **********
                    if (lAB_IDX != null)
                        a.LAB_IDX = lAB_IDX;
                    else
                    {
                        if (!string.IsNullOrEmpty(lAB_NAME))
                        {
                             a.LAB_NAME = lAB_NAME;

                            //see if matching lab name exists for this org
                            T_WQX_REF_LAB lab = db_Ref.GetT_WQX_REF_LAB_ByIDandContext(lAB_NAME, orgID);
                            if (lab == null) {
                                if (autoImportRefDataInd == true)
                                {
                                    db_Ref.InsertOrUpdateT_WQX_REF_LAB(null, lAB_NAME.Trim(), null, null, orgID, true);
                                }
                                else { sTATUS_CD = "F"; sTATUS_DESC += "No matching Lab Name found - please add it at the Reference Data screen first. "; }
                            }
                            else
                                a.LAB_IDX = lab.LAB_IDX;
                        }
                    }

                    if (lAB_ANALYSIS_START_DT != null)
                    {
                        //fix improperly formatted datetime
                        if (lAB_ANALYSIS_START_DT.ConvertOrDefault<DateTime>().Year < 1900)
                            lAB_ANALYSIS_START_DT = null;

                        a.LAB_ANALYSIS_START_DT = lAB_ANALYSIS_START_DT;
                    }
                    if (lAB_ANALYSIS_END_DT != null)
                    {
                        //fix improperly formatted datetime
                        if (lAB_ANALYSIS_END_DT.ConvertOrDefault<DateTime>().Year < 1900)
                            lAB_ANALYSIS_END_DT = null;

                        a.LAB_ANALYSIS_END_DT = lAB_ANALYSIS_END_DT;
                    }

                    if (!string.IsNullOrEmpty(lAB_ANALYSIS_TIMEZONE))
                    {
                        a.LAB_ANALYSIS_TIMEZONE = lAB_ANALYSIS_TIMEZONE.Trim().SubStringPlus(0, 4);
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("TimeZone", lAB_ANALYSIS_TIMEZONE.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "TimeZone not valid. "; }
                    }
                    else
                    {
                        //put in Timezone if missing
                        if (lAB_ANALYSIS_START_DT != null || lAB_ANALYSIS_END_DT != null)
                            a.LAB_ANALYSIS_TIMEZONE = Utils.GetWQXTimeZoneByDate(a.LAB_ANALYSIS_TIMEZONE.ConvertOrDefault<DateTime>());
                    }

                    if (!string.IsNullOrEmpty(rESULT_LAB_COMMENT_CODE))
                        a.RESULT_LAB_COMMENT_CODE = rESULT_LAB_COMMENT_CODE.Trim().SubStringPlus(0, 3);

                    if (!string.IsNullOrEmpty(mETHOD_DETECTION_LEVEL))
                        a.METHOD_DETECTION_LEVEL = mETHOD_DETECTION_LEVEL.Trim().SubStringPlus(0, 12);

                    if (!string.IsNullOrEmpty(lAB_REPORTING_LEVEL))
                        a.LAB_REPORTING_LEVEL = lAB_REPORTING_LEVEL.Trim().SubStringPlus(0, 12);

                    if (!string.IsNullOrEmpty(pQL))
                        a.PQL = pQL.Trim().SubStringPlus(0, 12);

                    if (!string.IsNullOrEmpty(lOWER_QUANT_LIMIT))
                        a.LOWER_QUANT_LIMIT = lOWER_QUANT_LIMIT.Trim().SubStringPlus(0, 12);

                    //if result is PBQL, but no value has been reported for MDL, LRL, PQL, or Lower Quant Limit, then grab from Org Char default
                    if (rESULT_DETECT_CONDITION == "Present Below Quantification Limit" && string.IsNullOrEmpty(mETHOD_DETECTION_LEVEL) && string.IsNullOrEmpty(lAB_REPORTING_LEVEL) && string.IsNullOrEmpty(pQL) && string.IsNullOrEmpty(lOWER_QUANT_LIMIT))
                    {
                        T_WQX_REF_CHAR_ORG rco = db_Ref.GetT_WQX_REF_CHAR_ORGByName(orgID, cHAR_NAME);
                        if (rco != null)
                            a.LOWER_QUANT_LIMIT = rco.DEFAULT_LOWER_QUANT_LIMIT;

                        //if still null, then error
                        if (a.LOWER_QUANT_LIMIT == null)
                            { sTATUS_CD = "F"; sTATUS_DESC += "No Lower Quantification limit reported or default value specified. "; }
                    }

                    if (!string.IsNullOrEmpty(uPPER_QUANT_LIMIT))
                        a.UPPER_QUANT_LIMIT = uPPER_QUANT_LIMIT.Trim().SubStringPlus(0, 12);

                    //if result is PAQL, but no value has been reported for MDL, LRL, PQL, or Lower Quant Limit, then grab from Org Char default
                    if (rESULT_DETECT_CONDITION == "Present Above Quantification Limit" && string.IsNullOrEmpty(mETHOD_DETECTION_LEVEL) && string.IsNullOrEmpty(lAB_REPORTING_LEVEL) && string.IsNullOrEmpty(pQL) && string.IsNullOrEmpty(uPPER_QUANT_LIMIT))
                    {
                        T_WQX_REF_CHAR_ORG rco = db_Ref.GetT_WQX_REF_CHAR_ORGByName(orgID, cHAR_NAME);
                        if (rco != null)
                            a.UPPER_QUANT_LIMIT = rco.DEFAULT_UPPER_QUANT_LIMIT;

                        //if still null, then error
                        if (a.UPPER_QUANT_LIMIT == null)
                        { sTATUS_CD = "F"; sTATUS_DESC += "No Upper Quantification limit reported. "; }
                    }

                    if (!string.IsNullOrEmpty(dETECTION_LIMIT_UNIT))
                    {
                        if (db_Ref.GetT_WQX_REF_DATA_ByKey("MeasureUnit", dETECTION_LIMIT_UNIT.Trim()) == false) { sTATUS_CD = "F"; sTATUS_DESC += "Detection Level Unit not valid. "; }
                        a.DETECTION_LIMIT_UNIT = dETECTION_LIMIT_UNIT.Trim().SubStringPlus(0, 12);
                    }

                    //********** LAB SAMPLE PREP
                    if (lAB_SAMP_PREP_IDX != null)
                        a.LAB_SAMP_PREP_IDX = lAB_SAMP_PREP_IDX;
                    else
                    {
                        if (!string.IsNullOrEmpty(lAB_SAMP_PREP_ID))
                        {
                            //set context to org id if none is provided
                            if (string.IsNullOrEmpty(lAB_SAMP_PREP_CTX))
                                lAB_SAMP_PREP_CTX = orgID;

                            a.LAB_SAMP_PREP_ID = lAB_SAMP_PREP_ID.Trim().SubStringPlus(0, 20);
                            a.LAB_SAMP_PREP_CTX = lAB_SAMP_PREP_CTX.Trim().SubStringPlus(0, 120);

                            //see if matching lab prep method exists for this org
                            T_WQX_REF_SAMP_PREP ppp = db_Ref.GetT_WQX_REF_SAMP_PREP_ByIDandContext(lAB_SAMP_PREP_ID, lAB_SAMP_PREP_CTX);
                            if (ppp == null) //no match found
                            {
                                if (autoImportRefDataInd == true)
                                {
                                    db_Ref.InsertOrUpdateT_WQX_REF_SAMP_PREP(null, lAB_SAMP_PREP_ID.Trim(), lAB_SAMP_PREP_CTX.Trim(), lAB_SAMP_PREP_ID.Trim(), "", true);
                                }
                                else
                                { sTATUS_CD = "F"; sTATUS_DESC += "No matching Lab Sample Prep ID found - please add it at the Reference Data screen first. "; }
                            }
                            else  //match found
                                a.LAB_SAMP_PREP_IDX = ppp.SAMP_PREP_IDX;

                        }
                    }

                    if (lAB_SAMP_PREP_START_DT != null)
                    {
                        //fix improperly formatted datetime
                        if (lAB_SAMP_PREP_START_DT.ConvertOrDefault<DateTime>().Year < 1900)
                            lAB_SAMP_PREP_START_DT = null;

                        a.LAB_SAMP_PREP_START_DT = lAB_SAMP_PREP_START_DT;
                    }

                    if (lAB_SAMP_PREP_END_DT != null)
                    {
                        //fix improperly formatted datetime
                        if (lAB_SAMP_PREP_END_DT.ConvertOrDefault<DateTime>().Year < 1900)
                            lAB_SAMP_PREP_END_DT = null;

                        a.LAB_SAMP_PREP_END_DT = lAB_SAMP_PREP_END_DT;
                    }

                    if (!string.IsNullOrEmpty(dILUTION_FACTOR))
                        a.DILUTION_FACTOR = dILUTION_FACTOR.Trim().SubStringPlus(0, 12);

                    if (sTATUS_CD != null) a.IMPORT_STATUS_CD = sTATUS_CD;
                    if (sTATUS_DESC != null) a.IMPORT_STATUS_DESC = sTATUS_DESC.SubStringPlus(0,100);

                    if (insInd) //insert case
                        ctx.AddToT_WQX_IMPORT_TEMP_RESULT(a);

                    ctx.SaveChanges();

                    return a.TEMP_RESULT_IDX;
                }
                catch (Exception ex)
                {
                    return 0;
                }
            }
        }