コード例 #1
0
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L5ME_SS_1120 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Guid();
            //Put your code here
            returnValue.Result = Guid.Empty;
            P_L2LN_GALFTID_1530 langParam = new P_L2LN_GALFTID_1530();
            langParam.Tenant_RefID = securityTicket.TenantID;
            var DBLanguages = cls_Get_All_Languages_ForTenantID.Invoke(Connection, Transaction, langParam, securityTicket).Result;
            //var serializer = new JsonNetSerializer();
            //var connection = new ElasticConnection((String)HttpContext.GetGlobalResourceObject("Global", "ElasticConnection"), 9200);

            #region Save
            if (Parameter.Substance_ID == Guid.Empty)
            {
                ORM_HEC_SUB_Substance newSubstance = new ORM_HEC_SUB_Substance();
                newSubstance.HEC_SUB_SubstanceID      = Guid.NewGuid();
                newSubstance.GlobalPropertyMatchingID = Parameter.Substance_Name;
                newSubstance.Tenant_RefID             = securityTicket.TenantID;
                newSubstance.Save(Connection, Transaction);

                ORM_HEC_SUB_Substance_Name newSubstanceName = new ORM_HEC_SUB_Substance_Name();
                newSubstanceName.HEC_SUB_Substance_NameID = Guid.NewGuid();
                newSubstanceName.HEC_SUB_Substance_RefID  = newSubstance.HEC_SUB_SubstanceID;
                Dict name = new Dict("hec_sub_substance_name");
                for (int i = 0; i < DBLanguages.Length; i++)
                {
                    name.AddEntry(DBLanguages[i].CMN_LanguageID, Parameter.Substance_Name);
                }
                Dict nameOrigin = new Dict("hec_sub_substance_name");
                for (int i = 0; i < DBLanguages.Length; i++)
                {
                    nameOrigin.AddEntry(DBLanguages[i].CMN_LanguageID, Parameter.Substance_Name);
                }
                newSubstanceName.SubstanceName_Label  = name;
                newSubstanceName.SubstanceName_Origin = nameOrigin;
                newSubstanceName.Tenant_RefID         = securityTicket.TenantID;
                newSubstanceName.Save(Connection, Transaction);
                //#region Upload To Elastic
                //bool indexExists = true;

                //#region set Mapping
                //string jsonMapping = new MapBuilder<Models.Substance>()
                //                        .RootObject("substance", ro => ro
                //                        .Properties(pr => pr
                //                            .MultiField("name", mfp => mfp.Fields(f => f
                //                                .String("name", sp => sp.IndexAnalyzer("autocomplete").SearchAnalyzer(DefaultAnalyzers.standard))
                //                                .String("lower_case_sort", sp => sp.Analyzer("caseinsensitive"))
                //                                )
                //                            )
                //                            )).BuildBeautified();
                //#endregion

                //try
                //{
                //    connection.Head(new IndexExistsCommand(securityTicket.TenantID.ToString()));
                //}
                //catch (OperationException ex)
                //{
                //    if (ex.HttpStatusCode == 404)
                //        indexExists = false;
                //}

                //if (!indexExists)
                //{
                //    #region set index settings
                //    string settings = new IndexSettingsBuilder()
                //                          .Analysis(anl => anl
                //                              .Filter(fil => fil
                //                                  .EdgeNGram("autocomplete_filter", gr => gr.MinGram(1).MaxGram(20)))
                //                              .Analyzer(a => a
                //                                  .Custom("caseinsensitive", custom => custom
                //                                      .Tokenizer(DefaultTokenizers.keyword)
                //                                      .Filter("lowercase")
                //                                  )
                //                                  .Custom("autocomplete", custom => custom
                //                                      .Tokenizer(DefaultTokenizers.standard)
                //                                      .Filter("lowercase", "autocomplete_filter")
                //                                  )
                //                              )
                //                          )
                //                          .BuildBeautified();
                //    #endregion
                //    connection.Put(securityTicket.TenantID.ToString(), settings);
                //}

                //#region check if type exists

                //bool typeExists = true;

                //try
                //{
                //    connection.Head(new IndexExistsCommand(securityTicket.TenantID.ToString() + "/substance"));
                //}
                //catch (OperationException ex)
                //{
                //    if (ex.HttpStatusCode == 404)
                //        typeExists = false;
                //}
                //#endregion


                //if (!typeExists)
                //    connection.Put(new PutMappingCommand(securityTicket.TenantID.ToString(), "substance"), jsonMapping);

                //string bulkCommand = new BulkCommand(index: securityTicket.TenantID.ToString(), type: "substance").Refresh();

                //List<Models.Substance> substanceList = new List<Models.Substance>();
                //Models.Substance substance = new Models.Substance();
                //substance.id = newSubstance.HEC_SUB_SubstanceID.ToString();
                //substance.name = Parameter.Substance_Name;

                //substanceList.Add(substance);

                //string bulkJson = new BulkBuilder(serializer)
                //        .BuildCollection(substanceList, (builder, pro) => builder.Index(data: pro, id: pro.id)
                //        );
                //connection.Post(bulkCommand, bulkJson);

                //#endregion
                returnValue.Result = newSubstance.HEC_SUB_SubstanceID;
            }
            #endregion
            else
            {
                var substanceQuery = new ORM_HEC_SUB_Substance.Query();
                substanceQuery.IsDeleted           = false;
                substanceQuery.HEC_SUB_SubstanceID = Parameter.Substance_ID;

                var substance = ORM_HEC_SUB_Substance.Query.Search(Connection, Transaction, substanceQuery).Single();

                var substanceNameQuery = new ORM_HEC_SUB_Substance_Name.Query();
                substanceNameQuery.IsDeleted = false;
                substanceNameQuery.HEC_SUB_Substance_RefID = Parameter.Substance_ID;

                var substanceName = ORM_HEC_SUB_Substance_Name.Query.Search(Connection, Transaction, substanceNameQuery).Single();
                #region Delete
                if (Parameter.IsDeleted)
                {
                    substance.IsDeleted = true;
                    substance.Save(Connection, Transaction);

                    substanceName.IsDeleted = true;
                    substanceName.Save(Connection, Transaction);

                    //// delete on Elastic
                    //connection.Delete(securityTicket.TenantID.ToString() + "/substance/" + Parameter.Substance_ID.ToString());
                }
                #endregion
                #region Edit
                else
                {
                    Dict name = new Dict("hec_sub_substance_name");
                    for (int i = 0; i < DBLanguages.Length; i++)
                    {
                        name.AddEntry(DBLanguages[i].CMN_LanguageID, Parameter.Substance_Name);
                    }
                    Dict nameOrigin = new Dict("hec_sub_substance_name");
                    for (int i = 0; i < DBLanguages.Length; i++)
                    {
                        nameOrigin.AddEntry(DBLanguages[i].CMN_LanguageID, Parameter.Substance_Name);
                    }

                    substanceName.SubstanceName_Label  = name;
                    substanceName.SubstanceName_Origin = nameOrigin;
                    substanceName.Save(Connection, Transaction);

                    //string command = Commands.Index(index: securityTicket.TenantID.ToString(), type: "substance", id: Parameter.Substance_ID.ToString()).Refresh();

                    //Models.Substance substanceEdit = new Models.Substance();
                    //substanceEdit.id = Parameter.Substance_ID.ToString();
                    //substanceEdit.name = Parameter.Substance_Name;

                    //string jsonData = serializer.ToJson(substanceEdit);
                    //string response = connection.Put(command, jsonData);
                }
                returnValue.Result = substance.HEC_SUB_SubstanceID;
                #endregion
            }


            return(returnValue);

            #endregion UserCode
        }
        protected static FR_Guid Execute(DbConnection Connection, DbTransaction Transaction, P_L3CO_SCfIPFC_1324 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            //Leave UserCode region to enable user code saving
            #region UserCode
            var returnValue = new FR_Guid();

            #region All tenant's languages

            var allTenantLanguages = cls_Get_All_Languages.Invoke(Connection, Transaction, securityTicket).Result.ToList();

            #endregion

            #region Delete Previous Components

            ORM_HEC_PRO_Product_Component.Query.SoftDelete(Connection, Transaction, new ORM_HEC_PRO_Product_Component.Query()
            {
                HEC_PRO_Product_RefID = Parameter.HEC_ProductID,
                Tenant_RefID          = securityTicket.TenantID,
                IsDeleted             = false
            });

            #endregion

            foreach (var componentFromCatalog in Parameter.ActiveComponents)
            {
                var component = new ORM_HEC_PRO_Component();
                component.HEC_PRO_ComponentID  = Guid.NewGuid();
                component.Component_SimpleName = componentFromCatalog.ComponentName;
                component.Component_Name       = new Dict(ORM_HEC_PRO_Component.TableName);
                foreach (var item in allTenantLanguages)
                {
                    component.Component_Name.AddEntry(item.CMN_LanguageID, componentFromCatalog.ComponentName);
                }
                component.Creation_Timestamp = DateTime.Now;
                component.Tenant_RefID       = securityTicket.TenantID;
                component.Save(Connection, Transaction);

                var productComponents = new ORM_HEC_PRO_Product_Component();
                productComponents.HEC_PRO_Product_ComponentID = Guid.NewGuid();
                productComponents.HEC_PRO_Component_RefID     = component.HEC_PRO_ComponentID;
                productComponents.HEC_PRO_Product_RefID       = Parameter.HEC_ProductID;
                productComponents.Creation_Timestamp          = DateTime.Now;
                productComponents.Tenant_RefID = securityTicket.TenantID;
                productComponents.Save(Connection, Transaction);


                foreach (var substanceFromCatalog in componentFromCatalog.Substances)
                {
                    var substance = ORM_HEC_SUB_Substance.Query.Search(Connection, Transaction,
                                                                       new ORM_HEC_SUB_Substance.Query()
                    {
                        GlobalPropertyMatchingID = substanceFromCatalog.Name,
                        Tenant_RefID             = securityTicket.TenantID,
                        IsDeleted = false
                    }).SingleOrDefault();

                    if (substance == default(ORM_HEC_SUB_Substance))
                    {
                        substance = new ORM_HEC_SUB_Substance();
                        substance.HEC_SUB_SubstanceID      = Guid.NewGuid();
                        substance.GlobalPropertyMatchingID = substanceFromCatalog.Name;
                        substance.IsActiveComponent        = substanceFromCatalog.IsActive;
                        substance.Creation_Timestamp       = DateTime.Now;
                        substance.Tenant_RefID             = securityTicket.TenantID;
                        substance.Save(Connection, Transaction);

                        var substanceName = new ORM_HEC_SUB_Substance_Name();
                        substanceName.HEC_SUB_Substance_NameID = Guid.NewGuid();
                        substanceName.HEC_SUB_Substance_RefID  = substance.HEC_SUB_SubstanceID;
                        substanceName.SubstanceName_Label      = new Dict(ORM_HEC_PRO_Component.TableName);
                        foreach (var item in allTenantLanguages)
                        {
                            substanceName.SubstanceName_Label.AddEntry(item.CMN_LanguageID, substanceFromCatalog.Name);
                        }
                        substanceName.SubstanceName_Origin = new Dict(ORM_HEC_PRO_Component.TableName);
                        substanceName.Creation_Timestamp   = DateTime.Now;
                        substanceName.Tenant_RefID         = securityTicket.TenantID;
                        substanceName.Save(Connection, Transaction);
                    }

                    var substanceIngredient = new ORM_HEC_PRO_Component_SubstanceIngredient();
                    substanceIngredient.HEC_PRO_Component_SubstanceIngredientID = Guid.NewGuid();
                    substanceIngredient.Component_RefID    = component.HEC_PRO_ComponentID;
                    substanceIngredient.Substance_RefID    = substance.HEC_SUB_SubstanceID;
                    substanceIngredient.Creation_Timestamp = DateTime.Now;
                    substanceIngredient.Tenant_RefID       = securityTicket.TenantID;
                    substanceIngredient.Save(Connection, Transaction);
                }
            }

            return(returnValue);

            #endregion UserCode
        }
コード例 #3
0
        protected static FR_Bool Execute(DbConnection Connection, DbTransaction Transaction, P_L5PA_SPEM_1413 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Bool();

            var medPro_Credentials = cls_Get_TenantMemershipData.Invoke(Connection, Transaction, securityTicket).Result;

            P_L2LN_GALFTID_1530 langParam = new P_L2LN_GALFTID_1530();
            langParam.Tenant_RefID = securityTicket.TenantID;
            var DBLanguages = cls_Get_All_Languages_ForTenantID.Invoke(Connection, Transaction, langParam, securityTicket).Result;

            var examination = ORM_HEC_ACT_PerformedAction.Query.Search(Connection, Transaction, new ORM_HEC_ACT_PerformedAction.Query()
            {
                IsDeleted    = false,
                Tenant_RefID = securityTicket.TenantID,
                HEC_ACT_PerformedActionID = Parameter.ExaminationID
            }).Single();
            #region Save

            foreach (var item in Parameter.new_medication)
            {
                //check if dosage exists

                var dosageQuery = new ORM_HEC_Dosage.Query();
                dosageQuery.IsDeleted    = false;
                dosageQuery.Tenant_RefID = securityTicket.TenantID;
                dosageQuery.DosageText   = item.dosage_text;

                var dosage_table = ORM_HEC_Dosage.Query.Search(Connection, Transaction, dosageQuery).SingleOrDefault();

                if (dosage_table == null)
                {
                    dosage_table = new ORM_HEC_Dosage();
                    dosage_table.HEC_DosageID           = Guid.NewGuid();
                    dosage_table.Tenant_RefID           = securityTicket.TenantID;
                    dosage_table.Creation_Timestamp     = DateTime.Now;
                    dosage_table.Modification_Timestamp = DateTime.Now;
                    dosage_table.DosageText             = item.dosage_text;
                    dosage_table.Save(Connection, Transaction);
                }

                ORM_HEC_ACT_PerformedAction_MedicationUpdate medicationUpdate = new ORM_HEC_ACT_PerformedAction_MedicationUpdate();
                medicationUpdate.HEC_ACT_PerformedAction_MedicationUpdateID = Guid.NewGuid();
                medicationUpdate.Tenant_RefID           = securityTicket.TenantID;
                medicationUpdate.Creation_Timestamp     = DateTime.Now;
                medicationUpdate.Modification_Timestamp = DateTime.Now;
                medicationUpdate.IsSubstance            = !item.is_product;
                medicationUpdate.IsHealthcareProduct    = item.is_product;
                medicationUpdate.IntendedApplicationDuration_in_days = item.days_valid;
                medicationUpdate.HEC_ACT_PerformedAction_RefID       = Parameter.ExaminationID;
                medicationUpdate.HEC_Patient_Medication_RefID        = Guid.NewGuid();

                ORM_HEC_Patient_Medication patient_medications = new ORM_HEC_Patient_Medication();
                patient_medications.HEC_Patient_MedicationID = medicationUpdate.HEC_Patient_Medication_RefID;
                patient_medications.Patient_RefID            = Parameter.PatientID;
                patient_medications.Creation_Timestamp       = DateTime.Now;
                patient_medications.Tenant_RefID             = securityTicket.TenantID;
                patient_medications.Modification_Timestamp   = DateTime.Now;
                patient_medications.R_IsActive            = true;
                patient_medications.R_DateOfAdding        = examination.IfPerfomed_DateOfAction;
                patient_medications.R_IsHealthcareProduct = item.is_product;
                patient_medications.R_IsSubstance         = !item.is_product;
                patient_medications.R_ActiveUntill        = patient_medications.R_DateOfAdding.AddDays(item.days_valid);


                if (item.is_product)//medication is a product
                {
                    Guid Hec_ProductID = Guid.Empty;

                    //check if product exists
                    var productQuery = new ORM_CMN_PRO_Product.Query();
                    productQuery.Tenant_RefID = securityTicket.TenantID;
                    productQuery.IsDeleted    = false;
                    productQuery.ProductITL   = item.product_itl;

                    var product = ORM_CMN_PRO_Product.Query.Search(Connection, Transaction, productQuery).SingleOrDefault();

                    //if product does not exist create it
                    if (product == null)
                    {
                        ORM_CMN_PRO_Product cmn_pro_product = new ORM_CMN_PRO_Product();
                        cmn_pro_product.CMN_PRO_ProductID      = Guid.NewGuid();
                        cmn_pro_product.Tenant_RefID           = securityTicket.TenantID;
                        cmn_pro_product.Creation_Timestamp     = DateTime.Now;
                        cmn_pro_product.Modification_Timestamp = DateTime.Now;

                        Dict product_name = new Dict("cmn_pro_products");
                        for (int i = 0; i < DBLanguages.Length; i++)
                        {
                            product_name.AddEntry(DBLanguages[i].CMN_LanguageID, item.product_name);
                        }

                        cmn_pro_product.Product_Name = product_name;
                        cmn_pro_product.ProductITL   = item.product_itl;
                        cmn_pro_product.ProducingBusinessParticipant_RefID = Guid.NewGuid(); //manufacturer
                        cmn_pro_product.PackageInfo_RefID = Guid.NewGuid();                  // package info
                        cmn_pro_product.Save(Connection, Transaction);

                        ORM_CMN_BPT_BusinessParticipant manufacturer = new ORM_CMN_BPT_BusinessParticipant();
                        manufacturer.CMN_BPT_BusinessParticipantID = cmn_pro_product.ProducingBusinessParticipant_RefID;
                        manufacturer.DisplayName            = item.product_manufacturer;
                        manufacturer.IsCompany              = true;
                        manufacturer.IsNaturalPerson        = false;
                        manufacturer.Tenant_RefID           = securityTicket.TenantID;
                        manufacturer.Creation_Timestamp     = DateTime.Now;
                        manufacturer.Modification_Timestamp = DateTime.Now;
                        manufacturer.Save(Connection, Transaction);

                        ORM_CMN_PRO_PAC_PackageInfo package_info = new ORM_CMN_PRO_PAC_PackageInfo();
                        package_info.CMN_PRO_PAC_PackageInfoID = cmn_pro_product.PackageInfo_RefID;
                        package_info.Tenant_RefID           = securityTicket.TenantID;
                        package_info.Creation_Timestamp     = DateTime.Now;
                        package_info.Modification_Timestamp = DateTime.Now;

                        string amount = String.Empty;
                        string unit   = String.Empty;
                        foreach (char c in item.product_strength)
                        {
                            // Do not use IsDigit as it will include more than the characters 0 through to 9
                            if (c >= '0' && c <= '9')
                            {
                                amount += c;
                            }
                            else
                            {
                                unit += c;
                            }
                        }
                        package_info.PackageContent_Amount       = Int32.Parse(amount);
                        package_info.PackageContent_DisplayLabel = amount;


                        var unitQuery = new ORM_CMN_Unit.Query();
                        unitQuery.IsDeleted    = false;
                        unitQuery.Tenant_RefID = securityTicket.TenantID;
                        unitQuery.ISOCode      = unit;

                        var cmn_unit = ORM_CMN_Unit.Query.Search(Connection, Transaction, unitQuery).FirstOrDefault();

                        if (cmn_unit == null)
                        {
                            cmn_unit                        = new ORM_CMN_Unit();
                            cmn_unit.CMN_UnitID             = Guid.NewGuid();
                            cmn_unit.Tenant_RefID           = securityTicket.TenantID;
                            cmn_unit.Creation_Timestamp     = DateTime.Now;
                            cmn_unit.Modification_Timestamp = DateTime.Now;
                            cmn_unit.ISOCode                = unit;
                            cmn_unit.Save(Connection, Transaction);
                        }

                        package_info.PackageContent_MeasuredInUnit_RefID = cmn_unit.CMN_UnitID;
                        package_info.Save(Connection, Transaction);


                        //hec_products
                        ORM_HEC_Product hec_product = new ORM_HEC_Product();
                        hec_product.HEC_ProductID          = Guid.NewGuid();
                        hec_product.Ext_PRO_Product_RefID  = cmn_pro_product.CMN_PRO_ProductID;
                        hec_product.Tenant_RefID           = securityTicket.TenantID;
                        hec_product.Creation_Timestamp     = DateTime.Now;
                        hec_product.Modification_Timestamp = DateTime.Now;

                        Hec_ProductID = hec_product.HEC_ProductID;

                        var dosage_formQuery = new ORM_HEC_Product_DosageForm.Query();
                        dosage_formQuery.Tenant_RefID             = securityTicket.TenantID;
                        dosage_formQuery.IsDeleted                = false;
                        dosage_formQuery.GlobalPropertyMatchingID = item.product_form;

                        var dosage_form = ORM_HEC_Product_DosageForm.Query.Search(Connection, Transaction, dosage_formQuery).SingleOrDefault();

                        if (dosage_form == null)
                        {
                            dosage_form = new ORM_HEC_Product_DosageForm();
                            dosage_form.HEC_Product_DosageFormID = Guid.NewGuid();
                            dosage_form.GlobalPropertyMatchingID = item.product_form;
                            dosage_form.Tenant_RefID             = securityTicket.TenantID;
                            dosage_form.Creation_Timestamp       = DateTime.Now;
                            dosage_form.Modification_Timestamp   = DateTime.Now;

                            Dict form_name = new Dict("hec_product_dosageforms");
                            for (int i = 0; i < DBLanguages.Length; i++)
                            {
                                form_name.AddEntry(DBLanguages[i].CMN_LanguageID, item.product_form);
                            }
                            dosage_form.DosageForm_Name = form_name;
                            dosage_form.Save(Connection, Transaction);
                        }

                        hec_product.ProductDosageForm_RefID = dosage_form.HEC_Product_DosageFormID;  //dosage form
                        hec_product.Save(Connection, Transaction);


                        //product component

                        ORM_HEC_PRO_Product_Component product_component = new ORM_HEC_PRO_Product_Component();
                        product_component.HEC_PRO_Product_ComponentID = Guid.NewGuid();
                        product_component.HEC_PRO_Component_RefID     = Guid.NewGuid();//pro_component
                        product_component.HEC_PRO_Product_RefID       = hec_product.HEC_ProductID;
                        product_component.Tenant_RefID           = securityTicket.TenantID;
                        product_component.Creation_Timestamp     = DateTime.Now;
                        product_component.Modification_Timestamp = DateTime.Now;
                        product_component.Save(Connection, Transaction);

                        ORM_HEC_PRO_Component pro_component = new ORM_HEC_PRO_Component();
                        pro_component.HEC_PRO_ComponentID    = product_component.HEC_PRO_Component_RefID;
                        pro_component.Tenant_RefID           = securityTicket.TenantID;
                        pro_component.Creation_Timestamp     = DateTime.Now;
                        pro_component.Modification_Timestamp = DateTime.Now;
                        pro_component.Save(Connection, Transaction);

                        ORM_HEC_PRO_Component_SubstanceIngredient component_SubstanceIngredient = new ORM_HEC_PRO_Component_SubstanceIngredient();
                        component_SubstanceIngredient.HEC_PRO_Component_SubstanceIngredientID = Guid.NewGuid();
                        component_SubstanceIngredient.Component_RefID        = pro_component.HEC_PRO_ComponentID;
                        component_SubstanceIngredient.Tenant_RefID           = securityTicket.TenantID;
                        component_SubstanceIngredient.Creation_Timestamp     = DateTime.Now;
                        component_SubstanceIngredient.Modification_Timestamp = DateTime.Now;


                        var substanceQuery = new ORM_HEC_SUB_Substance.Query();
                        substanceQuery.IsDeleted              = false;
                        substanceQuery.Tenant_RefID           = securityTicket.TenantID;
                        substanceQuery.HealthcareSubstanceITL = item.substance_itl;

                        var substance = ORM_HEC_SUB_Substance.Query.Search(Connection, Transaction, substanceQuery).SingleOrDefault();

                        if (substance == null)
                        {
                            substance = new ORM_HEC_SUB_Substance();
                            substance.HealthcareSubstanceITL   = item.substance_itl;
                            substance.HEC_SUB_SubstanceID      = Guid.NewGuid();
                            substance.GlobalPropertyMatchingID = item.substance_name;
                            substance.Tenant_RefID             = securityTicket.TenantID;
                            substance.Creation_Timestamp       = DateTime.Now;
                            substance.Modification_Timestamp   = DateTime.Now;
                            substance.Save(Connection, Transaction);

                            ORM_HEC_SUB_Substance_Name substance_name = new ORM_HEC_SUB_Substance_Name();
                            substance_name.HEC_SUB_Substance_NameID = Guid.NewGuid();
                            substance_name.HEC_SUB_Substance_RefID  = substance.HEC_SUB_SubstanceID;

                            Dict substance_name_ = new Dict("hec_sub_substance_names");
                            for (int i = 0; i < DBLanguages.Length; i++)
                            {
                                substance_name_.AddEntry(DBLanguages[i].CMN_LanguageID, item.substance_name);
                            }
                            substance_name.SubstanceName_Label    = substance_name_;
                            substance_name.Tenant_RefID           = securityTicket.TenantID;
                            substance_name.Creation_Timestamp     = DateTime.Now;
                            substance_name.Modification_Timestamp = DateTime.Now;
                            substance_name.Save(Connection, Transaction);
                        }

                        component_SubstanceIngredient.Substance_RefID = substance.HEC_SUB_SubstanceID;
                        component_SubstanceIngredient.Save(Connection, Transaction);
                    }
                    else
                    {
                        var hec_productQuery = new ORM_HEC_Product.Query();
                        hec_productQuery.IsDeleted             = false;
                        hec_productQuery.Tenant_RefID          = securityTicket.TenantID;
                        hec_productQuery.Ext_PRO_Product_RefID = product.CMN_PRO_ProductID;

                        var hec_product = ORM_HEC_Product.Query.Search(Connection, Transaction, hec_productQuery).Single();

                        Hec_ProductID = hec_product.HEC_ProductID;
                    }


                    medicationUpdate.HEC_Product_RefID      = Hec_ProductID;
                    patient_medications.R_HEC_Product_RefID = Hec_ProductID;
                    patient_medications.R_DosageText        = item.dosage_text;
                    medicationUpdate.DosageText             = item.dosage_text;
                }
                else// medication is a substance
                {
                    medicationUpdate.IfSubstance_Strength        = item.substance_strength;
                    patient_medications.R_IfSubstance_Strength   = item.substance_strength;
                    medicationUpdate.IfSubstance_Unit_RefID      = item.substance_unit;
                    patient_medications.R_IfSubstance_Unit_RefID = item.substance_unit;
                    patient_medications.R_DosageText             = item.dosage_text;
                    medicationUpdate.DosageText = item.dosage_text;

                    var substanceQuery = new ORM_HEC_SUB_Substance.Query();
                    substanceQuery.IsDeleted              = false;
                    substanceQuery.Tenant_RefID           = securityTicket.TenantID;
                    substanceQuery.HealthcareSubstanceITL = item.substance_itl;

                    var substance = ORM_HEC_SUB_Substance.Query.Search(Connection, Transaction, substanceQuery).SingleOrDefault();

                    if (substance == null)
                    {
                        substance = new ORM_HEC_SUB_Substance();
                        substance.HealthcareSubstanceITL   = item.substance_itl;
                        substance.HEC_SUB_SubstanceID      = Guid.NewGuid();
                        substance.GlobalPropertyMatchingID = item.substance_name;
                        substance.Tenant_RefID             = securityTicket.TenantID;
                        substance.Creation_Timestamp       = DateTime.Now;
                        substance.Modification_Timestamp   = DateTime.Now;
                        substance.Save(Connection, Transaction);

                        ORM_HEC_SUB_Substance_Name substance_name = new ORM_HEC_SUB_Substance_Name();
                        substance_name.HEC_SUB_Substance_NameID = Guid.NewGuid();
                        substance_name.HEC_SUB_Substance_RefID  = substance.HEC_SUB_SubstanceID;

                        Dict substance_name_ = new Dict("hec_sub_substance_names");
                        for (int i = 0; i < DBLanguages.Length; i++)
                        {
                            substance_name_.AddEntry(DBLanguages[i].CMN_LanguageID, item.substance_name);
                        }
                        substance_name.SubstanceName_Label    = substance_name_;
                        substance_name.Tenant_RefID           = securityTicket.TenantID;
                        substance_name.Creation_Timestamp     = DateTime.Now;
                        substance_name.Modification_Timestamp = DateTime.Now;
                        substance_name.Save(Connection, Transaction);
                    }

                    medicationUpdate.IfSubstance_Substance_RefiD      = substance.HEC_SUB_SubstanceID;
                    patient_medications.R_IfSubstance_Substance_RefiD = substance.HEC_SUB_SubstanceID;
                }

                patient_medications.Save(Connection, Transaction);
                medicationUpdate.Save(Connection, Transaction);
                returnValue.Result = true;
            }

            #endregion

            #region Delete
            foreach (var item in Parameter.deleted_medications)
            {
                var medicationUpdateQuery = new ORM_HEC_ACT_PerformedAction_MedicationUpdate.Query();
                medicationUpdateQuery.IsDeleted = false;
                medicationUpdateQuery.IsMedicationDeactivated = false;
                medicationUpdateQuery.Tenant_RefID            = securityTicket.TenantID;
                medicationUpdateQuery.HEC_ACT_PerformedAction_MedicationUpdateID = item.performedAction_medicationUpdate_id;

                var medicationUpdate = ORM_HEC_ACT_PerformedAction_MedicationUpdate.Query.Search(Connection, Transaction, medicationUpdateQuery).Single();
                medicationUpdate.IsMedicationDeactivated = true;
                medicationUpdate.Save(Connection, Transaction);
            }

            #endregion
            return(returnValue);

            #endregion UserCode
        }